Skip to content

Commit fbc23b0

Browse files
CopilotBernardXiong
andcommitted
refactor: Address code review feedback - extract constants and add error handling
Co-authored-by: BernardXiong <[email protected]>
1 parent 9af19c7 commit fbc23b0

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/project/cmd.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as path from 'path';
66
import { getWorkspaceFolder } from '../api';
77
import { executeCommand } from '../terminal';
88
import { readWorkspaceJson, writeWorkspaceJson } from '../webviews/project';
9-
import { getMenuconfigMethod } from '../smart';
9+
import { getMenuconfigMethod, MENUCONFIG_COMMANDS } from '../smart';
1010

1111
let _currentProject: string = '';
1212

@@ -33,20 +33,28 @@ export function configProject(arg: any) {
3333

3434
if (menuconfigMethod.type === 'extension') {
3535
// For rt-thread-kconfig extension, it handles multi-BSP scenarios automatically
36-
if (menuconfigMethod.command === 'rt-thread-kconfig.menuconfig.windows') {
36+
if (menuconfigMethod.command === MENUCONFIG_COMMANDS.RT_THREAD_KCONFIG) {
3737
// Change to the BSP directory first
3838
executeCommand('cd ' + arg.fn);
3939
// Execute the extension command
4040
vscode.commands.executeCommand(menuconfigMethod.command);
4141
}
4242
// For vscode-kconfig-visual-editor, we need to open the Kconfig file explicitly
43-
else if (menuconfigMethod.command === 'kconfig-visual-editor.open') {
43+
else if (menuconfigMethod.command === MENUCONFIG_COMMANDS.KCONFIG_VISUAL_EDITOR) {
4444
const kconfigPath = path.join(arg.fn, 'Kconfig');
4545
if (fs.existsSync(kconfigPath)) {
4646
// Open the Kconfig file with the visual editor
47-
vscode.workspace.openTextDocument(kconfigPath).then(doc => {
48-
vscode.window.showTextDocument(doc);
49-
});
47+
vscode.workspace.openTextDocument(kconfigPath).then(
48+
doc => {
49+
vscode.window.showTextDocument(doc);
50+
},
51+
(error: Error) => {
52+
vscode.window.showErrorMessage(`Failed to open Kconfig file: ${error.message}`);
53+
// Fallback to terminal on error
54+
let cmd = 'scons -C ' + arg.fn + ' --menuconfig';
55+
executeCommand(cmd);
56+
}
57+
);
5058
} else {
5159
// Fallback to terminal if Kconfig doesn't exist
5260
let cmd = 'scons -C ' + arg.fn + ' --menuconfig';

src/smart.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ export interface MenuconfigMethod {
3333
terminal?: string; // Terminal command for terminal type
3434
}
3535

36+
/**
37+
* Extension command constants for menuconfig
38+
*/
39+
export const MENUCONFIG_COMMANDS = {
40+
RT_THREAD_KCONFIG: 'rt-thread-kconfig.menuconfig.windows',
41+
KCONFIG_VISUAL_EDITOR: 'kconfig-visual-editor.open'
42+
};
43+
3644
/**
3745
* Get the appropriate menuconfig method based on installed extensions and configuration
3846
* Priority:
@@ -58,7 +66,7 @@ export function getMenuconfigMethod(kconfigPath?: string): MenuconfigMethod {
5866
if (rtThreadKconfig !== undefined) {
5967
return {
6068
type: 'extension',
61-
command: 'rt-thread-kconfig.menuconfig.windows'
69+
command: MENUCONFIG_COMMANDS.RT_THREAD_KCONFIG
6270
};
6371
}
6472

@@ -67,7 +75,7 @@ export function getMenuconfigMethod(kconfigPath?: string): MenuconfigMethod {
6775
if (kconfigVisualEditor !== undefined) {
6876
return {
6977
type: 'extension',
70-
command: 'kconfig-visual-editor.open',
78+
command: MENUCONFIG_COMMANDS.KCONFIG_VISUAL_EDITOR,
7179
};
7280
}
7381

0 commit comments

Comments
 (0)