-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspotless.ts
54 lines (51 loc) · 1.7 KB
/
spotless.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
import * as vscode from 'vscode';
import * as path from 'path';
import { logger } from './logger';
import { ExtensionApi as GradleTasksApi } from 'vscode-gradle';
function escapeStringRegexp(str: string): string {
return str.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
}
function sanitizePath(fsPath: string): string {
const platform = process.platform;
if (platform === 'win32') {
// vscode.Uri.fsPath will lowercase the drive letters
// https://github.com/microsoft/vscode/blob/dc348340fd1a6c583cb63a1e7e6b4fd657e01e01/src/vs/vscode.d.ts#L1338
return fsPath[0].toUpperCase() + fsPath.substr(1);
}
return fsPath;
}
export async function makeSpotless(
gradleApi: GradleTasksApi,
document: vscode.TextDocument
): Promise<null> {
if (document.isClosed || document.isUntitled) {
logger.warning('Document is closed or not saved yet, skipping');
return null;
}
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
if (!workspaceFolder) {
logger.error(
`Unable to find workspace folder for ${path.basename(
document.uri.fsPath
)}, skipping`
);
return null;
}
logger.info('Running spotless on', path.basename(document.uri.fsPath));
try {
if (document.isDirty) {
const saved = await document.save();
if (!saved) {
throw new Error(`Unable to save file ${document.fileName}`);
}
}
const normalizedPath = escapeStringRegexp(
sanitizePath(document.uri.fsPath)
);
const args = [`-PspotlessFiles=${normalizedPath}`];
await gradleApi.runTask(workspaceFolder.uri.fsPath, 'spotlessApply', args);
} catch (e) {
logger.error('Error running spotless formatter:', e.message);
}
return null;
}