diff options
Diffstat (limited to 'src/debputy/lsp/lsp_dispatch.py')
-rw-r--r-- | src/debputy/lsp/lsp_dispatch.py | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/src/debputy/lsp/lsp_dispatch.py b/src/debputy/lsp/lsp_dispatch.py index 9a54c2b..8f370ef 100644 --- a/src/debputy/lsp/lsp_dispatch.py +++ b/src/debputy/lsp/lsp_dispatch.py @@ -5,12 +5,25 @@ from typing import ( Union, Optional, TypeVar, - Callable, Mapping, List, TYPE_CHECKING, ) +from debputy import __version__ +from debputy.lsp.lsp_features import ( + DIAGNOSTIC_HANDLERS, + COMPLETER_HANDLERS, + HOVER_HANDLERS, + SEMANTIC_TOKENS_FULL_HANDLERS, + CODE_ACTION_HANDLERS, + SEMANTIC_TOKENS_LEGEND, + WILL_SAVE_WAIT_UNTIL_HANDLERS, + FORMAT_FILE_HANDLERS, + _DispatchRule, + C, +) +from debputy.util import _info from lsprotocol.types import ( DidOpenTextDocumentParams, DidChangeTextDocumentParams, @@ -39,19 +52,6 @@ from lsprotocol.types import ( TEXT_DOCUMENT_FORMATTING, ) -from debputy import __version__ -from debputy.lsp.lsp_features import ( - DIAGNOSTIC_HANDLERS, - COMPLETER_HANDLERS, - HOVER_HANDLERS, - SEMANTIC_TOKENS_FULL_HANDLERS, - CODE_ACTION_HANDLERS, - SEMANTIC_TOKENS_LEGEND, - WILL_SAVE_WAIT_UNTIL_HANDLERS, - FORMAT_FILE_HANDLERS, -) -from debputy.util import _info - _DOCUMENT_VERSION_TABLE: Dict[str, int] = {} @@ -115,15 +115,17 @@ async def _open_or_changed_document( doc = ls.workspace.get_text_document(doc_uri) _DOCUMENT_VERSION_TABLE[doc_uri] = version - id_source, language_id = ls.determine_language_id(doc) - handler = DIAGNOSTIC_HANDLERS.get(language_id) + id_source, language_id, normalized_filename = ls.determine_language_id(doc) + handler = _resolve_handler(DIAGNOSTIC_HANDLERS, language_id, normalized_filename) if handler is None: _info( - f"Opened/Changed document: {doc.path} ({language_id}, {id_source}) - no diagnostics handler" + f"Opened/Changed document: {doc.path} ({language_id}, {id_source}," + f" normalized filename: {normalized_filename}) - no diagnostics handler" ) return _info( - f"Opened/Changed document: {doc.path} ({language_id}, {id_source}) - running diagnostics for doc version {version}" + f"Opened/Changed document: {doc.path} ({language_id}, {id_source}, normalized filename: {normalized_filename})" + f" - running diagnostics for doc version {version}" ) last_publish_count = -1 @@ -253,23 +255,39 @@ def _dispatch_standard_handler( ls: "DebputyLanguageServer", doc_uri: str, params: P, - handler_table: Mapping[str, Callable[[L, P], R]], + handler_table: Mapping[str, List[_DispatchRule[C]]], request_type: str, ) -> Optional[R]: doc = ls.workspace.get_text_document(doc_uri) - id_source, language_id = ls.determine_language_id(doc) - handler = handler_table.get(language_id) + id_source, language_id, normalized_filename = ls.determine_language_id(doc) + handler = _resolve_handler(handler_table, language_id, normalized_filename) if handler is None: _info( - f"{request_type} for document: {doc.path} ({language_id}, {id_source}) - no handler" + f"{request_type} for document: {doc.path} ({language_id}, {id_source}," + f" normalized filename: {normalized_filename}) - no handler" ) return None _info( - f"{request_type} for document: {doc.path} ({language_id}, {id_source}) - delegating to handler" + f"{request_type} for document: {doc.path} ({language_id}, {id_source}," + f" normalized filename: {normalized_filename}) - delegating to handler" ) return handler( ls, params, ) + + +def _resolve_handler( + handler_table: Mapping[str, List[_DispatchRule[C]]], + language_id: str, + normalized_filename: str, +) -> Optional[C]: + dispatch_rules = handler_table.get(language_id) + if not dispatch_rules: + return None + for dispatch_rule in dispatch_rules: + if dispatch_rule.language_dispatch.filename_match(normalized_filename): + return dispatch_rule.handler + return None |