Skip to content

Commit

Permalink
⚡ Use tree-sitter-query
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Oct 30, 2023
1 parent 66bee7f commit 835c875
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
3 changes: 3 additions & 0 deletions requirements/languages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env -S pip install -r

tree-sitter-languages
2 changes: 2 additions & 0 deletions src/tree_sitter_lsp/assets/queries/error.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ERROR) @error
; ex: filetype=query
97 changes: 76 additions & 21 deletions src/tree_sitter_lsp/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Range,
TextEdit,
)
from tree_sitter import Node, Tree
from tree_sitter import Language, Node, Tree
from tree_sitter.binding import Query

from . import UNI, Finder
Expand All @@ -44,26 +44,6 @@ def __call__(self, uni: UNI) -> bool:
)


@dataclass
class ErrorFinder(Finder):
r"""Errorfinder."""

message: str = "{{uni.get_text()}}: error"
severity: DiagnosticSeverity = DiagnosticSeverity.Error

def __call__(self, uni: UNI) -> bool:
r"""Call.
:param uni:
:type uni: UNI
:rtype: bool
"""
node = uni.node
return node.has_error and not (
any(child.has_error for child in node.children)
)


@dataclass
class NotFileFinder(Finder):
r"""NotFilefinder."""
Expand Down Expand Up @@ -619,3 +599,78 @@ def captures2uni(self, capture: tuple[Node, str], uri: str) -> UNI:
:rtype: UNI
"""
return UNI(uri, capture[0])


@dataclass(init=False)
class ErrorFinder(QueryFinder):
r"""Errorfinder."""

def __init__(
self,
language: Language | None = None,
message: str = "{{uni.get_text()}}: error",
severity: DiagnosticSeverity = DiagnosticSeverity.Error,
):
r"""Init.
:param language:
:type language: Language | None
:param message:
:type message: str
:param severity:
:type severity: DiagnosticSeverity
"""
with open(
os.path.join(
os.path.join(
os.path.join(os.path.dirname(__file__), "assets"),
"queries",
),
"error.scm",
)
) as f:
text = f.read()
if language is not None:
query = language.query(text)
super().__init__(query, message, severity)
else:
Finder.__init__(self, message, severity)
self.find_all = Finder().find_all

def __call__(self, uni: UNI) -> bool:
r"""Call.
:param uni:
:type uni: UNI
:rtype: bool
"""
node = uni.node
return node.has_error and not (
any(child.has_error for child in node.children)
)


@dataclass(init=False)
class ErrorQueryFinder(ErrorFinder):
r"""Errorqueryfinder."""

def __init__(
self,
filetype: str,
message: str = "{{uni.get_text()}}: error",
severity: DiagnosticSeverity = DiagnosticSeverity.Error,
) -> None:
r"""Init.
:param filetype:
:type filetype: str
:param message:
:type message: str
:param severity:
:type severity: DiagnosticSeverity
:rtype: None
"""
from tree_sitter_languages import get_language

language = get_language(filetype)
super().__init__(language, message, severity)
4 changes: 2 additions & 2 deletions src/tree_sitter_lsp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def get_finders(
:rtype: tuple[list[Finder], list[type[Finder]]]
"""
if classes is None:
from .finders import ErrorFinder, MissingFinder
from .finders import ErrorFinder

classes = [ErrorFinder, MissingFinder]
classes = [ErrorFinder]

finders = []
finder_classes = []
Expand Down

0 comments on commit 835c875

Please sign in to comment.