update lsp_server

This commit is contained in:
Christoph Brandau
2025-07-31 16:19:09 +02:00
parent 44fd772151
commit 98e2d104a8
8 changed files with 299 additions and 200 deletions
+52 -35
View File
@@ -1,8 +1,10 @@
import enum
from typing import List
from tclint.syntax_tree import Visitor
from tclint.syntax_tree import Visitor, QuotedWord, Command, BareWord
from tclint.commands import get_commands
import attrs
from common.load_data import standard_items
from tools.completion_items import completion
class TokenModifier(enum.IntFlag):
@@ -10,6 +12,7 @@ class TokenModifier(enum.IntFlag):
readonly = enum.auto()
defaultLibrary = enum.auto()
definition = enum.auto()
declaration = enum.auto()
@attrs.define
@@ -30,6 +33,8 @@ TOKEN_TYPES = [
"parameter",
"type",
"class",
"string",
"parameter",
]
@@ -39,26 +44,52 @@ class _Highlighter(Visitor):
self._tokens = []
self.log_to_output = log_to_output
def visit_command(self, command):
def visit_quoted_word(self, word: QuotedWord):
if not word.contents:
return
line, col = word.contents_pos
self._tokens.append(((line - 1, col - 1), len(word.contents), "string", []))
pass
def visit_bare_word(self, word: BareWord):
if any(item.label == word.value for item in standard_items.nx_procs) or any(
item.label == word.value for item in completion.custom_functions
):
line, col = word.pos
self._tokens.append(
(((line - 1, col - 1), len(word.value), "function", []))
)
def visit_command(self, command: Command):
routine = command.routine
self.log_to_output(str(command.routine))
if routine.contents in self._commands:
line, col = routine.pos
self._tokens.append(((line - 1, col - 1), len(routine.contents), "keyword"))
if routine.contents == "set" and command.args:
first_arg = command.args[0]
if hasattr(first_arg, "pos") and hasattr(first_arg, "value"):
line, col = first_arg.pos
self._tokens.append(
(((line - 1, col - 1), len(first_arg.value), "variable"))
(
(
(line - 1, col - 1),
len(first_arg.value),
"variable",
[TokenModifier.declaration],
)
)
)
if routine.contents == "proc" and command.args:
first_arg = command.args[0]
if hasattr(first_arg, "pos") and hasattr(first_arg, "value"):
line, col = first_arg.pos
self._tokens.append(
(((line - 1, col - 1), len(first_arg.value), "function"))
(
(
(line - 1, col - 1),
len(first_arg.value),
"function",
[TokenModifier.declaration],
)
)
)
if len(command.args) >= 2:
@@ -71,7 +102,12 @@ class _Highlighter(Visitor):
if hasattr(child, "value") and child.value is not None:
line, col = child.pos
self._tokens.append(
((line - 1, col - 1), len(child.value), "variable")
(
(line - 1, col - 1),
len(child.value),
"parameter",
[TokenModifier.declaration],
)
)
# Parameter mit Default-Wert ist meist eine List (z.B. {arg default})
@@ -85,7 +121,8 @@ class _Highlighter(Visitor):
(
(line - 1, col - 1),
len(name_node.value),
"variable",
"parameter",
[TokenModifier.declaration],
)
)
@@ -94,7 +131,7 @@ class _Highlighter(Visitor):
if hasattr(first_arg, "pos") and hasattr(first_arg, "value"):
line, col = first_arg.pos
self._tokens.append(
(((line - 1, col - 1), len(first_arg.value), "class"))
(((line - 1, col - 1), len(first_arg.value), "class", []))
)
def visit_var_sub(self, var_sub):
@@ -108,35 +145,15 @@ class _Highlighter(Visitor):
tokens = []
last_line = 0
last_col = 0
for (line, col), length, tok_type in sorted(self._tokens, key=lambda x: x[0]):
for (line, col), length, tok_type, tok_modifier in sorted(
self._tokens, key=lambda x: x[0]
):
line_delta = line - last_line
col_delta = col
if line == last_line:
col_delta -= last_col
tokens.append(Token(line_delta, col_delta, length, tok_type))
tokens.append(Token(line_delta, col_delta, length, tok_type, tok_modifier))
last_line, last_col = line, col
return tokens
# @server.feature(
# lsp.TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL,
# lsp.SemanticTokensLegend(token_types=["keyword"], token_modifiers=[]),
# )
# def semantic_tokens(ls: TclspServer, params: lsp.SemanticTokensParams):
# logging.debug("Received %s: %s", lsp.TEXT_DOCUMENT_SEMANTIC_TOKENS_FULL, params)
# document = ls.workspace.get_text_document(params.text_document.uri)
# path = Path(document.path)
# root = ls.get_root(path)
# config = ls.get_config(path, root)
# plugins = [config.commands] if config.commands is not None else []
# parser = Parser(command_plugins=plugins)
# hl = _Highlighter(plugins)
# tree = parser.parse(document.source)
# tree.accept(hl, recurse=True)
# return lsp.SemanticTokens(data=hl.tokens())