@@ -158,6 +158,75 @@ def _get_severity(*_codes: list[str]) -> lsp.DiagnosticSeverity:
158158 return lsp .DiagnosticSeverity .Information
159159
160160
161+ # **********************************************************
162+ # Linting features end here
163+ # **********************************************************
164+
165+
166+ # **********************************************************
167+ # Code Action features start here
168+ # **********************************************************
169+
170+
171+ def severity_to_str (severity : int ) -> str :
172+ """Convert DiagnosticSeverity to a string."""
173+ return {
174+ lsp .DiagnosticSeverity .Error : "error" ,
175+ lsp .DiagnosticSeverity .Warning : "warning" ,
176+ lsp .DiagnosticSeverity .Information : "info" ,
177+ lsp .DiagnosticSeverity .Hint : "hint" ,
178+ }.get (severity , "unknown" )
179+
180+
181+ @LSP_SERVER .feature (
182+ lsp .TEXT_DOCUMENT_CODE_ACTION ,
183+ lsp .CodeActionOptions (
184+ code_action_kinds = [lsp .CodeActionKind .QuickFix ], resolve_provider = True
185+ ),
186+ )
187+ def code_action (params : lsp .CodeActionParams ) -> list [lsp .CodeAction ]:
188+ """LSP handler for textDocument/codeAction request."""
189+ uri = params .text_document .uri
190+ document = LSP_SERVER .workspace .get_document (params .text_document .uri )
191+ settings = copy .deepcopy (_get_settings_by_document (document ))
192+ code_actions = []
193+ if not settings ["enabled" ]:
194+ return code_actions
195+
196+ diagnostics = (d for d in params .context .diagnostics if d .source == TOOL_DISPLAY )
197+
198+ for diagnostic in diagnostics :
199+ # Create action to suppress the issue
200+ rule_id = diagnostic .code .split (":" )[0 ]
201+ severity = severity_to_str (diagnostic .severity )
202+ line_number = diagnostic .range .start .line
203+
204+ line_text = document .lines [line_number ]
205+ suppress_comment = f"{ line_text .rstrip ()} # nosec: { rule_id } "
206+
207+ replace_range = lsp .Range (
208+ start = lsp .Position (line = line_number , character = 0 ),
209+ end = lsp .Position (line = line_number , character = len (line_text )),
210+ )
211+ edit = lsp .TextEdit (range = replace_range , new_text = suppress_comment )
212+
213+ code_actions .append (
214+ lsp .CodeAction (
215+ title = f"Add '# nosec: { rule_id } ' to suppress { severity } " ,
216+ kind = lsp .CodeActionKind .QuickFix ,
217+ diagnostics = [diagnostic ],
218+ edit = lsp .WorkspaceEdit (changes = {uri : [edit ]}),
219+ )
220+ )
221+
222+ return code_actions
223+
224+
225+ # **********************************************************
226+ # Code Action features end here
227+ # **********************************************************
228+
229+
161230# **********************************************************
162231# Required Language Server Initialization and Exit handlers.
163232# **********************************************************
0 commit comments