Skip to content

Commit

Permalink
Add ft_snippets_map
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Aug 10, 2020
1 parent 0e442f2 commit d149b55
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
1 change: 1 addition & 0 deletions autoload/deoppet/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ EOF
endfunction
function! deoppet#init#_options() abort
return {
\ 'ft_snippets_map': {},
\ 'snippets_dirs': [],
\ }
endfunction
10 changes: 10 additions & 0 deletions doc/deoppet.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ s_<Plug>(deoppet_jump_forward)
------------------------------------------------------------------------------
OPTIONS *deoppet-options*

*deoppet-options-ft_snippets_map*
ft_snippets_map
From Filetype to snippets mappings.
You can extend `javascript` filetype with `typescript` filetype.
>
call deoppet#custom#option('ft_snippets_map',
\ {'typescript': ['typescript', 'javascript']})
<
Default: {}

*deoppet-options-snippets_dir*
snippets_dirs
Snippets directories list.
Expand Down
29 changes: 19 additions & 10 deletions rplugin/python3/deoppet/deoppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from deoppet.parser import Parser, Snippet
from deoppet.mapping import Mapping
# from deoppet.util import debug
from deoppet.util import debug

from pynvim import Nvim

Expand All @@ -30,6 +30,9 @@ def __init__(self, vim: Nvim) -> None:
self._vim.call('deoppet#mapping#_init')
self._vim.call('deoppet#handler#_init')

def debug(self, expr: typing.Any) -> None:
debug(self._vim, expr)

def mapping(self, name: str) -> None:
return self._mapping.mapping(name)

Expand All @@ -46,22 +49,28 @@ def event(self, name: str) -> None:
return self._mapping.clear()

def _load_snippets(self) -> None:
snippets: typing.Dict[str, Snippet] = {}
buf = self._vim.current.buffer
filetype: str = self._vim.call(
'deoppet#util#_get_context_filetype')
if not filetype:
filetype = 'nothing'
# debug(self._vim, filetype)
# debug(self._vim, self._vim.current.buffer.number)
snippets_dirs = self._vim.call(
'deoppet#custom#_get_option', 'snippets_dirs')
ft_snippets_map = self._vim.call(
'deoppet#custom#_get_option', 'ft_snippets_map')
if filetype in ft_snippets_map:
fts = ft_snippets_map[filetype]
else:
fts = filetype.split(',')

snippets: typing.Dict[str, Snippet] = {}
for dir in snippets_dirs:
for filename in glob.glob(
f'{dir}/{filetype}.snip') + glob.glob(f'{dir}/_.snip'):
# debug(self._vim, filename)
with open(filename) as f:
parser = Parser(self._vim, filename)
snippets.update(parser.parse(f.read()))
for ft in fts:
for filename in glob.glob(
f'{dir}/{ft}.snip') + glob.glob(f'{dir}/_.snip'):
# debug(self._vim, filename)
with open(filename) as f:
parser = Parser(self._vim, filename)
snippets.update(parser.parse(f.read()))
# debug(self._vim, snippets)
buf.vars['deoppet_snippets'] = snippets

0 comments on commit d149b55

Please sign in to comment.