Skip to content

Instantly share code, notes, and snippets.

@Shougo
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shougo/248de0274807fa39e3bc to your computer and use it in GitHub Desktop.
Save Shougo/248de0274807fa39e3bc to your computer and use it in GitHub Desktop.
get_buffer_config
#=============================================================================
# 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
import re
import traceback
import deoplete
from deoplete.deoplete import Deoplete
from deoplete.util import get_buffer_config
@neovim.plugin
class DeopleteHandlers(object):
def __init__(self, vim):
self.vim = vim
def debug(self, msg):
self.vim.command('echomsg string("' + str(msg) + '")')
def error(self, e):
tmp = self.vim.eval('tempname()')
with open(tmp, 'w') as f:
traceback.print_exc(None, f)
self.vim.command('call deoplete#util#print_error(' \
+ '"The error is occurred. Please read ".'
+ 'string("'+tmp+'")." file.")')
@neovim.command('DeopleteInitializePython', sync=True, nargs=0)
def init_python(self):
self.deoplete = Deoplete(self.vim)
self.vim.command('let g:deoplete#_channel_id = '
+ str(self.vim.channel_id))
@neovim.rpc_export('completion_begin')
def completion_begin(self, context):
# Encoding conversion
encoding = self.vim.eval('&encoding')
context = { k.decode(encoding) :
(v.decode(encoding) if isinstance(v, bytes) else v)
for k, v in context.items()}
# Call omni completion
omni_pattern = get_buffer_config(self.vim, context,
'b:deoplete#omni_pattern',
'g:deoplete#omni_patterns',
'g:deoplete#_omni_patterns')
# self.debug(omni_pattern)
if omni_pattern != '' \
and self.vim.eval('&l:omnifunc') != '' \
and re.search('('+omni_pattern+')$', context['input']) \
and self.vim.eval('mode()') == 'i':
self.vim.command(
'call feedkeys("\<C-x>\<C-o>", "n")')
try:
candidates = self.deoplete.gather_candidates(context)
except Exception as e:
self.error(e)
candidates = []
if not candidates or self.vim.eval('mode()') != 'i':
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['changedtick']))
self.vim.command(
'let g:deoplete#_context.candidates = ' + str(candidates))
self.vim.command(
'call feedkeys("\<Plug>(deoplete_start_complete)")')
#=============================================================================
# FILE: util.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.
# }}}
#=============================================================================
def get_buffer_config(vim, context, buffer_var, user_var, default_var):
return vim.eval('deoplete#util#get_buffer_config("{0}", "{1}", {2}, {3})'\
.format(context['filetype'],
buffer_var, user_var, default_var))
"=============================================================================
" FILE: util.vim
" 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.
" }}}
"=============================================================================
function! deoplete#util#set_default(var, val, ...) "{{{
if !exists(a:var) || type({a:var}) != type(a:val)
let alternate_var = get(a:000, 0, '')
let {a:var} = exists(alternate_var) ?
\ {alternate_var} : a:val
endif
endfunction"}}}
function! deoplete#util#set_pattern(variable, keys, pattern) "{{{
for key in split(a:keys, '\s*,\s*')
if !has_key(a:variable, key)
let a:variable[key] = a:pattern
endif
endfor
endfunction"}}}
function! deoplete#util#get_buffer_config(filetype, buffer_var, user_var, default_var) "{{{
return exists(a:buffer_var) ? a:buffer_var :
\ get(a:user_var, a:filetype, get(a:default_var, a:filetype, ''))
endfunction"}}}
function! deoplete#util#print_error(string) "{{{
echohl Error | echomsg '[deoplete] ' . a:string | echohl None
endfunction"}}}
function! deoplete#util#print_warning(string) "{{{
echohl WarningMsg | echomsg '[deoplete] ' . a:string | echohl None
endfunction"}}}
function! deoplete#util#convert2list(expr) "{{{
return type(a:expr) ==# type([]) ? a:expr : [a:expr]
endfunction"}}}
" vim: foldmethod=marker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment