Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(ls): add lint origin tags to the diagnostics (#2102)

Elijah Potterf71624bae7bd
diff --git a/harper-ls/src/diagnostics.rs b/harper-ls/src/diagnostics.rs
index 780f1dea6..69657d8b8 100644
--- a/harper-ls/src/diagnostics.rs
+++ b/harper-ls/src/diagnostics.rs
@@ -12,14 +12,18 @@ use tower_lsp_server::lsp_types::{
use crate::config::{CodeActionConfig, DiagnosticSeverity};
use crate::pos_conv::span_to_range;
-pub fn lints_to_diagnostics(
+pub fn lints_to_diagnostics<'a>(
source: &[char],
- lints: &[Lint],
+ lints: impl IntoIterator<Item = (&'a str, &'a [Lint])>,
severity: DiagnosticSeverity,
) -> Vec<Diagnostic> {
lints
- .iter()
- .map(|lint| lint_to_diagnostic(lint, source, severity))
+ .into_iter()
+ .flat_map(|(origin_tag, lints)| {
+ lints
+ .iter()
+ .map(|lint| lint_to_diagnostic(lint, source, origin_tag, severity))
+ })
.collect()
}
@@ -116,7 +120,12 @@ pub fn lint_to_code_actions<'a>(
results
}
-fn lint_to_diagnostic(lint: &Lint, source: &[char], severity: DiagnosticSeverity) -> Diagnostic {
+fn lint_to_diagnostic(
+ lint: &Lint,
+ source: &[char],
+ origin_tag: &str,
+ severity: DiagnosticSeverity,
+) -> Diagnostic {
let range = span_to_range(source, lint.span);
Diagnostic {
@@ -124,7 +133,7 @@ fn lint_to_diagnostic(lint: &Lint, source: &[char], severity: DiagnosticSeverity
severity: Some(severity.to_lsp()),
code: None,
code_description: None,
- source: Some("Harper".to_string()),
+ source: Some(format!("Harper ({})", origin_tag)),
message: lint.message.clone(),
related_information: None,
tags: None,
diff --git a/harper-ls/src/document_state.rs b/harper-ls/src/document_state.rs
index 548cee032..0f3775271 100644
--- a/harper-ls/src/document_state.rs
+++ b/harper-ls/src/document_state.rs
@@ -3,7 +3,7 @@ use crate::diagnostics::{lint_to_code_actions, lints_to_diagnostics};
use crate::pos_conv::range_to_span;
use harper_core::linting::{Lint, LintGroup, Linter};
use harper_core::spell::{MergedDictionary, MutableDictionary};
-use harper_core::{Document, IgnoredLints, TokenKind, remove_overlaps};
+use harper_core::{Document, IgnoredLints, TokenKind, remove_overlaps_map};
use harper_core::{Lrc, Token};
use tower_lsp_server::lsp_types::{CodeActionOrCommand, Command, Diagnostic, Range, Uri};
@@ -26,15 +26,23 @@ impl DocumentState {
let temp = self.linter.config.clone();
self.linter.config.fill_with_curated();
- let mut lints = self.linter.lint(&self.document);
+ let mut lints = self.linter.organized_lints(&self.document);
self.linter.config = temp;
- remove_overlaps(&mut lints);
- self.ignored_lints
- .remove_ignored(&mut lints, &self.document);
+ for value in lints.values_mut() {
+ self.ignored_lints.remove_ignored(value, &self.document);
+ }
+
+ remove_overlaps_map(&mut lints);
- lints_to_diagnostics(self.document.get_full_content(), &lints, severity)
+ lints_to_diagnostics(
+ self.document.get_full_content(),
+ lints
+ .iter()
+ .map(|(origin_tag, lints)| (origin_tag.as_str(), lints.as_slice())),
+ severity,
+ )
}
/// Generate code actions results for a selected area.
diff --git a/packages/vscode-plugin/src/tests/suite/helper.ts b/packages/vscode-plugin/src/tests/suite/helper.ts
index 10961ef7b..093692ff8 100644
--- a/packages/vscode-plugin/src/tests/suite/helper.ts
+++ b/packages/vscode-plugin/src/tests/suite/helper.ts
@@ -48,9 +48,9 @@ export async function setTextDocumentLanguage(uri: Uri, languageId: string): Pro
}
export function createExpectedDiagnostics(
- ...data: { message: string; range: Range }[]
+ ...data: { message: string; range: Range; source: string }[]
): Diagnostic[] {
- return data.map((d) => ({ ...d, source: 'Harper', severity: DiagnosticSeverity.Information }));
+ return data.map((d) => ({ ...d, severity: DiagnosticSeverity.Information }));
}
export function compareActualVsExpectedDiagnostics(
@@ -79,7 +79,7 @@ export function createRange(
}
function getActualDiagnostics(resource: Uri): Diagnostic[] {
- return languages.getDiagnostics(resource).filter((d) => d.source === 'Harper');
+ return languages.getDiagnostics(resource).filter((d) => d.source?.includes('Harper'));
}
/** Note that this function times out if there is no change detected. */
diff --git a/packages/vscode-plugin/src/tests/suite/integration.test.ts b/packages/vscode-plugin/src/tests/suite/integration.test.ts
index 21317291f..03073ab15 100644
--- a/packages/vscode-plugin/src/tests/suite/integration.test.ts
+++ b/packages/vscode-plugin/src/tests/suite/integration.test.ts
@@ -34,17 +34,20 @@ describe('Integration >', () => {
compareActualVsExpectedDiagnostics(
await waitForDiagnosticsChange(markdownUri),
createExpectedDiagnostics(
- {
- message: 'Did you mean to spell `errorz` this way?',
- range: createRange(2, 26, 2, 32),
- },
{
message: 'Did you mean to repeat this word?',
range: createRange(2, 39, 2, 48),
+ source: 'Harper (RepeatedWords)',
+ },
+ {
+ message: 'Did you mean to spell `errorz` this way?',
+ range: createRange(2, 26, 2, 32),
+ source: 'Harper (SpellCheck)',
},
{
message: 'Did you mean to spell `realise` this way?',
range: createRange(4, 26, 4, 33),
+ source: 'Harper (SpellCheck)',
},
),
);
@@ -58,6 +61,7 @@ describe('Integration >', () => {
createExpectedDiagnostics({
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(0, 0, 0, 6),
+ source: 'Harper (SpellCheck)',
}),
);
});
@@ -74,10 +78,12 @@ describe('Integration >', () => {
{
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(0, 0, 0, 6),
+ source: 'Harper (SpellCheck)',
},
{
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(0, 9, 0, 15),
+ source: 'Harper (SpellCheck)',
},
),
);
@@ -90,6 +96,7 @@ describe('Integration >', () => {
createExpectedDiagnostics({
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(0, 9, 0, 15),
+ source: 'Harper (SpellCheck)',
}),
);
});
@@ -106,10 +113,12 @@ describe('Integration >', () => {
{
message: 'Did you mean to spell `errorz` this way?',
range: createRange(2, 26, 2, 32),
+ source: 'Harper (SpellCheck)',
},
{
message: 'Did you mean to spell `realise` this way?',
range: createRange(4, 26, 4, 33),
+ source: 'Harper (SpellCheck)',
},
),
);
@@ -130,13 +139,15 @@ describe('Integration >', () => {
async () => await config.update('dialect', 'British', ConfigurationTarget.Workspace),
),
createExpectedDiagnostics(
- {
- message: 'Did you mean to spell `errorz` this way?',
- range: createRange(2, 26, 2, 32),
- },
{
message: 'Did you mean to repeat this word?',
range: createRange(2, 39, 2, 48),
+ source: 'Harper (RepeatedWords)',
+ },
+ {
+ message: 'Did you mean to spell `errorz` this way?',
+ range: createRange(2, 26, 2, 32),
+ source: 'Harper (SpellCheck)',
},
),
);
diff --git a/packages/vscode-plugin/src/tests/suite/languages.test.ts b/packages/vscode-plugin/src/tests/suite/languages.test.ts
index c099284d4..89ecdd5cd 100644
--- a/packages/vscode-plugin/src/tests/suite/languages.test.ts
+++ b/packages/vscode-plugin/src/tests/suite/languages.test.ts
@@ -57,6 +57,7 @@ describe('Languages >', () => {
createExpectedDiagnostics({
message: 'Did you mean to spell `Errorz` this way?',
range: createRange(testCase.row, testCase.column, testCase.row, testCase.column + 6),
+ source: 'Harper (SpellCheck)',
}),
);
});