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
122 lines (109 loc) · 3.73 KB
/
standardLanguageClientUtils.ts
File metadata and controls
122 lines (109 loc) · 3.73 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
'use strict';
import { commands, QuickPickItem, Uri, window } from "vscode";
import * as path from "path";
import { TextDocumentIdentifier } from "vscode-languageclient";
import { LanguageClient } from "vscode-languageclient/node";
import { buildFilePatterns } from "./plugin";
import { ProjectConfigurationUpdateRequest } from "./protocol";
import { Commands } from "./commands";
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): Promise<Uri[]> {
const projectPicks: QuickPickItem[] = await generateProjectPicks(activeFileUri);
if (!projectPicks?.length) {
return [];
} else if (projectPicks.length === 1) {
return [Uri.file(projectPicks[0].detail)];
} else {
const choices: QuickPickItem[] | undefined = await window.showQuickPick(projectPicks, {
matchOnDetail: true,
placeHolder: placeHolder,
ignoreFocusOut: true,
canPickMany: true,
});
if (choices?.length) {
return choices.map(c => Uri.file(c.detail));
}
}
return [];
}
/**
* 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 commands.executeCommand<string[]>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_ALL_JAVA_PROJECTS);
} catch (e) {
return undefined;
}
const projectPicks: QuickPickItem[] = projectUriStrings.map(uriString => {
const projectPath = Uri.parse(uriString).fsPath;
if (path.basename(projectPath) === "jdt.ls-java-project") {
return undefined;
}
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;
}