-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathdbtDocumentFormattingEditProvider.ts
More file actions
305 lines (279 loc) · 9.32 KB
/
dbtDocumentFormattingEditProvider.ts
File metadata and controls
305 lines (279 loc) · 9.32 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { CommandProcessExecutionFactory } from "@altimateai/dbt-integration";
import { exec } from "child_process";
import fs from "fs";
import { inject } from "inversify";
import os from "os";
import parseDiff from "parse-diff";
import path from "path";
import { promisify } from "util";
import {
CancellationToken,
DocumentFormattingEditProvider,
FormattingOptions,
ProviderResult,
Range,
TextDocument,
TextEdit,
window,
workspace,
} from "vscode";
import which from "which";
import { PythonEnvironment } from "../dbt_client/pythonEnvironment";
import { TelemetryService } from "../telemetry";
import { extendErrorWithSupportLinks, getFirstWorkspacePath } from "../utils";
const execAsync = promisify(exec);
// prettier-ignore
export class DbtDocumentFormattingEditProvider implements DocumentFormattingEditProvider {
private cachedSqlFmtPath: string | undefined;
private cachedPythonPath: string | undefined;
constructor(
private commandProcessExecutionFactory: CommandProcessExecutionFactory,
private telemetry: TelemetryService,
@inject(PythonEnvironment)
private pythonEnvironment: PythonEnvironment,
) {}
provideDocumentFormattingEdits(
document: TextDocument,
options: FormattingOptions,
token: CancellationToken,
): ProviderResult<TextEdit[]> {
return this.executeSqlFmt(document);
}
private async executeSqlFmt(document: TextDocument) {
const sqlFmtPathSetting =
this.pythonEnvironment.getResolvedConfigValue("sqlFmtPath");
const sqlFmtAdditionalParamsSetting = workspace
.getConfiguration("dbt")
.get<string[]>("sqlFmtAdditionalParams", [])
.join(" ")
.split(" ")
.filter((s) => s !== "");
const sqlFmtArgs = [
"-",
"--diff",
"--no-progressbar",
"--quiet",
...sqlFmtAdditionalParamsSetting,
];
try {
// try to find sqlfmt on PATH if not set
const sqlFmtPath = sqlFmtPathSetting || (await this.findSqlFmtPath());
if (!sqlFmtPath) {
throw new Error("sqlfmt not found");
}
this.telemetry.sendTelemetryEvent("formatDbtModel", {
sqlFmtPath: sqlFmtPathSetting ? "setting" : "path",
});
try {
const { stderr } = await this.commandProcessExecutionFactory
.createCommandProcessExecution({
command: sqlFmtPath,
args: sqlFmtArgs,
stdin: document.getText(),
cwd: getFirstWorkspacePath(),
})
.complete();
if (stderr) {
throw new Error(stderr);
}
return [];
} catch (e) {
try {
return this.processDiffOutput(document, (e as Error).message);
} catch (error) {
this.telemetry.sendTelemetryError(
"formatDbtModelApplyDiffError",
error,
);
window.showErrorMessage(
extendErrorWithSupportLinks(
"Could not process difference output from sqlfmt. Detailed error: " +
error +
".",
),
);
}
}
} catch (error) {
this.telemetry.sendTelemetryError("formatDbtModelApplyDiffError", error);
window.showErrorMessage(
extendErrorWithSupportLinks(
'Could not run sqlfmt. If sqlfmt is installed (e.g. via `uv tool install "shandy-sqlfmt[jinjafmt]"` or `pipx install "shandy-sqlfmt[jinjafmt]"`), ' +
"try setting the `dbt.sqlFmtPath` setting to the full path of the sqlfmt binary, " +
"or restart VS Code to pick up PATH changes. Detailed error: " +
error +
".",
),
);
}
return [];
}
private async findSqlFmtPath(): Promise<string | undefined> {
const currentPythonPath = this.pythonEnvironment.pythonPath;
// Return cached result if still valid (same interpreter + binary exists)
if (
this.cachedSqlFmtPath &&
this.cachedPythonPath === currentPythonPath &&
fs.existsSync(this.cachedSqlFmtPath)
) {
return this.cachedSqlFmtPath;
}
const result = await this.discoverSqlFmtPath();
this.cachedSqlFmtPath = result;
this.cachedPythonPath = currentPythonPath;
return result;
}
private async discoverSqlFmtPath(): Promise<string | undefined> {
const isWindows = process.platform === "win32";
const exe = isWindows ? "sqlfmt.exe" : "sqlfmt";
// 1. Check Python venv bin directory
const pythonPath = this.pythonEnvironment.pythonPath;
if (pythonPath) {
const candidatePath = path.join(path.dirname(pythonPath), exe);
if (fs.existsSync(candidatePath)) {
return candidatePath;
}
}
// 2. Check well-known tool binary locations (uv, pipx)
for (const candidate of this.getToolBinCandidates(exe)) {
if (fs.existsSync(candidate)) {
return candidate;
}
}
// 3. Try uv tool dir (async, non-blocking) for custom uv install locations
const uvToolPath = await this.findSqlFmtInUvTools(exe);
if (uvToolPath) {
return uvToolPath;
}
// 4. Fall back to system PATH via which
try {
return await which("sqlfmt");
} catch {
return undefined;
}
}
private getToolBinCandidates(exe: string): string[] {
const home = os.homedir();
const candidates: string[] = [];
// UV_TOOL_BIN_DIR / PIPX_BIN_DIR override default locations
const uvToolBinDir = process.env.UV_TOOL_BIN_DIR;
if (uvToolBinDir) {
candidates.push(path.join(uvToolBinDir, exe));
}
const pipxBinDir = process.env.PIPX_BIN_DIR;
if (pipxBinDir) {
candidates.push(path.join(pipxBinDir, exe));
}
if (process.platform === "win32") {
const appData = process.env.APPDATA;
if (appData) {
candidates.push(
path.join(
appData,
"uv",
"data",
"tools",
"shandy-sqlfmt",
"Scripts",
exe,
),
path.join(appData, "Python", "Scripts", exe),
);
}
candidates.push(
path.join(home, ".local", "bin", exe),
path.join(home, "pipx", "venvs", "shandy-sqlfmt", "Scripts", exe),
);
} else {
// Linux/macOS: default location for uv tool install and pipx
candidates.push(path.join(home, ".local", "bin", exe));
}
return candidates;
}
private async findSqlFmtInUvTools(exe: string): Promise<string | undefined> {
try {
const { stdout } = await execAsync("uv tool dir", { timeout: 3000 });
const uvToolDir = stdout.trim();
if (uvToolDir) {
const executable =
process.platform === "win32"
? path.join(uvToolDir, "shandy-sqlfmt", "Scripts", exe)
: path.join(uvToolDir, "shandy-sqlfmt", "bin", exe);
if (fs.existsSync(executable)) {
return executable;
}
}
} catch {
// uv is not installed or not on PATH — ignore
}
return undefined;
}
private processDiffOutput(
document: TextDocument,
diffOutput: string,
): TextEdit[] {
const textEdits: TextEdit[] = [];
const diffs = parseDiff(diffOutput);
if (document.lineCount === 0) {
return textEdits;
}
diffs.forEach((diff) => {
diff.chunks.forEach((chunk) => {
// Build new content from add + normal changes (these are the lines
// that should appear in the formatted output for this chunk's range).
const newLines = chunk.changes
.filter((c) => this.isAddChange(c) || this.isNormalChange(c))
.map((c) => c.content.slice(1));
const newContent =
newLines.length > 0 ? newLines.join("\n") + "\n" : "";
if (chunk.oldLines === 0) {
// Pure insertion — no old lines to replace
const insertLine = Math.min(chunk.oldStart, document.lineCount) - 1;
textEdits.push(
TextEdit.insert(
document.lineAt(Math.max(insertLine, 0)).range.start,
newContent,
),
);
} else {
// Replace the chunk's old range with new content.
// VSCode applies all TextEdits simultaneously against the original
// document, so each chunk references original line positions.
const startLine = Math.max(chunk.oldStart - 1, 0);
const endLine = Math.min(
startLine + chunk.oldLines - 1,
document.lineCount - 1,
);
if (startLine >= document.lineCount) {
return;
}
const range = new Range(
document.lineAt(startLine).range.start,
document.lineAt(endLine).rangeIncludingLineBreak.end,
);
textEdits.push(TextEdit.replace(range, newContent));
}
});
});
return textEdits;
}
private isAddChange(change: parseDiff.Change): change is parseDiff.AddChange {
return change.type === "add";
}
private isNormalChange(
change: parseDiff.Change,
): change is parseDiff.NormalChange {
return change.type === "normal";
}
private isDeleteChange(
change: parseDiff.Change,
): change is parseDiff.DeleteChange {
return (
/*
parseDiff reads sqlfmt's "\ No newline at end of file" diff output as a delete change.
This deceptive delete change should be skipped. So, adding an edge case to the expression.
*/
change.type === "del" && change.content !== "\\ No newline at end of file"
);
}
}