Skip to content

Commit

Permalink
Merge pull request #9 from pappasam/support-jedi-17
Browse files Browse the repository at this point in the history
Support jedi 17
  • Loading branch information
pappasam authored Apr 20, 2020
2 parents c52532c + d5e44b8 commit 19317db
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 192 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.5.0

### Added

- Support for Jedi `0.17`

### Changed

- Major internal updates to helper functions. Jedi `0.17` has a different public API.

### Removed

- Remove support for Workspace symbols. I never used this feature and I figure we can do this better with Jedi's new project constructs.
- Remove support for any version of Jedi before `0.17`. If you must use an older Jedi, stick to `0.4.2`.

## 0.4.2

### Changed
Expand Down
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ pip install -U jedi jedi-language-server

jedi-language-server aims to support all of Jedi's capabilities and expose them through the Language Server Protocol. It currently supports the following Language Server requests:

* [textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion)
* [textDocument/definition](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition)
* [textDocument/documentSymbol](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol)
* [textDocument/hover](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover)
* [textDocument/references](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references)
* [textDocument/rename](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename)
* [workspace/symbol](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_symbol)
- [textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion)
- [textDocument/definition](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_definition)
- [textDocument/documentSymbol](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_documentSymbol)
- [textDocument/hover](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_hover)
- [textDocument/references](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references)
- [textDocument/rename](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename)

These language server requests are not currently configurable by the user, but we expect to relax this constraint in a future release.

Expand Down Expand Up @@ -107,12 +106,12 @@ To build and run this project from source:

Install the following tools manually:

