Skip to content

Commit 7cdc5f0

Browse files
committed
slicing document text by vscode.api
1 parent 882e749 commit 7cdc5f0

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/client/common/utils.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as path from 'path';
88
import * as fs from 'fs';
99
import * as child_process from 'child_process';
1010
import * as settings from './configSettings';
11-
import { CancellationToken } from 'vscode';
11+
import { CancellationToken, TextDocument, Range, Position } from 'vscode';
1212
import { isNotInstalledError } from './helpers';
1313
import { mergeEnvVariables, parseEnvFile } from './envFileParser';
1414

@@ -303,16 +303,19 @@ export function getCustomEnvVars(): any {
303303
return null;
304304
}
305305

306-
export function getWindowsLineEndingCount(text:String, offset:Number) {
307-
const eolPattern = new RegExp('\r', 'g');
306+
export function getWindowsLineEndingCount(document:TextDocument, offset:Number) {
307+
const eolPattern = new RegExp('\r\n', 'g');
308308
const readBlock = 1024;
309309
let count = 0;
310310

311311
// In order to prevent the one-time loading of large files from taking up too much memory
312312
for (let pos = 0; pos < offset; pos += readBlock) {
313-
let partialText = text.slice(pos, pos + readBlock);
314-
let cr = partialText.match(eolPattern);
313+
let startAt = document.positionAt(pos)
314+
let endAt = document.positionAt(pos + readBlock);
315315

316+
let text = document.getText(new Range(startAt, endAt));
317+
let cr = text.match(eolPattern);
318+
316319
count += cr ? cr.length : 0;
317320
}
318321
return count;

src/client/refactor/proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export class RefactorProxy extends vscode.Disposable {
4141
// So for each line, reduce one characer (for CR)
4242
// But Not all Windows users use CRLF
4343
const offset = document.offsetAt(position);
44-
45-
const winEols = getWindowsLineEndingCount(document.getText(), offset);
44+
const winEols = getWindowsLineEndingCount(document, offset);
45+
4646
return offset - winEols;
4747
}
4848
rename<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise<T> {

0 commit comments

Comments
 (0)