Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable fallback to pyls for goto definition of normal python functions #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It contains a [language server](https://microsoft.github.io/language-server-prot

Install dependencies
```bash
pip install --user attrs python-jsonrpc-server``
pip install --user attrs python-jsonrpc-server python-language-server``
```
Then we need to ake sure tvm_ffi_navigator is in your python path in bashrc.
```bash
Expand All @@ -40,3 +40,9 @@ Add the following configuration
Set the project root to be ```/path/to/tvm``` using `M-x` `lsp-workspace-folders-add` `[RET]` `/path/to/tvm`
Try out the goto definition by opening a python file
- Move cursor to python/tvm/api.py line 59 `_api_internal._min_value`, type `M-x` `lsp-find-definition`

If you use eglot instead, add the following to your init.el file.
```el
(add-to-list 'eglot-server-programs
`(python-mode . ("python3" "-m" "tvm_ffi_navigator.langserver")))
```
14 changes: 7 additions & 7 deletions python/tvm_ffi_navigator/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import sys
from urllib.parse import urlparse
from . import workspace, pattern, lsp
from pyls_jsonrpc import dispatchers, endpoint, streams
from pyls_jsonrpc import endpoint, streams
from pyls import python_ls


def uri2path(uri):
Expand All @@ -17,9 +18,10 @@ def path2uri(path):
return pathlib.Path(os.path.abspath(path)).as_uri()


class BaseServer(dispatchers.MethodDispatcher):
class BaseServer(python_ls.PythonLanguageServer):
"""Base language server can be used for unittesting."""
def __init__(self):
super(BaseServer, self).__init__(sys.stdin.buffer, sys.stdout.buffer)
self.endpoint = None
self.logger = logging
self.ws = workspace.Workspace()
Expand All @@ -30,11 +32,7 @@ def m_initialize(self, **kwargs):
if rooturi is not None:
root_path = uri2path(kwargs["rootUri"])
self.ws.initialize(root_path)
return {
"capabilities": {
"definitionProvider": True,
}
}
return super(BaseServer, self).m_initialize(**kwargs)

def m_initialized(self, **kwargs):
pass
Expand All @@ -49,6 +47,8 @@ def m_text_document__definition(self, **kwargs):
return []
asloc = lambda decl: attr.asdict(lsp.Location(uri=path2uri(decl.path), range=decl.range))
res = [asloc(x) for x in self.ws.get_definition(path, expr)]
if res == []:
res = super(BaseServer, self).m_text_document__definition(**kwargs)
logging.info("textDocument/definition return %s", res)
return res

Expand Down