Skip to content

Commit a6405e9

Browse files
committed
Refactored commands to use enum to load
1 parent 25d4eb6 commit a6405e9

22 files changed

+158
-124
lines changed

package.json

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@
6060
"onLanguage:gcode",
6161
"onView:gcode.gcodeTree",
6262
"onView:gcode.gcodeStats",
63+
"onCommand:gcode.addComment",
64+
"onCommand:gcode.addLineNumbers",
65+
"onCommand:gcode.removeComment",
66+
"onCommand:gcode.removeLineNumbers",
67+
"onCommand:gcode.showSettings",
68+
"onCommand:gcode.supportGCode",
6369
"onCommand:gcode.views.navTree.refresh",
64-
"onCommand:gcode.views.stats.refresh",
65-
"onCommand:gcode.supportGCode"
70+
"onCommand:gcode.views.stats.refresh"
6671
],
6772
"capabilities": {
6873
"virtualWorkspaces": true
@@ -353,38 +358,63 @@
353358
],
354359
"commandPalette": [
355360
{
356-
"command": "gcode.views.navTree.refresh",
357-
"when": "true"
361+
"command": "gcode.addComment",
362+
"when": "editorLangId == gcode"
358363
},
359364
{
360-
"command": "gcode.views.stats.refresh",
365+
"command": "gcode.addLineNumbers",
366+
"when": "editorLangId == gcode"
367+
},
368+
{
369+
"command": "gcode.removeComment",
370+
"when": "editorLangId == gcode"
371+
},
372+
{
373+
"command": "gcode.removeLineNumbers",
374+
"when": "editorLangId == gcode"
375+
},
376+
{
377+
"command": "gcode.showSettings",
361378
"when": "true"
362379
},
363380
{
364381
"command": "gcode.supportGCode",
365382
"when": "false"
383+
},
384+
{
385+
"command": "gcode.views.navTree.refresh",
386+
"when": "editorLangId == gcode"
387+
},
388+
{
389+
"command": "gcode.views.stats.refresh",
390+
"when": "editorLangId == gcode"
366391
}
367392
],
368393
"editor/context": [
369394
{
370-
"when": "resourceLangId == gcode",
395+
"when": "editorLangId == gcode",
371396
"command": "gcode.views.navTree.refresh",
372397
"group": "gcode"
373398
},
374399
{
375-
"when": "resourceLangId == gcode",
400+
"when": "editorLangId == gcode",
376401
"command": "gcode.views.stats.refresh",
377402
"group": "gcode"
378403
},
379404
{
380-
"when": "resourceLangId == gcode",
405+
"when": "editorLangId == gcode",
381406
"command": "gcode.addComment",
382407
"group": "gcode"
383408
},
384409
{
385-
"when": "resourceLangId == gcode",
410+
"when": "editorLangId == gcode",
386411
"command": "gcode.removeComment",
387412
"group": "gcode"
413+
},
414+
{
415+
"when": "editorLangId == gcode",
416+
"command": "gcode.showSettings",
417+
"group": "gcode"
388418
}
389419
]
390420
},

src/control.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import { StatusBarControl } from './util/statusBar';
1212
import { NavTreeView } from './views/navTreeView';
1313
import { GCodeUnitsController } from './gcodeUnits';
1414
import { StatsView } from './views/statsView';
15-
import { constants, Contexts, PIcon, VSBuiltInCommands } from './util/constants';
16-
import { UtilCommands } from './util/commands/common';
15+
import { constants, Contexts, GCommands, PIcon, VSBuiltInCommands } from './util/constants';
1716
import { Version } from './util/version';
1817
import { Messages } from './util/messages';
1918
import { StateControl } from './util/stateControl';
2019
import { CodesWebview } from './webviews/codesWebview';
2120
import { MachineTypeControl } from './util/machineType';
2221
import { GCodeHoverControl } from './hovers/gcodeHoverControl';
2322
import { defaults } from './util/configuration/defaults';
23+
import { registerCommands } from './util/commands/functions';
2424

