Skip to content

Commit

Permalink
✨ Support document_link
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Feb 14, 2024
1 parent 2d93566 commit a05a7a4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

platformdirs
pygls
# get_completion_list_by_enum()
tree-sitter-lsp >= 0.0.9
# UNI.get_uri()
tree-sitter-lsp >= 0.0.11
tree-sitter-zathurarc >= 0.0.3
webcolors
1 change: 1 addition & 0 deletions src/zathura_language_server/assets/queries/import.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(path) @path
33 changes: 32 additions & 1 deletion src/zathura_language_server/finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
===========
"""

import os
from dataclasses import dataclass

from lsprotocol.types import DiagnosticSeverity
from tree_sitter_lsp.finders import ErrorFinder, SchemaFinder
from tree_sitter_lsp.finders import ErrorFinder, QueryFinder, SchemaFinder
from tree_sitter_zathurarc import language

from .schema import ZathurarcTrie
Expand Down Expand Up @@ -34,6 +35,36 @@ def __init__(
super().__init__(language, message, severity)


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

def __init__(
self,
message: str = "{{uni.get_text()}}: error",
severity: DiagnosticSeverity = DiagnosticSeverity.Information,
):
r"""Init.
: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",
),
"import.scm",
)
) as f:
text = f.read()
query = language.query(text)
super().__init__(query, message, severity)


@dataclass(init=False)
class ZathurarcFinder(SchemaFinder):
r"""Zathurarcfinder."""
Expand Down
18 changes: 17 additions & 1 deletion src/zathura_language_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
TEXT_DOCUMENT_COMPLETION,
TEXT_DOCUMENT_DID_CHANGE,
TEXT_DOCUMENT_DID_OPEN,
TEXT_DOCUMENT_DOCUMENT_LINK,
TEXT_DOCUMENT_HOVER,
CompletionItem,
CompletionItemKind,
CompletionList,
CompletionParams,
DidChangeTextDocumentParams,
DocumentLink,
DocumentLinkParams,
Hover,
MarkupContent,
MarkupKind,
Expand All @@ -28,7 +31,7 @@
from tree_sitter_lsp.finders import PositionFinder
from tree_sitter_zathurarc import parser

from .finders import DIAGNOSTICS_FINDER_CLASSES
from .finders import DIAGNOSTICS_FINDER_CLASSES, ImportZathurarcFinder
from .utils import get_schema


Expand Down Expand Up @@ -64,6 +67,19 @@ def did_change(params: DidChangeTextDocumentParams) -> None:
)
self.publish_diagnostics(params.text_document.uri, diagnostics)

@self.feature(TEXT_DOCUMENT_DOCUMENT_LINK)
def document_link(params: DocumentLinkParams) -> list[DocumentLink]:
r"""Get document links.
:param params:
:type params: DocumentLinkParams
:rtype: list[DocumentLink]
"""
document = self.workspace.get_document(params.text_document.uri)
return ImportZathurarcFinder().get_document_links(
document.uri, self.trees[document.uri]
)

@self.feature(TEXT_DOCUMENT_HOVER)
def hover(params: TextDocumentPositionParams) -> Hover | None:
r"""Hover.
Expand Down
13 changes: 13 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
r"""Test utils."""

import os

from tree_sitter_zathurarc import parser
from zathura_language_server.finders import ImportZathurarcFinder
from zathura_language_server.utils import get_schema


Expand All @@ -19,3 +23,12 @@ def test_get_schema() -> None:
.get("description", "")
.splitlines()
)

@staticmethod
def test_ImportZathurarcFinder() -> None:
with open(
os.path.join(os.path.dirname(__file__), "zathurarc"), "rb"
) as f:
text = f.read()
tree = parser.parse(text)
assert ImportZathurarcFinder().get_document_links("", tree)

0 comments on commit a05a7a4

Please sign in to comment.