Coverage for src/debputy/lsp/languages/lsp_debian_tests_control.py: 78%

60 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-09-07 09:27 +0000

1import re 

2from typing import ( 

3 Union, 

4 Sequence, 

5 Optional, 

6) 

7 

8from debputy.linting.lint_util import LintState, with_range_in_continuous_parts 

9from debputy.lsp.debputy_ls import DebputyLanguageServer 

10from debputy.lsp.lsp_debian_control_reference_data import ( 

11 DTestsCtrlFileMetadata, 

12) 

13from debputy.lsp.lsp_features import ( 

14 lint_diagnostics, 

15 lsp_completer, 

16 lsp_hover, 

17 lsp_standard_handler, 

18 lsp_folding_ranges, 

19 lsp_semantic_tokens_full, 

20 lsp_will_save_wait_until, 

21 lsp_format_document, 

22 SecondaryLanguage, 

23 LanguageDispatchRule, 

24 lsp_cli_reformat_document, 

25) 

26from debputy.lsp.lsp_generic_deb822 import ( 

27 deb822_completer, 

28 deb822_hover, 

29 deb822_folding_ranges, 

30 deb822_semantic_tokens_full, 

31 deb822_format_file, 

32 scan_for_syntax_errors_and_token_level_diagnostics, 

33) 

34from debputy.lsp.vendoring._deb822_repro import Deb822ParagraphElement 

35from debputy.lsprotocol.types import ( 

36 CompletionItem, 

37 CompletionList, 

38 CompletionParams, 

39 HoverParams, 

40 Hover, 

41 TEXT_DOCUMENT_CODE_ACTION, 

42 SemanticTokens, 

43 SemanticTokensParams, 

44 FoldingRangeParams, 

45 FoldingRange, 

46 WillSaveTextDocumentParams, 

47 TextEdit, 

48 DocumentFormattingParams, 

49) 

50 

51try: 

52 from debputy.lsp.vendoring._deb822_repro.locatable import ( 

53 Position as TEPosition, 

54 Range as TERange, 

55 START_POSITION, 

56 ) 

57 

58 from pygls.server import LanguageServer 

59 from pygls.workspace import TextDocument 

60except ImportError: 

61 pass 

62 

63 

64_CONTAINS_SPACE_OR_COLON = re.compile(r"[\s:]") 

65_DISPATCH_RULE = LanguageDispatchRule.new_rule( 

66 "debian/tests/control", 

67 None, 

68 "debian/tests/control", 

69 [ 

70 # emacs's name - from elpa-dpkg-dev-el (>> 37.11) 

71 SecondaryLanguage("debian-autopkgtest-control-mode"), 

72 # vim-debian's id: https://salsa.debian.org/vim-team/vim-debian/-/commit/14776de4f28f82177ef6e2397510d01b266f3b41 

73 SecondaryLanguage("autopkgtest"), 

74 ], 

75) 

76 

77_DTESTS_CTRL_FILE_METADATA = DTestsCtrlFileMetadata() 

78 

79lsp_standard_handler(_DISPATCH_RULE, TEXT_DOCUMENT_CODE_ACTION) 

80 

81 

82@lsp_hover(_DISPATCH_RULE) 

83def debian_tests_control_hover( 

84 ls: "DebputyLanguageServer", 

85 params: HoverParams, 

86) -> Optional[Hover]: 

87 return deb822_hover(ls, params, _DTESTS_CTRL_FILE_METADATA) 

88 

89 

90@lsp_completer(_DISPATCH_RULE) 

91def debian_tests_control_completions( 

92 ls: "DebputyLanguageServer", 

93 params: CompletionParams, 

94) -> Optional[Union[CompletionList, Sequence[CompletionItem]]]: 

95 return deb822_completer(ls, params, _DTESTS_CTRL_FILE_METADATA) 

96 

97 

98@lsp_folding_ranges(_DISPATCH_RULE) 

99def debian_tests_control_folding_ranges( 

100 ls: "DebputyLanguageServer", 

101 params: FoldingRangeParams, 

102) -> Optional[Sequence[FoldingRange]]: 

103 return deb822_folding_ranges(ls, params, _DTESTS_CTRL_FILE_METADATA) 

104 

105 

106@lint_diagnostics(_DISPATCH_RULE) 

107async def _lint_debian_tests_control(lint_state: LintState) -> None: 

108 deb822_file = lint_state.parsed_deb822_file_content 

109 

110 if not _DTESTS_CTRL_FILE_METADATA.file_metadata_applies_to_file(deb822_file): 110 ↛ 111line 110 didn't jump to line 111 because the condition on line 110 was never true

111 return 

112 

113 first_error = await scan_for_syntax_errors_and_token_level_diagnostics( 

114 deb822_file, 

115 lint_state, 

116 ) 

117 

118 stanza_no = 0 

119 

120 async for stanza_range, stanza in lint_state.slow_iter( 

121 with_range_in_continuous_parts(deb822_file.iter_parts()) 

122 ): 

123 if not isinstance(stanza, Deb822ParagraphElement): 

124 continue 

125 stanza_position = stanza_range.start_pos 

126 if stanza_position.line_position >= first_error: 126 ↛ 127line 126 didn't jump to line 127 because the condition on line 126 was never true

127 break 

128 stanza_no += 1 

129 stanza_metadata = _DTESTS_CTRL_FILE_METADATA.classify_stanza( 

130 stanza, 

131 stanza_no, 

132 ) 

133 await stanza_metadata.stanza_diagnostics( 

134 deb822_file, 

135 stanza, 

136 stanza_position, 

137 lint_state, 

138 ) 

139 

140 

141@lsp_will_save_wait_until(_DISPATCH_RULE) 

142def _debian_tests_control_on_save_formatting( 

143 ls: "DebputyLanguageServer", 

144 params: WillSaveTextDocumentParams, 

145) -> Optional[Sequence[TextEdit]]: 

146 doc = ls.workspace.get_text_document(params.text_document.uri) 

147 lint_state = ls.lint_state(doc) 

148 return deb822_format_file(lint_state, _DTESTS_CTRL_FILE_METADATA) 

149 

150 

151@lsp_cli_reformat_document(_DISPATCH_RULE) 

152def _reformat_debian_tests_control( 

153 lint_state: LintState, 

154) -> Optional[Sequence[TextEdit]]: 

155 return deb822_format_file(lint_state, _DTESTS_CTRL_FILE_METADATA) 

156 

157 

158@lsp_format_document(_DISPATCH_RULE) 

159def _debian_tests_control_on_save_formatting( 

160 ls: "DebputyLanguageServer", 

161 params: DocumentFormattingParams, 

162) -> Optional[Sequence[TextEdit]]: 

163 doc = ls.workspace.get_text_document(params.text_document.uri) 

164 lint_state = ls.lint_state(doc) 

165 return deb822_format_file(lint_state, _DTESTS_CTRL_FILE_METADATA) 

166 

167 

168@lsp_semantic_tokens_full(_DISPATCH_RULE) 

169async def _debian_tests_control_semantic_tokens_full( 

170 ls: "DebputyLanguageServer", 

171 request: SemanticTokensParams, 

172) -> Optional[SemanticTokens]: 

173 return await deb822_semantic_tokens_full( 

174 ls, 

175 request, 

176 _DTESTS_CTRL_FILE_METADATA, 

177 )