From 1b355b08341c990f59b6e6e98c58d4e7357efd35 Mon Sep 17 00:00:00 2001 From: johnlevidy Date: Sat, 20 Apr 2024 13:30:42 -0400 Subject: [PATCH] feat: allow users to supply custom distance functions #66 (#67) * feat: allow users to supply custom distance functions #66 * docs:add the docs and read wise method --------- Co-authored-by: mohsen --- doc/hop.txt | 10 ++++++++++ lua/hop/defaults.lua | 1 + lua/hop/hint.lua | 19 +++++++++++++++++++ lua/hop/jump_target.lua | 12 +----------- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/doc/hop.txt b/doc/hop.txt index 988bac9..4484724 100644 --- a/doc/hop.txt +++ b/doc/hop.txt @@ -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 diff --git a/lua/hop/defaults.lua b/lua/hop/defaults.lua index e0bfc95..3666c75 100644 --- a/lua/hop/defaults.lua +++ b/lua/hop/defaults.lua @@ -10,6 +10,7 @@ M.quit_key = '' 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 diff --git a/lua/hop/hint.lua b/lua/hop/hint.lua index 86b4dd0..b1d5d0f 100644 --- a/lua/hop/hint.lua +++ b/lua/hop/hint.lua @@ -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. diff --git a/lua/hop/jump_target.lua b/lua/hop/jump_target.lua index b0105b6..59ea051 100644 --- a/lua/hop/jump_target.lua +++ b/lua/hop/jump_target.lua @@ -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 @@ -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