Skip to content

Commit

Permalink
Merge pull request #48 from appliedengdesign/bugfix/linenumberer
Browse files Browse the repository at this point in the history
Line Numberer Fix
  • Loading branch information
mikecentola authored Sep 23, 2022
2 parents dfe8a05 + 05af17b commit 91d9a26
Show file tree
Hide file tree
Showing 17 changed files with 320 additions and 291 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ G-Code Syntax is customizable and provides many configuration settings to allow
| `gcode.general.units` | Choose the units for the file. Options are Auto, Inch or Metric |
| `gcode.general.outputLevel` | Configure Output level for debugging purposes |
| `gcode.lineNumberer.addSpaceAfter` | Add space after line number |
| `gcode.lineNumberer.defaultIncrement` | Default Line Numberer Increment |
| `gcode.lineNumberer.defaultStart` | Default Line Numberer Start |
| `gcode.lineNumberer.enableQuickPick` | Enable or Disable Input for the Line Numberer (Will use above defaults) |
| `gcode.lineNumberer.frequency` | Frequency of line number additions (Every Line or at Tool Changes) |
| `gcode.lineNumberer.ignoreBlank` | Ignore Blank lines when numbering |
| `gcode.lineNumberer.ignoreComments` | Ignore Comments when numbering |
| `gcode.lineNumberer.ignoreExtra` | Additional characters to ignore (Beginning of Line, Default is %) |
| `gcode.lineNumberer.ignoreProgramNumbers` | Ignore Program numbers, e.g. `O12345` |
| `gcode.lineNumberer.matchLineNumber` | When numbering, match the N number to file's line number. (Default is off) |
| `gcode.views.maxAutoRefresh` | Value for limiting the autoRefresh maximum lines |
| `gcode.views.navTree.autoRefresh` | Tree auto-refreshes as changes are made to the g-code. ( Disabled by default ) |
| `gcode.views.stats.autoRefresh` | Auto-refresh the stats view when changes are made to the g-code. (Disabled by default) |
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@
"markdownDescription": "Add Space After Line Number",
"scope": "window"
},
"gcode.lineNumberer.defaultIncrement": {
"type": "number",
"default": 10,
"markdownDescription": "Default Line Numberer Increment",
"scope": "window"
},
"gcode.lineNumberer.defaultStart": {
"type": "number",
"default": 10,
"markdownDescription": "Default Line Numberer Start",
"scope": "window"
},
"gcode.lineNumbere.enableQuickPick": {
"type": "boolean",
"default": true,
"markdownDescription": "Enable or Disable Input for Line Numberer (Will use above defaults).",
"scope": "window"
},
"gcode.lineNumberer.frequency": {
"type": "string",
"default": "Every Line",
Expand All @@ -264,12 +282,29 @@
"markdownDescription": "Ignore Comments when Numbering",
"scope": "window"
},
"gcode.lineNumberer.ignoreExtra": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"%"
],
"markdownDescription": "Additional characters to ignore (Beginning of Line)",
"scope": "window"
},
"gcode.lineNumberer.ignoreProgramNumbers": {
"type": "boolean",
"default": true,
"markdownDescription": "Ignore program numbers, e.g. `O12345`",
"scope": "window"
},
"gcode.lineNumberer.matchLineNumber": {
"type": "boolean",
"default": false,
"markdownDescription": "When numbering, match N number to file's line number.",
"scope": "window"
},
"gcode.views.maxAutoRefresh": {
"type": "integer",
"default": "10000",
Expand Down
42 changes: 26 additions & 16 deletions src/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { CodesWebview } from './webviews/codesWebview';
import { MachineTypeControl } from './util/machineType';
import { GCodeHoverControl } from './hovers/gcodeHoverControl';
import { defaults } from './util/configuration/defaults';
import { registerCommands } from './util/commands/functions';
import { registerCommands } from './util/commands';

const cfgUnits = 'general.units';
const cfgAutoRef = {
Expand Down Expand Up @@ -48,28 +48,26 @@ export class Control {
// Webviews
private static _codesWebview: CodesWebview | undefined;

private static async checkVersion() {
const gcodeVersion = new Version(constants.extension.version);

private static async _checkVersion() {
const prevVer = this._stateController.getVersion();

const newVer = gcodeVersion.compareWith(prevVer.getVersion()) === 1 ? true : false;
const newVer = this._version.compareWith(prevVer.getVersion()) === 1 ? true : false;

if (newVer) {
// Extension has been updated

// Update globalState version
Logger.log('Updating...');
void this._stateController.updateVer(gcodeVersion);
await this._stateController.updateVer(this._version);

Logger.log(`G-Code upgraded from ${prevVer.getVersionAsString()} to ${gcodeVersion.getVersionAsString()}`);
await this.showWhatsNew(gcodeVersion);
Logger.log(`G-Code upgraded from ${prevVer.getVersionAsString()} to ${this._version.getVersionAsString()}`);
await this._showWhatsNew(this._version);
} else {
return;
}
}

private static async showWhatsNew(ver: Version) {
private static async _showWhatsNew(ver: Version) {
// Show Whats New Message
await Messages.showWhatsNewMessage(ver);
}
Expand All @@ -79,23 +77,33 @@ export class Control {
// Initialize G-Code Extension
this._context = context;
this._config = config;
this._version = new Version(constants.extension.version);

// Initialze Configuration
Logger.log('Loading Configuration...');
Config.initialize(this._context, this._config);

// Load State Controller
Logger.log('Loading State Controller...');
this._stateController = new StateControl(context);

// Check Version
Logger.log('Checking Version...');
void this._checkVersion();

// Register Commands
Logger.log('Registering Commands...');
context.subscriptions.push(...registerCommands());

// Load StatusBars
context.subscriptions.push((this._statusBarControl = new StatusBarControl()));

// Load Machine Type
Logger.log('Loading Machine Type Controller...');
context.subscriptions.push((this._machineTypeControl = new MachineTypeControl()));

// Load State Controller
this._stateController = new StateControl(context);

// Load Hover Controller
Logger.log('Loading Hover Controller...');
context.subscriptions.push((this._hoverController = new GCodeHoverControl()));

// Units
Expand Down Expand Up @@ -125,7 +133,6 @@ export class Control {

// Load Nav Tree
Logger.log('Loading Nav Tree...');

context.subscriptions.push((this._navTree = new NavTreeView()));

Logger.log(
Expand All @@ -147,11 +154,10 @@ export class Control {
GCommands.ShowSupportGCode,
);

// Check Version
void this.checkVersion();

// Set Up Webviews
context.subscriptions.push((this._codesWebview = new CodesWebview()));

Logger.log('Done Initializing.');
}

static terminate() {
Expand Down Expand Up @@ -186,6 +192,10 @@ export class Control {
return this._config;
}

static get version() {
return this._version;
}

static get machineType() {
return this._machineTypeControl;
}
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function activate(context: ExtensionContext) {
Logger.initialize(context);
Logger.enable();

Logger.log('Initializing G-Code...');

// Initialize Controller
Control.initialize(context, configuration);

Expand Down
6 changes: 3 additions & 3 deletions src/hovers/gcodeHoverControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class GCodeHoverControl implements Disposable {
this._enabled = <boolean>configuration.getParam('general.hovers.enabled');

if (this._enabled) {
Logger.log('Loading Hover Controller...');
Logger.log('Hovers: Enabled');
this.register(window.activeTextEditor);
}
}
Expand All @@ -36,11 +36,11 @@ export class GCodeHoverControl implements Disposable {
if (configuration.changed(e, 'general.hovers.enabled')) {
if (this._enabled) {
// Disable and Dispose
Logger.log('Disabling Hover Controller...');
Logger.log('Hovers: Disabled');
this.unregister();
} else {
// Enable
Logger.log('Enabling Hover Controller...');
Logger.log('Hovers: Enabled');
this.register(window.activeTextEditor);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/util/commands/addComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

import { Range, window } from 'vscode';
import { constants, GCommands } from '../constants';
import { Messages } from '../messages';
import { GCommand } from './base';

export class AddComment extends GCommand {
constructor() {
super(GCommands.AddComment);
}

execute() {
async execute() {
const editor = window.activeTextEditor;
let replace = '';

Expand All @@ -34,9 +35,11 @@ export class AddComment extends GCommand {
replace += `(${lines[i]})${i + 1 === lines.length ? '' : '\n'}`;
}

void editor.edit(editBuilder => {
await editor.edit(editBuilder => {
editBuilder.replace(select, replace);
});
} else {
await Messages.showErrorMessage('Editor does not contain G-Code');
}
}
}
21 changes: 9 additions & 12 deletions src/util/commands/addLineNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { configuration } from '../configuration/config';
import { GCommands } from '../constants';
import { LineNumberer, LineNumbererOptions } from '../lineNumberer';
import { LineNumberer } from '../lineNumberer';
import { LineNumbersInput } from '../quickpicks/lineNumbers';
import { GCommand } from './base';

Expand All @@ -17,18 +17,15 @@ export class AddLineNumbers extends GCommand {
}

async execute() {
const lnInputs = new LineNumbersInput();
const state = await lnInputs.collect();

const opts: LineNumbererOptions = {};
const ln = new LineNumberer();

opts.addSpaceAfter = configuration.getParam('lineNumberer.addSpaceAfter');
opts.frequency = configuration.getParam('lineNumberer.frequency');
opts.ignoreBlank = configuration.getParam('lineNumberer.ignoreBlank');
opts.ignoreComments = configuration.getParam('lineNumberer.ignoreComments');
opts.ignoreProgramNumbers = configuration.getParam('lineNumberer.ignoreProgramNumbers');
if (configuration.getParam('lineNumberer.enableQuickPick')) {
const lnInputs = new LineNumbersInput();
const state = await lnInputs.collect();

const ln = new LineNumberer();
await ln.addNumbers(state.start, state.increment, true, opts);
await ln.addNumbers(state.start, state.increment);
} else {
await ln.addNumbers();
}
}
}
23 changes: 0 additions & 23 deletions src/util/commands/functions.ts

This file was deleted.

19 changes: 19 additions & 0 deletions src/util/commands/cmds.ts → src/util/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@

'use strict';

import { Disposable } from 'vscode';
import { AddComment } from './addComment';
import { AddLineNumbers } from './addLineNumbers';
import { RemoveComment } from './removeComment';
import { RemoveLineNumbers } from './removeLineNumbers';
import { ShowGCodeSettings } from './showGCodeSettings';
import { ShowSupportGCode } from './showSupportGCode';

export { AddComment } from './addComment';
export { AddLineNumbers } from './addLineNumbers';
export { RemoveComment } from './removeComment';
export { RemoveLineNumbers } from './removeLineNumbers';
export { ShowGCodeSettings } from './showGCodeSettings';
export { ShowSupportGCode } from './showSupportGCode';

export function registerCommands(): Disposable[] {
return [
new AddComment(),
new AddLineNumbers(),
new RemoveComment(),
new RemoveLineNumbers(),
new ShowGCodeSettings(),
new ShowSupportGCode(),
];
}
2 changes: 1 addition & 1 deletion src/util/commands/removeLineNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export class RemoveLineNumbers extends GCommand {

async execute() {
const ln = new LineNumberer();
await ln.removeNumbers(true);
await ln.removeNumbers();
}
}
16 changes: 9 additions & 7 deletions src/util/configuration/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { StatusBarAlignment } from 'vscode';
import { GCodeUnits } from '../../gcodeUnits';
import { LineNumberFrequency } from '../lineNumberer';
import { LineNumbererOptions, LineNumberFrequency } from '../lineNumberer';

export enum TraceLevel {
Silent = 'silent',
Expand Down Expand Up @@ -35,12 +35,7 @@ export interface GCodeConfiguration {
outputLevel: TraceLevel;
};

lineNumberer: {
addSpaceAfter: boolean;
frequency: LineNumberFrequency;
ignoreBlank: boolean;
ignoreProgramNumbers: boolean;
};
lineNumberer: LineNumbererOptions;

views: {
maxAutoRefresh: number;
Expand Down Expand Up @@ -74,9 +69,16 @@ export const defaults: GCodeConfiguration = {
},
lineNumberer: {
addSpaceAfter: true,
defaultIncrement: 10,
defaultStart: 10,
enableQuickPick: true,
frequency: LineNumberFrequency.EveryLine,
ignoreBlank: true,
ignoreComments: true,
ignoreExtra: [],
ignoreProgramNumbers: true,
matchLineNumber: false,
showProgress: true,
},
views: {
maxAutoRefresh: 10000,
Expand Down
Loading

0 comments on commit 91d9a26

Please sign in to comment.