From 37c34c79f1c1ac1d37e6cb2273b29dd9c51391eb Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 14:27:50 -0800 Subject: [PATCH 1/9] Simplify main grouping algorithm --- autoload/EasyMotion.vim | 136 ++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 88 deletions(-) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index 7ccdcf88..fb493657 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -799,96 +799,57 @@ let s:grouping_algorithms = { \ 1: 'SCTree' \ , 2: 'Original' \ } -" -- Single-key/closest target priority tree {{{ -" This algorithm tries to assign one-key jumps to all the targets closest to the cursor. -" It works recursively and will work correctly with as few keys as two. -function! s:GroupingAlgorithmSCTree(targets, keys) "{{{ - " Prepare variables for working - let targets_len = len(a:targets) - let keys_len = len(a:keys) - - let groups = {} - - let keys = reverse(copy(a:keys)) - - " Semi-recursively count targets {{{ - " We need to know exactly how many child nodes (targets) this branch will have - " in order to pass the correct amount of targets to the recursive function. - - " Prepare sorted target count list {{{ - " This is horrible, I know. But dicts aren't sorted in vim, so we need to - " work around that. That is done by having one sorted list with key counts, - " and a dict which connects the key with the keys_count list. - - let keys_count = [] - let keys_count_keys = {} - - let i = 0 - for key in keys - call add(keys_count, 0) - - let keys_count_keys[key] = i - - let i += 1 - endfor - " }}} - - let targets_left = targets_len - let level = 0 - let i = 0 - - while targets_left > 0 - " Calculate the amount of child nodes based on the current level - let childs_len = (level == 0 ? 1 : (keys_len - 1) ) - - for key in keys - " Add child node count to the keys_count array - let keys_count[keys_count_keys[key]] += childs_len - - " Subtract the child node count - let targets_left -= childs_len - - if targets_left <= 0 - " Subtract the targets left if we added too many too - " many child nodes to the key count - let keys_count[keys_count_keys[key]] += targets_left - - break - endif - - let i += 1 - endfor - - let level += 1 - endwhile - " }}} - " Create group tree {{{ - let i = 0 - let key = 0 - - call reverse(keys_count) - - for key_count in keys_count - if key_count > 1 - " We need to create a subgroup - " Recurse one level deeper - let groups[a:keys[key]] = s:GroupingAlgorithmSCTree(a:targets[i : i + key_count - 1], a:keys) - elseif key_count == 1 - " Assign single target key - let groups[a:keys[key]] = a:targets[i] - else - " No target - continue +function! s:GetChildrenCountsForKeys(n_keys, targets_rem) + " returns a list corresponding to s:jump_tokens; each + " count represents how many hits are in the subtree + " rooted at the corresponding jump token + let counts = repeat([0], a:n_keys) + let targets_rem = a:targets_rem + + let is_first_lvl = 1 + while targets_rem > 0 + " if we can't fit all the hits in the first lvl, + " fit the remainder starting from the last jump token + let n_children = is_first_lvl + \ ? 1 + \ : a:n_keys - 1 + for j in range(a:n_keys) + let counts[j] += n_children + let targets_rem -= n_children + if targets_rem <= 0 + let counts[j] += targets_rem + break endif - - let key += 1 - let i += key_count endfor - " }}} + let is_first_lvl = 0 + endwhile - " Finally! - return groups -endfunction "}}} + return reverse(counts) +endfunction + +" -- Single-key/closest target priority tree {{{ +function! s:GroupingAlgorithmSCTree(targets, keys) "{{{ + let tree = {} + + " i: index into targets + " j: index into keys + let i = 0 + let j = 0 + for key_count in s:GetChildrenCountsForKeys(len(a:keys), len(a:targets)) + let node = a:keys[j] + if key_count == 1 + let tree[node] = a:targets[i] + elseif key_count > 1 + let tree[node] = s:MyFunc(a:targets[i:i + key_count - 1], a:keys) + else + continue + endif + let j += 1 + let i += key_count + endfor + + return tree +endfunction " }}} " -- Original ---------------------------- {{{ function! s:GroupingAlgorithmOriginal(targets, keys) @@ -922,7 +883,6 @@ function! s:GroupingAlgorithmOriginal(targets, keys) return groups endfunction " }}} - " -- Coord/key dictionary creation ------- {{{ function! s:CreateCoordKeyDict(groups, ...) " Dict structure: From 602c3661e52d1f8178d3e4e0fb8e0e654ae544a7 Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 14:31:20 -0800 Subject: [PATCH 2/9] fix fold --- autoload/EasyMotion.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index fb493657..c1fd8ac9 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -828,7 +828,7 @@ function! s:GetChildrenCountsForKeys(n_keys, targets_rem) endfunction " -- Single-key/closest target priority tree {{{ -function! s:GroupingAlgorithmSCTree(targets, keys) "{{{ +function! s:GroupingAlgorithmSCTree(targets, keys) let tree = {} " i: index into targets From 7ed9094c61aeb96115eaa51314b604277cfc21aa Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 14:31:53 -0800 Subject: [PATCH 3/9] fix copy-pasta --- autoload/EasyMotion.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index c1fd8ac9..068c6a0c 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -840,7 +840,7 @@ function! s:GroupingAlgorithmSCTree(targets, keys) if key_count == 1 let tree[node] = a:targets[i] elseif key_count > 1 - let tree[node] = s:MyFunc(a:targets[i:i + key_count - 1], a:keys) + let tree[node] = s:GroupingAlgorithmSCTree(a:targets[i:i + key_count - 1], a:keys) else continue endif From 6c0d59f572603cef5d645266b48e469a0dfe20d9 Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 14:53:56 -0800 Subject: [PATCH 4/9] adding docstring --- autoload/EasyMotion.vim | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index 068c6a0c..4467dff6 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -829,6 +829,11 @@ endfunction " -- Single-key/closest target priority tree {{{ function! s:GroupingAlgorithmSCTree(targets, keys) + " returns a tree where non-leaf nodes are keys and leaves are targets, which + " are tuples [lineno, colno]. + " + " each level of the tree is filled such that the average path depth of the tree + " is minimized and the closest targets come first. let tree = {} " i: index into targets From 806ac1e71604fccc028c6391d73288ab82271c2e Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 14:56:07 -0800 Subject: [PATCH 5/9] fix indentation --- autoload/EasyMotion.vim | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index 4467dff6..1127420c 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -829,31 +829,31 @@ endfunction " -- Single-key/closest target priority tree {{{ function! s:GroupingAlgorithmSCTree(targets, keys) - " returns a tree where non-leaf nodes are keys and leaves are targets, which - " are tuples [lineno, colno]. - " - " each level of the tree is filled such that the average path depth of the tree - " is minimized and the closest targets come first. - let tree = {} - - " i: index into targets - " j: index into keys - let i = 0 - let j = 0 - for key_count in s:GetChildrenCountsForKeys(len(a:keys), len(a:targets)) + " returns a tree where non-leaf nodes are keys and leaves are targets, which + " are tuples [lineno, colno]. + " + " each level of the tree is filled such that the average path depth of the tree + " is minimized and the closest targets come first. + let tree = {} + + " i: index into targets + " j: index into keys + let i = 0 + let j = 0 + for key_count in s:GetChildrenCountsForKeys(len(a:keys), len(a:targets)) let node = a:keys[j] if key_count == 1 - let tree[node] = a:targets[i] + let tree[node] = a:targets[i] elseif key_count > 1 - let tree[node] = s:GroupingAlgorithmSCTree(a:targets[i:i + key_count - 1], a:keys) + let tree[node] = s:GroupingAlgorithmSCTree(a:targets[i:i + key_count - 1], a:keys) else - continue + continue endif let j += 1 let i += key_count - endfor +endfor - return tree +return tree endfunction " }}} " -- Original ---------------------------- {{{ From 7269b4f674022ca2ef152241aba88029647088ab Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 14:57:32 -0800 Subject: [PATCH 6/9] english --- autoload/EasyMotion.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index 1127420c..58acb842 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -808,7 +808,7 @@ function! s:GetChildrenCountsForKeys(n_keys, targets_rem) let is_first_lvl = 1 while targets_rem > 0 - " if we can't fit all the hits in the first lvl, + " if we can't fit all the hits in the first level, " fit the remainder starting from the last jump token let n_children = is_first_lvl \ ? 1 From 7adfab9e0333fd6f118fea85af75528cac74b630 Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 15:01:21 -0800 Subject: [PATCH 7/9] improve comment --- autoload/EasyMotion.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index 58acb842..086ed28d 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -800,9 +800,9 @@ let s:grouping_algorithms = { \ , 2: 'Original' \ } function! s:GetChildrenCountsForKeys(n_keys, targets_rem) - " returns a list corresponding to s:jump_tokens; each + " returns a list corresponding to g:EasyMotion_keys; each " count represents how many hits are in the subtree - " rooted at the corresponding jump token + " rooted at the corresponding key let counts = repeat([0], a:n_keys) let targets_rem = a:targets_rem From a1da268bc771e9306850ced99f7a0536bd360314 Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 15:28:15 -0800 Subject: [PATCH 8/9] formatting --- autoload/EasyMotion.vim | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index 086ed28d..149d007c 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -841,19 +841,19 @@ function! s:GroupingAlgorithmSCTree(targets, keys) let i = 0 let j = 0 for key_count in s:GetChildrenCountsForKeys(len(a:keys), len(a:targets)) - let node = a:keys[j] - if key_count == 1 - let tree[node] = a:targets[i] - elseif key_count > 1 - let tree[node] = s:GroupingAlgorithmSCTree(a:targets[i:i + key_count - 1], a:keys) - else - continue - endif - let j += 1 - let i += key_count -endfor + let node = a:keys[j] + if key_count == 1 + let tree[node] = a:targets[i] + elseif key_count > 1 + let tree[node] = s:GroupingAlgorithmSCTree(a:targets[i:i + key_count - 1], a:keys) + else + continue + endif + let j += 1 + let i += key_count + endfor -return tree + return tree endfunction " }}} " -- Original ---------------------------- {{{ @@ -931,7 +931,6 @@ endfunction " }}} "}}} " Core Functions: {{{ -function! s:PromptUser(groups) "{{{ " Recursive let group_values = values(a:groups) From 1e775c341eb6a0a4075b2dcb2475f7d10b876187 Mon Sep 17 00:00:00 2001 From: yangmillstheory Date: Sun, 26 Nov 2017 15:29:32 -0800 Subject: [PATCH 9/9] put back removed --- autoload/EasyMotion.vim | 1 + 1 file changed, 1 insertion(+) diff --git a/autoload/EasyMotion.vim b/autoload/EasyMotion.vim index 149d007c..f5a4c1ab 100644 --- a/autoload/EasyMotion.vim +++ b/autoload/EasyMotion.vim @@ -931,6 +931,7 @@ endfunction " }}} "}}} " Core Functions: {{{ +function! s:PromptUser(groups) "{{{ " Recursive let group_values = values(a:groups)