Skip to content

Commit 7561cc1

Browse files
committed
📝 Add documentation for g:gitmoji_aliases
✨ Add support for user defined aliases for abbreviations
1 parent 5cce366 commit 7561cc1

File tree

4 files changed

+82
-33
lines changed

4 files changed

+82
-33
lines changed

after/ftplugin/gitcommit.vim

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
setlocal completefunc=gitmoji#complete
22
setlocal omnifunc=gitmoji#complete
33

4+
function s:warn(...)
5+
echohl WarningMsg
6+
echomsg "gitmoji.vim: " a:000->join()
7+
echohl None
8+
endfunction
9+
10+
let s:builtins = gitmoji#builtins()
11+
let s:aliases = gitmoji#aliases()
12+
413
if g:gitmoji_abbreviations && g:gitmoji_insert_emoji
5-
for [name, gitmoji] in gitmoji#builtins()->items()
14+
for [name, gitmoji] in s:builtins->items()
615
execute 'iabbrev' gitmoji.code gitmoji.emoji
16+
for alias in s:aliases->get(name, [])
17+
" NOTE: For some reason not assigning this to a variable before use
18+
" results in an invalid argument error, with no additional information :/
19+
let code = printf(":%s:", alias)
20+
execute 'iabbrev' code gitmoji.emoji
21+
endfor
722
endfor
823
endif

autoload/gitmoji.vim

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ let g:gitmoji = 1
33

44
let s:directory = expand('<sfile>:p')->resolve()->fnamemodify(':h')
55

6+
function s:warn(...)
7+
echohl WarningMsg
8+
echomsg "gitmoji.vim: " a:000->join()
9+
echohl None
10+
endfunction
11+
612
function s:compare(lhs, rhs)
713
if a:lhs.name == a:rhs.name
814
return 0
@@ -20,35 +26,18 @@ function s:findlocal(filename)
2026
return findfile(a:filename, s:directory)
2127
endfunction
2228

23-
" There needs to be a better way to handle this
24-
" TODO: We need to provide a separate way for users to define their aliases
25-
" where kind: is set to *not* gitmoji, but 'user', or 'plugin'
2629
" TODO: Look into applying iabbrev(s) during the CompleteDonePre or
2730
" CompleteDone event.
2831
function s:builtin(idx, name)
2932
let gitmoji = s:gitmoji[a:name]
30-
echomsg "Gitmoji.code: " gitmoji.code
3133
let word = g:gitmoji_insert_emoji ? gitmoji.emoji : gitmoji.code
32-
let result =<< trim EOT
33-
#{
34-
word: g:gitmoji_insert_emoji ? gitmoji.emoji : gitmoji.code,
35-
abbr: gitmoji.emoji .. ' ' .. gitmoji.name,
36-
menu: gitmoji.description,
37-
kind: 'gitmoji',
38-
}
39-
EOT
40-
return result->join()->eval()
41-
endfunction
42-
43-
function s:warn(...)
44-
echohl WarningMsg
45-
echomsg "gitmoji.vim: " a:000->join()
46-
echohl None
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 }
4738
endfunction
4839

49-
" This will be configurable at some point for additional things, like custom
50-
" files
51-
function s:load()
40+
function s:builtins()
5241
let data = s:findlocal('gitmojis.json')->s:readjson()
5342
let result = {}
5443
if !has_key(data, 'gitmojis')
@@ -62,13 +51,40 @@ function s:load()
6251
return result
6352
endfunction
6453

54+
function s:aliases()
55+
if !exists('g:gitmoji_aliases')
56+
return {}
57+
endif
58+
let type = type(g:gitmoji_aliases)
59+
let data = {}
60+
if type == v:t_string
61+
let data = s:readjson(g:gitmoji_aliases)
62+
elseif type == v:t_dict
63+
let data = g:gitmoji_aliases
64+
elseif type == v:t_func
65+
let data = call g:gitmoji_aliases
66+
if type(data) != v:t_dict
67+
s:warn('gitmoji.vim', 'g:gitmoji_aliases function did not return a dictionary')
68+
return {}
69+
endif
70+
endif
71+
return data
72+
endfunction
73+
6574
function gitmoji#builtins()
6675
if !exists('s:gitmoji')
67-
let s:gitmoji = s:load()
76+
let s:gitmoji = s:builtins()
6877
endif
6978
return s:gitmoji
7079
endfunction
7180

