Skip to content

Commit

Permalink
Merge pull request #293 from pappasam/fix-diagnostics
Browse files Browse the repository at this point in the history
Ensure that diagnostic position cannot be less than 0
  • Loading branch information
pappasam committed Nov 18, 2023
2 parents 2ca1f8b + 2d0d26b commit f531dd7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 155 deletions.
2 changes: 1 addition & 1 deletion jedi_language_server/initialization_options.py
Expand Up @@ -141,7 +141,7 @@ def structure(cls: type) -> Any:
return make_dict_structure_fn(
cls,
initialization_options_converter,
**{
**{ # type: ignore[arg-type]
a.name: override(rename=convert_class_keys(a.name))
for a in fields(cls)
},
Expand Down
34 changes: 17 additions & 17 deletions jedi_language_server/jedi_utils.py
Expand Up @@ -9,14 +9,21 @@
import threading
from ast import PyCF_ONLY_AST
from inspect import Parameter
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple
from typing import Any, Callable, Dict, Iterator, List, Optional, Tuple, Union

import docstring_to_markdown
import jedi.api.errors
import jedi.inference.references
import jedi.settings
from jedi import Project, Script
from jedi.api.classes import BaseName, Completion, Name, ParamName, Signature
from jedi.api.classes import (
BaseName,
BaseSignature,
Completion,
Name,
ParamName,
Signature,
)
from lsprotocol.types import (
CompletionItem,
CompletionItemKind,
Expand Down Expand Up @@ -298,21 +305,19 @@ def lsp_python_diagnostic(uri: str, source: str) -> Optional[Diagnostic]:
compile(source, uri, "exec", PyCF_ONLY_AST)
return None
except SyntaxError as err:
column = err.offset - 1 if err.offset is not None else 0
line = err.lineno - 1 if err.lineno is not None else 0

column = max(0, err.offset - 1 if err.offset is not None else 0)
line = max(0, err.lineno - 1 if err.lineno is not None else 0)
_until_column = getattr(err, "end_offset", None)
_until_line = getattr(err, "end_lineno", None)

until_column = (
_until_column - 1 if _until_column is not None else column + 1
until_column = max(
0, _until_column - 1 if _until_column is not None else column + 1
)
until_line = max(
0, _until_line - 1 if _until_line is not None else line
)
until_line = _until_line - 1 if _until_line is not None else line

if (line, column) >= (until_line, until_column):
until_column, until_line = column, line
column = 0

return Diagnostic(
range=Range(
start=Position(line=line, character=column),
Expand Down Expand Up @@ -419,7 +424,7 @@ def clean_completion_name(name: str, char_before_cursor: str) -> str:
_PARAM_NAME_IGNORE = {"/", "*"}


def get_snippet_signature(signature: Signature) -> str:
def get_snippet_signature(signature: Union[Signature, BaseSignature]) -> str:
"""Return the snippet signature."""
params: List[ParamName] = signature.params
if not params:
Expand Down Expand Up @@ -527,11 +532,6 @@ def _md_bold(value: str, markup_kind: MarkupKind) -> str:
return f"**{value}**" if markup_kind == MarkupKind.Markdown else value


def _md_italic(value: str, markup_kind: MarkupKind) -> str:
"""Add italic surrounding when markup_kind is markdown."""
return f"*{value}*" if markup_kind == MarkupKind.Markdown else value


def _md_text(value: str, markup_kind: MarkupKind) -> str:
"""Surround a markdown string with a Python fence."""
return (
Expand Down

0 comments on commit f531dd7

Please sign in to comment.