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 = / ( M 0 ? 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