* [Poetry](https://github.com/sdispater/poetry#installation)
* [GNU Make](https://www.gnu.org/software/make/)
- [Poetry](https://github.com/sdispater/poetry#installation)
- [GNU Make](https://www.gnu.org/software/make/)

#### Recommended

* [asdf](https://github.com/asdf-vm/asdf)
- [asdf](https://github.com/asdf-vm/asdf)

### Get source code

Expand All @@ -139,10 +138,10 @@ make test

Palantir's [python-language-server](https://github.com/palantir/python-language-server) inspired this project. Unlike python-language-server, jedi-language-server:

* Uses `pygls` instead of creating its own low-level Language Server Protocol bindings
* Supports one powerful 3rd party library: Jedi. By only supporting Jedi, we can focus on supporting all Jedi features without exposing ourselves to too many broken 3rd party dependencies (I'm looking at you, [rope](https://github.com/python-rope/rope)).
* Is supremely simple because of its scope constraints. Leave complexity to the Jedi [master](https://github.com/davidhalter). If the force is strong with you, please submit a PR!
- Uses `pygls` instead of creating its own low-level Language Server Protocol bindings
- Supports one powerful 3rd party library: Jedi. By only supporting Jedi, we can focus on supporting all Jedi features without exposing ourselves to too many broken 3rd party dependencies (I'm looking at you, [rope](https://github.com/python-rope/rope)).
- Is supremely simple because of its scope constraints. Leave complexity to the Jedi [master](https://github.com/davidhalter). If the force is strong with you, please submit a PR!

## Written by

Samuel Roeca *samuel.roeca@gmail.com*
Samuel Roeca _samuel.roeca@gmail.com_
85 changes: 37 additions & 48 deletions jedi_language_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
HOVER,
REFERENCES,
RENAME,
WORKSPACE_SYMBOL,
)
from pygls.server import LanguageServer
from pygls.types import (
Expand All @@ -31,26 +30,25 @@
TextDocumentPositionParams,
TextEdit,
WorkspaceEdit,
WorkspaceSymbolParams,
)

from .server_utils import (
get_jedi_document_names,
get_jedi_script,
get_jedi_workspace_names,
get_location_from_definition,
get_symbol_information_from_definition,
get_location_from_name,
get_symbol_information_from_name,
)
from .type_map import get_lsp_completion_type

SERVER = LanguageServer()


@SERVER.feature(COMPLETION, triggerCharacters=["."])
def lsp_completion(server: LanguageServer, params: CompletionParams = None):
def lsp_completion(server: LanguageServer, params: CompletionParams):
"""Returns completion items."""
script = get_jedi_script(server, params)
jedi_completions = script.completions()
script = get_jedi_script(server, params.textDocument.uri)
jedi_completions = script.complete(
line=params.position.line + 1, column=params.position.character,
)
return CompletionList(
is_incomplete=False,
items=[
Expand All @@ -71,27 +69,28 @@ def lsp_definition(
server: LanguageServer, params: TextDocumentPositionParams
) -> List[Location]:
"""Support Goto Definition"""
script = get_jedi_script(server, params)
definitions = script.goto_assignments(
follow_imports=True, follow_builtin_imports=True
script = get_jedi_script(server, params.textDocument.uri)
names = script.goto(
line=params.position.line + 1,
column=params.position.character,
follow_imports=True,
follow_builtin_imports=True,
)
return [
get_location_from_definition(definition) for definition in definitions
]
return [get_location_from_name(name) for name in names]


@SERVER.feature(HOVER)
def lsp_hover(
server: LanguageServer, params: TextDocumentPositionParams
) -> Hover:
"""Support the hover feature"""
script = get_jedi_script(server, params)
definitions = script.goto_definitions()
script = get_jedi_script(server, params.textDocument.uri)
names = script.infer(
line=params.position.line + 1, column=params.position.character,
)
return Hover(
contents=(
definitions[0].docstring()
if definitions
else "No docstring definition found."
names[0].docstring() if names else "No docstring definition found."
)
)

Expand All @@ -101,29 +100,29 @@ def lsp_references(
server: LanguageServer, params: TextDocumentPositionParams
) -> List[Location]:
"""Obtain all references to document"""
script = get_jedi_script(server, params)
script = get_jedi_script(server, params.textDocument.uri)
try:
definitions = script.usages()
names = script.get_references(
line=params.position.line + 1, column=params.position.character,
)
except Exception: # pylint: disable=broad-except
return []
return [
get_location_from_definition(definition) for definition in definitions
]
return [get_location_from_name(name) for name in names]


@SERVER.feature(RENAME)
def lsp_rename(
server: LanguageServer, params: RenameParams
) -> Optional[WorkspaceEdit]:
"""Rename a symbol across a workspace"""
script = get_jedi_script(server, params)
script = get_jedi_script(server, params.textDocument.uri)
try:
definitions = script.usages()
names = script.get_references(
line=params.position.line + 1, column=params.position.character,
)
except Exception: # pylint: disable=broad-except
return None
locations = [
get_location_from_definition(definition) for definition in definitions
]
locations = [get_location_from_name(name) for name in names]
if not locations:
return None
changes = {} # type: Dict[str, List[TextEdit]]
Expand All @@ -141,21 +140,11 @@ def lsp_document_symbol(
server: LanguageServer, params: DocumentSymbolParams
) -> List[SymbolInformation]:
"""Document Python document symbols"""
jedi_names = get_jedi_document_names(server, params)
return [
get_symbol_information_from_definition(definition)
for definition in jedi_names
]


@SERVER.feature(WORKSPACE_SYMBOL)
def lsp_workspace_symbol(
server: LanguageServer,
params: WorkspaceSymbolParams, # pylint: disable=unused-argument
) -> List[SymbolInformation]:
"""Document Python workspace symbols"""
jedi_names = get_jedi_workspace_names(server)
return [
get_symbol_information_from_definition(definition)
for definition in jedi_names
]
script = get_jedi_script(server, params.textDocument.uri)
try:
names = script.get_names(
line=params.position.line + 1, column=params.position.character,
)
except Exception: # pylint: disable=broad-except
return []
return [get_symbol_information_from_name(name) for name in names]
Loading

0 comments on commit 19317db

Please sign in to comment.