Skip to content

Commit 6c3293d

Browse files
authored
Merge pull request #11 from PsychoLlama/feature/autojump-support
Feature: autojump support
2 parents d42b186 + d5eccb2 commit 6c3293d

File tree

7 files changed

+122
-2
lines changed

7 files changed

+122
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ it.
8080
### [zoxide](https://github.com/ajeetdsouza/zoxide)
8181
Zoxide is automatically detected - you don't have to do anything.
8282

83+
### [autojump](https://github.com/wting/autojump)
84+
In order to get the `j` alias, you're probably sourcing an autojump file in
85+
your bash/zsh/fish setup. Find that file and set it in your vimrc:
86+
```viml
87+
let teleport#path = expand('~/path/to/autojump.sh')
88+
```
89+
8390
### Setting the integration
8491
If you have more than one of these installed (why???) there's a chance
8592
`teleport.vim` could choose the wrong one. You can force it to use
@@ -89,6 +96,7 @@ a particular driver by setting `teleport#driver`.
8996
let teleport#driver = 'z' " rupa/z
9097
let teleport#driver = 'z.lua' " skywind3000/z.lua
9198
let teleport#driver = 'zoxide' " ajeetdsouza/zoxide
99+
let teleport#driver = 'autojump' " wting/autojump
92100
```
93101

94102
If you don't see your favorite program listed here, feel free to [open

autoload/teleport/driver.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ let s:drivers = {}
77
let s:drivers.z = teleport#drivers#rupaz#()
88
let s:drivers['z.lua'] = teleport#drivers#zlua#()
99
let s:drivers.zoxide = teleport#drivers#zoxide#()
10+
let s:drivers.autojump = teleport#drivers#autojump#()
1011
let s:drivers.test = teleport#drivers#test#()
1112

1213
func! s:get_driver_setting() abort
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
let s:autojump = { 'name': 'autojump' }
2+
3+
func! s:autojump.is_supported() abort
4+
if !executable('autojump') || !exists('g:teleport#path')
5+
return 0
6+
endif
7+
8+
let l:path_matches = fnamemodify(g:teleport#path, ':t') is# 'autojump.sh'
9+
let l:explicitly_set_driver = get(g:, 'teleport#driver', v:null) is# 'autojump'
10+
return l:path_matches || l:explicitly_set_driver
11+
endfunc
12+
13+
func! s:autojump.query(search_term) abort
14+
let l:output = s:exec_query(a:search_term)
15+
return teleport#parse_output#autojump(a:search_term, l:output)
16+
endfunc
17+
18+
func! teleport#drivers#autojump#() abort
19+
return deepcopy(s:autojump)
20+
endfunc
21+
22+
func! s:exec_query(search_term) abort
23+
let l:cmd = 'source ' . fnameescape(g:teleport#path) . '; '
24+
let l:cmd .= 'autojump --complete ' . fnameescape(a:search_term)
25+
26+
return systemlist(l:cmd)
27+
endfunc

autoload/teleport/parse_output.vim

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,50 @@ endfunc
3030
func! teleport#parse_output#zlua(output) abort
3131
return teleport#parse_output#z(a:output)
3232
endfunc
33+
34+
" Sample output (contains duplicates):
35+
" search-term__1__/path/to/result-1
36+
" search-term__2__/path/to/result-2
37+
" search-term__3__/path/to/result-3
38+
" search-term__4__/path/to/result-1
39+
" search-term__5__/path/to/result-2
40+
" search-term__6__/path/to/result-4
41+
func! teleport#parse_output#autojump(search_term, output) abort
42+
let l:results = []
43+
44+
for l:line in a:output
45+
let l:line_without_prefix = l:line[strlen(a:search_term):]
46+
let [l:rank, l:directory] = split(l:line_without_prefix, '__')
47+
call add(l:results, { 'frecency': str2float(l:rank), 'directory': l:directory })
48+
endfor
49+
50+
call sort(l:results, function('s:order_by_directory_name'))
51+
call uniq(l:results, function('s:filter_duplicates'))
52+
call sort(l:results, function('s:order_by_rank'))
53+
call map(l:results, function('s:set_rank_by_index', [copy(l:results)]))
54+
55+
return l:results
56+
endfunc
57+
58+
func! s:order_by_directory_name(first, second) abort
59+
if a:first.directory is# a:second.directory
60+
return 0
61+
endif
62+
63+
return a:first.directory > a:second.directory ? -1 : 1
64+
endfunc
65+
66+
func! s:filter_duplicates(first, second) abort
67+
return a:first.directory isnot# a:second.directory
68+
endfunc
69+
70+
func! s:order_by_rank(first, second) abort
71+
return float2nr(a:first.frecency - a:second.frecency)
72+
endfunc
73+
74+
" As you might've noticed, the command doesn't reveal frecency. The best we
75+
" can do is approximate by the order it occurs in search results.
76+
func! s:set_rank_by_index(results, index, result) abort
77+
let a:result.frecency = len(a:results) - a:index + 0.0
78+
return a:result
79+
endfunc

autoload/teleport/tests/parse_output.vader

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
### rupa/z
12
Execute (returns a list):
23
let output = teleport#parse_output#z([])
34
silent AssertEqual v:t_list, type(output)
@@ -27,11 +28,34 @@ Execute (orders the output by frecency):
2728
# common: /some/file/path
2829
# That information is already obvious from frecency.
2930
Execute (ignores suggestions for common directories):
30-
let output = teleport#parse_output#z( [
31+
let output = teleport#parse_output#z([
3132
\ 'common: /first/file',
3233
\ '0.555 /second/file',
3334
\ '555.123 /first/file',
3435
\ ])
3536

3637
silent AssertEqual 2, len(output)
3738
silent AssertEqual '/first/file', output[0].directory
39+
40+
### autojump
41+
Execute (removes duplicates):
42+
let output = teleport#parse_output#autojump('prefix', [
43+
\ 'prefix__1__/path/to/duplicate',
44+
\ 'prefix__2__/path/to/duplicate',
45+
\ 'prefix__3__/path/to/duplicate',
46+
\ ])
47+
48+
AssertEqual [{ 'frecency': 1.0, 'directory': '/path/to/duplicate' }], output
49+
50+
Execute (removes intermingled duplicates):
51+
let output = teleport#parse_output#autojump('prefix', [
52+
\ 'prefix__1__/first',
53+
\ 'prefix__2__/second',
54+
\ 'prefix__3__/first',
55+
\ 'prefix__4__/third',
56+
\ ])
57+
58+
let expected = [{ 'frecency': 3.0, 'directory': '/first' }]
59+
call add(expected, { 'frecency': 2.0, 'directory': '/second' })
60+
call add(expected, { 'frecency': 1.0, 'directory': '/third' })
61+
AssertEqual expected, output

doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
autojump teleport.txt /*autojump*
12
teleport#api#find_match() teleport.txt /*teleport#api#find_match()*
23
teleport#api#find_matches() teleport.txt /*teleport#api#find_matches()*
34
teleport#api#query() teleport.txt /*teleport#api#query()*

doc/teleport.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author: Jesse Gibson <JesseTheGibson@gmail.com>
44
Homepage: https://github.com/PsychoLlama/teleport.vim
55
License: MIT
6-
Tags: *z.sh* *zoxide* *z.lua* *z.vim* (old plugin name)
6+
Tags: *z.sh* *zoxide* *z.lua* *autojump* *z.vim* (old plugin name)
77

88

99
Press `gO` to show a table of contents.
@@ -17,6 +17,7 @@ If you're using this plugin, chances are you've installed one of these tools:
1717
- https://github.com/rupa/z
1818
- https://github.com/skywind3000/z.lua
1919
- https://github.com/ajeetdsouza/zoxide
20+
- https://github.com/wting/autojump
2021

2122
They all do pretty much the same thing: they learn your most common
2223
directories, and when given a pattern, they jump to the most likely match.
@@ -59,6 +60,7 @@ These are your options:
5960
- `z`
6061
- `z.lua`
6162
- `zoxide`
63+
- `autojump`
6264

6365
rupa/z~
6466
>
@@ -75,6 +77,11 @@ zoxide~
7577
" Uses the global command "zoxide".
7678
let teleport#driver = 'zoxide'
7779
<
80+
autojump~
81+
>
82+
let teleport#path = expand('~/path/to/autojump.sh')
83+
let telport#driver = 'autojump'
84+
<
7885
------------------------------------------------------------------------------
7986
OPTIONS *teleport-options*
8087

@@ -178,5 +185,10 @@ Changed:
178185
Added:
179186
- New |teleport#update_cwd| option.
180187

188+
0.6.0 - UNRELEASED~
189+
190+
Added:
191+
- Experimental autojump support.
192+
181193
==============================================================================
182194
vim: ft=help tw=78:

0 commit comments

Comments
 (0)