Skip to content

Commit

Permalink
👽 Update tree-sitter-lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Oct 25, 2023
1 parent 88836ec commit 4d3e819
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 163 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ classifiers = [
# dynamic = ["version", "dependencies", "optional-dependencies"]
# https://github.com/pypa/twine/issues/753
dynamic = ["version"]
dependencies = ["platformdirs", "pygls", "tree-sitter"]
dependencies = ["colorama", "jinja2", "jsonschema", "platformdirs", "pygls", "tree-sitter"]

[project.optional-dependencies]
dev = ["pytest-cov"]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

colorama
jinja2
jsonschema
platformdirs
pygls
tree-sitter
10 changes: 9 additions & 1 deletion src/autotools_language_server/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from .parser import parse as _parse
from .tree_sitter_lsp import UNI, Finder
from .tree_sitter_lsp.finders import RepeatedFinder
from .tree_sitter_lsp.finders import ErrorFinder, MissingFinder, RepeatedFinder


class InvalidPathFinder(Finder):
Expand Down Expand Up @@ -300,3 +300,11 @@ def __call__(self, uni: UNI) -> bool:
:rtype: bool
"""
return self.is_reference(uni)


DIAGNOSTICS_FINDER_CLASSES = [
ErrorFinder,
MissingFinder,
InvalidPathFinder,
RepeatedTargetFinder,
]
10 changes: 6 additions & 4 deletions src/autotools_language_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
from pygls.server import LanguageServer

from .documents import get_document, get_filetype
from .finders import DefinitionFinder, ReferenceFinder
from .finders import (
DIAGNOSTICS_FINDER_CLASSES,
DefinitionFinder,
ReferenceFinder,
)
from .parser import parse
from .tree_sitter_lsp import UNI
from .tree_sitter_lsp.diagnose import get_diagnostics
from .tree_sitter_lsp.finders import PositionFinder
from .utils import DIAGNOSTICS_FINDERS


class AutotoolsLanguageServer(LanguageServer):
Expand Down Expand Up @@ -71,9 +73,9 @@ def did_change(params: DidChangeTextDocumentParams) -> None:
document = self.workspace.get_document(params.text_document.uri)
self.trees[document.uri] = parse(document.source.encode())
diagnostics = get_diagnostics(
DIAGNOSTICS_FINDERS,
document.uri,
self.trees[document.uri],
DIAGNOSTICS_FINDER_CLASSES,
)
self.publish_diagnostics(params.text_document.uri, diagnostics)

Expand Down
48 changes: 13 additions & 35 deletions src/autotools_language_server/tree_sitter_lsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import os
from copy import deepcopy
from dataclasses import dataclass
from typing import Any

from jinja2 import Template
Expand All @@ -22,37 +23,20 @@
LEVEL = 5


@dataclass
class UNI:
r"""Unified node identifier."""

def __init__(self, uri: str, node: Node) -> None:
r"""Init.
uri: str
node: Node

:param uri:
:type uri: str
:param node:
:type node: Node
:rtype: None
"""
self.uri = uri
self.node = node

def __repr__(self) -> str:
r"""Repr.
def __str__(self) -> str:
r"""Str.
:rtype: str
"""
return f"{self.get_text()}@{self.uri}:{self.node.start_point[0] + 1}:{self.node.start_point[1] + 1}-{self.node.end_point[0] + 1}:{self.node.end_point[1]}"

def __eq__(self, that: "UNI") -> bool:
r"""Eq.
:param that:
:type that: UNI
:rtype: bool
"""
return self.node == that.node

def get_text(self) -> str:
r"""Get text.
Expand Down Expand Up @@ -170,25 +154,18 @@ def join(path, text) -> str:
return os.path.join(os.path.dirname(path), text)


@dataclass
class Finder:
r"""Finder."""

def __init__(
self,
message: str = "",
severity: DiagnosticSeverity = DiagnosticSeverity.Error,
) -> None:
r"""Init.
message: str = ""
severity: DiagnosticSeverity = DiagnosticSeverity.Error

def __post_init__(self) -> None:
r"""Post init.
:param message:
:type message: str
:param severity:
:type severity: DiagnosticSeverity
:rtype: None
"""
self.level = 0
self.message = message
self.severity = severity
self.reset()

def __call__(self, uni: UNI) -> bool:
Expand Down Expand Up @@ -330,6 +307,7 @@ def reset(self) -> None:
:rtype: None
"""
self.level = 0
self.unis = []

def prepare(
Expand Down
91 changes: 80 additions & 11 deletions src/autotools_language_server/tree_sitter_lsp/diagnose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
r"""Diagnose
============
Wrap
``Diagnostic <https://microsoft.github.io/language-server-protocol/specifications/specification-current#diagnostic>``_
to a linter.
"""
import sys
from typing import Callable, Literal
Expand All @@ -8,19 +12,20 @@
from tree_sitter import Tree

from . import Finder
from .utils import get_finders, get_paths


def get_diagnostics(
finders: list[Finder], uri: str, tree: Tree
def get_diagnostics_by_finders(
uri: str, tree: Tree, finders: list[Finder]
) -> list[Diagnostic]:
r"""Get diagnostics.
r"""Get diagnostics by finders.
:param finders:
:type finders: list[Finder]
:param uri:
:type uri: str
:param tree:
:type tree: Tree
:param finders:
:type finders: list[Finder]
:rtype: list[Diagnostic]
"""
return [
Expand All @@ -30,6 +35,35 @@ def get_diagnostics(
]


def get_diagnostics(
uri: str,
tree: Tree,
classes: list[type[Finder]] | None = None,
filetype: str | None = None,
) -> list[Diagnostic]:
r"""Get diagnostics.
:param uri:
:type uri: str
:param tree:
:type tree: Tree
:param classes:
:type classes: list[type[Finder]] | None
:param filetype:
:type filetype: str | None
:rtype: list[Diagnostic]
"""
finders, finder_classes = get_finders(classes)
if filetype is None:
return get_diagnostics_by_finders(uri, tree, finders)
return [
diagnostic
for diagnostic in get_diagnostics_by_finders(
uri, tree, finders + [cls(filetype) for cls in finder_classes]
)
]


def count_level(
diagnostics: list[Diagnostic],
level: DiagnosticSeverity = DiagnosticSeverity.Warning,
Expand Down Expand Up @@ -95,20 +129,20 @@ def diagnostics2linter_messages(
]


def check(
def check_by_finders(
paths: list[str],
finders: list[Finder],
parse: Callable[[bytes], Tree],
finders: list[Finder],
color: Literal["auto", "always", "never"] = "auto",
) -> int:
r"""Check.
r"""Check by finders.
:param paths:
:type paths: list[str]
:param finders:
:type finders: list[Finder]
:param parse:
:type parse: Callable[[bytes], Tree]
:param finders:
:type finders: list[Finder]
:param color:
:type color: Literal["auto", "always", "never"]
:rtype: int
Expand All @@ -119,8 +153,43 @@ def check(
with open(path, "rb") as f:
src = f.read()
tree = parse(src)
diagnostics = get_diagnostics(finders, path, tree)
diagnostics = get_diagnostics_by_finders(path, tree, finders)
count += count_level(diagnostics)
lines += diagnostics2linter_messages(path, diagnostics, color)
print("\n".join(lines))
return count


def check(
paths: list[str],
parse: Callable[[bytes], Tree],
classes: list[type[Finder]] | None = None,
get_filetype: Callable[[str], str] | None = None,
color: Literal["auto", "always", "never"] = "auto",
) -> int:
r"""Check.
:param paths:
:type paths: list[str]
:param parse:
:type parse: Callable[[bytes], Tree]
:param classes:
:type classes: list[type[Finder]] | None
:param get_filetype:
:type get_filetype: Callable[[str], str] | None
:param color:
:type color: Literal["auto", "always", "never"]
:rtype: int
"""
finders, finder_classes = get_finders(classes)
if get_filetype is None:
return check_by_finders(paths, parse, finders, color)
return sum(
check_by_finders(
filepaths,
parse,
finders + [cls(filetype) for cls in finder_classes],
color,
)
for filetype, filepaths in get_paths(paths, get_filetype).items()
)
Loading

0 comments on commit 4d3e819

Please sign in to comment.