Skip to content

Commit

Permalink
feat: allow users to supply custom distance functions #66 (#67)
Browse files Browse the repository at this point in the history
* feat: allow users to supply custom distance functions #66

* docs:add the docs and read wise method

---------

Co-authored-by: mohsen <[email protected]>
  • Loading branch information
johnlevidy and smoka7 authored Apr 20, 2024
1 parent 6d853ad commit 1b355b0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
10 changes: 10 additions & 0 deletions doc/hop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,16 @@ below.

Defaults:~
`x_bias = 10`

`distance_method` *hop-config-distance_method*
This Determines the method which hops uses to evaluate the distance between
jump target and the cursor.

We currently provide two `manh_distance` and `readwise_distance`
distance methods in `hint` module.

Defaults:~
`distance_method = require('hop.hint').manh_distance`

`teasing` *hop-config-teasing*
Boolean value stating whether Hop should tease you when you do something
Expand Down
1 change: 1 addition & 0 deletions lua/hop/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ M.quit_key = '<Esc>'
M.perm_method = require('hop.perm').TrieBacktrackFilling
M.reverse_distribution = false
M.x_bias = 10
M.distance_method = hint.manh_distance
M.teasing = true
M.virtual_cursor = false
M.jump_on_sole_occurrence = true
Expand Down
19 changes: 19 additions & 0 deletions lua/hop/hint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ M.HintPriority = {
CURSOR = 65535,
}

-- Manhattan distance with column and row, weighted on x so that results are more packed on y.
---@param a CursorPos
---@param b CursorPos
---@param x_bias number
---@return number
function M.manh_distance(a, b, x_bias)
return (x_bias * math.abs(b.row - a.row)) + math.abs(b.col - a.col)
end

--- Distance method that prioritises hints based on the
--- left to right reading distance
---@param a CursorPos Cursor Position
---@param b CursorPos Jump target position
---@param x_bias number
---@return number
function M.readwise_distance(a, b, x_bias)
return (100 * math.abs(b.row - a.row)) + (b.col - a.col)
end

-- Reduce a hint.
-- This function will remove hints not starting with the input key and will reduce the other ones
-- with one level.
Expand Down
12 changes: 1 addition & 11 deletions lua/hop/jump_target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ local window = require('hop.window')
---@class JumpTargetModule
local M = {}

-- Manhattan distance with column and row, weighted on x so that results are more packed on y.
---@param a CursorPos
---@param b CursorPos
---@param x_bias number
---@return number
local function manh_dist(a, b, x_bias)
return (x_bias * math.abs(b.row - a.row)) + math.abs(b.col - a.col)
end

-- Create jump targets within line
---@param jump_ctx JumpContext
---@param opts Options
Expand Down Expand Up @@ -113,10 +104,9 @@ local function create_line_indirect_jump_targets(jump_ctx, locations, opts)
local win_bias = math.abs(vim.api.nvim_get_current_win() - jump_ctx.win_ctx.win_handle) * 1000
for _, jump_target in pairs(line_jump_targets) do
locations.jump_targets[#locations.jump_targets + 1] = jump_target

locations.indirect_jump_targets[#locations.indirect_jump_targets + 1] = {
index = #locations.jump_targets,
score = manh_dist(jump_ctx.win_ctx.cursor, jump_target.cursor, opts.x_bias) + win_bias,
score = opts.distance_method(jump_ctx.win_ctx.cursor, jump_target.cursor, opts.x_bias) + win_bias,
}
end
end
Expand Down

0 comments on commit 1b355b0

Please sign in to comment.