Skip to content

Commit

Permalink
Added an option to set severity levels automatically based on Rule (s…
Browse files Browse the repository at this point in the history
…pelling) and Category (grammar, punctuation, typography).
  • Loading branch information
kanutron committed Feb 22, 2024
1 parent 66d1678 commit 64d3c22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
"error"
]
},
"languageToolLinter.diagnosticSeverityAuto": {
"type": "boolean",
"default": false,
"description": "Spelling mistakes will be set as Error, while punctuation, grammar and typography mistakes as Warning. Anything else as the configured Diagnostic Severity level (recommended to be set as Information when using this option)."
},
"languageToolLinter.enabled": {
"type": "boolean",
"default": true,
Expand Down
10 changes: 9 additions & 1 deletion src/ConfigurationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ export class ConfigurationManager implements Disposable {
}
}

public getDiagnosticSeverityAuto(): boolean {
const severityAuto = this.config.get("diagnosticSeverityAuto");
if (severityAuto === true) {
return true
}
return false
}

public getClassPath(): string {
const jarFile: string = this.get("managed.jarFile") as string;
const classPath: string = this.get("managed.classPath") as string;
Expand Down Expand Up @@ -400,7 +408,7 @@ export class ConfigurationManager implements Disposable {
if (username && apiKey) {
parameters.set("username", username);
parameters.set("apiKey", apiKey);
}
}
}
// Make sure disabled rules and disabled categories do not contain spaces
const CONFIG_DISABLED_RULES = "languageTool.disabledRules";
Expand Down
16 changes: 16 additions & 0 deletions src/Linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export class Linter implements CodeActionProvider {
);
}

public static isWarningCategory(categoryId: string): boolean {
return (
categoryId.indexOf("GRAMMAR") !== -1 ||
categoryId.indexOf("PUNCTUATION") !== -1 ||
categoryId.indexOf("TYPOGRAPHY") !== -1
);
}

public diagnosticCollection: DiagnosticCollection;
public remarkBuilderOptions: RemarkBuilder.IOptions = RemarkBuilder.defaults;
public rehypeBuilderOptions: RehypeBuilder.IOptions = RehypeBuilder.defaults;
Expand Down Expand Up @@ -366,6 +374,7 @@ export class Linter implements CodeActionProvider {
const end: Position = document.positionAt(match.offset + match.length);
const ignored: IIgnoreItem[] = this.getIgnoreList(document, start);
const diagnosticSeverity: DiagnosticSeverity = this.configManager.getDiagnosticSeverity();
const diagnosticSeverityAuto: boolean = this.configManager.getDiagnosticSeverityAuto();
const diagnosticRange: Range = new Range(start, end);
const diagnosticMessage: string = match.message;
const diagnostic: LTDiagnostic = new LTDiagnostic(
Expand All @@ -388,6 +397,13 @@ export class Linter implements CodeActionProvider {
};
}
diagnostics.push(diagnostic);
if (diagnosticSeverityAuto) {
if (Linter.isSpellingRule(match.rule.id)) {
diagnostic.severity = DiagnosticSeverity.Error
} else if (Linter.isWarningCategory(match.rule.category.id)) {
diagnostic.severity = DiagnosticSeverity.Warning
}
}
if (
Linter.isSpellingRule(match.rule.id) &&
this.configManager.isIgnoredWord(document.getText(diagnostic.range)) &&
Expand Down

0 comments on commit 64d3c22

Please sign in to comment.