diff options
Diffstat (limited to 'src/debputy/lsp/lsp_debian_rules.py')
-rw-r--r-- | src/debputy/lsp/lsp_debian_rules.py | 53 |
1 files changed, 22 insertions, 31 deletions
diff --git a/src/debputy/lsp/lsp_debian_rules.py b/src/debputy/lsp/lsp_debian_rules.py index 7f0e5fb..86b114c 100644 --- a/src/debputy/lsp/lsp_debian_rules.py +++ b/src/debputy/lsp/lsp_debian_rules.py @@ -15,8 +15,6 @@ from typing import ( from lsprotocol.types import ( CompletionItem, - DidOpenTextDocumentParams, - DidChangeTextDocumentParams, Diagnostic, Range, Position, @@ -27,6 +25,7 @@ from lsprotocol.types import ( TEXT_DOCUMENT_CODE_ACTION, ) +from debputy.debhelper_emulation import parse_drules_for_addons from debputy.lsp.lsp_features import ( lint_diagnostics, lsp_standard_handler, @@ -126,40 +125,26 @@ def _as_hook_targets(command_name: str) -> Iterable[str]: yield f"{prefix}{command_name}{suffix}" -def _diagnostics_debian_rules( - ls: "LanguageServer", - params: Union[DidOpenTextDocumentParams, DidChangeTextDocumentParams], -) -> None: - doc = ls.workspace.get_text_document(params.text_document.uri) - if not doc.path.endswith("debian/rules"): - return - lines = doc.lines - diagnostics = _lint_debian_rules( - doc.uri, - doc.path, - lines, - doc.position_codec, - ) - ls.publish_diagnostics( - doc.uri, - diagnostics, - ) - - lsp_standard_handler(_LANGUAGE_IDS, TEXT_DOCUMENT_CODE_ACTION) lsp_standard_handler(_LANGUAGE_IDS, TEXT_DOCUMENT_WILL_SAVE_WAIT_UNTIL) +def is_valid_file(path: str) -> bool: + # For debian/rules, the language ID is often set to makefile meaning we get random "non-debian/rules" + # makefiles here. Skip those. + return path.endswith("debian/rules") + + @lint_diagnostics(_LANGUAGE_IDS) -def _lint_debian_rules_via_debputy_lsp( +def _lint_debian_rules( doc_reference: str, path: str, lines: List[str], position_codec: LintCapablePositionCodec, ) -> Optional[List[Diagnostic]]: - if not path.endswith("debian/rules"): + if not is_valid_file(path): return None - return _lint_debian_rules( + return _lint_debian_rules_impl( doc_reference, path, lines, @@ -245,7 +230,7 @@ def iter_make_lines( yield line_no, line -def _lint_debian_rules( +def _lint_debian_rules_impl( _doc_reference: str, path: str, lines: List[str], @@ -259,7 +244,8 @@ def _lint_debian_rules( make_error = _run_make_dryrun(source_root, lines) if make_error is not None: diagnostics.append(make_error) - all_dh_commands = _all_dh_commands(source_root) + + all_dh_commands = _all_dh_commands(source_root, lines) if all_dh_commands: all_hook_targets = {ht for c in all_dh_commands for ht in _as_hook_targets(c)} all_hook_targets.update(_KNOWN_TARGETS) @@ -330,10 +316,15 @@ def _lint_debian_rules( return diagnostics -def _all_dh_commands(source_root: str) -> Optional[Sequence[str]]: +def _all_dh_commands(source_root: str, lines: List[str]) -> Optional[Sequence[str]]: + drules_sequences = set() + parse_drules_for_addons(lines, drules_sequences) + cmd = ["dh_assistant", "list-commands", "--output-format=json"] + if drules_sequences: + cmd.append(f"--with={','.join(drules_sequences)}") try: output = subprocess.check_output( - ["dh_assistant", "list-commands", "--output-format=json"], + cmd, stderr=subprocess.DEVNULL, cwd=source_root, ) @@ -364,7 +355,7 @@ def _debian_rules_completions( params: CompletionParams, ) -> Optional[Union[CompletionList, Sequence[CompletionItem]]]: doc = ls.workspace.get_text_document(params.text_document.uri) - if not doc.path.endswith("debian/rules"): + if not is_valid_file(doc.path): return None lines = doc.lines server_position = doc.position_codec.position_from_client_units( @@ -378,7 +369,7 @@ def _debian_rules_completions( return None source_root = os.path.dirname(os.path.dirname(doc.path)) - all_commands = _all_dh_commands(source_root) + all_commands = _all_dh_commands(source_root, lines) items = [CompletionItem(ht) for c in all_commands for ht in _as_hook_targets(c)] return items |