Skip to content

Commit

Permalink
Add g:ncm_jedi#environment
Browse files Browse the repository at this point in the history
close #7
  • Loading branch information
roxma committed Jan 20, 2019
1 parent d6379bc commit 47e0caf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ncm2-jedi

Python completion for ncm2 via the greate
[jedi](https://github.com/davidhalter/jedi) library.

## Config

### `g:ncm2_jedi#python_version`

Defaults to 3.

If set to `2`, the jedi completion process is started with the python
specified by `:help g:python_host_prog`.

If set to `3`, the jedi completion process is started with the python
specified by `:help g:python3_host_prog`.

### `g:ncm2_jedi#environment`

When you have virtualenv, you could set this variable to the python executable
of your virtualenv.

Read
[jedi-environments](https://jedi.readthedocs.io/en/latest/docs/api.html#environments)
for more information.
7 changes: 6 additions & 1 deletion autoload/ncm2_jedi.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ let g:ncm2_jedi#python_version = get(g:,
\ 'ncm2_jedi#python_version',
\ 3)

let g:ncm2_jedi#environment = get(g:,
\ 'ncm2_jedi#environment',
\ '')

if g:ncm2_jedi#python_version != 2
let g:ncm2_jedi#proc = yarp#py3('ncm2_jedi')
else
Expand Down Expand Up @@ -44,6 +48,7 @@ func! ncm2_jedi#on_warmup(ctx)
endfunc

func! ncm2_jedi#on_complete(ctx)
call g:ncm2_jedi#proc.try_notify('on_complete', a:ctx, getline(1, '$'))
let env = get(b:, 'ncm2_jedi_environment', g:ncm2_jedi#environment)
call g:ncm2_jedi#proc.try_notify('on_complete', a:ctx, getline(1, '$'), env)
endfunc

16 changes: 14 additions & 2 deletions pythonx/ncm2_jedi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@

class Source(Ncm2Source):

def on_complete(self, ctx, lines):
def __init__(self, vim):
Ncm2Source.__init__(self, vim)
self._envs = {}

def get_env(self, env):
if not env:
return jedi.get_default_environment()
if env not in self._envs:
self._envs[env] = jedi.create_environment(env)
return self._envs[env]

def on_complete(self, ctx, lines, env):
path = ctx['filepath']
typed = ctx['typed']
lnum = ctx['lnum']
Expand All @@ -37,7 +48,8 @@ def on_complete(self, ctx, lines):

logger.info('context [%s]', ctx)

script = jedi.Script(src, lnum, len(typed), path)
env = self.get_env(env)
script = jedi.Script(src, lnum, len(typed), path, environment=env)

is_import = False
if import_pat.search(typed):
Expand Down

0 comments on commit 47e0caf

Please sign in to comment.