forked from redhat-developer/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstandardLanguageClientUtils.ts
More file actions
151 lines (136 loc) · 5.03 KB
/
standardLanguageClientUtils.ts
File metadata and controls
151 lines (136 loc) · 5.03 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
'use strict';
import { commands, ConfigurationTarget, Position, ProgressLocation, QuickPickItem, Range, Uri, window, workspace } from "vscode";
import * as path from "path";
import * as fse from "fs-extra";
import { TextDocumentIdentifier } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { buildFilePatterns } from "./plugin";
import { ProjectConfigurationUpdateRequest } from "./protocol";
import { Commands } from "./commands";
import { getAllJavaProjects } from "./utils";
export async function projectConfigurationUpdate(languageClient: LanguageClient, uris?: TextDocumentIdentifier | Uri | Uri[]) {
let resources = [];
if (!uris) {
const activeFileUri: Uri | undefined = window.activeTextEditor?.document.uri;
if (activeFileUri && isJavaConfigFile(activeFileUri.fsPath)) {
resources = [activeFileUri];
} else {
resources = await askForProjects(activeFileUri, "Please select the project(s) to update.");
}
} else if (uris instanceof Uri) {
resources.push(uris);
} else if (Array.isArray(uris)) {
for (const uri of uris) {
if (uri instanceof Uri) {
resources.push(uri);
}
}
} else if ("uri" in uris) {
resources.push(Uri.parse(uris.uri));
}
if (resources.length === 1) {
languageClient.sendNotification(ProjectConfigurationUpdateRequest.type, {
uri: resources[0].toString(),
});
} else if (resources.length > 1) {
languageClient.sendNotification(ProjectConfigurationUpdateRequest.typeV2, {
identifiers: resources.map(r => {
return { uri: r.toString() };
}),
});
}
}
function isJavaConfigFile(filePath: string) {
const fileName = path.basename(filePath);
const regEx = new RegExp(buildFilePatterns.map(r => `(${r})`).join('|'), 'i');
return regEx.test(fileName);
}
/**
* Ask user to select projects and return the selected projects' uris.
* @param activeFileUri the uri of the active file.
* @param placeHolder message to be shown in quick pick.
*/
export async function askForProjects(activeFileUri: Uri | undefined, placeHolder: string, canPickMany: boolean = true): Promise<Uri[]> {
const projectPicks: QuickPickItem[] = await generateProjectPicks(activeFileUri);
if (!projectPicks?.length) {
return [];
} else if (projectPicks.length === 1) {
return [Uri.file(projectPicks[0].detail)];
}
const choices: QuickPickItem[] | QuickPickItem | undefined = await window.showQuickPick(projectPicks, {
matchOnDetail: true,
placeHolder: placeHolder,
ignoreFocusOut: true,
canPickMany: canPickMany,
});
if (!choices) {
return [];
}
if (Array.isArray(choices)) {
return choices.map(c => Uri.file(c.detail));
}
return [Uri.file(choices.detail)];
}
/**
* Generate the quick picks for projects selection. An `undefined` value will be return if
* it's failed to generate picks.
* @param activeFileUri the uri of the active document.
*/
async function generateProjectPicks(activeFileUri: Uri | undefined): Promise<QuickPickItem[] | undefined> {
let projectUriStrings: string[];
try {
projectUriStrings = await getAllJavaProjects();
} catch (e) {
return undefined;
}
const projectPicks: QuickPickItem[] = projectUriStrings.map(uriString => {
const projectPath = Uri.parse(uriString).fsPath;
return {
label: path.basename(projectPath),
detail: projectPath,
};
}).filter(Boolean);
// pre-select an active project based on the uri candidate.
if (activeFileUri?.scheme === "file") {
const candidatePath = activeFileUri.fsPath;
let belongingIndex = -1;
for (let i = 0; i < projectPicks.length; i++) {
if (candidatePath.startsWith(projectPicks[i].detail)) {
if (belongingIndex < 0
|| projectPicks[i].detail.length > projectPicks[belongingIndex].detail.length) {
belongingIndex = i;
}
}
}
if (belongingIndex >= 0) {
projectPicks[belongingIndex].picked = true;
}
}
return projectPicks;
}
export async function upgradeGradle(projectUri: string, version?: string): Promise<void> {
const useWrapper = workspace.getConfiguration().get<boolean>("java.import.gradle.wrapper.enabled");
if (!useWrapper) {
await workspace.getConfiguration().update("java.import.gradle.wrapper.enabled", true, ConfigurationTarget.Workspace);
}
const result = await window.withProgress({
location: ProgressLocation.Notification,
title: "Upgrading Gradle wrapper...",
cancellable: true,
}, (_progress, token) => {
return commands.executeCommand<string>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.UPGRADE_GRADLE_WRAPPER, projectUri, version, token);
});
if (result) {
if (await fse.pathExists(result)) {
const content = await fse.readFile(result);
const offset = content.toString().indexOf("distributionUrl");
if (offset >= 0) {
const document = await workspace.openTextDocument(result);
const position = document.positionAt(offset);
const distributionUrlRange = document.getWordRangeAtPosition(position);
window.showTextDocument(document, {selection: new Range(distributionUrlRange.start, new Position(distributionUrlRange.start.line + 1, 0))});
}
}
commands.executeCommand(Commands.IMPORT_PROJECTS_CMD);
}
}