2525
const cfgUnits = 'general.units';
2626
const cfgAutoRef = {
@@ -32,6 +32,7 @@ export class Control {
3232
private static _config: Config | undefined;
3333
private static _context: ExtensionContext;
3434
private static _units: string | undefined;
35+
private static _version: Version;
3536

3637
// Controllers
3738
private static _machineTypeControl: MachineTypeControl | undefined;
@@ -75,9 +76,16 @@ export class Control {
7576

7677
// Static Methods
7778
static initialize(context: ExtensionContext, config: Config) {
79+
// Initialize G-Code Extension
7880
this._context = context;
7981
this._config = config;
8082

83+
// Initialze Configuration
84+
Config.initialize(this._context, this._config);
85+
86+
// Register Commands
87+
context.subscriptions.push(...registerCommands());
88+
8189
// Load StatusBars
8290
context.subscriptions.push((this._statusBarControl = new StatusBarControl()));
8391

@@ -136,7 +144,7 @@ export class Control {
136144
'support',
137145
'Support G-Code Syntax ❤',
138146
undefined,
139-
UtilCommands.ShowSupportGCode,
147+
GCommands.ShowSupportGCode,
140148
);
141149

142150
// Check Version

src/extension.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
* -------------------------------------------------------------------------------------------- */
55
'use strict';
66
import { ExtensionContext } from 'vscode';
7-
import { Config, configuration } from './util/configuration/config';
7+
import { configuration } from './util/configuration/config';
88
import { constants } from './util/constants';
99
import { Logger } from './util/logger';
10-
import { registerCommands } from './util/commands';
1110
import { Control } from './control';
1211

1312
export function activate(context: ExtensionContext) {
@@ -17,13 +16,6 @@ export function activate(context: ExtensionContext) {
1716
Logger.initialize(context);
1817
Logger.enable();
1918

20-
// Initialize Config
21-
const cfg = configuration;
22-
Config.initialize(context, cfg);
23-
24-
// Register Commands
25-
void registerCommands(context);
26-
2719
// Initialize Controller
2820
Control.initialize(context, configuration);
2921

src/gcodeUnits.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import { ConfigurationChangeEvent, Disposable, TextDocumentChangeEvent, TextEditor, window, workspace } from 'vscode';
88
import { Control } from './control';
9-
import { UtilCommands } from './util/commands/common';
109
import { configuration } from './util/configuration/config';
1110
import { defaults } from './util/configuration/defaults';
11+
import { GCommands } from './util/constants';
1212
import { Logger } from './util/logger';
1313
import { StatusBar, StatusBarControl } from './util/statusBar';
1414

@@ -39,7 +39,7 @@ export class GCodeUnitsController implements Disposable {
3939
this.unitsStatusBar,
4040
undefined,
4141
undefined,
42-
UtilCommands.ShowGCodeSettings,
42+
GCommands.ShowGCodeSettings,
4343
);
4444

4545
Control.context.subscriptions.push(configuration.onDidChange(this.onConfigurationChanged, this));
@@ -61,7 +61,7 @@ export class GCodeUnitsController implements Disposable {
6161
this.unitsStatusBar,
6262
undefined,
6363
undefined,
64-
UtilCommands.ShowGCodeSettings,
64+
GCommands.ShowGCodeSettings,
6565
);
6666
} else {
6767
Logger.log(`Units: ${this._units}`);
@@ -71,7 +71,7 @@ export class GCodeUnitsController implements Disposable {
7171
this.unitsStatusBar,
7272
undefined,
7373
undefined,
74-
UtilCommands.ShowGCodeSettings,
74+
GCommands.ShowGCodeSettings,
7575
);
7676
}
7777
}
@@ -91,7 +91,7 @@ export class GCodeUnitsController implements Disposable {
9191
this.unitsStatusBar,
9292
undefined,
9393
undefined,
94-
UtilCommands.ShowGCodeSettings,
94+
GCommands.ShowGCodeSettings,
9595
);
9696
} else {
9797
return;
@@ -112,7 +112,7 @@ export class GCodeUnitsController implements Disposable {
112112
this.unitsStatusBar,
113113
undefined,
114114
undefined,
115-
UtilCommands.ShowGCodeSettings,
115+
GCommands.ShowGCodeSettings,
116116
);
117117
} else {
118118
return;

src/util/commands.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/util/commands/addComment.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
'use strict';
77

88
import { Range, window } from 'vscode';
9-
import { constants } from '../constants';
10-
import { GCommand, UtilCommands } from './common';
9+
import { constants, GCommands } from '../constants';
10+
import { GCommand } from './base';
1111

1212
export class AddComment extends GCommand {
1313
constructor() {
14-
super(UtilCommands.AddComment);
14+
super(GCommands.AddComment);
1515
}
1616

1717
execute() {

src/util/commands/addLineNumbers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
'use strict';
77

88
import { configuration } from '../configuration/config';
9+
import { GCommands } from '../constants';
910
import { LineNumberer, LineNumbererOptions } from '../lineNumberer';
1011
import { LineNumbersInput } from '../quickpicks/lineNumbers';
11-
import { GCommand, UtilCommands } from './common';
12+
import { GCommand } from './base';
1213

1314
export class AddLineNumbers extends GCommand {
1415
constructor() {
15-
super(UtilCommands.AddLineNumbers);
16+
super(GCommands.AddLineNumbers);
1617
}
1718

1819
async execute() {
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66
'use strict';
77

88
import { commands, Disposable } from 'vscode';
9-
10-
export const enum UtilCommands {
11-
ShowGCodeSettings = 'gcode.showSettings',
12-
ShowSupportGCode = 'gcode.supportGCode',
13-
AddComment = 'gcode.addComment',
14-
RemoveComment = 'gcode.removeComment',
15-
AddLineNumbers = 'gcode.addLineNumbers',
16-
RemoveLineNumbers = 'gcode.removeLineNumbers',
17-
}
9+
import { GCommands } from '../constants';
1810

1911
export abstract class GCommand implements Disposable {
20-
private _disposable: Disposable;
12+
private readonly _disposable: Disposable;
13+
14+
constructor(cmd: GCommands | GCommands[]) {
15+
if (typeof cmd === 'string') {
16+
this._disposable = commands.registerCommand(cmd, (...args: any[]) => this._execute(cmd, ...args), this);
2117

22-
constructor(cmd: UtilCommands) {
23-
this._disposable = commands.registerCommand(cmd, (...args: any[]) => this._execute(cmd, ...args), this);
18+
return;
19+
}
2420

25-
return;
21+
const subscriptions = cmd.map(cmd =>
22+
commands.registerCommand(cmd, (...args: any[]) => this._execute(cmd, ...args), this),
23+
);
24+
this._disposable = Disposable.from(...subscriptions);
2625
}
2726

2827
dispose() {

src/util/commands/cmds.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
/* eslint-disable max-classes-per-file */
21
/* ---------------------------------------------------------------------------------------------
32
* Copyright (c) Applied Eng & Design All rights reserved.
43
* Licensed under the MIT License. See License.md in the project root for license information.
54
* -------------------------------------------------------------------------------------------- */
65

76
'use strict';
87

9-
export * from './addComment';
10-
export * from './removeComment';
11-
export * from './showGCodeSettings';
12-
export * from './showSupportGCode';
13-
export * from './addLineNumbers';
14-
export * from './removeLineNumbers';
8+
export { AddComment } from './addComment';
9+
export { AddLineNumbers } from './addLineNumbers';
10+
export { RemoveComment } from './removeComment';
11+
export { RemoveLineNumbers } from './removeLineNumbers';
12+
export { ShowGCodeSettings } from './showGCodeSettings';
13+
export { ShowSupportGCode } from './showSupportGCode';

src/util/commands/functions.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* ---------------------------------------------------------------------------------------------
2+
* Copyright (c) Applied Eng & Design All rights reserved.
3+
* Licensed under the MIT License. See License.md in the project root for license information.
4+
* -------------------------------------------------------------------------------------------- */
5+
6+
'use strict';
7+
8+
import { Disposable } from 'vscode';
9+
import { GCommands } from '../constants';
10+
import { GCommand } from './base';
11+
import * as Cmds from './cmds';
12+
13+
export function registerCommands(): Disposable[] {
14+
const gcmds: GCommand[] = [];
15+
16+
Object.values(Cmds).forEach(c => {
17+
if (Object.keys(GCommands).includes(c.name)) {
18+
gcmds.push(new c());
19+
}
20+
});
21+
22+
return gcmds;
23+
}

0 commit comments

Comments
 (0)