Skip to content

Commit 1306cc8

Browse files
Merge pull request #58 from magebitcom/bugfix/55
fix: getActiveWorkspaceFolder returns undefined if no active text edi…
2 parents ee7f4f3 + e7dc3f7 commit 1306cc8

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

src/common/ExtensionState.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { ExtensionContext } from 'vscode';
1+
import { ExtensionContext, WorkspaceFolder } from 'vscode';
22

33
export default class ExtensionState {
44
private static _context: ExtensionContext;
5+
private static _magentoWorkspaces: WorkspaceFolder[] = [];
56

6-
public static init(context: ExtensionContext) {
7+
public static init(context: ExtensionContext, magentoWorkspaces: WorkspaceFolder[]) {
78
this._context = context;
9+
this._magentoWorkspaces = magentoWorkspaces;
810
}
911

1012
public static get context() {
1113
return this._context;
1214
}
15+
16+
public static get magentoWorkspaces() {
17+
return this._magentoWorkspaces;
18+
}
1319
}

src/extension.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import GenerateRoutesXmlFileCommand from 'command/GenerateRoutesXmlFileCommand';
2424
import GenerateAclXmlFileCommand from 'command/GenerateAclXmlFileCommand';
2525
import GenerateDiXmlFileCommand from 'command/GenerateDiXmlFileCommand';
2626
import GeneratePreferenceCommand from 'command/GeneratePreferenceCommand';
27+
import Magento from 'util/Magento';
28+
import { WorkspaceFolder } from 'vscode';
2729

2830
// This method is called when your extension is activated
2931
// Your extension is activated the very first time the command is executed
@@ -46,15 +48,32 @@ export async function activate(context: vscode.ExtensionContext) {
4648
GeneratePreferenceCommand,
4749
];
4850

49-
ExtensionState.init(context);
51+
const magentoWorkspaces: WorkspaceFolder[] = [];
52+
53+
if (vscode.workspace.workspaceFolders) {
54+
for (const folder of vscode.workspace.workspaceFolders) {
55+
if (await Magento.isMagentoWorkspace(folder)) {
56+
magentoWorkspaces.push(folder);
57+
}
58+
}
59+
}
60+
61+
ExtensionState.init(context, magentoWorkspaces);
5062

5163
commands.forEach(command => {
5264
const instance = new command();
5365

5466
Common.log('Registering command', instance.getCommand());
5567

56-
const disposable = vscode.commands.registerCommand(instance.getCommand(), (...args) => {
57-
instance.execute(...args);
68+
const disposable = vscode.commands.registerCommand(instance.getCommand(), async (...args) => {
69+
try {
70+
await instance.execute(...args);
71+
} catch (error) {
72+
console.error(error);
73+
vscode.window.showErrorMessage(
74+
'An error occurred while executing the command: ' + instance.getCommand()
75+
);
76+
}
5877
});
5978

6079
context.subscriptions.push(disposable);

src/util/Common.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ExtensionState from 'common/ExtensionState';
12
import { workspace, WorkspaceFolder, window } from 'vscode';
23

34
export default class Common {
@@ -32,14 +33,14 @@ export default class Common {
3233
throw new Error('Workspace is empty');
3334
}
3435

35-
if (workspace.workspaceFolders.length === 1) {
36-
return workspace.workspaceFolders[0];
36+
if (window.activeTextEditor?.document.uri) {
37+
return workspace.getWorkspaceFolder(window.activeTextEditor.document.uri);
3738
}
3839

39-
if (!window.activeTextEditor?.document.uri) {
40-
throw new Error('No active text editor');
40+
if (ExtensionState.magentoWorkspaces.length > 0) {
41+
return ExtensionState.magentoWorkspaces[0];
4142
}
4243

43-
return workspace.getWorkspaceFolder(window.activeTextEditor.document.uri);
44+
return undefined;
4445
}
4546
}

src/util/Magento.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import PhpNamespace from 'common/PhpNamespace';
22
import lowerFirst from 'lodash-es/lowerFirst';
33
import { MagentoScope } from 'types';
4-
import { Uri } from 'vscode';
5-
4+
import { Uri, WorkspaceFolder } from 'vscode';
5+
import FileSystem from './FileSystem';
66
export default class Magento {
77
public static isPluginMethod(method: string) {
88
return /^around|^before|^after/.test(method);
@@ -40,4 +40,14 @@ export default class Magento {
4040
public static getModuleNamespace(vendor: string, module: string): PhpNamespace {
4141
return PhpNamespace.fromParts([vendor, module]);
4242
}
43+
44+
public static async isMagentoWorkspace(workspaceFolder: WorkspaceFolder): Promise<boolean> {
45+
const diXmlPath = Uri.joinPath(workspaceFolder.uri, 'app/etc/di.xml');
46+
// Check if the signature Magento file exists in the workspace
47+
try {
48+
return await FileSystem.fileExists(diXmlPath);
49+
} catch (error) {
50+
return false;
51+
}
52+
}
4353
}

0 commit comments

Comments
 (0)