Skip to content

Commit 7ef13b3

Browse files
authored
Merge pull request pappasam#230 from dimbleby/pygls-1.0.0a
pygls 1.0.0a0
2 parents 8416fcf + 951cf48 commit 7ef13b3

File tree

12 files changed

+118
-76
lines changed

12 files changed

+118
-76
lines changed

jedi_language_server/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
"""Jedi Language Server."""
2+
import sys
3+
4+
if sys.version_info < (3, 8):
5+
from importlib_metadata import version
6+
else:
7+
from importlib.metadata import version
8+
9+
__version__ = version("jedi-language-server")

jedi_language_server/cli.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,13 @@
44
import logging
55
import sys
66

7+
from . import __version__
78
from .server import SERVER
89

910

1011
def get_version() -> str:
1112
"""Get the program version."""
12-
# pylint: disable=import-outside-toplevel
13-
try:
14-
# Type checker for Python < 3.8 fails.
15-
# Since this ony happens here, we just ignore.
16-
from importlib.metadata import version # type: ignore
17-
except ImportError:
18-
try:
19-
# Below ignored both because this a redefinition from above, and
20-
# because importlib_metadata isn't known by mypy. Ignored because
21-
# this is intentional.
22-
from importlib_metadata import version # type: ignore
23-
except ImportError:
24-
print(
25-
"Error: unable to get version. "
26-
"If using Python < 3.8, you must install "
27-
"`importlib_metadata` to get the version.",
28-
file=sys.stderr,
29-
)
30-
sys.exit(1)
31-
return version("jedi-language-server")
13+
return __version__
3214

3315

3416
def cli() -> None:

jedi_language_server/initialization_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from typing import List, Optional, Pattern, Set
88

9+
from lsprotocol.types import MarkupKind
910
from pydantic import BaseModel, Field
10-
from pygls.lsp.types import MarkupKind
1111

1212
# pylint: disable=missing-class-docstring
1313
# pylint: disable=too-few-public-methods

