From da46be7044c3b16eee0618154fe4e1c5e8d502b2 Mon Sep 17 00:00:00 2001 From: Stephen Sugden Date: Wed, 6 Feb 2013 16:28:22 -0800 Subject: [PATCH] Add jedi-based completion. This first version only uses the Jedi completion engine after a ".", similar to how the ClangCompleter works. It is also entirely synchronous and blocks for quite a while the first time it is called. --- .gitmodules | 3 + python/completers/python/__init__.py | 0 python/completers/python/hook.py | 21 +++++++ python/completers/python/jedi | 1 + python/completers/python/jedi_completer.py | 68 ++++++++++++++++++++++ python/jedi | 1 + 6 files changed, 94 insertions(+) create mode 100644 .gitmodules create mode 100644 python/completers/python/__init__.py create mode 100644 python/completers/python/hook.py create mode 160000 python/completers/python/jedi create mode 100644 python/completers/python/jedi_completer.py create mode 160000 python/jedi diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..5ef74ec6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "python/completers/python/jedi"] + path = python/completers/python/jedi + url = https://github.com/davidhalter/jedi.git diff --git a/python/completers/python/__init__.py b/python/completers/python/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/python/completers/python/hook.py b/python/completers/python/hook.py new file mode 100644 index 00000000..1743b733 --- /dev/null +++ b/python/completers/python/hook.py @@ -0,0 +1,21 @@ +# Copyright (C) 2011, 2012 Stephen Sugden +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +from jedi_completer import JediCompleter + +def GetCompleter(): + return JediCompleter() diff --git a/python/completers/python/jedi b/python/completers/python/jedi new file mode 160000 index 00000000..23f36c86 --- /dev/null +++ b/python/completers/python/jedi @@ -0,0 +1 @@ +Subproject commit 23f36c86d7d7c5b890fef9eb9eb46cd1fe553329 diff --git a/python/completers/python/jedi_completer.py b/python/completers/python/jedi_completer.py new file mode 100644 index 00000000..81b2b316 --- /dev/null +++ b/python/completers/python/jedi_completer.py @@ -0,0 +1,68 @@ +# Copyright (C) 2011, 2012 Stephen Sugden +# +# This file is part of YouCompleteMe. +# +# YouCompleteMe is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# YouCompleteMe is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with YouCompleteMe. If not, see . + +import vim +from completers.completer import Completer +from vimsupport import PostVimMessage, CurrentLineAndColumn + +import sys +from os.path import join, abspath, dirname + +# We need to add the jedi package to sys.path, but it's important that we clean +# up after ourselves, because ycm.YouCompletMe.GetFiletypeCompleterForFiletype +# removes sys.path[0] after importing completers.python.hook +sys.path.insert(0, join(abspath(dirname(__file__)), 'jedi')) +from jedi import Script +sys.path.pop(0) + +class JediCompleter( Completer ): + """ + A Completer that uses the Jedi completion engine + https://jedi.readthedocs.org/en/latest/ + """ + + def __init__( self ): + self.candidates = None + + def SupportedFiletypes( self ): + """ Just python """ + return ['python'] + + def ShouldUseNow(self, start_column): + """ Only use jedi-completion after a . """ + line = vim.current.line + return len(line) >= start_column and line[start_column - 1] == '.' + + def CandidatesFromStoredRequest( self ): + return self.candidates + + def OnFileReadyToParse(self): + pass + + def AsyncCandidateRequestReady(self): + return self.candidates is not None + + def CandidatesFromStoredRequest(self): + return self.candidates + + def CandidatesForQueryAsync( self, query ): + buffer = vim.current.buffer + filename = buffer.name + line, column = CurrentLineAndColumn() + script = Script("\n".join(buffer), line + 1, column, filename) + self.candidates = [completion.word for completion + in script.complete() if completion.word.startswith(query)] diff --git a/python/jedi b/python/jedi new file mode 160000 index 00000000..23f36c86 --- /dev/null +++ b/python/jedi @@ -0,0 +1 @@ +Subproject commit 23f36c86d7d7c5b890fef9eb9eb46cd1fe553329