Skip to content

Commit 33f5b9c

Browse files
Share indexes among workspaces (#2811)
* Make default index location to be os-specific & add a command to clean shared indexes * Add sharedIndexes settings as New in 1.14.0 in the README Signed-off-by: Jinbo Wang <[email protected]>
1 parent 9a22a58 commit 33f5b9c

File tree

10 files changed

+115
-5
lines changed

10 files changed

+115
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,16 @@ The following settings are supported:
214214
* `java.cleanup.actionsOnSave`: The list of clean ups to be run on the current document when it's saved. Clean ups can automatically fix code style or programming mistakes. [Click here](document/_java.learnMoreAboutCleanUps.md#java-clean-ups) to learn more about what each clean up does.
215215
* `java.import.gradle.annotationProcessing.enabled`: Enable/disable the annotation processing on Gradle projects and delegate to JDT APT. Only works for Gradle 5.2 or higher.
216216

217+
New in 1.14.0
218+
* `java.sharedIndexes.enabled`: [Experimental] Specify whether to share indexes between different workspaces. Defaults to `auto` and the shared indexes is automatically enabled in Visual Studio Code - Insiders.
219+
- auto
220+
- on
221+
- off
222+
* `java.sharedIndexes.location`: Specifies a common index location for all workspaces. See default values as follows:
223+
- Windows: First use `"$APPDATA\\.jdt\\index"`, or `"~\\.jdt\\index"` if it does not exist
224+
- macOS: `"~/Library/Caches/.jdt/index"`
225+
- Linux: First use `"$XDG_CACHE_HOME/.jdt/index"`, or `"~/.cache/.jdt/index"` if it does not exist
226+
217227
Semantic Highlighting
218228
===============
219229
[Semantic Highlighting](https://github.com/redhat-developer/vscode-java/wiki/Semantic-Highlighting) fixes numerous syntax highlighting issues with the default Java Textmate grammar. However, you might experience a few minor issues, particularly a delay when it kicks in, as it needs to be computed by the Java Language server, when opening a new file or when typing. Semantic highlighting can be disabled for all languages using the `editor.semanticHighlighting.enabled` setting, or for Java only using [language-specific editor settings](https://code.visualstudio.com/docs/getstarted/settings#_languagespecific-editor-settings).

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,23 @@
10391039
},
10401040
"default": [],
10411041
"scope": "window"
1042+
},
1043+
"java.sharedIndexes.enabled": {
1044+
"type": "string",
1045+
"enum": [
1046+
"auto",
1047+
"on",
1048+
"off"
1049+
],
1050+
"default": "auto",
1051+
"markdownDescription": "[Experimental] Specify whether to share indexes between different workspaces. When set to `auto`, shared indexes will be enabled in Visual Studio Code - Insiders.",
1052+
"scope": "window"
1053+
},
1054+
"java.sharedIndexes.location": {
1055+
"type": "string",
1056+
"markdownDescription": "Specifies a common index location for all workspaces. See default values as follows:\n \nWindows: First use `\"$APPDATA\\\\.jdt\\\\index\"`, or `\"~\\\\.jdt\\\\index\"` if it does not exist\n \nmacOS: `\"~/Library/Caches/.jdt/index\"`\n \nLinux: First use `\"$XDG_CACHE_HOME/.jdt/index\"`, or `\"~/.cache/.jdt/index\"` if it does not exist",
1057+
"default": "",
1058+
"scope": "window"
10421059
}
10431060
}
10441061
},
@@ -1176,6 +1193,11 @@
11761193
"command": "_java.learnMoreAboutCleanUps",
11771194
"title": "Learn more about Java Clean Ups",
11781195
"category": "Java"
1196+
},
1197+
{
1198+
"command": "java.clean.sharedIndexes",
1199+
"title": "%java.clean.sharedIndexes%",
1200+
"category": "Java"
11791201
}
11801202
],
11811203
"keybindings": [

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"java.action.showSupertypeHierarchy": "Show Supertype Hierarchy",
2323
"java.action.showSubtypeHierarchy": "Show Subtype Hierarchy",
2424
"java.action.changeBaseType": "Base on this Type",
25-
"java.project.createModuleInfo.command": "Create module-info.java"
25+
"java.project.createModuleInfo.command": "Create module-info.java",
26+
"java.clean.sharedIndexes": "Clean Shared Indexes"
2627
}

