Skip to content

Commit 991ed0d

Browse files
committed
Added clickable Tree Items
Signed-off-by: Mike Centola <[email protected]>
1 parent 50250bb commit 991ed0d

File tree

6 files changed

+85
-68
lines changed

6 files changed

+85
-68
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,18 @@
107107
"type": "object",
108108
"title": "G-Code",
109109
"properties": {
110-
"gcode.colorization.enabled": {
110+
"gcode.colorization": {
111111
"type": "boolean",
112112
"default": false,
113113
"description": "Enable G-Code specific colorization options",
114114
"scope": "window"
115115
},
116116
"gcode.machineType": {
117117
"type": "string",
118-
"default": "mill",
118+
"default": "Mill",
119119
"enum": [
120-
"mill",
121-
"lathe"
120+
"Mill",
121+
"Lathe"
122122
],
123123
"enumDescriptions": [
124124
"Loads specific snippets for CNC Mill G-Code",

src/extension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import * as vscode from 'vscode';
22
import { config } from './config';
3-
import * as util from './util';
4-
import * as manifest from './manifest.json';
3+
import * as consts from './util/constants';
54

65
import { GCodeTreeProvider } from './providers/gcodeTree';
76
//import { getColorization } from './colorization';
87

9-
const name = manifest.name;
10-
const version = manifest.version;
8+
const name = consts.name;
9+
const version = consts.version;
1110

1211
// Create output channel
1312
const conout = vscode.window.createOutputChannel("G-Code");
@@ -22,6 +21,7 @@ export function activate(context: vscode.ExtensionContext) {
2221
vscode.window.registerTreeDataProvider('gcodeTree', gcodeTree);
2322

2423
vscode.commands.registerCommand('gcodeTree.refreshEntry', () => gcodeTree.refresh());
24+
vscode.commands.registerCommand('extension.gcodeSelection', range => gcodeTree.select(range));
2525

2626
conout.appendLine("G-Code Tree View Enabled");
2727
conout.appendLine('Tree AutoRefresh: ' + (config.getParam('treeAutoRefresh') ? 'Enabled' : 'Disabled') );

src/providers/gcodeParser.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class GCodeParser {
2929
continue;
3030
}
3131

32-
const result = this.parseLine(line);
32+
const result = this.parseLine(line, i);
3333
if (result.length !== 0) {
3434
nodes.push(result);
3535
}
@@ -40,10 +40,11 @@ export class GCodeParser {
4040
}
4141

4242
// Parse Line for Blocks
43-
private parseLine(line: string): Array<GCodeTreeNode> {
43+
private parseLine(line: string, lnum:number): Array<GCodeTreeNode> {
4444

4545
let blocks: Array<GCodeTreeNode> = [];
4646
let node: GCodeTreeNode;
47+
let len = line.length;
4748

4849
// Regexp to Pull key words
4950
const re = /((GOTO)|(IF)|(EQ)|(NE)|(LT)|(GT)|(LE)|(GE)|(DO)|(WHILE)|(END)|(AND)|(OR)|(XOR)|(SIN)|(COS)|(TAN)|(ASIN)|(ACOS)|(ATAN)|(FIX)|(FUP)|(LN)|(ROUND)|(SQRT)|(FIX)|(FUP)|(ROUND)|(ABS))|((?:\$\$)|(?:\$[a-zA-Z0-9#]*))|([a-zA-Z][0-9\+\-\.]+)|(\*[0-9]+)|([#][0-9]+)|([#][\[].+[\]])/igm;
@@ -73,6 +74,11 @@ export class GCodeParser {
7374
);
7475
node.tooltip = '[G00] Rapid Motion';
7576
node.setIcon('rapid');
77+
node.command = {
78+
command: 'extension.gcodeSelection',
79+
title: "",
80+
arguments: [new vscode.Range(lnum, 0, lnum, len)]
81+
};
7682

7783
blocks.push(node);
7884
break;
@@ -86,6 +92,11 @@ export class GCodeParser {
8692
);
8793
node.tooltip = '[G01] Linear]';
8894
node.setIcon('cutting');
95+
node.command = {
96+
command: 'extension.gcodeSelection',
97+
title: "",
98+
arguments: [new vscode.Range(lnum, 0, lnum, len)]
99+
};
89100

90101
blocks.push(node);
91102
break;
@@ -99,6 +110,11 @@ export class GCodeParser {
99110
);
100111
node.tooltip = '[G02] CW Interpolation';
101112
node.setIcon('cwcutting');
113+
node.command = {
114+
command: 'extension.gcodeSelection',
115+
title: "",
116+
arguments: [new vscode.Range(lnum, 0, lnum, len)]
117+
};
102118

103119
blocks.push(node);
104120
break;
@@ -112,6 +128,11 @@ export class GCodeParser {
112128
);
113129
node.tooltip = '[G03] CCW Interpolation';
114130
node.setIcon('ccwcutting');
131+
node.command = {
132+
command: 'extension.gcodeSelection',
133+
title: "",
134+
arguments: [new vscode.Range(lnum, 0, lnum, len)]
135+
};
115136

116137
blocks.push(node);
117138
break;
@@ -134,7 +155,12 @@ export class GCodeParser {
134155
);
135156
node.tooltip = 'Tool Change';
136157
node.setIcon('toolchange');
137-
158+
node.command = {
159+
command: 'extension.gcodeSelection',
160+
title: "",
161+
arguments: [new vscode.Range(lnum, 0, lnum, len)]
162+
};
163+
138164
blocks.push(node);
139165
break;
140166

@@ -148,6 +174,11 @@ export class GCodeParser {
148174
);
149175
node.tooltip = 'Coolant Turned On';
150176
node.setIcon('coolanton');
177+
node.command = {
178+
command: 'extension.gcodeSelection',
179+
title: "",
180+
arguments: [new vscode.Range(lnum, 0, lnum, len)]
181+
};
151182

152183
blocks.push(node);
153184
break;
@@ -161,6 +192,11 @@ export class GCodeParser {
161192
);
162193
node.tooltip = 'Coolant Turned Off';
163194
node.setIcon('coolantoff');
195+
node.command = {
196+
command: 'extension.gcodeSelection',
197+
title: "",
198+
arguments: [new vscode.Range(lnum, 0, lnum, len)]
199+
};
164200

165201
blocks.push(node);
166202

src/providers/gcodeTree.ts

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
22
import * as path from 'path';
33
import { config } from '../config';
44
import * as gcodeparser from './gcodeParser';
5+
import { iconsPath } from '../util/constants';
56

67
export class GCodeTreeProvider implements vscode.TreeDataProvider<GCodeTreeNode> {
78

@@ -41,6 +42,7 @@ export class GCodeTreeProvider implements vscode.TreeDataProvider<GCodeTreeNode>
4142
vscode.commands.executeCommand('setContext', 'gcodeTreeEnabled', enabled);
4243

4344
if (enabled) {
45+
this.editor = vscode.window.activeTextEditor;
4446
this.refresh();
4547
}
4648
}
@@ -50,12 +52,21 @@ export class GCodeTreeProvider implements vscode.TreeDataProvider<GCodeTreeNode>
5052
}
5153

5254
private onDocumentChanged(changeEvent: vscode.TextDocumentChangeEvent): void {
53-
if (this.editor && this.autoRefresh) {
54-
this.refresh();
55+
if (vscode.window.activeTextEditor) {
56+
if (vscode.window.activeTextEditor.document.uri.scheme === 'file') {
57+
const enabled = vscode.window.activeTextEditor.document.languageId === 'gcode';
58+
vscode.commands.executeCommand('setContext', 'gcodeTreeEnabled', enabled);
59+
60+
if (enabled) {
61+
this.editor = vscode.window.activeTextEditor;
62+
this.refresh();
63+
}
64+
}
65+
} else {
66+
vscode.commands.executeCommand('setContext', 'gcodeTreeEnabled', false);
5567
}
5668
}
5769

58-
5970
getTreeItem(element: any): vscode.TreeItem {
6071
return element[0];
6172
}
@@ -67,7 +78,6 @@ export class GCodeTreeProvider implements vscode.TreeDataProvider<GCodeTreeNode>
6778

6879
}
6980

70-
7181
private parseTree(): GCodeTreeNode[] {
7282

7383
this.text = '';
@@ -86,6 +96,13 @@ export class GCodeTreeProvider implements vscode.TreeDataProvider<GCodeTreeNode>
8696

8797
return [];
8898
}
99+
100+
select(range: vscode.Range) {
101+
if (this.editor) {
102+
this.editor.selection = new vscode.Selection(range.start, range.end);
103+
this.editor.revealRange(range, vscode.TextEditorRevealType.InCenter);
104+
}
105+
}
89106

90107
}
91108

@@ -103,58 +120,22 @@ export class GCodeTreeNode extends vscode.TreeItem {
103120

104121
switch (type) {
105122
case "toolchange":
106-
this.iconPath = {
107-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'toolchange.svg'),
108-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'toolchange.svg')
109-
};
110-
break;
111-
112-
case "rapid":
113-
this.iconPath = {
114-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'rapid.svg'),
115-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'rapid.svg')
116-
};
117-
break;
118-
123+
case "rapid":
119124
case "cutting":
120-
this.iconPath = {
121-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'cutting.svg'),
122-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'cutting.svg')
123-
};
124-
break;
125-
126125
case "cwcutting":
127-
this.iconPath = {
128-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'cwcutting.svg'),
129-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'cwcutting.svg')
130-
};
131-
break;
132-
133126
case "ccwcutting":
134-
this.iconPath = {
135-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'ccwcutting.svg'),
136-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'ccwcutting.svg')
137-
};
138-
break;
139-
140127
case "coolanton":
141-
this.iconPath = {
142-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'coolanton.svg'),
143-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'coolanton.svg')
144-
};
145-
break;
146-
147128
case "coolantoff":
148129
this.iconPath = {
149-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'coolantoff.svg'),
150-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'coolantoff.svg')
130+
light: path.join(iconsPath, 'light', type+'.svg'),
131+
dark: path.join(iconsPath, 'dark', type+'.svg')
151132
};
152133
break;
153134

154135
default:
155136
this.iconPath = {
156-
light: path.join(__dirname, '..','..', 'resources', 'icons', 'light', 'gcode.svg'),
157-
dark: path.join(__dirname, '..', '..', 'resources', 'icons', 'dark', 'gcode.svg')
137+
light: path.join(iconsPath, 'light', 'gcode.svg'),
138+
dark: path.join(iconsPath, 'dark', 'gcode.svg')
158139
};
159140
}
160141

src/util.ts

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

src/util/constants.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
import * as path from 'path';
7+
import * as manifest from '../manifest.json';
8+
9+
export const name = manifest.name;
10+
export const version = manifest.version;
11+
12+
export const iconsPath = path.join(__dirname, "..", "..", "resources", "icons");

0 commit comments

Comments
 (0)