Skip to content

Commit a7e265b

Browse files
authored
fix: Project explorer grab the focus unexpectedly (#525)
1 parent 031fa22 commit a7e265b

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

src/commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export namespace Commands {
8282

8383
export const JAVA_RESOLVE_BUILD_FILES = "vscode.java.resolveBuildFiles";
8484

85+
export const JAVA_PROJECT_LIST_SOURCE_PATHS = "java.project.listSourcePaths";
86+
8587
/**
8688
* Commands from Visual Studio Code
8789
*/

src/constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ export namespace Explorer {
2424
export namespace Build {
2525
export const FILE_NAMES: string[] = ["pom.xml", "build.gradle"];
2626
}
27+
28+
export namespace ExtensionName {
29+
export const JAVA_LANGUAGE_SUPPORT: string = "redhat.java";
30+
}

src/extension.ts

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

4-
import { ExtensionContext, tasks } from "vscode";
4+
import { Event, Extension, ExtensionContext, extensions, tasks, Uri } from "vscode";
55
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation, sendInfo } from "vscode-extension-telemetry-wrapper";
66
import { contextManager } from "../extension.bundle";
7-
import { Build, Context } from "./constants";
7+
import { Build, Context, ExtensionName } from "./constants";
88
import { LibraryController } from "./controllers/libraryController";
99
import { ProjectController } from "./controllers/projectController";
1010
import { init as initExpService } from "./ExperimentationService";
@@ -31,6 +31,31 @@ async function activateExtension(_operationId: string, context: ExtensionContext
3131
context.subscriptions.push(contextManager);
3232
context.subscriptions.push(syncHandler);
3333
context.subscriptions.push(tasks.registerTaskProvider(ExportJarTaskProvider.exportJarType, new ExportJarTaskProvider()));
34+
35+
const javaLanguageSupport: Extension<any> | undefined = extensions.getExtension(ExtensionName.JAVA_LANGUAGE_SUPPORT);
36+
if (!javaLanguageSupport) {
37+
return;
38+
}
39+
javaLanguageSupport.activate().then(() => {
40+
const extensionApi: any = javaLanguageSupport.exports;
41+
if (!extensionApi) {
42+
return;
43+
}
44+
45+
if (extensionApi.onDidClasspathUpdate) {
46+
const onDidClasspathUpdate: Event<Uri> = extensionApi.onDidClasspathUpdate;
47+
context.subscriptions.push(onDidClasspathUpdate(async () => {
48+
syncHandler.updateFileWatcher(Settings.autoRefresh());
49+
}));
50+
}
51+
52+
if (extensionApi.onDidServerModeChange) {
53+
const onDidServerModeChange: Event<string> = extensionApi.onDidServerModeChange;
54+
context.subscriptions.push(onDidServerModeChange(async () => {
55+
syncHandler.updateFileWatcher(Settings.autoRefresh());
56+
}));
57+
}
58+
});
3459
}
3560

3661
// this method is called when your extension is deactivated

src/syncHandler.ts

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

44
import * as path from "path";
5-
import { commands, Disposable, FileSystemWatcher, Uri, workspace } from "vscode";
5+
import { commands, Disposable, FileSystemWatcher, RelativePattern, Uri, workspace } from "vscode";
66
import { instrumentOperation } from "vscode-extension-telemetry-wrapper";
77
import { Commands } from "./commands";
88
import { NodeKind } from "./java/nodeData";
@@ -19,10 +19,11 @@ class SyncHandler implements Disposable {
1919
private disposables: Disposable[] = [];
2020

2121
public updateFileWatcher(autoRefresh?: boolean): void {
22+
this.dispose();
2223
if (autoRefresh) {
2324
instrumentOperation(ENABLE_AUTO_REFRESH, () => this.enableAutoRefresh())();
2425
} else {
25-
instrumentOperation(DISABLE_AUTO_REFRESH, () => this.dispose())();
26+
instrumentOperation(DISABLE_AUTO_REFRESH, () => {})();
2627
}
2728
}
2829

@@ -40,9 +41,25 @@ class SyncHandler implements Disposable {
4041
this.refresh();
4142
}));
4243

43-
const fileSystemWatcher: FileSystemWatcher = workspace.createFileSystemWatcher("**/{*.java,src/**}");
44-
this.setupWatchers(fileSystemWatcher);
45-
this.disposables.push(fileSystemWatcher);
44+
try {
45+
const result: IListCommandResult | undefined = await commands.executeCommand<IListCommandResult>(Commands.EXECUTE_WORKSPACE_COMMAND,
46+
Commands.JAVA_PROJECT_LIST_SOURCE_PATHS);
47+
if (!result || !result.status || !result.data || result.data.length === 0) {
48+
throw new Error("Failed to list the source paths");
49+
}
50+
51+
for (const sourcePathData of result.data) {
52+
const normalizedPath: string = Uri.file(sourcePathData.path).fsPath;
53+
const pattern: RelativePattern = new RelativePattern(normalizedPath, "**/*");
54+
const watcher: FileSystemWatcher = workspace.createFileSystemWatcher(pattern);
55+
this.disposables.push(watcher);
56+
this.setupWatchers(watcher);
57+
}
58+
} catch (e) {
59+
const fileSystemWatcher: FileSystemWatcher = workspace.createFileSystemWatcher("**/{*.java,src/**}");
60+
this.disposables.push(fileSystemWatcher);
61+
this.setupWatchers(fileSystemWatcher);
62+
}
4663
}
4764

4865
private setupWatchers(watcher: FileSystemWatcher): void {
@@ -100,4 +117,17 @@ class SyncHandler implements Disposable {
100117
}
101118
}
102119

120+
interface ISourcePath {
121+
path: string;
122+
displayPath: string;
123+
projectName: string;
124+
projectType: string;
125+
}
126+
127+
interface IListCommandResult {
128+
status: boolean;
129+
message: string;
130+
data?: ISourcePath[];
131+
}
132+
103133
export const syncHandler: SyncHandler = new SyncHandler();

src/views/dependencyExplorer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class DependencyExplorer implements Disposable {
6060
}
6161
}),
6262
this._dataProvider.onDidChangeTreeData(() => {
63-
if (window.activeTextEditor) {
63+
if (this._dependencyViewer.visible && window.activeTextEditor) {
6464
this.reveal(window.activeTextEditor.document.uri);
6565
}
6666
}),

0 commit comments

Comments
 (0)