Skip to content

Commit da55614

Browse files
authored
Adopt APIs in LightWeight mode (#272)
1 parent 1a9ba5e commit da55614

File tree

6 files changed

+145
-11
lines changed

6 files changed

+145
-11
lines changed

package.json

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@
152152
},
153153
"menus": {
154154
"commandPalette": [
155+
{
156+
"command": "java.view.package.refresh",
157+
"when": "java:serverMode != LightWeight"
158+
},
159+
{
160+
"command": "java.view.package.changeToHierarchicalPackageView",
161+
"when": "java:serverMode != LightWeight"
162+
},
163+
{
164+
"command": "java.view.package.changeToFlatPackageView",
165+
"when": "java:serverMode != LightWeight"
166+
},
167+
{
168+
"command": "java.view.package.linkWithFolderExplorer",
169+
"when": "java:serverMode != LightWeight"
170+
},
171+
{
172+
"command": "java.view.package.unlinkWithFolderExplorer",
173+
"when": "java:serverMode != LightWeight"
174+
},
155175
{
156176
"command": "java.view.package.revealFileInOS",
157177
"when": "never"
@@ -184,27 +204,27 @@
184204
"view/title": [
185205
{
186206
"command": "java.view.package.refresh",
187-
"when": "view == javaProjectExplorer",
207+
"when": "view == javaProjectExplorer && java:serverMode!= LightWeight",
188208
"group": "navigation@2"
189209
},
190210
{
191211
"command": "java.view.package.changeToHierarchicalPackageView",
192-
"when": "view == javaProjectExplorer && config.java.dependency.packagePresentation == flat",
212+
"when": "view == javaProjectExplorer && config.java.dependency.packagePresentation == flat && java:serverMode!= LightWeight",
193213
"group": "navigation@1"
194214
},
195215
{
196216
"command": "java.view.package.changeToFlatPackageView",
197-
"when": "view == javaProjectExplorer && config.java.dependency.packagePresentation != flat",
217+
"when": "view == javaProjectExplorer && config.java.dependency.packagePresentation != flat && java:serverMode!= LightWeight",
198218
"group": "navigation@1"
199219
},
200220
{
201221
"command": "java.view.package.linkWithFolderExplorer",
202-
"when": "view == javaProjectExplorer && config.java.dependency.syncWithFolderExplorer != true",
222+
"when": "view == javaProjectExplorer && config.java.dependency.syncWithFolderExplorer != true && java:serverMode!= LightWeight",
203223
"group": "navigation@0"
204224
},
205225
{
206226
"command": "java.view.package.unlinkWithFolderExplorer",
207-
"when": "view == javaProjectExplorer && config.java.dependency.syncWithFolderExplorer == true",
227+
"when": "view == javaProjectExplorer && config.java.dependency.syncWithFolderExplorer == true && java:serverMode!= LightWeight",
208228
"group": "navigation@0"
209229
}
210230
],

src/commands.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ export namespace Commands {
4848

4949
export const JAVA_GETPACKAGEDATA = "java.getPackageData";
5050

51+
export const JAVA_PROJECT_SWITCH_SERVER_MODE = "java.project.switch.server.mode";
52+
53+
/**
54+
* command from VS Code Java to switch the language server mode
55+
*/
56+
export const JAVA_SWITCH_SERVER_MODE = "java.server.mode.switch";
57+
5158
export const JAVA_RESOLVEPATH = "java.resolvePath";
5259

5360
export const VSCODE_OPEN_FOLDER = "vscode.openFolder";

src/extension.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4-
import { Extension, ExtensionContext, extensions } from "vscode";
5-
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation } from "vscode-extension-telemetry-wrapper";
4+
import { commands, Event, Extension, ExtensionContext, extensions, Uri } from "vscode";
5+
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation, instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
6+
import { Commands } from "./commands";
67
import { Context } from "./constants";
78
import { contextManager } from "./contextManager";
89
import { LibraryController } from "./controllers/libraryController";
@@ -17,6 +18,38 @@ export async function activate(context: ExtensionContext): Promise<any> {
1718
}
1819

1920
async function activateExtension(_operationId: string, context: ExtensionContext): Promise<void> {
21+
const extension: Extension<any> | undefined = extensions.getExtension("redhat.java");
22+
if (extension && extension.isActive) {
23+
const extensionApi: any = extension.exports;
24+
if (!extensionApi) {
25+
return;
26+
}
27+
28+
serverMode = extensionApi.serverMode;
29+
30+
if (extensionApi.onDidClasspathUpdate) {
31+
const onDidClasspathUpdate: Event<Uri> = extensionApi.onDidClasspathUpdate;
32+
context.subscriptions.push(onDidClasspathUpdate(async () => {
33+
await commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */true);
34+
}));
35+
}
36+
37+
if (extensionApi.onDidServerModeChange) {
38+
const onDidServerModeChange: Event<string> = extensionApi.onDidServerModeChange;
39+
context.subscriptions.push(onDidServerModeChange(async (mode: string) => {
40+
serverMode = mode;
41+
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */false);
42+
}));
43+
}
44+
45+
if (extensionApi.onDidProjectsImport) {
46+
const onDidProjectsImport: Event<Uri[]> = extensionApi.onDidProjectsImport;
47+
context.subscriptions.push(onDidProjectsImport(async () => {
48+
commands.executeCommand(Commands.VIEW_PACKAGE_REFRESH, /* debounce = */true);
49+
}));
50+
}
51+
}
52+
2053
Settings.initialize(context);
2154
contextManager.initialize(context);
2255
setMavenExtensionState();
@@ -28,6 +61,13 @@ async function activateExtension(_operationId: string, context: ExtensionContext
2861
contextManager.setContextValue(Context.EXTENSION_ACTIVATED, true);
2962

3063
initExpService(context);
64+
65+
context.subscriptions.push(instrumentOperationAsVsCodeCommand(Commands.JAVA_PROJECT_SWITCH_SERVER_MODE, async () => {
66+
if (isSwitchingServer()) {
67+
return;
68+
}
69+
await commands.executeCommand(Commands.JAVA_SWITCH_SERVER_MODE, "Standard" /*mode*/);
70+
}));
3171
}
3272

3373
// determine if the add dependency shortcut will show or not
@@ -47,3 +87,22 @@ function setMavenExtensionState() {
4787
export async function deactivate() {
4888
await disposeTelemetryWrapper();
4989
}
90+
91+
export function isStandardServerReady(): boolean {
92+
// undefined serverMode indicates an older version language server
93+
if (serverMode === undefined) {
94+
return true;
95+
}
96+
97+
if (serverMode !== "Standard") {
98+
return false;
99+
}
100+
101+
return true;
102+
}
103+
104+
export function isSwitchingServer(): boolean {
105+
return serverMode === "Hybrid";
106+
}
107+
108+
let serverMode: string | undefined;

src/views/dependencyDataProvider.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33

44
import * as _ from "lodash";
55
import {
6-
commands, Event, EventEmitter, ExtensionContext, ProviderResult, Range,
7-
Selection, TextEditorRevealType, TreeDataProvider, TreeItem, Uri, window, workspace,
6+
commands, Event, EventEmitter, ExtensionContext, extensions, ProviderResult,
7+
Range, Selection, TextEditorRevealType, TreeDataProvider, TreeItem, Uri, window, workspace,
88
} from "vscode";
99
import { instrumentOperation, instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper";
1010
import { Commands } from "../commands";
11+
import { isStandardServerReady, isSwitchingServer } from "../extension";
1112
import { Jdtls } from "../java/jdtls";
1213
import { INodeData, NodeKind } from "../java/nodeData";
1314
import { Settings } from "../settings";
1415
import { DataNode } from "./dataNode";
1516
import { ExplorerNode } from "./explorerNode";
17+
import { LightWeightNode } from "./lightWeightNode";
1618
import { ProjectNode } from "./projectNode";
1719
import { WorkspaceNode } from "./workspaceNode";
1820

@@ -87,7 +89,19 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
8789
return element.getTreeItem();
8890
}
8991

90-
public getChildren(element?: ExplorerNode): ProviderResult<ExplorerNode[]> {
92+
public async getChildren(element?: ExplorerNode): Promise<ExplorerNode[]> {
93+
if (isSwitchingServer()) {
94+
await new Promise<void>((resolve: () => void): void => {
95+
extensions.getExtension("redhat.java")!.exports.onDidServerModeChange(resolve);
96+
});
97+
}
98+
99+
if (!isStandardServerReady()) {
100+
return [
101+
new LightWeightNode(),
102+
];
103+
}
104+
91105
if (!this._rootItems || !element) {
92106
return this.getRootNodes();
93107
} else {

src/views/dependencyExplorer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
// Licensed under the MIT license.
33

44
import { Disposable, ExtensionContext, TextEditor, TreeView, TreeViewVisibilityChangeEvent, Uri, window } from "vscode";
5+
import { isStandardServerReady } from "../extension";
56
import { Jdtls } from "../java/jdtls";
67
import { INodeData } from "../java/nodeData";
78
import { Settings } from "../settings";
8-
import { DataNode } from "./dataNode";
99
import { DependencyDataProvider } from "./dependencyDataProvider";
1010
import { ExplorerNode } from "./explorerNode";
1111

@@ -51,6 +51,10 @@ export class DependencyExplorer implements Disposable {
5151
}
5252

5353
public async reveal(uri: Uri): Promise<void> {
54+
if (!isStandardServerReady()) {
55+
return;
56+
}
57+
5458
const paths: INodeData[] = await Jdtls.resolvePath(uri.toString());
5559
if (!paths || paths.length === 0) {
5660
return;

src/views/lightWeightNode.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
import * as _ from "lodash";
5+
import { ProviderResult, ThemeIcon, TreeItem, TreeItemCollapsibleState } from "vscode";
6+
import { Commands } from "../commands";
7+
import { ExplorerNode } from "./explorerNode";
8+
9+
export class LightWeightNode extends ExplorerNode {
10+
constructor() {
11+
super(null);
12+
}
13+
14+
public getTreeItem(): TreeItem | Promise<TreeItem> {
15+
return {
16+
label: "Click to load dependencies...",
17+
collapsibleState: TreeItemCollapsibleState.None,
18+
command: {
19+
command: Commands.JAVA_PROJECT_SWITCH_SERVER_MODE,
20+
title: "Switch to Standard mode",
21+
},
22+
tooltip: "Switch the Java Language Server to Standard mode to show all the dependencies",
23+
iconPath: new ThemeIcon("info"),
24+
};
25+
}
26+
27+
public getChildren(): ProviderResult<ExplorerNode[]> {
28+
return null;
29+
}
30+
}

0 commit comments

Comments
 (0)