Skip to content

Commit 765ec5d

Browse files
xqzlgy2jdneo
andauthored
fix: NPE error when open non-project file (#417)
Co-authored-by: Sheng Chen <[email protected]>
1 parent ecb14dd commit 765ec5d

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

src/utility.ts

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

4-
import { window, workspace, WorkspaceFolder } from "vscode";
4+
import { Uri, window, workspace, WorkspaceFolder } from "vscode";
55
import { setUserError } from "vscode-extension-telemetry-wrapper";
6+
import { languageServerApiManager } from "./languageServerApi/languageServerApiManager";
7+
import { Settings } from "./settings";
68

79
export class Utility {
810

@@ -21,6 +23,19 @@ export class Utility {
2123
return undefined;
2224
}
2325

26+
public static async isRevealable(uri: Uri): Promise<boolean> {
27+
if (!SUPPORTED_URI_SCHEMES.includes(uri.scheme)) {
28+
return false;
29+
}
30+
if (uri.scheme === "file" && !workspace.getWorkspaceFolder(uri)) {
31+
return false;
32+
}
33+
if (!Settings.syncWithFolderExplorer() || !await languageServerApiManager.isStandardServerReady()) {
34+
return false;
35+
}
36+
return true;
37+
}
38+
2439
}
2540

2641
export class UserError extends Error {
@@ -61,6 +76,8 @@ const keywords: Set<string> = new Set([
6176
"const", "for", "new", "switch", "continue", "goto", "package", "synchronized", "true", "false", "null", "assert", "enum",
6277
]);
6378

79+
const SUPPORTED_URI_SCHEMES: string[] = ["file", "jdt"];
80+
6481
export function isKeyword(identifier: string): boolean {
6582
return keywords.has(identifier);
6683
}

src/views/dataNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export abstract class DataNode extends ExplorerNode {
110110

111111
protected abstract get iconPath(): string | Uri | { light: string | Uri; dark: string | Uri } | ThemeIcon;
112112

113-
protected async abstract loadData(): Promise<any[] | undefined>;
113+
protected abstract loadData(): Promise<any[] | undefined>;
114114

115115
protected abstract createChildNodeList(): ExplorerNode[] | undefined;
116116
}

src/views/dependencyDataProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class DependencyDataProvider implements TreeDataProvider<ExplorerNode> {
125125
const projects = await this.getRootProjects();
126126
const project = projects ? <DataNode>projects.find((node: DataNode) =>
127127
node.path === projectNodeData?.path && node.nodeData.name === projectNodeData?.name) : undefined;
128-
return project ? project.revealPaths(paths) : undefined;
128+
return project?.revealPaths(paths);
129129
}
130130

131131
private doRefresh(element?: ExplorerNode): void {

src/views/dependencyExplorer.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import { renameFile } from "../explorerCommands/rename";
1313
import { getCmdNode } from "../explorerCommands/utility";
1414
import { Jdtls } from "../java/jdtls";
1515
import { INodeData } from "../java/nodeData";
16-
import { languageServerApiManager } from "../languageServerApi/languageServerApiManager";
17-
import { Settings } from "../settings";
16+
import { Utility } from "../utility";
1817
import { Lock } from "../utils/Lock";
1918
import { DataNode } from "./dataNode";
2019
import { DependencyDataProvider } from "./dependencyDataProvider";
@@ -38,34 +37,30 @@ export class DependencyExplorer implements Disposable {
3837

3938
private _dataProvider: DependencyDataProvider;
4039

41-
private readonly SUPPORTED_URI_SCHEMES: string[] = ["file", "jdt"];
42-
4340
constructor(public readonly context: ExtensionContext) {
4441
this._dataProvider = new DependencyDataProvider(context);
4542
this._dependencyViewer = window.createTreeView("javaProjectExplorer", { treeDataProvider: this._dataProvider, showCollapseAll: true });
4643

4744
context.subscriptions.push(
48-
window.onDidChangeActiveTextEditor((textEditor: TextEditor) => {
49-
if (this._dependencyViewer.visible && textEditor && textEditor.document && Settings.syncWithFolderExplorer()) {
45+
window.onDidChangeActiveTextEditor((textEditor: TextEditor | undefined) => {
46+
if (this._dependencyViewer.visible && textEditor?.document) {
5047
const uri: Uri = textEditor.document.uri;
51-
if (this.SUPPORTED_URI_SCHEMES.includes(uri.scheme)) {
52-
this.reveal(uri);
53-
}
48+
this.reveal(uri);
5449
}
5550
}),
5651
);
5752

5853
context.subscriptions.push(
5954
this._dependencyViewer.onDidChangeVisibility((e: TreeViewVisibilityChangeEvent) => {
60-
if (e.visible && window.activeTextEditor && Settings.syncWithFolderExplorer()) {
55+
if (e.visible && window.activeTextEditor) {
6156
this.reveal(window.activeTextEditor.document.uri);
6257
}
6358
}),
6459
);
6560

6661
context.subscriptions.push(
6762
this._dataProvider.onDidChangeTreeData(() => {
68-
if (window.activeTextEditor && Settings.syncWithFolderExplorer()) {
63+
if (window.activeTextEditor) {
6964
this.reveal(window.activeTextEditor.document.uri);
7065
}
7166
}),
@@ -140,7 +135,7 @@ export class DependencyExplorer implements Disposable {
140135
try {
141136
await this._lock.acquire();
142137

143-
if (!await languageServerApiManager.isStandardServerReady()) {
138+
if (!await Utility.isRevealable(uri)) {
144139
return;
145140
}
146141

0 commit comments

Comments
 (0)