Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

feat: add support for diagnostics severity configuration #319

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@
"description": "Start text checking session in paused state",
"default": false
},
"grammarly.diagnostics.severity": {
"markdownDescription": "Severity of Grammarly messages",
"enum": [
"error",
"warning",
"information",
"hint"
],
"default": "error",
"scope": "language-overridable"
},
"grammarly.config.documentDialect": {
"markdownDescription": "Specific variety of English being written. See [this article](https://support.grammarly.com/hc/en-us/articles/115000089992-Select-between-British-English-American-English-Canadian-English-and-Australian-English) for differences.",
"enum": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,44 @@ export type SuggestionDiagnostic = {
suggestion: Suggestion
}

const diagnosticSeverityMap = new Map<string, DiagnosticSeverity>([
['error', 1],
['warning', 2],
['information', 3],
['hint', 4],
])

@injectable()
export class DiagnosticsService implements Registerable {
#connection: Connection
#documents: DocumentService
#diagnostics: Map<string, Map<SuggestionId, SuggestionDiagnostic>>

#severity?: DiagnosticSeverity
public constructor(@inject(CONNECTION) connection: Connection, documents: DocumentService) {
this.#connection = connection
this.#documents = documents
this.#diagnostics = new Map()
}

private _getSeverity: Promise<void> | null = null

public async getSeverity(): Promise<void> {
if (this._getSeverity != null) await this._getSeverity
this._getSeverity = (async () => {
const severity: string[] = await this.#connection.workspace.getConfiguration([
{ section: 'grammarly.diagnostics.severity' },
])
this.#severity = diagnosticSeverityMap.get(severity[0])
})()

await this._getSeverity
}

public register(): Disposable {
this.#documents.onDidOpen((document) => this.#setupDiagnostics(document))
this.#documents.onDidOpen(async (document) => {
await this.getSeverity()
this.#setupDiagnostics(document)
})
this.#documents.onDidClose((document) => this.#clearDiagnostics(document))
this.#connection.onRequest('$/pause', ([uri]: [uri: string]) => {
const document = this.#documents.get(uri)
Expand All @@ -37,6 +61,9 @@ export class DiagnosticsService implements Registerable {
document.resume()
this.#sendDiagnostics(document)
})
this.#connection.onDidChangeConfiguration(async () => {
await this.getSeverity()
})
return { dispose() {} }
}

Expand Down Expand Up @@ -119,7 +146,7 @@ export class DiagnosticsService implements Registerable {
message: suggestion.title,
range: document.findOriginalRange(highlight.start, highlight.end),
source: 'Grammarly',
severity: suggestion.type === 'corrective' ? 1 : 3,
severity: this.#severity ?? (suggestion.type === 'corrective' ? 1 : 3),
}
}
}