-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathextension.ts
More file actions
163 lines (141 loc) · 4.93 KB
/
Copy pathextension.ts
File metadata and controls
163 lines (141 loc) · 4.93 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
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
'use strict';
import * as vscode from 'vscode';
function isEmpty(obj: any) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return false;
}
return true;
}
function getWorkspaceFolder(): string | undefined {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage(
"Unable to get the location of cmake-format executable"
+ " - no active workspace selected");
return undefined;
}
if (!vscode.workspace.workspaceFolders) {
vscode.window.showErrorMessage(
"Unable to get the location of clang-format executable"
+ " - no workspaces available");
return undefined
}
const currentDocumentUri = editor.document.uri
let workspacePath = vscode.workspace.getWorkspaceFolder(currentDocumentUri);
if (!workspacePath) {
const fallbackWorkspace = vscode.workspace.workspaceFolders[0];
vscode.window.showWarningMessage(
"Unable to deduce the location of clang-format executable for file"
+ "outside the workspace - expanding \${workspaceFolder} to "
+ `'${fallbackWorkspace.name}' path`);
workspacePath = fallbackWorkspace;
}
return workspacePath.uri.path;
}
function varSub(value: string): string {
if (vscode.workspace.rootPath !== undefined) {
value = value.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath)
}
var workspaceFolder = getWorkspaceFolder();
if (workspaceFolder !== undefined) {
value = value.replace(/\${workspaceFolder}/g, workspaceFolder);
}
return value
.replace(/\${cwd}/g, process.cwd())
.replace(/\${env\.([^}]+)}/g, (sub: string, envName: string) => {
var value = process.env[envName];
if (value === undefined) {
return "";
} else {
return value;
}
});
}
export function activate(context: vscode.ExtensionContext) {
console.log('"cmake-format" extension is now active!');
let disposable = vscode.languages.registerDocumentFormattingEditProvider(
{ scheme: 'file', language: 'cmake' }, {
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
var fs = require('fs')
var path = require('path')
var config = vscode.workspace.getConfiguration('cmakeFormat');
var exePath = varSub(config.get("exePath"));
var args = config.get<string[]>("args", [])
// NOTE(josh): in case the final user-supplied argument is a
// nargs="*", we need to tell argparse that the next argument
// ("-") is a positional argument. We do that by adding "--"
// after the last user-supplied argument and before the "-"
if (args[args.length - 1] != "--") {
args = args.concat(["--"]);
}
args = args.concat(["-"]);
var args2: string[] = []
for (var idx = 0; idx < args.length; idx++) {
args2.push(varSub(args[idx]));
}
args = args2;
var opts: any = {
input: document.getText(),
encoding: 'utf-8'
};
var cwd = config.get<string>("cwd");
if (cwd != null) {
cwd = varSub(cwd);
}
if (cwd == null && document.uri.fsPath != null) {
cwd = path.dirname(document.uri.fsPath)
console.log("No cwd configured, using: " + cwd);
}
if (cwd == null) {
var folder = vscode.workspace.getWorkspaceFolder(document.uri);
if (folder != null) {
cwd = folder.uri.fsPath;
console.log("No cwd configured, using workspace path: " + cwd);
}
}
if (cwd != null && fs.statSync(cwd).isDirectory()) {
opts["cwd"] = cwd;
} else {
console.log("Can't use cwd: " + cwd);
}
var env: any = {}
if (config.get("mergeEnv", true)) {
env = JSON.parse(JSON.stringify(process.env));
}
var cenv: any = config.get("env", {});
if (cenv !== null) {
var delim = path.delimiter;
for (var [key, value] of Object.entries(cenv)) {
if (key.endsWith("PATH")) {
var items = cenv[key].split(delim);
if (key in env) {
items = items.concat(env[key].split(delim));
}
env[key] = items.join(delim);
} else {
env[key] = value;
}
}
}
if (!isEmpty(env)) {
opts["env"] = env;
}
const cp = require('child_process')
// NOTE(josh): execFileSync will throw an Error if the
// subprocess exits with non-zero status. The vscode GUI will
// display the stacktrace in the corner.
var replacementText = cp.execFileSync(exePath, args, opts);
var firstLine = document.lineAt(0);
var lastLine = document.lineAt(document.lineCount - 1);
var wholeRange = new vscode.Range(0,
firstLine.range.start.character,
document.lineCount - 1,
lastLine.range.end.character);
return [vscode.TextEdit.replace(wholeRange, replacementText)];
}
});
context.subscriptions.push(disposable);
}
export function deactivate() {
}