jedi_language_server/jedi_utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import jedi.settings
1414
from jedi import Project, Script
1515
from jedi.api.classes import BaseName, Completion, Name, ParamName, Signature
16-
from pygls.lsp.types import (
16+
from lsprotocol.types import (
1717
CompletionItem,
1818
CompletionItemKind,
1919
Diagnostic,
@@ -150,14 +150,23 @@ def lsp_document_symbols(names: List[Name]) -> List[DocumentSymbol]:
150150
accessible with dot notation are removed from display. See comments
151151
inline for cleaning steps.
152152
"""
153+
# pylint: disable=too-many-branches
153154
_name_lookup: Dict[Name, DocumentSymbol] = {}
154155
results: List[DocumentSymbol] = []
155156
for name in names:
157+
symbol_range = _document_symbol_range(name)
158+
if symbol_range is None:
159+
continue
160+
161+
selection_range = lsp_range(name)
162+
if selection_range is None:
163+
continue
164+
156165
symbol = DocumentSymbol(
157166
name=name.name,
158167
kind=get_lsp_symbol_type(name.type),
159-
range=_document_symbol_range(name),
160-
selection_range=lsp_range(name),
168+
range=symbol_range,
169+
selection_range=selection_range,
161170
detail=name.description,
162171
children=[],
163172
)

jedi_language_server/pygls_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from typing import Optional
88

9-
from pygls.lsp.types import Position, Range
9+
from lsprotocol.types import Position, Range
1010
from pygls.workspace import Document
1111

1212

jedi_language_server/server.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,27 @@
99
import itertools
1010
from typing import Any, List, Optional, Union
1111

12-
from jedi import Project
12+
from jedi import Project, __version__
1313
from jedi.api.refactoring import RefactoringError
14-
from pydantic import ValidationError
15-
from pygls.lsp.methods import (
16-
CODE_ACTION,
17-
COMPLETION,
14+
from lsprotocol.types import (
1815
COMPLETION_ITEM_RESOLVE,
19-
DEFINITION,
20-
DOCUMENT_HIGHLIGHT,
21-
DOCUMENT_SYMBOL,
22-
HOVER,
2316
INITIALIZE,
24-
REFERENCES,
25-
RENAME,
26-
SIGNATURE_HELP,
17+
TEXT_DOCUMENT_CODE_ACTION,
18+
TEXT_DOCUMENT_COMPLETION,
19+
TEXT_DOCUMENT_DEFINITION,
2720
TEXT_DOCUMENT_DID_CHANGE,
2821
TEXT_DOCUMENT_DID_CLOSE,
2922
TEXT_DOCUMENT_DID_OPEN,
3023
TEXT_DOCUMENT_DID_SAVE,
31-
TYPE_DEFINITION,
24+
TEXT_DOCUMENT_DOCUMENT_HIGHLIGHT,
25+
TEXT_DOCUMENT_DOCUMENT_SYMBOL,
26+
TEXT_DOCUMENT_HOVER,
27+
TEXT_DOCUMENT_REFERENCES,
28+
TEXT_DOCUMENT_RENAME,
29+
TEXT_DOCUMENT_SIGNATURE_HELP,
30+
TEXT_DOCUMENT_TYPE_DEFINITION,
3231
WORKSPACE_DID_CHANGE_CONFIGURATION,
3332
WORKSPACE_SYMBOL,
34-
)
35-
from pygls.lsp.types import (
3633
CodeAction,
3734
CodeActionKind,
3835
CodeActionOptions,
@@ -66,6 +63,8 @@
6663
WorkspaceEdit,
6764
WorkspaceSymbolParams,
6865
)
66+
from pydantic import ValidationError
67+
from pygls.capabilities import get_capability
6968
from pygls.protocol import LanguageServerProtocol, lsp_method
7069
from pygls.server import LanguageServer
7170

@@ -126,7 +125,7 @@ def lsp_initialize(self, params: InitializeParams) -> InitializeResult:
126125
server.feature(TEXT_DOCUMENT_DID_CLOSE)(did_close)
127126

128127
if server.initialization_options.hover.enable:
129-
server.feature(HOVER)(hover)
128+
server.feature(TEXT_DOCUMENT_HOVER)(hover)
130129

131130
initialize_result: InitializeResult = super().lsp_initialize(params)
132131
workspace_options = initialization_options.workspace
@@ -160,7 +159,11 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
160159
super().__init__(*args, **kwargs)
161160

162161

163-
SERVER = JediLanguageServer(protocol_cls=JediLanguageServerProtocol)
162+
SERVER = JediLanguageServer(
163+
name="jedi-language-server",
164+
version=__version__,
165+
protocol_cls=JediLanguageServerProtocol,
166+
)
164167

165168

166169
# Server capabilities
@@ -178,7 +181,7 @@ def completion_item_resolve(
178181

179182

180183
@SERVER.feature(
181-
COMPLETION,
184+
TEXT_DOCUMENT_COMPLETION,
182185
CompletionOptions(
183186
trigger_characters=[".", "'", '"'], resolve_provider=True
184187
),
@@ -205,8 +208,10 @@ def completion(
205208
for comp in completions_jedi_raw
206209
if not any(i.match(comp.name) for i in ignore_patterns)
207210
)
208-
snippet_support = server.client_capabilities.get_capability(
209-
"text_document.completion.completion_item.snippet_support", False
211+
snippet_support = get_capability(
212+
server.client_capabilities,
213+
"text_document.completion.completion_item.snippet_support",
214+
False,
210215
)
211216
markup_kind = _choose_markup(server)
212217
is_import_context = jedi_utils.is_import(
@@ -244,7 +249,8 @@ def completion(
244249

245250

246251
@SERVER.feature(
247-
SIGNATURE_HELP, SignatureHelpOptions(trigger_characters=["(", ","])
252+
TEXT_DOCUMENT_SIGNATURE_HELP,
253+
SignatureHelpOptions(trigger_characters=["(", ","]),
248254
)
249255
def signature_help(
250256
server: JediLanguageServer, params: TextDocumentPositionParams
@@ -290,7 +296,7 @@ def signature_help(
290296
)
291297

292298

293-
@SERVER.feature(DEFINITION)
299+
@SERVER.feature(TEXT_DOCUMENT_DEFINITION)
294300
def definition(
295301
server: JediLanguageServer, params: TextDocumentPositionParams
296302
) -> Optional[List[Location]]:
@@ -311,7 +317,7 @@ def definition(
311317
return definitions if definitions else None
312318

313319

314-
@SERVER.feature(TYPE_DEFINITION)
320+
@SERVER.feature(TEXT_DOCUMENT_TYPE_DEFINITION)
315321
def type_definition(
316322
server: JediLanguageServer, params: TextDocumentPositionParams
317323
) -> Optional[List[Location]]:
@@ -328,7 +334,7 @@ def type_definition(
328334
return definitions if definitions else None
329335

330336

331-
@SERVER.feature(DOCUMENT_HIGHLIGHT)
337+
@SERVER.feature(TEXT_DOCUMENT_DOCUMENT_HIGHLIGHT)
332338
def highlight(
333339
server: JediLanguageServer, params: TextDocumentPositionParams
334340
) -> Optional[List[DocumentHighlight]]:
@@ -384,7 +390,7 @@ def hover(
384390
return Hover(contents=contents, range=_range)
385391

386392

387-
@SERVER.feature(REFERENCES)
393+
@SERVER.feature(TEXT_DOCUMENT_REFERENCES)
388394
def references(
389395
server: JediLanguageServer, params: TextDocumentPositionParams
390396
) -> Optional[List[Location]]:
@@ -401,7 +407,7 @@ def references(
401407
return locations if locations else None
402408

403409

404-
@SERVER.feature(DOCUMENT_SYMBOL)
410+
@SERVER.feature(TEXT_DOCUMENT_DOCUMENT_SYMBOL)
405411
def document_symbol(
406412
server: JediLanguageServer, params: DocumentSymbolParams
407413
) -> Optional[Union[List[DocumentSymbol], List[SymbolInformation]]]:
@@ -426,7 +432,8 @@ def document_symbol(
426432
document = server.workspace.get_document(params.text_document.uri)
427433
jedi_script = jedi_utils.script(server.project, document)
428434
names = jedi_script.get_names(all_scopes=True, definitions=True)
429-
if server.client_capabilities.get_capability(
435+
if get_capability(
436+
server.client_capabilities,
430437
"text_document.document_symbol.hierarchical_document_symbol_support",
431438
False,
432439
):
@@ -499,7 +506,7 @@ def workspace_symbol(
499506
return symbols if symbols else None
500507

501508

502-
@SERVER.feature(RENAME)
509+
@SERVER.feature(TEXT_DOCUMENT_RENAME)
503510
def rename(
504511
server: JediLanguageServer, params: RenameParams
505512
) -> Optional[WorkspaceEdit]:
@@ -518,7 +525,7 @@ def rename(
518525

519526

520527
@SERVER.feature(
521-
CODE_ACTION,
528+
TEXT_DOCUMENT_CODE_ACTION,
522529
CodeActionOptions(
523530
code_action_kinds=[
524531
CodeActionKind.RefactorInline,
@@ -702,7 +709,8 @@ def did_close_default(
702709
def _choose_markup(server: JediLanguageServer) -> MarkupKind:
703710
"""Returns the preferred or first of supported markup kinds."""
704711
markup_preferred = server.initialization_options.markup_kind_preferred
705-
markup_supported = server.client_capabilities.get_capability(
712+
markup_supported = get_capability(
713+
server.client_capabilities,
706714
"text_document.completion.completion_item.documentation_format",
707715
[MarkupKind.PlainText],
708716
)

jedi_language_server/text_edit_utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
from typing import Iterator, List, NamedTuple, Union
1212

1313
from jedi.api.refactoring import ChangedFile, Refactoring
14-
from pygls.lsp.types import (
14+
from lsprotocol.types import (
15+
AnnotatedTextEdit,
16+
CreateFile,
17+
DeleteFile,
18+
OptionalVersionedTextDocumentIdentifier,
1519
Position,
1620
Range,
1721
RenameFile,
1822
RenameFileOptions,
19-
ResourceOperationKind,
2023
TextDocumentEdit,
2124
TextEdit,
22-
VersionedTextDocumentIdentifier,
2325
)
2426
from pygls.workspace import Document, Workspace
2527

@@ -36,7 +38,7 @@ def is_valid_python(code: str) -> bool:
3638
def lsp_document_changes(
3739
workspace: Workspace,
3840
refactoring: Refactoring,
39-
) -> List[Union[TextDocumentEdit, RenameFile]]:
41+
) -> List[Union[TextDocumentEdit, RenameFile, CreateFile, DeleteFile]]:
4042
"""Get lsp text document edits from Jedi refactoring.
4143
4244
This is the main public function that you probably want
@@ -59,7 +61,7 @@ def lsp_renames(self) -> Iterator[RenameFile]:
5961
"""Get all File rename operations."""
6062
for old_name, new_name in self.refactoring.get_renames():
6163
yield RenameFile(
62-
kind=ResourceOperationKind.Rename,
64+
kind="rename",
6365
old_uri=old_name.as_uri(),
6466
new_uri=new_name.as_uri(),
6567
options=RenameFileOptions(
@@ -77,7 +79,7 @@ def lsp_text_document_edits(self) -> Iterator[TextDocumentEdit]:
7779
text_edits = lsp_text_edits(document, changed_file)
7880
if text_edits:
7981
yield TextDocumentEdit(
80-
text_document=VersionedTextDocumentIdentifier(
82+
text_document=OptionalVersionedTextDocumentIdentifier(
8183
uri=uri,
8284
version=version,
8385
),
@@ -90,7 +92,7 @@ def lsp_text_document_edits(self) -> Iterator[TextDocumentEdit]:
9092

9193
def lsp_text_edits(
9294
document: Document, changed_file: ChangedFile
93-
) -> List[TextEdit]:
95+
) -> List[Union[TextEdit, AnnotatedTextEdit]]:
9496
"""Take a jedi `ChangedFile` and convert to list of text edits.
9597
9698
Handles inserts, replaces, and deletions within a text file.
@@ -103,7 +105,7 @@ def lsp_text_edits(
103105

104106
old_code = document.source
105107
position_lookup = PositionLookup(old_code)
106-
text_edits = []
108+
text_edits: List[Union[TextEdit, AnnotatedTextEdit]] = []
107109
for opcode in get_opcodes(old_code, new_code):
108110
if opcode.op in _OPCODES_CHANGE:
109111
start = position_lookup.get(opcode.old_start)

jedi_language_server/type_map.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Jedi types mapped to LSP types."""
22

3-
from pygls.lsp.types import CompletionItemKind, SymbolKind
3+
from lsprotocol.types import CompletionItemKind, SymbolKind
44

55
# See docs:
66
# https://jedi.readthedocs.io/en/latest/docs/api-classes.html?highlight=type#jedi.api.classes.BaseName.type

0 commit comments

Comments
 (0)