Skip to content

Commit bd51adb

Browse files
authored
Merge pull request pappasam#243 from pappasam/fix-tests-for-syntax-checking
Fix tests for syntax checking
2 parents 1fa4255 + 7bd439b commit bd51adb

File tree

8 files changed

+129
-104
lines changed

8 files changed

+129
-104
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
44

55
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).
66

7+
## 0.40.0
8+
9+
### Added
10+
11+
- Diagnostic support using Python's builtin `compile` function.
12+
13+
### Changed
14+
15+
- `pygls` 1.0!
16+
- Diagnostics (for syntax errors) are once-again enabled by default.
17+
718
## 0.39.0
819

920
### Changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ jedi-language-server aims to support Jedi's capabilities and expose them through
4242

4343
### Text Synchronization (for diagnostics)
4444

45-
**Note:** diagnostics are disabled by default because Jedi's syntax checking is not supported for new syntax in Python 3.10+. For a detailed discussion on this topic, please refer to [this issue](https://github.com/pappasam/jedi-language-server/issues/187).
46-
4745
- [textDocument/didChange](https://microsoft.github.io/language-server-protocol/specification#textDocument_didChange)
4846
- [textDocument/didOpen](https://microsoft.github.io/language-server-protocol/specification#textDocument_didOpen)
4947
- [textDocument/didSave](https://microsoft.github.io/language-server-protocol/specification#textDocument_didSave)
@@ -207,7 +205,7 @@ See coc-jedi's [configuration instructions](https://github.com/pappasam/coc-jedi
207205

208206
## Diagnostics
209207

210-
Jedi-powered diagnostics are disabled by default because Jedi's syntax checking is not supported for new syntax in Python 3.10+. For a detailed discussion on this topic, please refer to [this issue](https://github.com/pappasam/jedi-language-server/issues/187).
208+
Diagnostics are provided by Python's built-in `compile` function.
211209

212210
If you would like diagnostics (from [pylint](https://github.com/PyCQA/pylint), [mypy](https://github.com/python/mypy), etc.), we recommend using the powerful [diagnostic-language-server](https://github.com/iamcco/diagnostic-languageserver).
213211

jedi_language_server/initialization_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Completion(Model):
3838

3939

4040
class Diagnostics(Model):
41-
enable: bool = False
41+
enable: bool = True
4242
did_open: bool = True
4343
did_save: bool = True
4444
did_change: bool = True

jedi_language_server/jedi_utils.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
Translates pygls types back and forth with Jedi
44
"""
55

6+
import functools
7+
import inspect
68
import sys
9+
import threading
10+
from ast import PyCF_ONLY_AST
711
from inspect import Parameter
812
from typing import Dict, Iterator, List, Optional, Tuple
913

@@ -33,17 +37,14 @@
3337
from .initialization_options import HoverDisableOptions, InitializationOptions
3438
from .type_map import get_lsp_completion_type, get_lsp_symbol_type
3539

36-
import functools
37-
import threading
38-
import inspect
39-
40-
from ast import PyCF_ONLY_AST
4140

4241
def debounce(interval_s, keyed_by=None):
42+
"""Debounce calls to this function until interval_s seconds have passed.
43+
44+
Decorator copied from https://github.com/python-lsp/python-lsp-
45+
server
4346
"""
44-
Debounce calls to this function until interval_s seconds have passed.
45-
Decorator copied from https://github.com/python-lsp/python-lsp-server
46-
"""
47+
4748
def wrapper(func):
4849
timers = {}
4950
lock = threading.Lock()
@@ -67,7 +68,9 @@ def run():
6768
timer = threading.Timer(interval_s, run)
6869
timers[key] = timer
6970
timer.start()
71+
7072
return debounced
73+
7174
return wrapper
7275

7376

@@ -275,15 +278,22 @@ def lsp_diagnostic(error: jedi.api.errors.SyntaxError) -> Diagnostic:
275278
)
276279

277280

278-
def lsp_python_diagnostic(uri: str, source: str) -> Diagnostic:
281+
def lsp_python_diagnostic(uri: str, source: str) -> Optional[Diagnostic]:
279282
"""Get LSP Diagnostic using the compile builtin."""
280283
try:
281284
compile(source, uri, "exec", PyCF_ONLY_AST)
282285
return None
283286
except SyntaxError as err:
284-
column, line = err.offset - 1, err.lineno - 1
285-
until_column = getattr(err, "end_offset", 0) - 1
286-
until_line = getattr(err, "end_lineno", 0) - 1
287+
column = err.offset - 1 if err.offset is not None else 0
288+
line = err.lineno - 1 if err.lineno is not None else 0
289+
290+
_until_column = getattr(err, "end_offset", None)
291+
_until_line = getattr(err, "end_lineno", None)
292+
293+
until_column = (
294+
_until_column - 1 if _until_column is not None else column + 1
295+
)
296+
until_line = _until_line - 1 if _until_line is not None else line + 1
287297

288298
if (line, column) >= (until_line, until_column):
289299
until_column, until_line = column, line
@@ -294,7 +304,7 @@ def lsp_python_diagnostic(uri: str, source: str) -> Diagnostic:
294304
start=Position(line=line, character=column),
295305
end=Position(line=until_line, character=until_column),
296306
),
297-
message=err.__class__.__name__ + ': ' + str(err),
307+
message=err.__class__.__name__ + ": " + str(err),
298308
severity=DiagnosticSeverity.Error,
299309
source="compile",
300310
)

jedi_language_server/server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,14 +637,14 @@ def did_change_configuration(
637637
# Static capability or initializeOptions functions that rely on a specific
638638
# client capability or user configuration. These are associated with
639639
# JediLanguageServer within JediLanguageServerProtocol.lsp_initialize
640-
@jedi_utils.debounce(1, keyed_by='uri')
640+
@jedi_utils.debounce(1, keyed_by="uri")
641641
def _publish_diagnostics(server: JediLanguageServer, uri: str) -> None:
642642
"""Helper function to publish diagnostics for a file."""
643643
# The debounce decorator delays the execution by 1 second
644644
# canceling notifications that happen in that interval.
645645
# Since this function is executed after a delay, we need to check
646646
# whether the document still exists
647-
if not (uri in server.workspace.documents):
647+
if uri not in server.workspace.documents:
648648
return
649649

650650
doc = server.workspace.get_document(uri)
@@ -653,6 +653,7 @@ def _publish_diagnostics(server: JediLanguageServer, uri: str) -> None:
653653

654654
server.publish_diagnostics(uri, diagnostics)
655655

656+
656657
# TEXT_DOCUMENT_DID_SAVE
657658
def did_save_diagnostics(
658659
server: JediLanguageServer, params: DidSaveTextDocumentParams

0 commit comments

Comments
 (0)