diff options
author | Satya Rohith <me@satyarohith.com> | 2021-09-14 17:46:51 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 17:46:51 +0530 |
commit | bb7ee4f4450882419ac46378df882d243eb150da (patch) | |
tree | 9988b101c62cfd088d001cabc6e45ebf5f6bd22b /cli/lsp/analysis.rs | |
parent | 6e3c8a4b058b774b7eb9eebebe8532d04c6fc61e (diff) |
feat(lsp): ignore specific lint for entire file (#12023)
Diffstat (limited to 'cli/lsp/analysis.rs')
-rw-r--r-- | cli/lsp/analysis.rs | 74 |
1 files changed, 71 insertions, 3 deletions
diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index 768ef2651..d1cf14500 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -707,9 +707,10 @@ impl CodeActionCollection { }) .unwrap(); - let line_content = document.map(|d| { - d.source() - .text_info() + let document_source = document.map(|d| d.source()); + + let line_content = document_source.map(|d| { + d.text_info() .line_text(diagnostic.range.start.line as usize) .to_string() }); @@ -752,6 +753,73 @@ impl CodeActionCollection { .actions .push(CodeActionKind::DenoLint(ignore_error_action)); + // Disable a lint error for the entire file. + let parsed_source = + document_source.and_then(|d| d.module().and_then(|r| r.as_ref().ok())); + let maybe_ignore_comment = parsed_source.and_then(|ps| { + // Note: we can use ps.get_leading_comments() but it doesn't + // work when shebang is present at the top of the file. + ps.comments().get_vec().iter().find_map(|c| { + let comment_text = c.text.trim(); + comment_text.split_whitespace().next().and_then(|prefix| { + if prefix == "deno-lint-ignore-file" { + Some(c.clone()) + } else { + None + } + }) + }) + }); + + let mut new_text = format!("// deno-lint-ignore-file {}\n", code); + let mut range = lsp::Range { + start: lsp::Position { + line: 0, + character: 0, + }, + end: lsp::Position { + line: 0, + character: 0, + }, + }; + // If ignore file comment already exists, append the lint code + // to the existing comment. + if let Some(ignore_comment) = maybe_ignore_comment { + new_text = format!(" {}", code); + // Get the end position of the comment. + let line = parsed_source + .unwrap() + .source() + .line_and_column_index(ignore_comment.span.hi()); + let position = lsp::Position { + line: line.line_index as u32, + character: line.column_index as u32, + }; + // Set the edit range to the end of the comment. + range.start = position; + range.end = position; + } + + let mut changes = HashMap::new(); + changes.insert(specifier.clone(), vec![lsp::TextEdit { new_text, range }]); + let ignore_file_action = lsp::CodeAction { + title: format!("Disable {} for the entire file", code), + kind: Some(lsp::CodeActionKind::QUICKFIX), + diagnostics: Some(vec![diagnostic.clone()]), + command: None, + is_preferred: None, + disabled: None, + data: None, + edit: Some(lsp::WorkspaceEdit { + changes: Some(changes), + change_annotations: None, + document_changes: None, + }), + }; + self + .actions + .push(CodeActionKind::DenoLint(ignore_file_action)); + let mut changes = HashMap::new(); changes.insert( specifier.clone(), |