├── README.md ├── LICENSE └── rplugin └── python3 └── deoplete └── source └── dictionary.py /README.md: -------------------------------------------------------------------------------- 1 | # deoplete-dictionary 2 | deoplete source for dictionary 3 | 4 | ## Description 5 | This source collects "keyword_patterns" keywords from 'dictionary'. 6 | 7 | Note: It uses buffer-local 'dictionary' set up. 8 | 9 | Note: It also supports directory. 10 | 11 | rank: 100 12 | 13 | ## Configuration examples 14 | 15 | ```vim 16 | " Sample configuration for dictionary source with multiple 17 | " dictionary files. 18 | setlocal dictionary+=/usr/share/dict/words 19 | setlocal dictionary+=/usr/share/dict/american-english 20 | " Remove this if you'd like to use fuzzy search 21 | call deoplete#custom#source( 22 | \ 'dictionary', 'matchers', ['matcher_head']) 23 | " If dictionary is already sorted, no need to sort it again. 24 | call deoplete#custom#source( 25 | \ 'dictionary', 'sorters', []) 26 | " Do not complete too short words 27 | call deoplete#custom#source( 28 | \ 'dictionary', 'min_pattern_length', 4) 29 | ``` 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Shougo Matsushita 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /rplugin/python3/deoplete/source/dictionary.py: -------------------------------------------------------------------------------- 1 | # ============================================================================ 2 | # FILE: dictionary.py 3 | # AUTHOR: Shougo Matsushita 4 | # License: MIT license 5 | # ============================================================================ 6 | 7 | from os import scandir 8 | from os.path import getmtime, exists, isdir 9 | from collections import namedtuple 10 | 11 | from deoplete.base.source import Base 12 | from deoplete.util import expand 13 | 14 | DictCacheItem = namedtuple('DictCacheItem', 'mtime candidates') 15 | 16 | 17 | class Source(Base): 18 | 19 | def __init__(self, vim): 20 | super().__init__(vim) 21 | 22 | self.name = 'dictionary' 23 | self.mark = '[D]' 24 | self.events = ['InsertEnter'] 25 | 26 | self._cache = {} 27 | 28 | def on_event(self, context): 29 | self._make_cache(context) 30 | 31 | def gather_candidates(self, context): 32 | if not self._cache: 33 | self._make_cache(context) 34 | 35 | candidates = [] 36 | for filename in [x for x in self._get_dictionaries(context) 37 | if x in self._cache]: 38 | candidates.append(self._cache[filename].candidates) 39 | return {'sorted_candidates': candidates} 40 | 41 | def _make_cache(self, context): 42 | for filename in self._get_dictionaries(context): 43 | mtime = getmtime(filename) 44 | if filename in self._cache and self._cache[ 45 | filename].mtime == mtime: 46 | continue 47 | with open(filename, 'r', errors='replace') as f: 48 | self._cache[filename] = DictCacheItem( 49 | mtime, [{'word': x} for x in sorted( 50 | (x.strip() for x in f), key=str.lower)] 51 | ) 52 | 53 | def _get_dictionaries(self, context): 54 | dict_opt = self.get_buf_option('dictionary') 55 | if not dict_opt: 56 | return [] 57 | 58 | dicts = [] 59 | for d in [expand(x) for x in dict_opt.split(',') if exists(x)]: 60 | if isdir(d): 61 | with scandir(d) as it: 62 | dicts += [x.path for x in it if x.is_file()] 63 | else: 64 | dicts.append(d) 65 | return dicts 66 | --------------------------------------------------------------------------------