package.nls.zh-cn.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"java.action.showSupertypeHierarchy": "显示父类层次结构",
2323
"java.action.showSubtypeHierarchy": "显示子类层次结构",
2424
"java.action.changeBaseType": "基于此类型",
25-
"java.project.createModuleInfo.command": "创建 module-info.java"
25+
"java.project.createModuleInfo.command": "创建 module-info.java",
26+
"java.clean.sharedIndexes": "清理共享的索引文件"
2627
}

src/commands.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,9 @@ export namespace Commands {
315315
* JDT.LS will call this command before set the server to ready state.
316316
*/
317317
export const REFRESH_BUNDLES_COMMAND = "_java.reloadBundles.command";
318+
319+
/**
320+
* Clean everything in the shared index directory.
321+
*/
322+
export const CLEAN_SHARED_INDEXES = "java.clean.sharedIndexes";
318323
}

src/extension.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ClientErrorHandler } from './clientErrorHandler';
1313
import { Commands } from './commands';
1414
import { ClientStatus, ExtensionAPI } from './extension.api';
1515
import * as fileEventHandler from './fileEventHandler';
16-
import { HEAP_DUMP_LOCATION, prepareExecutable } from './javaServerStarter';
16+
import { getSharedIndexCache, HEAP_DUMP_LOCATION, prepareExecutable } from './javaServerStarter';
1717
import { initializeLogFile, logger } from './log';
1818
import { cleanupLombokCache } from "./lombokSupport";
1919
import { markdownPreviewProvider } from "./markdownPreviewProvider";
@@ -304,6 +304,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
304304
}));
305305

306306
context.subscriptions.push(commands.registerCommand(Commands.CLEAN_WORKSPACE, (force?: boolean) => cleanWorkspace(workspacePath, force)));
307+
context.subscriptions.push(commands.registerCommand(Commands.CLEAN_SHARED_INDEXES, () => cleanSharedIndexes(context)));
307308

308309
context.subscriptions.push(commands.registerCommand(Commands.GET_WORKSPACE_PATH, () => workspacePath));
309310

@@ -603,6 +604,19 @@ async function cleanWorkspace(workspacePath, force?: boolean) {
603604
commands.executeCommand(Commands.RELOAD_WINDOW);
604605
}
605606

