55'use strict' ;
66
77import { Progress , ProgressLocation , Range , TextEditor , window } from 'vscode' ;
8+ import { constants } from './constants' ;
89
910export 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