Skip to content

Commit 291741e

Browse files
committed
Line Numeberer Configuration Options
1 parent 8ff963c commit 291741e

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@
253253
"markdownDescription": "Ignore Blank Lines when Numbering",
254254
"scope": "window"
255255
},
256+
"gcode.lineNumberer.ignoreComments": {
257+
"type": "boolean",
258+
"default": true,
259+
"markdownDescription": "Ignore Comments when Numbering",
260+
"scope": "window"
261+
},
256262
"gcode.lineNumberer.ignoreProgramNumbers": {
257263
"type": "boolean",
258264
"default": true,

src/util/commands/addLineNumbers.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
'use strict';
77

8-
import { LineNumberer } from '../lineNumberer';
8+
import { configuration } from '../configuration/config';
9+
import { LineNumberer, LineNumbererOptions } from '../lineNumberer';
910
import { LineNumbersInput } from '../quickpicks/lineNumbers';
1011
import { GCommand, UtilCommands } from './common';
1112

@@ -18,7 +19,15 @@ export class AddLineNumbers extends GCommand {
1819
const lnInputs = new LineNumbersInput();
1920
const state = await lnInputs.collect();
2021

22+
const opts: LineNumbererOptions = {};
23+
24+
opts.addSpaceAfter = configuration.getParam('lineNumberer.addSpaceAfter');
25+
opts.frequency = configuration.getParam('lineNumberer.frequency');
26+
opts.ignoreBlank = configuration.getParam('lineNumberer.ignoreBlank');
27+
opts.ignoreComments = configuration.getParam('lineNumberer.ignoreComments');
28+
opts.ignoreProgramNumbers = configuration.getParam('lineNumberer.ignoreProgramNumbers');
29+
2130
const ln = new LineNumberer();
22-
await ln.addNumbers(state.start, state.increment, true);
31+
await ln.addNumbers(state.start, state.increment, true, opts);
2332
}
2433
}

src/util/lineNumberer.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
'use strict';
66

77
import { Progress, ProgressLocation, Range, TextEditor, window } from 'vscode';
8+
import { constants } from './constants';
89

910
export enum LineNumberFrequency {
1011
EveryLine = 'Every Line',
1112
AtToolChanges = 'At Tool Changes',
1213
}
1314

14-
type LineNumbererOptions = {
15+
export type LineNumbererOptions = {
1516
addSpaceAfter?: boolean;
1617
frequency?: LineNumberFrequency;
1718
ignoreComments?: boolean;
@@ -38,8 +39,18 @@ export class LineNumberer {
3839
showProgress: boolean,
3940
options?: LineNumbererOptions,
4041
): Promise<boolean> {
41-
if (this._editor && this._editor.document) {
42-
this._beforeText = this._editor.document.getText();
42+
if (
43+
this._editor &&
44+
this._editor.document &&
45+
this._editor.document.uri.scheme === 'file' &&
46+
this._editor.document.languageId === constants.langId
47+
) {
48+
if (!this._editor.selection.isEmpty) {
49+
const select = new Range(this._editor.selection.start, this._editor.selection.end);
50+
this._beforeText = this._editor.document.getText(select);
51+
} else {
52+
this._beforeText = this._editor.document.getText();
53+
}
4354
}
4455
// Remove any numbers first
4556
const newtext = this._removeNumbers(this._beforeText);
@@ -171,9 +182,20 @@ export class LineNumberer {
171182
}
172183

173184
async removeNumbers(showProgress: boolean = false): Promise<boolean> {
174-
if (this._editor && this._editor.document) {
175-
this._beforeText = this._editor.document.getText();
185+
if (
186+
this._editor &&
187+
this._editor.document &&
188+
this._editor.document.uri.scheme === 'file' &&
189+
this._editor.document.languageId === constants.langId
190+
) {
191+
if (!this._editor.selection.isEmpty) {
192+
const select = new Range(this._editor.selection.start, this._editor.selection.end);
193+
this._beforeText = this._editor.document.getText(select);
194+
} else {
195+
this._beforeText = this._editor.document.getText();
196+
}
176197
}
198+
177199
if (showProgress) {
178200
await window.withProgress(
179201
{
@@ -209,14 +231,19 @@ export class LineNumberer {
209231
private async _updateTextEditor(text: string): Promise<boolean> {
210232
const len = this._beforeText.length;
211233

234+
let rng: Range;
235+
212236
if (this._editor && this._editor.document) {
237+
if (!this._editor.selection.isEmpty) {
238+
rng = new Range(this._editor.selection.start, this._editor.selection.end);
239+
} else {
240+
rng = new Range(this._editor.document.positionAt(0), this._editor.document.positionAt(len - 1));
241+
}
242+
213243
return Promise.resolve(
214244
this._editor.edit(editBuilder => {
215245
if (this._editor && this._editor.document) {
216-
editBuilder.replace(
217-
new Range(this._editor.document.positionAt(0), this._editor.document.positionAt(len - 1)),
218-
text,
219-
);
246+
editBuilder.replace(rng, text);
220247

221248
this._beforeText = '';
222249
}

0 commit comments

Comments
 (0)