607+
async function cleanSharedIndexes(context: ExtensionContext) {
608+
const sharedIndexLocation: string = getSharedIndexCache(context);
609+
if (sharedIndexLocation && fs.existsSync(sharedIndexLocation)) {
610+
const doIt = 'Clean and Reload';
611+
const ans = await window.showWarningMessage('The shared indexes might be in use by other workspaces, do you want to clear it? New indexes will be built after reloading.',
612+
doIt, "Cancel");
613+
if (ans === doIt) {
614+
deleteDirectory(sharedIndexLocation);
615+
commands.executeCommand(Commands.RELOAD_WINDOW);
616+
}
617+
}
618+
}
619+
606620
function openServerLogFile(workspacePath, column: ViewColumn = ViewColumn.Active): Thenable<boolean> {
607621
const serverLogFile = path.join(workspacePath, '.metadata', '.log');
608622
return openLogFile(serverLogFile, 'Could not open Java Language Server log file', column);

src/javaServerStarter.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as glob from 'glob';
44
import * as net from 'net';
55
import * as os from 'os';
66
import * as path from 'path';
7-
import { ExtensionContext, workspace } from 'vscode';
7+
import { ExtensionContext, version, workspace } from 'vscode';
88
import { Executable, ExecutableOptions, StreamInfo } from 'vscode-languageclient/node';
99
import { logger } from './log';
1010
import { addLombokParam, isLombokSupportEnabled } from './lombokSupport';
@@ -130,6 +130,11 @@ function prepareParams(requirements: RequirementsData, javaConfiguration, worksp
130130
if (vmargs.indexOf(HEAP_DUMP_LOCATION) < 0) {
131131
params.push(`${HEAP_DUMP_LOCATION}${path.dirname(workspacePath)}`);
132132
}
133+
134+
const sharedIndexLocation: string = resolveIndexCache(context);
135+
if (sharedIndexLocation) {
136+
params.push(`-Djdt.core.sharedIndexLocation=${sharedIndexLocation}`);
137+
}
133138
}
134139

135140
// "OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify
@@ -165,6 +170,55 @@ function prepareParams(requirements: RequirementsData, javaConfiguration, worksp
165170
return params;
166171
}
167172

173+
function resolveIndexCache(context: ExtensionContext) {
174+
let enabled: string = getJavaConfiguration().get("sharedIndexes.enabled");
175+
if (enabled === "auto") {
176+
enabled = version.includes("insider") ? "on" : "off";
177+
}
178+
179+
if (enabled !== "on") {
180+
return;
181+
}
182+
183+
const location: string = getSharedIndexCache(context);
184+
if (location) {
185+
ensureExists(location);
186+
if (!fs.existsSync(location)) {
187+
logger.error(`Failed to create the shared index directory '${location}', fall back to local index.`);
188+
return;
189+
}
190+
}
191+
192+
return location;
193+
}
194+
195+
export function getSharedIndexCache(context: ExtensionContext): string {
196+
let location: string = getJavaConfiguration().get("sharedIndexes.location");
197+
if (!location) {
198+
switch (process.platform) {
199+
case "win32":
200+
location = process.env.APPDATA ? path.join(process.env.APPDATA, ".jdt", "index")
201+
: path.join(os.homedir(), ".jdt", "index");
202+
break;
203+
case "darwin":
204+
location = path.join(os.homedir(), "Library", "Caches", ".jdt", "index");
205+
break;
206+
case "linux":
207+
location = process.env.XDG_CACHE_HOME ? path.join(process.env.XDG_CACHE_HOME, ".jdt", "index")
208+
: path.join(os.homedir(), ".cache", ".jdt", "index");
209+
break;
210+
default:
211+
const globalStoragePath = context.globalStorageUri?.fsPath; // .../Code/User/globalStorage/redhat.java
212+
location = globalStoragePath ? path.join(globalStoragePath, "index") : undefined;
213+
}
214+
} else {
215+
// expand homedir
216+
location = location.startsWith(`~${path.sep}`) ? path.join(os.homedir(), location.slice(2)) : location;
217+
}
218+
219+
return location;
220+
}
221+
168222
function resolveConfiguration(context, configDir) {
169223
ensureExists(context.globalStoragePath);
170224
const extensionPath = path.resolve(context.extensionPath, "package.json");

src/settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ function hasJavaConfigChanged(oldConfig: WorkspaceConfiguration, newConfig: Work
132132
|| hasConfigKeyChanged('home', oldConfig, newConfig)
133133
|| hasConfigKeyChanged('jdt.ls.vmargs', oldConfig, newConfig)
134134
|| hasConfigKeyChanged('progressReports.enabled', oldConfig, newConfig)
135-
|| hasConfigKeyChanged('server.launchMode', oldConfig, newConfig);
135+
|| hasConfigKeyChanged('server.launchMode', oldConfig, newConfig)
136+
|| hasConfigKeyChanged('sharedIndexes.location', oldConfig, newConfig);;
136137
}
137138

138139
function hasConfigKeyChanged(key, oldConfig, newConfig) {

test/lightweight-mode-suite/extension.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ suite('Java Language Extension - LightWeight', () => {
2323
Commands.CLEAN_WORKSPACE,
2424
Commands.SWITCH_SERVER_MODE,
2525
Commands.OPEN_FILE,
26+
Commands.CLEAN_SHARED_INDEXES,
2627
].sort();
2728
const foundJavaCommands = commands.filter((value) => {
2829
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

test/standard-mode-suite/extension.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ suite('Java Language Extension - Standard', () => {
8888
Commands.SHOW_CLASS_HIERARCHY,
8989
Commands.LEARN_MORE_ABOUT_CLEAN_UPS,
9090
Commands.OPEN_FILE,
91+
Commands.CLEAN_SHARED_INDEXES,
9192
].sort();
9293
const foundJavaCommands = commands.filter((value) => {
9394
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)