Skip to content

Commit 4b9e14b

Browse files
committed
Finished initial implementation of Stats
Signed-off-by: Mike Centola <mcentola@appliedengdesign.com>
1 parent e097810 commit 4b9e14b

3 files changed

Lines changed: 86 additions & 39 deletions

File tree

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
},
142142
"gcode.treeAutoRefresh": {
143143
"type": "boolean",
144-
"default": false,
144+
"default": true,
145145
"description": "Enable G-Code Tree Auto Refresh",
146146
"scope": "window"
147147
},
@@ -150,6 +150,12 @@
150150
"default": false,
151151
"description": "Enable G-Code Statistics Panel",
152152
"scope": "window"
153+
},
154+
"gcode.statsAutoRefresh": {
155+
"type": "boolean",
156+
"default": false,
157+
"description": "Enable G-Code Stats Auto Refresh",
158+
"scope": "window"
153159
}
154160
}
155161
},
@@ -173,6 +179,7 @@
173179
{
174180
"id": "gcode.gcodeStats",
175181
"name": "G-Code Stats",
182+
"when": "config.gcode.statsEnable",
176183
"contextualTitle": "G-Code",
177184
"type": "tree"
178185
}
@@ -182,12 +189,12 @@
182189
{
183190
"view": "gcode.gcodeTree",
184191
"contents": "Tree only available when viewing G-Code",
185-
"when": "!gcodeViewEnabled"
192+
"when": "!gcodeTreeViewEnabled"
186193
},
187194
{
188195
"view": "gcode.gcodeStats",
189-
"contents": "G-Code Stats are disabled\n[Enable Stats](command:gcode.gcodeStats.enable)\n",
190-
"when": "!config.gcode.statsEnable"
196+
"contents": "G-Code Stats only available when viewing G-Code",
197+
"when": "!gcodeStatsViewEnabled && config.gcode.statsEnable"
191198
}
192199
],
193200
"menus": {

src/extension.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function activate(context: vscode.ExtensionContext) {
3737

3838
vscode.commands.registerCommand('gcode.gcodeTree.refresh', () => {
3939
if (vscode.window.activeTextEditor?.document.languageId === constants.langId) {
40-
vscode.commands.executeCommand('setContext', 'gcodeViewEnabled', true);
40+
vscode.commands.executeCommand('setContext', 'gcodeTreeViewEnabled', true);
4141
}
4242
gcodeTree.refresh();
4343
});
@@ -52,7 +52,10 @@ export async function activate(context: vscode.ExtensionContext) {
5252
vscode.window.registerTreeDataProvider('gcode.gcodeStats', gcodeStats);
5353

5454
vscode.commands.registerCommand('gcode.gcodeStats.refresh', () => {
55-
//gcodeStats.refresh();
55+
if (vscode.window.activeTextEditor?.document.languageId === constants.langId) {
56+
vscode.commands.executeCommand('setContext', 'gcodeStatsViewEnabled', true);
57+
}
58+
gcodeStats.refresh();
5659
});
5760
vscode.commands.registerCommand('gcode.gcodeStats.enable', () => {
5861
Logger.log('Enabling Stats...');
@@ -136,4 +139,5 @@ export function deactivate() {
136139
// Clean up
137140
Logger.close();
138141
StatusBar.dispose();
142+
139143
}

src/providers/gcodeStats.ts

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,40 @@
33
* Licensed under the MIT License. See License.md in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
6-
import * as vscode from 'vscode';
7-
8-
9-
export class GCodeStatsProvider implements vscode.TreeDataProvider<GCodeStatsNode> {
10-
11-
private _onDidChangeTreeData: vscode.EventEmitter<GCodeStatsNode | undefined> = new vscode.EventEmitter<GCodeStatsNode | undefined>();
12-
readonly onDidChangeTreeData: vscode.Event<GCodeStatsNode | undefined> = this._onDidChangeTreeData.event;
6+
import {
7+
commands,
8+
Event,
9+
EventEmitter,
10+
ExtensionContext,
11+
TextDocumentChangeEvent,
12+
TextEditor,
13+
TreeDataProvider,
14+
TreeItem,
15+
TreeItemCollapsibleState,
16+
window,
17+
workspace
18+
} from 'vscode';
19+
import { configuration } from '../util/config';
20+
21+
22+
export class GCodeStatsProvider implements TreeDataProvider<GCodeStatsNode> {
23+
24+
private _onDidChangeTreeData: EventEmitter<GCodeStatsNode | undefined> = new EventEmitter<GCodeStatsNode | undefined>();
25+
readonly onDidChangeTreeData: Event<GCodeStatsNode | undefined> = this._onDidChangeTreeData.event;
1326

1427
private text = '';
1528
private tree: Array<GCodeStatsNode>;
16-
private editor: vscode.TextEditor | undefined;
29+
private editor: TextEditor | undefined;
1730
private autoRefresh = false;
1831

19-
constructor(private context: vscode.ExtensionContext) {
32+
constructor(private context: ExtensionContext) {
2033

2134
this.tree = [];
22-
this.editor = vscode.window.activeTextEditor;
23-
vscode.window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());
24-
vscode.workspace.onDidChangeTextDocument(e => this.onDocumentChanged(e));
35+
this.editor = window.activeTextEditor;
36+
window.onDidChangeActiveTextEditor(() => this.onActiveEditorChanged());
37+
workspace.onDidChangeTextDocument(e => this.onDocumentChanged(e));
38+
39+
this.autoRefresh = configuration.getParam('statsAutoRefresh');
2540

2641
}
2742

@@ -33,39 +48,43 @@ export class GCodeStatsProvider implements vscode.TreeDataProvider<GCodeStatsNod
3348
}
3449

3550
private onActiveEditorChanged(): void {
36-
if (vscode.window.activeTextEditor) {
37-
if (vscode.window.activeTextEditor.document.uri.scheme === 'file') {
38-
const enabled = vscode.window.activeTextEditor.document.languageId === 'gcode';
39-
vscode.commands.executeCommand('setContext', 'gcodeViewEnabled', enabled);
51+
if (window.activeTextEditor) {
52+
if (window.activeTextEditor.document.uri.scheme === 'file') {
53+
const enabled = window.activeTextEditor.document.languageId === 'gcode';
54+
commands.executeCommand('setContext', 'gcodeStatsViewEnabled', enabled);
4055

4156
if (enabled) {
42-
this.editor = vscode.window.activeTextEditor;
43-
this.refresh();
57+
this.editor = window.activeTextEditor;
58+
this.autoRefresh = configuration.getParam('statsAutoRefresh');
59+
if (this.autoRefresh) this.refresh();
4460
}
4561
}
4662
} else {
47-
vscode.commands.executeCommand('setContext', 'gcodeViewEnabled', false);
63+
commands.executeCommand('setContext', 'gcodeStatsViewEnabled', false);
64+
this.refresh();
4865
}
4966
}
5067

51-
private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void {
52-
if (vscode.window.activeTextEditor) {
53-
if (vscode.window.activeTextEditor.document.uri.scheme === 'file') {
54-
const enabled = vscode.window.activeTextEditor.document.languageId === 'gcode';
55-
vscode.commands.executeCommand('setContext', 'gcodeViewEnabled', enabled);
68+
private onDocumentChanged(changeEvent: TextDocumentChangeEvent): void {
69+
if (window.activeTextEditor) {
70+
if (window.activeTextEditor.document.uri.scheme === 'file') {
71+
const enabled = window.activeTextEditor.document.languageId === 'gcode';
72+
commands.executeCommand('setContext', 'gcodeStatsViewEnabled', enabled);
5673

5774
if (enabled) {
58-
this.editor = vscode.window.activeTextEditor;
59-
this.refresh();
75+
this.editor = window.activeTextEditor;
76+
this.autoRefresh = configuration.getParam('statsAutoRefresh');
77+
if(this.autoRefresh) this.refresh();
6078
}
6179
}
6280
} else {
63-
vscode.commands.executeCommand('setContext', 'gcodeViewEnabled', false);
81+
commands.executeCommand('setContext', 'gcodeStatsViewEnabled', false);
82+
this.refresh();
6483
}
6584
}
6685

67-
getTreeItem(element: any): vscode.TreeItem {
68-
return element[0];
86+
getTreeItem(element: any): TreeItem {
87+
return element;
6988
}
7089

7190
getChildren(element?: GCodeStatsNode): Thenable<GCodeStatsNode[]> {
@@ -79,7 +98,7 @@ export class GCodeStatsProvider implements vscode.TreeDataProvider<GCodeStatsNod
7998

8099
this.text = '';
81100
this.tree = [];
82-
const editor = vscode.window.activeTextEditor;
101+
const editor = window.activeTextEditor;
83102

84103
if (editor && editor.document) {
85104
this.text = editor.document.getText();
@@ -95,17 +114,34 @@ export class GCodeStatsProvider implements vscode.TreeDataProvider<GCodeStatsNod
95114

96115
private genStats(text: string): Array<GCodeStatsNode> {
97116

117+
const stats: Array<GCodeStatsNode> = [];
118+
119+
// Tool Changes
120+
stats.push(this.getToolChanges(text));
121+
122+
123+
124+
return stats;
125+
}
126+
127+
private getToolChanges(text: string):GCodeStatsNode {
128+
const re = /(M0?6)/igm;
129+
const num = text.match(re)?.length || 0;
98130

131+
const node: GCodeStatsNode = new GCodeStatsNode(
132+
'Tool Changes: ' + num,
133+
TreeItemCollapsibleState.None,
134+
);
99135

100-
return [];
136+
return node;
101137
}
102138
}
103139

104-
export class GCodeStatsNode extends vscode.TreeItem {
140+
export class GCodeStatsNode extends TreeItem {
105141

106142
constructor(
107143
public readonly label: string,
108-
public readonly collapsibleState: vscode.TreeItemCollapsibleState,
144+
public readonly collapsibleState: TreeItemCollapsibleState,
109145
) {
110146
super (label, collapsibleState);
111147
}

0 commit comments

Comments
 (0)