Skip to content

Commit

Permalink
Skip default project when detecting lombok (#2633)
Browse files Browse the repository at this point in the history
* Extract utility to filter default project when getting all java projects

Signed-off-by: Sheng Chen <sheche@microsoft.com>
  • Loading branch information
jdneo committed Aug 18, 2022
1 parent b7fd9dc commit ab3e745
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/lombokSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { apiManager } from "./apiManager";
import { supportsLanguageStatus } from "./languageStatusItemFactory";
import { runtimeStatusBarProvider } from './runtimeStatusBarProvider';
import { logger } from './log';
import { getAllJavaProjects } from "./utils";

export const JAVA_LOMBOK_PATH = "java.lombokPath";

Expand Down Expand Up @@ -126,7 +127,7 @@ export async function checkLombokDependency(context: ExtensionContext) {
let currentLombokVersion: string = undefined;
let previousLombokVersion: string = undefined;
let currentLombokClasspath: string = undefined;
const projectUris: string[] = await commands.executeCommand<string[]>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_ALL_JAVA_PROJECTS);
const projectUris: string[] = await getAllJavaProjects();
for (const projectUri of projectUris) {
const classpathResult = await apiManager.getApiInstance().getClasspaths(projectUri, {scope: 'test'});
for (const classpath of classpathResult.classpaths) {
Expand Down
4 changes: 2 additions & 2 deletions src/runtimeStatusBarProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { apiManager } from "./apiManager";
import * as semver from "semver";
import { ACTIVE_BUILD_TOOL_STATE } from "./settings";
import { BuildFileStatusItemFactory, RuntimeStatusItemFactory, StatusCommands, supportsLanguageStatus } from "./languageStatusItemFactory";
import { getJavaConfiguration } from "./utils";
import { getAllJavaProjects, getJavaConfiguration } from "./utils";
import { hasBuildToolConflicts } from "./extension";
import { LombokVersionItemFactory, getLombokVersion, isLombokImported } from "./lombokSupport";

Expand Down Expand Up @@ -51,7 +51,7 @@ class RuntimeStatusBarProvider implements Disposable {

let projectUriStrings: string[];
try {
projectUriStrings = await commands.executeCommand<string[]>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_ALL_JAVA_PROJECTS);
projectUriStrings = await getAllJavaProjects(false);
} catch (e) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/standardLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getJdkUrl, RequirementsData, sortJdksBySource, sortJdksByVersion } from
import * as net from 'net';
import * as fse from 'fs-extra';
import * as path from 'path';
import { getJavaConfiguration } from "./utils";
import { getAllJavaProjects, getJavaConfiguration } from "./utils";
import { logger } from "./log";
import * as buildPath from './buildpath';
import * as sourceAction from './sourceAction';
Expand Down Expand Up @@ -609,8 +609,8 @@ async function showImportFinishNotification(context: ExtensionContext) {
options.unshift("Show errors");
choice = await window.showWarningMessage("Errors occurred during import of Java projects.", ...options);
} else {
const projectUris: string[] = await commands.executeCommand<string[]>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_ALL_JAVA_PROJECTS);
if (projectUris.length === 0 || (projectUris.length === 1 && projectUris[0].includes("jdt.ls-java-project"))) {
const projectUris: string[] = await getAllJavaProjects();
if (projectUris.length === 0) {
return;
}

Expand Down
7 changes: 2 additions & 5 deletions src/standardLanguageClientUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 = [];
Expand Down Expand Up @@ -85,17 +86,13 @@ export async function askForProjects(activeFileUri: Uri | undefined, placeHolder
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);
projectUriStrings = await getAllJavaProjects();
} 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,
Expand Down
19 changes: 18 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import * as fs from 'fs';
import * as path from 'path';
import { workspace, WorkspaceConfiguration, TextDocument } from 'vscode';
import { workspace, WorkspaceConfiguration, TextDocument, commands, Uri } from 'vscode';
import { Commands } from './commands';

export function getJavaConfiguration(): WorkspaceConfiguration {
return workspace.getConfiguration('java');
Expand Down Expand Up @@ -115,3 +116,19 @@ function parseToStringGlob(patterns: string[]): string {

return `{${patterns.join(",")}}`;
}

/**
* Get all Java projects from Java Language Server.
* @param excludeDefaultProject whether the default project should be excluded from the list, defaults to true.
* @returns string array for the project uris.
*/
export async function getAllJavaProjects(excludeDefaultProject: boolean = true): Promise<string[]> {
let projectUris: string[] = await commands.executeCommand<string[]>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_ALL_JAVA_PROJECTS);
if (excludeDefaultProject) {
projectUris = projectUris.filter((uriString) => {
const projectPath = Uri.parse(uriString).fsPath;
return path.basename(projectPath) !== "jdt.ls-java-project";
});
}
return projectUris;
}

0 comments on commit ab3e745

Please sign in to comment.