feat(lsp): add Tcl command-aware completion and integration
- Introduced a Tcl command-aware completion engine and wired to LSP. - Added a new module with Tcl commands, options, and contextual matching. - Updated completion to favor command-aware items and args.
This commit is contained in:
@@ -6,9 +6,11 @@ from collections.abc import Iterable, Sequence
|
||||
from enum import Enum
|
||||
|
||||
import lsprotocol.types as lsp
|
||||
from common.load_data import standard_items
|
||||
from tclint.syntax_tree import BareWord, Command, List, Visitor
|
||||
|
||||
from common.load_data import standard_items
|
||||
from tools.tcl_command_completion import line_prefix_at_position
|
||||
|
||||
BUILTIN_VAR_LABELS = {ci.label for ci in standard_items.nx_variables}
|
||||
BUILTIN_PROC_LABELS = {ci.label for ci in standard_items.nx_procs}
|
||||
|
||||
@@ -35,27 +37,12 @@ _VARIABLE_PREFIX_RE = re.compile(r"(?<!\\)\$(?:\{)?[A-Za-z0-9_:]*$")
|
||||
_COMMAND_PREFIX_RE = re.compile(r"(?:^|[;\[\{])\s*[^\s;\[\]\{\}]*$")
|
||||
|
||||
|
||||
def _codepoint_offset(line: str, utf16_offset: int) -> int:
|
||||
"""Translate an LSP UTF-16 character offset into a Python string offset."""
|
||||
if utf16_offset <= 0:
|
||||
return 0
|
||||
|
||||
units = 0
|
||||
for offset, character in enumerate(line):
|
||||
units += 2 if ord(character) > 0xFFFF else 1
|
||||
if units >= utf16_offset:
|
||||
return offset + 1
|
||||
return len(line)
|
||||
|
||||
|
||||
def completion_context(
|
||||
source_lines: Sequence[str], position: lsp.Position
|
||||
) -> CompletionContext:
|
||||
if position.line < 0 or position.line >= len(source_lines):
|
||||
prefix = line_prefix_at_position(source_lines, position)
|
||||
if prefix is None:
|
||||
return CompletionContext.GENERAL
|
||||
|
||||
line = source_lines[position.line]
|
||||
prefix = line[: _codepoint_offset(line, position.character)]
|
||||
if _VARIABLE_PREFIX_RE.search(prefix):
|
||||
return CompletionContext.VARIABLE
|
||||
if _COMMAND_PREFIX_RE.search(prefix):
|
||||
|
||||
Reference in New Issue
Block a user