Last active
August 29, 2015 14:22
-
-
Save Shougo/22dc62f365b1e26c855f to your computer and use it in GitHub Desktop.
deoplete.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#============================================================================= | |
# FILE: __init__.py | |
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com> | |
# License: MIT license {{{ | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included | |
# in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# }}} | |
#============================================================================= | |
import neovim | |
from .deoplete import Deoplete | |
@neovim.plugin | |
class DeopleteHandlers(object): | |
def __init__(self, vim): | |
self.vim = vim | |
@neovim.command('DeopleteInitializePython', sync=True, nargs=1) | |
def init_python(self, base_dir): | |
self.deoplete = Deoplete(base_dir) | |
self.vim.command('let g:deoplete#_channel_id = ' | |
+ str(self.vim.channel_id)) | |
@neovim.rpc_export('completion_begin') | |
def completion_begin(self, context): | |
candidates = self.deoplete.gather_candidates(self.vim, context) | |
if not candidates: | |
return | |
self.vim.command( | |
'let g:deoplete#_context = {}') | |
self.vim.command( | |
'let g:deoplete#_context.complete_position = 0') | |
self.vim.command( | |
'let g:deoplete#_context.changedtick = ' | |
+ str(context[b'changedtick'])) | |
self.vim.command( | |
'let g:deoplete#_context.candidates = ' + str(candidates)) | |
self.vim.command( | |
'call feedkeys("\<Plug>(deoplete_start_auto_complete)")') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#============================================================================= | |
# FILE: deoplete.py | |
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com> | |
# License: MIT license {{{ | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included | |
# in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# }}} | |
#============================================================================= | |
import neovim | |
import importlib | |
from .sources.buffer import Buffer | |
from .filters.matcher_head import Filter | |
class Deoplete(object): | |
def __init__(self, base_dir): | |
self.base_dir = base_dir | |
def gather_candidates(self, vim, context): | |
# Encoding conversion | |
encoding = vim.eval('&encoding') | |
context = { k.decode(encoding) : | |
(v.decode(encoding) if isinstance(v, bytes) else v) | |
for k, v in context.items()} | |
debug(vim, context) | |
if context['complete_str'] == '': | |
return [] | |
buffer = Buffer() | |
context['candidates'] = buffer.gather_candidates(vim, context) | |
filter = Filter() | |
context['candidates'] = filter.filter(vim, context) | |
return context['candidates'] | |
def debug(vim, msg): | |
vim.command('echomsg string(' + str(msg) + ')') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#============================================================================= | |
# FILE: matcher_fuzzy.py | |
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com> | |
# License: MIT license {{{ | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included | |
# in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# }}} | |
#============================================================================= | |
import re | |
class Filter(object): | |
def __init__(self): | |
pass | |
def filter(self, vim, context): | |
p = re.compile(fuzzy_escape(context['complete_str'])) | |
debug(vim, fuzzy_escape(context['complete_str'])) | |
return [x for x in context['candidates'] if p.match(x)] | |
def fuzzy_escape(string): | |
# Escape string for python regexp. | |
string = re.sub(r'([a-zA-Z0-9_])', r'\1.*', escape(string)) | |
return string | |
def escape(string): | |
# Escape string for python regexp. | |
return re.sub('[\[\]().*+?^$-]', '\\\1', string) | |
def debug(vim, msg): | |
vim.command('echomsg string("' + msg + '")') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#============================================================================= | |
# FILE: matcher_head.py | |
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com> | |
# License: MIT license {{{ | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included | |
# in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# }}} | |
#============================================================================= | |
class Filter(object): | |
def __init__(self): | |
pass | |
def filter(self, vim, context): | |
max = len(context['complete_str']) | |
return [x for x in context['candidates'] | |
if x.find(context['complete_str'], 0, max) == 0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment