Skip to content

Commit 7c953eb

Browse files
committed
Refactor commands
1 parent f395a75 commit 7c953eb

16 files changed

Lines changed: 248 additions & 196 deletions

src/control.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { NavTreeView } from './views/navTreeView';
1313
import { GCodeUnitsController } from './gcodeUnits';
1414
import { StatsView } from './views/statsView';
1515
import { constants, Contexts, PIcon, VSBuiltInCommands } from './util/constants';
16-
import { Commands } from './util/commands';
16+
import { UtilCommands } from './util/commands/common';
1717
import { Version } from './util/version';
1818
import { Messages } from './util/messages';
1919
import { StateControl } from './util/stateControl';
@@ -32,6 +32,33 @@ export class Control {
3232
private static _statsView: StatsView | undefined;
3333
private static _navTree: NavTreeView | undefined;
3434

35+
private static async checkVersion() {
36+
const gcodeVersion = new Version(constants.extension.version);
37+
38+
const prevVer = this._stateController.getVersion();
39+
40+
const newVer = gcodeVersion.compareWith(prevVer.getVersion()) === 1 ? true : false;
41+
42+
if (newVer) {
43+
// Extension has been updated
44+
45+
// Update globalState version
46+
Logger.log('Updating...');
47+
void this._stateController.updateVer(gcodeVersion);
48+
49+
Logger.log(`G-Code upgraded from ${prevVer.getVersionAsString()} to ${gcodeVersion.getVersionAsString()}`);
50+
await this.showWhatsNew(gcodeVersion);
51+
} else {
52+
return;
53+
}
54+
}
55+
56+
private static async showWhatsNew(ver: Version) {
57+
// Show Whats New Message
58+
await Messages.showWhatsNewMessage(ver);
59+
}
60+
61+
// Static Methods
3562
static initialize(context: ExtensionContext, config: Config) {
3663
this._context = context;
3764
this._config = config;
@@ -99,7 +126,7 @@ export class Control {
99126
'support',
100127
'Support G-Code Syntax ❤',
101128
undefined,
102-
Commands.GCSUPPORT,
129+
UtilCommands.ShowSupportGCode,
103130
);
104131

105132
// Check Version
@@ -118,32 +145,6 @@ export class Control {
118145
return secs * 1000 + nanosecs / 1000000;
119146
}
120147

121-
private static async checkVersion() {
122-
const gcodeVersion = new Version(constants.extension.version);
123-
124-
const prevVer = this._stateController.getVersion();
125-
126-
const newVer = gcodeVersion.compareWith(prevVer.getVersion()) === 1 ? true : false;
127-
128-
if (newVer) {
129-
// Extension has been updated
130-
131-
// Update globalState version
132-
Logger.log('Updating...');
133-
void this._stateController.updateVer(gcodeVersion);
134-
135-
Logger.log(`G-Code upgraded from ${prevVer.getVersionAsString()} to ${gcodeVersion.getVersionAsString()}`);
136-
await this.showWhatsNew(gcodeVersion);
137-
} else {
138-
return;
139-
}
140-
}
141-
142-
private static async showWhatsNew(ver: Version) {
143-
// Show Whats New Message
144-
await Messages.showWhatsNewMessage(ver);
145-
}
146-
147148
static setContext(key: Contexts | string, value: any) {
148149
return commands.executeCommand(VSBuiltInCommands.SetContext, key, value);
149150
}

src/gcodeUnits.ts

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

77
import { ConfigurationChangeEvent, Disposable, TextDocumentChangeEvent, TextEditor, window, workspace } from 'vscode';
88
import { Control } from './control';
9-
import { Commands } from './util/commands';
9+
import { UtilCommands } from './util/commands/common';
1010
import { configuration } from './util/config';
1111
import { Logger } from './util/logger';
1212
import { StatusBar, StatusBarControl } from './util/statusBar';
1313

1414
export const enum GCodeUnits {
15-
INCH = 'Inch',
15+
Inch = 'Inch',
1616
MM = 'Metric',
17-
AUTO = 'Auto',
18-
DEF = 'Defautlt (Inch)',
17+
Auto = 'Auto',
18+
Default = 'Defautlt (Inch)',
1919
}
2020

2121
export class GCodeUnitsController implements Disposable {
@@ -29,7 +29,7 @@ export class GCodeUnitsController implements Disposable {
2929
constructor() {
3030
this._statusbar = Control.statusBarController;
3131

32-
this._auto = (this._units = <GCodeUnits>configuration.getParam('general.units')) === GCodeUnits.AUTO;
32+
this._auto = (this._units = <GCodeUnits>configuration.getParam('general.units')) === GCodeUnits.Auto;
3333

3434
this._statusbar.updateStatusBar(this._units, this.unitsStatusBar);
3535

@@ -52,7 +52,7 @@ export class GCodeUnitsController implements Disposable {
5252
this.unitsStatusBar,
5353
undefined,
5454
undefined,
55-
Commands.GCSHOWSETTIGNS,
55+
UtilCommands.ShowGCodeSettings,
5656
);
5757
} else {
5858
Logger.log(`Units: ${this._units}`);
@@ -62,7 +62,7 @@ export class GCodeUnitsController implements Disposable {
6262
this.unitsStatusBar,
6363
undefined,
6464
undefined,
65-
Commands.GCSHOWSETTIGNS,
65+
UtilCommands.ShowGCodeSettings,
6666
);
6767
}
6868
}
@@ -82,7 +82,7 @@ export class GCodeUnitsController implements Disposable {
8282
this.unitsStatusBar,
8383
undefined,
8484
undefined,
85-
Commands.GCSHOWSETTIGNS,
85+
UtilCommands.ShowGCodeSettings,
8686
);
8787
} else {
8888
return;
@@ -103,7 +103,7 @@ export class GCodeUnitsController implements Disposable {
103103
this.unitsStatusBar,
104104
undefined,
105105
undefined,
106-
Commands.GCSHOWSETTIGNS,
106+
UtilCommands.ShowGCodeSettings,
107107
);
108108
} else {
109109
return;
@@ -118,11 +118,11 @@ export class GCodeUnitsController implements Disposable {
118118

119119
// Check for Inch
120120
if (units === null) {
121-
return GCodeUnits.DEF;
121+
return GCodeUnits.Default;
122122
} else if (units[0] === 'G21') {
123123
return GCodeUnits.MM;
124124
} else {
125-
return GCodeUnits.INCH;
125+
return GCodeUnits.Inch;
126126
}
127127
}
128128
}

src/util/commands.ts

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,10 @@
66

77
'use strict';
88

9-
import { commands, Disposable, ExtensionContext } from 'vscode';
10-
import { constants } from './constants';
11-
import { Messages } from './messages';
12-
13-
export const enum Commands {
14-
GCSHOWSETTIGNS = 'gcode.showSettings',
15-
GCSTATSENABLE = 'gcode.views.stats.enable',
16-
GCSTATSREFRESH = 'gcode.views.stats.refresh',
17-
GCSUPPORT = 'gcode.supportGCode',
18-
GCTREEREFRESH = 'gcode.views.navTree.refresh',
19-
GCTREESELECT = 'gcode.gcodeTree.selection',
20-
}
21-
22-
export abstract class GCommand implements Disposable {
23-
private _disposable: Disposable;
24-
25-
constructor(cmd: Commands) {
26-
this._disposable = commands.registerCommand(cmd, (...args: any[]) => this._execute(cmd, ...args), this);
27-
28-
return;
29-
}
30-
31-
dispose() {
32-
this._disposable && this._disposable.dispose();
33-
}
34-
35-
abstract execute(...args: any[]): any;
36-
37-
protected _execute(cmd: string, ...args: any[]): void {
38-
this.execute(...args);
39-
}
40-
}
41-
42-
class SupportGCodeCmd extends GCommand {
43-
constructor() {
44-
super(Commands.GCSUPPORT);
45-
}
46-
47-
execute() {
48-
return Messages.showSupportGCodeMessage();
49-
}
50-
}
51-
52-
class ShowGCodeSettings extends GCommand {
53-
constructor() {
54-
super(Commands.GCSHOWSETTIGNS);
55-
}
56-
57-
execute() {
58-
void commands.executeCommand('workbench.action.openSettings', `@ext:${constants.extensionQualifiedId}`);
59-
}
60-
}
9+
import { ExtensionContext } from 'vscode';
10+
import { ShowGCodeSettings } from './commands/showGCodeSettings';
11+
import { ShowSupportGCode } from './commands/showSupportGCode';
6112

6213
export function registerCommands(context: ExtensionContext): void {
63-
context.subscriptions.push(new SupportGCodeCmd(), new ShowGCodeSettings());
14+
context.subscriptions.push(new ShowGCodeSettings(), new ShowSupportGCode());
6415
}

src/util/commands/common.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 { commands, Disposable } from 'vscode';
9+
10+
export const enum UtilCommands {
11+
ShowGCodeSettings = 'gcode.showSettings',
12+
ShowSupportGCode = 'gcode.supportGCode',
13+
}
14+
15+
export abstract class GCommand implements Disposable {
16+
private _disposable: Disposable;
17+
18+
constructor(cmd: UtilCommands) {
19+
this._disposable = commands.registerCommand(cmd, (...args: any[]) => this._execute(cmd, ...args), this);
20+
21+
return;
22+
}
23+
24+
dispose() {
25+
this._disposable && this._disposable.dispose();
26+
}
27+
28+
abstract execute(...args: any[]): any;
29+
30+
protected _execute(cmd: string, ...args: any[]): void {
31+
this.execute(...args);
32+
}
33+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 { commands } from 'vscode';
9+
import { GCommand, UtilCommands } from './common';
10+
import { constants, VSBuiltInCommands } from '../constants';
11+
12+
export class ShowGCodeSettings extends GCommand {
13+
constructor() {
14+
super(UtilCommands.ShowGCodeSettings);
15+
}
16+
17+
execute() {
18+
void commands.executeCommand(VSBuiltInCommands.OpenSettings, `@ext:${constants.extensionQualifiedId}`);
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 { GCommand, UtilCommands } from './common';
9+
import { Messages } from '../messages';
10+
11+
export class ShowSupportGCode extends GCommand {
12+
constructor() {
13+
super(UtilCommands.ShowSupportGCode);
14+
}
15+
16+
execute() {
17+
return Messages.showSupportGCodeMessage();
18+
}
19+
}

src/util/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,6 @@ export const enum VSBuiltInCommands {
6969
}
7070

7171
export const enum Contexts {
72-
NavTreeViewEnabled = 'gcode:navTree:enabled',
72+
ViewsNavTreeEnabled = 'gcode:navTree:enabled',
73+
ViewsStatsEnabled = 'gcode:stats:enabled',
7374
}

src/util/statusBar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
ThemeColor,
1414
window,
1515
} from 'vscode';
16-
import { Commands } from './commands';
16+
import { UtilCommands } from './commands/common';
1717
import { configuration } from './config';
1818
import { Logger } from './logger';
1919

@@ -106,7 +106,7 @@ export class StatusBarControl implements Disposable {
106106
bar: StatusBar,
107107
tooltip?: string,
108108
color?: string | ThemeColor,
109-
cmd?: Commands | string,
109+
cmd?: UtilCommands | string,
110110
): boolean {
111111
if (!this._enabled) {
112112
return false;

0 commit comments

Comments
 (0)