Skip to content

Commit be4d5c8

Browse files
committed
Working hover
1 parent 709c6c4 commit be4d5c8

6 files changed

Lines changed: 125 additions & 18 deletions

File tree

src/control.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ import { Version } from './util/version';
1818
import { Messages } from './util/messages';
1919
import { StateControl } from './util/stateControl';
2020
import { CodesWebview } from './webviews/codesWebview';
21+
import { MachineTypeControl } from './util/machineType';
22+
import { GCodeHoverControl } from './hovers/gcodeHoverControl';
2123

2224
export class Control {
2325
private static _config: Config | undefined;
2426
private static _context: ExtensionContext;
2527
private static _units: string | undefined;
2628

2729
// Controllers
30+
private static _machineTypeControl: MachineTypeControl | undefined;
2831
private static _statusBarControl: StatusBarControl;
2932
private static _unitsController: GCodeUnitsController | undefined;
3033
private static _stateController: StateControl;
34+
private static _hoverController: GCodeHoverControl;
3135

3236
// Views
3337
private static _statsView: StatsView | undefined;
@@ -67,12 +71,18 @@ export class Control {
6771
this._context = context;
6872
this._config = config;
6973

74+
// Load Machine Type
75+
context.subscriptions.push((this._machineTypeControl = new MachineTypeControl()));
76+
7077
// Load State Controller
7178
this._stateController = new StateControl(context);
7279

7380
// Load StatusBars
7481
context.subscriptions.push((this._statusBarControl = new StatusBarControl(this._context)));
7582

83+
// Load Hover Controller
84+
context.subscriptions.push((this._hoverController = new GCodeHoverControl()));
85+
7686
// Units
7787
this._units = <string>config.getParam('general.units');
7888
Logger.log(`Units: ${this._units}`);
@@ -143,7 +153,15 @@ export class Control {
143153
static terminate() {
144154
Logger.log('Terminating Extension...');
145155

146-
// Dispose of status bars
156+
// Dispose Views
157+
this._codesWebview?.dispose();
158+
this._statsView?.dispose();
159+
this._statsView?.dispose();
160+
161+
// Dispose Controllers
162+
this._hoverController.dispose();
163+
this._unitsController?.dispose();
164+
this._machineTypeControl?.dispose();
147165
this._statusBarControl?.dispose();
148166
}
149167

@@ -164,6 +182,10 @@ export class Control {
164182
return this._config;
165183
}
166184

185+
static get machineType() {
186+
return this._machineTypeControl;
187+
}
188+
167189
static get navTree() {
168190
if (this._navTree === undefined) {
169191
this._context.subscriptions.push((this._navTree = new NavTreeView()));

src/hovers/dictionary.ts

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

src/hovers/gcodeHoverControl.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { ConfigurationChangeEvent, Disposable, languages, TextEditor, Uri, window } from 'vscode';
88
import { configuration } from '../util/config';
99
import { constants } from '../util/constants';
10+
import { Logger } from '../util/logger';
1011
import { GCodeHoverProvider } from './gcodeHoverProvider';
1112

1213
export class GCodeHoverControl implements Disposable {
@@ -19,6 +20,11 @@ export class GCodeHoverControl implements Disposable {
1920
this._disposable = Disposable.from(configuration.onDidChange(this.onConfigurationChanged, this));
2021

2122
this._enabled = <boolean>configuration.getParam('general.hovers.enabled');
23+
24+
if (this._enabled) {
25+
Logger.log('Loading Hover Controller...');
26+
this.register(window.activeTextEditor);
27+
}
2228
}
2329

2430
dispose() {
@@ -29,9 +35,11 @@ export class GCodeHoverControl implements Disposable {
2935
if (configuration.changed(e, 'general.hovers.enabled')) {
3036
if (this._enabled) {
3137
// Disable and Dispose
38+
Logger.log('Disabling Hover Controller...');
3239
this.unregister();
3340
} else {
3441
// Enable
42+
Logger.log('Enabling Hover Controller...');
3543
this.register(window.activeTextEditor);
3644
}
3745
}

src/hovers/gcodeHoverProvider.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,39 @@
44
* -------------------------------------------------------------------------------------------- */
55
'use strict';
66

7-
import { CancellationToken, Hover, HoverProvider, MarkdownString, Position, TextDocument } from 'vscode';
7+
import {
8+
CancellationToken,
9+
Hover,
10+
HoverProvider,
11+
MarkdownString,
12+
Position,
13+
ProviderResult,
14+
TextDocument,
15+
} from 'vscode';
16+
import { Control } from '../control';
817

918
export class GCodeHoverProvider implements HoverProvider {
10-
async provideHover(document: TextDocument, position: Position, _token: CancellationToken): Promise<Hover> {
19+
provideHover(document: TextDocument, position: Position, _token: CancellationToken): ProviderResult<Hover> {
1120
const range = document.getWordRangeAtPosition(position);
1221
const text = document.getText(range);
1322

14-
return new Hover();
23+
const def = this.lookup(text);
24+
if (def === undefined) {
25+
return;
26+
} else {
27+
return new Hover(def, range);
28+
}
1529
}
1630

17-
private async gcodeLookup(text: string): MarkdownString {}
31+
private lookup(text: string): MarkdownString | undefined {
32+
const ref = Control.machineType?.gReference;
33+
34+
const code = ref?.get(text);
35+
36+
if (code === undefined) {
37+
return;
38+
}
39+
40+
return new MarkdownString(`**${text}**: `).appendMarkdown(code.shortDesc ?? '');
41+
}
1842
}

src/util/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const enum VSBuiltInCommands {
7171
}
7272

7373
export const enum Contexts {
74+
MachineType = 'gcode:general:machineType',
7475
ViewsNavTreeEnabled = 'gcode:navTree:enabled',
7576
ViewsStatsEnabled = 'gcode:stats:enabled',
7677
}

src/util/machineType.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
'use strict';
6+
7+
import { ConfigurationChangeEvent, Disposable, Event, EventEmitter, workspace } from 'vscode';
8+
import { GReference, MachineTypes } from '@appliedengdesign/gcode-reference';
9+
import { configuration } from './config';
10+
import { Logger } from './logger';
11+
12+
export class MachineTypeControl implements Disposable {
13+
private readonly _dispoable: Disposable | undefined;
14+
private _machineType: MachineTypes | undefined;
15+
private _gReference: GReference;
16+
17+
private _onDidChange = new EventEmitter<ConfigurationChangeEvent>();
18+
get onDidChange(): Event<ConfigurationChangeEvent> {
19+
return this._onDidChange.event;
20+
}
21+
22+
constructor() {
23+
this.update();
24+
this._gReference = new GReference(this._machineType);
25+
26+
this._dispoable = Disposable.from(workspace.onDidChangeConfiguration(this.onConfigurationChanged, this));
27+
}
28+
29+
dispose() {
30+
this._dispoable && this._dispoable.dispose();
31+
}
32+
33+
private onConfigurationChanged(e: ConfigurationChangeEvent) {
34+
if (configuration.changed(e, 'general.machineType')) {
35+
this.update();
36+
} else {
37+
return;
38+
}
39+
}
40+
41+
private update() {
42+
const cfgMachineType = <string>configuration.getParam('general.machineType');
43+
Logger.log(`Machine Type: ${cfgMachineType}`);
44+
switch (cfgMachineType) {
45+
case 'Mill':
46+
this._machineType = MachineTypes.Mill;
47+
break;
48+
49+
case 'Lathe':
50+
this._machineType = MachineTypes.Lathe;
51+
break;
52+
53+
case '3D Printer':
54+
this._machineType = MachineTypes.Printer;
55+
break;
56+
57+
default:
58+
return;
59+
}
60+
}
61+
62+
get gReference() {
63+
return this._gReference;
64+
}
65+
}

0 commit comments

Comments
 (0)