@@ -10,12 +10,12 @@ function s:warn(...)
10
10
endfunction
11
11
12
12
function s: compare (lhs, rhs)
13
- if a: lhs .name == a: rhs .name
14
- return 0
15
- elseif a: lhs .name < a: rhs .name
13
+ if a: lhs .kind < a: rhs .kind
16
14
return -1
15
+ elseif a: lhs .kind > a: rhs .kind
16
+ return 1
17
17
endif
18
- return 1
18
+ return 0
19
19
endfunction
20
20
21
21
function s: readjson (filename)
@@ -29,29 +29,35 @@ endfunction
29
29
" TODO: Look into applying iabbrev(s) during the CompleteDonePre or
30
30
" CompleteDone event.
31
31
function s: builtin (idx, name)
32
- let gitmoji = s: gitmoji [a: name ]
33
- let word = g: gitmoji_insert_emoji ? gitmoji.emoji : gitmoji.code
34
- let abbr = printf (' %s %s' , gitmoji.emoji , gitmoji.name)
35
- let menu = gitmoji.description
36
- let kind = ' builtin'
37
- return #{ word: word, abbr: abbr, menu : menu , kind: kind }
32
+ return s: completion (s: builtins [a: name ], ' builtin' )
38
33
endfunction
39
34
40
- function s: builtins ()
35
+ function s: alias (idx, name)
36
+ let gitmoji = s: aliases [a: name ]
37
+ return s: completion (s: aliases [a: name ], ' alias' )
38
+ endfunction
39
+
40
+ function s: completion (gitmoji, type )
41
+ let word = g: gitmoji_insert_emoji ? a: gitmoji .emoji : a: gitmoji .code
42
+ let abbr = printf (' %s %s' , a: gitmoji .emoji , a: gitmoji .name)
43
+ let menu = a: gitmoji .description
44
+ return #{ word: word, abbr: abbr, menu : menu , kind: a: type }
45
+ endfunction
46
+
47
+ function s: getbuiltins ()
41
48
let data = s: findlocal (' gitmojis.json' )- >s: readjson ()
42
49
let result = {}
43
50
if ! has_key (data, ' gitmojis' )
44
- s: warn (' gitmojis.json' , " is missing the 'gitmojis' key" )
51
+ s: warn (' gitmojis.json' , " is missing the 'gitmojis' key. " )
45
52
return {}
46
53
endif
47
- let data.gitmojis = data.gitmojis- >sort (function (' s:compare' ))
48
54
for item in data.gitmojis
49
55
let result[item.name] = item
50
56
endfor
51
57
return result
52
58
endfunction
53
59
54
- function s: aliases ()
60
+ function s: getaliases ()
55
61
if ! exists (' g:gitmoji_aliases' )
56
62
return {}
57
63
endif
@@ -64,38 +70,58 @@ function s:aliases()
64
70
elseif type == v: t_func
65
71
let data = call g: gitmoji_aliases
66
72
if type (data) != v: t_dict
67
- s: warn (' gitmoji.vim' , ' g:gitmoji_aliases function did not return a dictionary' )
68
- return {}
73
+ s: warn (' gitmoji.vim' , ' g:gitmoji_aliases function did not return a dictionary. ' )
74
+ let data = {}
69
75
endif
70
76
endif
71
- return data
77
+ let builtins = gitmoji#builtins ()
78
+ let results = {}
79
+ for [name, aliases] in data- >items ()
80
+ let gitmoji = builtins[name]- >deepcopy ()
81
+ for alias in aliases
82
+ let results[alias] = gitmoji- >extend (#{ name: alias }, ' force' )
83
+ endfor
84
+ endfor
85
+ return results
72
86
endfunction
73
87
74
88
function gitmoji#builtins ()
75
- if ! exists (' s:gitmoji ' )
76
- let s: gitmoji = s: builtins ()
89
+ if ! exists (' s:builtins ' )
90
+ let s: builtins = s: getbuiltins ()
77
91
endif
78
- return s: gitmoji
92
+ return s: builtins
79
93
endfunction
80
94
81
95
function gitmoji#aliases ()
82
96
if ! exists (' s:aliases' )
83
- let s: aliases = s: aliases ()
97
+ let s: aliases = s: getaliases ()
84
98
endif
85
99
return s: aliases
86
100
endfunction
87
101
88
102
function gitmoji#complete (findstart, base)
103
+ " Ensure that the dictionaries have been loaded.
89
104
call gitmoji#builtins ()
90
- " TODO: Permit configuration setting to allow 'matching' if the line is
91
- " empty
105
+ call gitmoji#aliases ()
92
106
if a: base- >empty () && a: findstart == 1
93
107
let line = getline (' .' )[0 : col (' .' ) - 1 ]
94
- return line - >match (' :[^: \t]*$' )
108
+ let column = line - >match (' :[^: \t]*$' )
109
+ if column < 0 && g: gitmoji_complete_anywhere
110
+ return col (' .' )
111
+ endif
112
+ return column
95
113
endif
96
- let keys = s: gitmoji- >keys ()- >sort ()
114
+ let builtins = s: builtins- >keys ()
115
+ let aliases = s: aliases- >keys ()
116
+ " In this case, there is 'a match' of sorts, and so we need to weed out the
117
+ " names. The issue is, we need to *then* also return both the builtins and
118
+ " the aliases defined. Thus our 'simple' code path will no longer work, and
119
+ " we must branch for when the a:base is... empty.
97
120
if ! a: base- >empty () && a: findstart == 0
98
- let keys = keys - >matchfuzzy (a: base [1 :])
121
+ let builtins = builtins- >matchfuzzy (a: base [1 :])
122
+ let aliases = aliases- >matchfuzzy (a: base [1 :])
99
123
endif
100
- return keys - >map (function (' s:builtin' ))
124
+ let completions = builtins- >map (function (' s:builtin' ))
125
+ let completions += aliases- >map (function (' s:alias' ))
126
+ return completions- >sort (function (' s:compare' ))
101
127
endfunction
0 commit comments