Skip to content

Support project update selection #2513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ export namespace ClassFileContentsRequest {

export namespace ProjectConfigurationUpdateRequest {
export const type = new NotificationType<TextDocumentIdentifier> ('java/projectConfigurationUpdate');
export const typeV2 = new NotificationType<ProjectConfigurationsUpdateParam> ('java/projectConfigurationsUpdate');
}

export interface ProjectConfigurationsUpdateParam {
identifiers: TextDocumentIdentifier[];
}

export namespace ActionableNotification {
Expand Down
96 changes: 85 additions & 11 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import { ExtensionContext, window, workspace, commands, Uri, ProgressLocation, ViewColumn, EventEmitter, extensions, Location, languages, CodeActionKind, TextEditor, CancellationToken, ConfigurationTarget, Range, Position } from "vscode";
import { ExtensionContext, window, workspace, commands, Uri, ProgressLocation, ViewColumn, EventEmitter, extensions, Location, languages, CodeActionKind, TextEditor, CancellationToken, ConfigurationTarget, Range, Position, QuickPickItem } from "vscode";
import { Commands } from "./commands";
import { serverStatus, ServerStatusKind } from "./serverStatus";
import { prepareExecutable, awaitServerConnection } from "./javaServerStarter";
Expand Down Expand Up @@ -585,23 +585,97 @@ function setIncompleteClasspathSeverity(severity: string) {
);
}

function projectConfigurationUpdate(languageClient: LanguageClient, uri?: Uri) {
let resource = uri;
if (!(resource instanceof Uri)) {
if (window.activeTextEditor) {
resource = window.activeTextEditor.document.uri;
async function projectConfigurationUpdate(languageClient: LanguageClient, uris?: Uri | Uri[]) {
let resources = [];
if (!uris) {
resources = await askForProjectToUpdate();
} 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);
}
}
}
if (!resource) {
return window.showWarningMessage('No Java project to update!').then(() => false);
}
if (isJavaConfigFile(resource.path)) {
if (resources.length === 1) {
languageClient.sendNotification(ProjectConfigurationUpdateRequest.type, {
uri: resource.toString()
uri: resources[0].toString(),
});
} else if (resources.length > 1) {
languageClient.sendNotification(ProjectConfigurationUpdateRequest.typeV2, {
identifiers: resources.map(r => {
return { uri: r.toString() };
}),
});
}
}

async function askForProjectToUpdate(): Promise<Uri[]> {
let uriCandidate: Uri;
if (window.activeTextEditor) {
uriCandidate = window.activeTextEditor.document.uri;
}

if (uriCandidate && isJavaConfigFile(uriCandidate.fsPath)) {
return [uriCandidate];
}

let projectUriStrings: string[];
try {
projectUriStrings = await commands.executeCommand<string[]>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_ALL_JAVA_PROJECTS);
} catch (e) {
return uriCandidate ? [uriCandidate] : [];
}

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);

if (projectPicks.length === 0) {
return [];
} else if (projectPicks.length === 1) {
return [Uri.file(projectPicks[0].detail)];
} else {
// pre-select an active project based on the uri candidate.
if (uriCandidate) {
const candidatePath = uriCandidate.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;
}
}

const choices: QuickPickItem[] | undefined = await window.showQuickPick(projectPicks, {
matchOnDetail: true,
placeHolder: "Please select the project(s) to update.",
ignoreFocusOut: true,
canPickMany: true,
});
if (choices && choices.length) {
return choices.map(c => Uri.file(c.detail));
}
}

return [];
}

function isJavaConfigFile(filePath: string) {
const fileName = path.basename(filePath);
const regEx = new RegExp(buildFilePatterns.map(r => `(${r})`).join('|'), 'i');
Expand Down