81+
function gitmoji#aliases()
82+
if !exists('s:aliases')
83+
let s:aliases = s:aliases()
84+
endif
85+
return s:aliases
86+
endfunction
87+
7288
function gitmoji#complete(findstart, base)
7389
call gitmoji#builtins()
7490
" TODO: Permit configuration setting to allow 'matching' if the line is
@@ -83,7 +99,3 @@ function gitmoji#complete(findstart, base)
8399
endif
84100
return keys->map(function('s:builtin'))
85101
endfunction
86-
87-
" Registers aliases from a dictionary to be usable as gitmoji names.
88-
function gitmoji#register(aliases)
89-
endfunction

doc/gitmoji.txt

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Author: Isabella Muerte
88
===============================================================================
99
CONTENTS *gitmoji* *gitmoji-contents*
1010

11-
1. Introduction .............................. |gitmoji-introduction|
12-
2. Installation .............................. |gitmoji-installation|
13-
3. Configuration ............................. |gitmoji-configuration|
11+
1. Introduction .............................. |gitmoji-introduction|
12+
2. Installation .............................. |gitmoji-installation|
13+
3. Configuration ............................. |gitmoji-configuration|
1414

1515
===============================================================================
1616
INTRODUCTION *gitmoji-intro*
@@ -28,7 +28,7 @@ INSTALLATION *gitmoji-installation
2828

2929
Using your vim package manager of choice, simply add gitmoji.vim to your list
3030
of plugins. For example, if using vim-plug: >
31-
Plug 'slurps-mad-rips/gitmoji.vim'
31+
Plug 'slurps-mad-rips/gitmoji.vim'
3232
<
3333
===============================================================================
3434
CONFIGURATION *gitmoji-configuration*
@@ -45,6 +45,8 @@ g:gitmoji_insert_emoji~
4545
g:gitmoji_complete_anywhere~
4646
When set to |v:true|, both the 'omnifunc' and 'completefunc' can
4747
activate anywhere.
48+
NOTE: This is not currently implemented in the most recent release of
49+
gitmoji.vim
4850
The default is |v:false|
4951

5052
*g:gitmoji_abbreviations*
@@ -55,5 +57,26 @@ g:gitmoji_abbreviations~
5557
the 'omnifunc' or 'completefunc' that gitmoji sets.
5658
The default is |v:true|
5759

60+
*g:gitmoji_aliases*
61+
g:gitmoji_aliases~
62+
The gitmoji names mentioned on https://gitmoji.dev are not always easy
63+
to remember for what they do exactly. For this reason, users can supply
64+
a |g:gitmoji_aliases| variable. This variable can be either a
65+
|String|, a |Dict|, or a |Funcref|.
66+
If |g:gitmoji_aliases| is a |String|, it is read as a JSON file,
67+
converted to a |Dict| and treated AS IF |g:gitmoji_aliases| is a
68+
|Dict|.
69+
If |g:gitmoji_aliases| is a |Funcref|, it MUST return a |Dict|, and
70+
this returned value will be treated AS IF |g:gitmoji_aliases| was a
71+
|Dict| the entire time.
72+
If |g:gitmoji_aliases| is a |Dict|, each key MUST be the NAME (not the
73+
emoji or code) of a gitmoji name. If it is not, it will be ignored and
74+
a warning will be |:echo|ed to the |:messages| log. Each value of the
75+
|Dict| MUST be a |List| of |Strings|.
76+
Each alias is used to create an |:iabbrev| if both
77+
|g:gitmoji_abbreviations| and |g:gitmoji_insert_emoji| are |v:true|.
78+
NOTE: These aliases are not used in the complete function at this time,
79+
but will be in the near future.
80+
5881
===============================================================================
5982
vim:ft=help:tw=78

plugin/gitmoji.vim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ if !exists('g:gitmoji_complete_anywhere')
99
let g:gitmoji_complete_anywhere = v:false
1010
endif
1111

12-
" TODO: This is not yet implemented
1312
if !exists('g:gitmoji_abbreviations')
1413
let g:gitmoji_abbreviations = v:true
1514
endif

0 commit comments

Comments
 (0)