diff --git a/server/src/lsp_server.py b/server/src/lsp_server.py index 4946446..0f45819 100644 --- a/server/src/lsp_server.py +++ b/server/src/lsp_server.py @@ -49,7 +49,7 @@ from tools import checks, parser from tools.semantic_tokens import _Highlighter, TOKEN_TYPES, TokenModifier from tools.completion_items import completion from tools.inlay_hint import InlayHintGenerator - +from tools.file_sourcing import get_all_psc_files, read_psc_file, SourcedFile DIAGNOSTIC_SOURCE = "nx-post-support" @@ -61,6 +61,7 @@ class TclLanguageServer(server.LanguageServer): for command in commands: self.parser._commands.update(command) self.diagnostics = {} + self.poco_completion: dict = {} def format( self, @@ -190,17 +191,6 @@ def did_open(params: lsp.DidOpenTextDocumentParams) -> None: """LSP handler for textDocument/didOpen request.""" document = LSP_SERVER.workspace.get_text_document(params.text_document.uri) LSP_SERVER.compute_diagnostics(document) - completion.reset() - # all_tcl_files = get_all_tcl_files(LSP_SERVER.workspace.root_path) - - # for filepath in all_tcl_files: - # try: - # with open(filepath, "r", encoding="utf-8") as f: - # source = f.read() - # tree = LSP_SERVER.parser.parse(source) - # tree.accept(completion, recurse=True) - # except Exception as e: - # log_to_output(f"Fehler beim Parsen von {filepath}: {e}") @LSP_SERVER.feature(lsp.TEXT_DOCUMENT_DID_SAVE) @@ -219,9 +209,6 @@ def did_change(params: lsp.DidChangeTextDocumentParams) -> None: """LSP handler for textDocument/didChange request""" document = LSP_SERVER.workspace.get_text_document(params.text_document.uri) LSP_SERVER.compute_diagnostics(document) - # completion.reset() - # tree = LSP_SERVER.parser.parse(document.source) - # tree.accept(completion, recurse=True) @LSP_SERVER.feature( @@ -252,12 +239,15 @@ def document_diagnostic(params: lsp.DocumentDiagnosticParams): @LSP_SERVER.feature(lsp.TEXT_DOCUMENT_COMPLETION) def on_completion(params: lsp.CompletionParams) -> list[lsp.CompletionItem]: _ = LSP_SERVER.workspace.get_text_document(params.text_document.uri) + poco = [] + for key, value in LSP_SERVER.poco_completion.items(): + poco.extend(value) items = ( standard_items.tcl_keyword_list + standard_items.nx_procs + standard_items.nx_variables - + completion.custom_functions + + poco ) return lsp.CompletionList(is_incomplete=False, items=items) @@ -455,8 +445,31 @@ def initialize(params: lsp.InitializeParams) -> lsp.InitializeResult: @LSP_SERVER.feature(lsp.INITIALIZED) -def initialized(params: lsp.InitializedParams) -> lsp.InitializeResult: - pass +def initialized(params: lsp.InitializedParams): + root = LSP_SERVER.workspace.root_path + psc_files = get_all_psc_files(pathlib.Path(root)) + for psc_file in psc_files: + poco_files = read_psc_file(psc_file) + for sourced_layer in poco_files: + completion.reset() + try: + file_root = pathlib.Path(root).joinpath( + sourced_layer.subfolder if sourced_layer.subfolder else "" + ) + for tcl_file in sourced_layer.files: + filepath = pathlib.Path(file_root).joinpath(f"{tcl_file}.tcl") + if not filepath.exists(): + continue + completion.reset() + source = filepath.read_text(encoding="utf-8") + tree = LSP_SERVER.parser.parse(source) + tree.accept(completion, recurse=True) + + LSP_SERVER.poco_completion[str(filepath)] = ( + completion.custom_functions + ) + except Exception as e: + log_to_output(f"Fehler beim Parsen von {filepath}: {e}") @LSP_SERVER.feature(lsp.EXIT) diff --git a/server/src/tools/file_sourcing.py b/server/src/tools/file_sourcing.py index 1cdeab4..8db75b3 100644 --- a/server/src/tools/file_sourcing.py +++ b/server/src/tools/file_sourcing.py @@ -37,7 +37,6 @@ def read_psc_file(psc_file: Path) -> List[SourcedFile]: def get_all_psc_files(root_path: Path) -> list[Path]: - """Sammelt alle .tcl-Dateien rekursiv im gegebenen Verzeichnis""" return [path for path in root_path.rglob("*.psc")] diff --git a/server/src/tools/formatter.py b/server/src/tools/formatter.py new file mode 100644 index 0000000..e69de29