-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathannotations.ts
217 lines (188 loc) · 8.61 KB
/
annotations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Portions of this software are derived from [ghcid](https://github.com/ndmitchell/ghcid) and subject to the original authors licensing terms, reproduced below.
> Copyright Neil Mitchell 2014-2023.
> All rights reserved.
>
> Redistribution and use in source and binary forms, with or without
> modification, are permitted provided that the following conditions are
> met:
>
> * Redistributions of source code must retain the above copyright
> notice, this list of conditions and the following disclaimer.
>
> * Redistributions in binary form must reproduce the above
> copyright notice, this list of conditions and the following
> disclaimer in the documentation and/or other materials provided
> with the distribution.
>
> * Neither the name of Neil Mitchell nor the names of other
> contributors may be used to endorse or promote products derived
> from this software without specific prior written permission.
>
> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
> LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
> A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
> OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
> SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
> LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
> DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
> THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
> OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import { dirname, isAbsolute } from 'path'
import * as vscode from 'vscode'
import { Annotation, AnnotationsConfig, LanguageConfig, alloglot } from './config'
export function makeAnnotations(output: vscode.OutputChannel, config: LanguageConfig): vscode.Disposable {
output.appendLine(alloglot.ui.startingAnnotations)
const { languageId, annotations } = config
if (!languageId || !annotations || annotations.length === 0) return vscode.Disposable.from()
const watchers: Array<vscode.Disposable> = annotations.map(cfg => watchAnnotationsFile(languageId, cfg))
const quickFixes = vscode.languages.registerCodeActionsProvider(
languageId,
{ provideCodeActions: (document, range, context) => context.diagnostics.map(asQuickFixes).flat() },
{ providedCodeActionKinds: [vscode.CodeActionKind.QuickFix] }
)
output.appendLine(alloglot.ui.annotationsStarted)
return vscode.Disposable.from(
quickFixes,
...watchers
)
}
function watchAnnotationsFile(languageId: string, cfg: AnnotationsConfig): vscode.Disposable {
const diagnostics = vscode.languages.createDiagnosticCollection(`${alloglot.collections.annotations}-${languageId}-${cfg.file}`)
const messagePath = path<string>(cfg.mapping.message)
const filePath = path<string>(cfg.mapping.file)
const startLinePath = path<number>(cfg.mapping.startLine)
const startColumnPath = path<number>(cfg.mapping.startColumn)
const endLinePath = path<number>(cfg.mapping.endLine)
const endColumnPath = path<number>(cfg.mapping.endColumn)
const sourcePath = path<string>(cfg.mapping.source)
const severityPath = path<string>(cfg.mapping.severity)
const replacementsPath = path<string | Array<string>>(cfg.mapping.replacements)
const referenceCodePath = path<string | number>(cfg.mapping.referenceCode)
function marshalAnnotation(json: any): Annotation | undefined {
const message = messagePath(json)
const file = filePath(json)
if (!message || !file) return
const startLine = startLinePath(json) || 0
const startColumn = startColumnPath(json) || 0
const endLine = endLinePath(json) || startLine
const endColumn = endColumnPath(json) || startColumn
const replacements: Array<string> =
typeof replacementsPath(json) === 'string'
? [replacementsPath(json) as string]
: replacementsPath(json) as Array<string>
return {
message, file, startLine, startColumn, endLine, endColumn, replacements,
source: sourcePath(json) || `${cfg.file}`,
severity: parseSeverity(severityPath(json)),
referenceCode: referenceCodePath(json)?.toString(),
}
}
function readAnnotations(bytes: Uint8Array): Array<Annotation> {
const contents = Buffer.from(bytes).toString('utf-8')
const jsons: Array<any> = cfg.format === 'jsonl'
? contents.split('\n').map(line => JSON.parse(line))
: JSON.parse(contents)
const annotations = jsons.map(marshalAnnotation).filter(x => x) as Array<Annotation>
return annotations
}
function annotationsBySourceFile(annotations: Array<Annotation>): Map<string, Array<Annotation>> {
const sorted = new Map<string, Array<Annotation>>()
annotations.forEach(annotation => {
const annotationsForFile = sorted.get(annotation.file)
annotationsForFile
? annotationsForFile.push(annotation)
: sorted.set(annotation.file, [annotation])
})
return sorted
}
function addAnnotations(annFile: vscode.Uri): void {
diagnostics.clear()
const basedir = vscode.Uri.file(dirname(annFile.fsPath))
vscode.workspace.fs.readFile(annFile).then(bytes => {
annotationsBySourceFile(readAnnotations(bytes)).forEach((anns, srcFile) => {
const srcUri = fileUri(basedir, srcFile)
const diags = anns.map(ann => annotationAsDiagnostic(basedir, ann))
diagnostics.set(srcUri, diags)
})
})
}
const watchers = vscode.workspace.workspaceFolders?.map(ws => {
const pattern = new vscode.RelativePattern(ws, cfg.file)
const watcher = vscode.workspace.createFileSystemWatcher(pattern, false, false, false)
watcher.onDidChange(addAnnotations)
watcher.onDidCreate(addAnnotations)
watcher.onDidDelete(() => diagnostics.clear())
return watcher
}) || []
const cleanup = vscode.workspace.onDidSaveTextDocument(doc => {
if (doc.languageId === languageId) {
diagnostics.delete(doc.uri)
}
})
return vscode.Disposable.from(cleanup, ...watchers)
}
function annotationAsDiagnostic(basedir: vscode.Uri, ann: Annotation): vscode.Diagnostic {
const range = new vscode.Range(
new vscode.Position(ann.startLine - 1, ann.startColumn - 1),
new vscode.Position(ann.endLine - 1, ann.endColumn - 1)
)
// we are abusing the relatedInformation field to store replacements
// we look them up later when we need to create quick fixes
const relatedInformation = ann.replacements.map(replacement => {
const srcUri = fileUri(basedir, ann.file)
const srcLocation = new vscode.Location(srcUri, range)
return new vscode.DiagnosticRelatedInformation(srcLocation, replacement)
})
// i wish they gave an all-args constructor
const diagnostic = new vscode.Diagnostic(range, ann.message, asDiagnosticSeverity(ann.severity))
diagnostic.source = ann.source
diagnostic.relatedInformation = relatedInformation
diagnostic.code = ann.referenceCode
return diagnostic
}
function asDiagnosticSeverity(sev: Annotation['severity']): vscode.DiagnosticSeverity {
switch (sev) {
case 'error': return vscode.DiagnosticSeverity.Error
case 'warning': return vscode.DiagnosticSeverity.Warning
case 'info': return vscode.DiagnosticSeverity.Information
case 'hint': return vscode.DiagnosticSeverity.Hint
}
}
// this depends on the fact that we're abusing the `relatedInformation` field
// see `annotationAsDiagnostic` above
function asQuickFixes(diag: vscode.Diagnostic): Array<vscode.CodeAction> {
const actions = diag.relatedInformation?.map(info => {
const action = new vscode.CodeAction(diag.message, vscode.CodeActionKind.QuickFix)
action.diagnostics = [diag]
action.edit = new vscode.WorkspaceEdit
action.edit.replace(info.location.uri, info.location.range, info.message)
return action
})
return actions || []
}
function path<T>(keys: Array<string> | undefined): (json: any) => T | undefined {
if (!keys) return () => undefined
else return json => {
const result = keys.reduce((acc, key) => acc?.[key], json)
if (result) return result as T
else return undefined
}
}
function parseSeverity(raw: string | undefined): Annotation['severity'] {
if (!raw) return 'error'
const lower = raw.toLowerCase()
if (lower.includes('error')) return 'error'
if (lower.includes('warning')) return 'warning'
if (lower.includes('info')) return 'info'
if (lower.includes('hint')) return 'hint'
return 'error'
}
function fileUri(basedir: vscode.Uri, file: string): vscode.Uri {
return isAbsolute(file)
? vscode.Uri.file(file)
: vscode.Uri.joinPath(basedir, file)
}