-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfiles.ts
More file actions
55 lines (46 loc) · 2.27 KB
/
Copy pathfiles.ts
File metadata and controls
55 lines (46 loc) · 2.27 KB
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
import * as vscode from 'vscode';
export async function writeSuppressionToProjectFile(projectFileUri : vscode.Uri, warningType : String) {
const fileType = projectFileUri.toString().split('.')[1];
if (fileType !== 'cppcheck') {
throw new Error(`Function writeSuppressionToProjectFile only supports writing to .cppcheck project files! Recieved file is of type .${fileType}`);
}
// Open project file with vscode workspace API
const document = await vscode.workspace.openTextDocument(projectFileUri);
const text = document.getText();
const edit = new vscode.WorkspaceEdit();
var textToInsert = '';
var positionToInsertAt = 0;
// Search for suppressions section
const match = /<suppressions\b[^>]*>([\s\S]*?)<\/suppressions>/m.exec(text);
if (match !== null) {
// match !== null -> Suppressions section exists
const closeIndex = text.indexOf("</suppressions>");
// Determine indentation and construct the new suppression line
const line = document.lineAt(document.positionAt(closeIndex).line - 1);
const indentation = line.text.match(/^\s*/)?.[0] ?? " ";
const newSuppressionLine = `\n${indentation}<suppression id="${warningType}"/>\n`;
// We splice in the new line just before the end of the suppressions block
textToInsert = newSuppressionLine;
positionToInsertAt = closeIndex;
} else {
// match === null -> Suppressions section does not exist
const closeIndex = text.indexOf("</project>");
// Determine indentation and construct the new suppressions block
const line = document.lineAt(document.positionAt(closeIndex).line - 1);
const indentation = line.text.match(/^\s*/)?.[0] ?? " ";
const suppressionsBlock =
`\n${indentation}<suppressions>
${indentation} <suppression id="${warningType}"/>
${indentation}</suppressions>\n`;
// Splice in the new suppressions block just before the end of the project-file
textToInsert = suppressionsBlock;
positionToInsertAt = closeIndex;
}
// Write file
edit.insert(
document.uri,
document.positionAt(positionToInsertAt),
textToInsert
);
await vscode.workspace.applyEdit(edit);
}