Skip to content

Commit 218bb2d

Browse files
drzunnyDonJayamanne
authored andcommitted
fix rename error if using 'LF' eol in windows (#748)
* fix rename error if using 'LF' eol in windows * slicing document text by vscode.api
1 parent 833a74f commit 218bb2d

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/client/common/utils.ts

Lines changed: 19 additions & 1 deletion
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

@@ -301,4 +301,22 @@ export function getCustomEnvVars(): any {
301301
}
302302
}
303303
return null;
304+
}
305+
306+
export function getWindowsLineEndingCount(document:TextDocument, offset:Number) {
307+
const eolPattern = new RegExp('\r\n', 'g');
308+
const readBlock = 1024;
309+
let count = 0;
310+
311+
// In order to prevent the one-time loading of large files from taking up too much memory
312+
for (let pos = 0; pos < offset; pos += readBlock) {
313+
let startAt = document.positionAt(pos)
314+
let endAt = document.positionAt(pos + readBlock);
315+
316+
let text = document.getText(new Range(startAt, endAt));
317+
let cr = text.match(eolPattern);
318+
319+
count += cr ? cr.length : 0;
320+
}
321+
return count;
304322
}

src/client/refactor/proxy.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as child_process from 'child_process';
66
import { IPythonSettings } from '../common/configSettings';
77
import { REFACTOR } from '../common/telemetryContracts';
88
import { sendTelemetryEvent, Delays } from '../common/telemetry';
9-
import { IS_WINDOWS, getCustomEnvVars } from '../common/utils';
9+
import { IS_WINDOWS, getCustomEnvVars, getWindowsLineEndingCount } from '../common/utils';
1010
import { mergeEnvVariables } from '../common/envFileParser';
1111

1212
export class RefactorProxy extends vscode.Disposable {
@@ -39,8 +39,11 @@ export class RefactorProxy extends vscode.Disposable {
3939
// get line count
4040
// Rope always uses LF, instead of CRLF on windows, funny isn't it
4141
// So for each line, reduce one characer (for CR)
42+
// But Not all Windows users use CRLF
4243
const offset = document.offsetAt(position);
43-
return offset - position.line;
44+
const winEols = getWindowsLineEndingCount(document, offset);
45+
46+
return offset - winEols;
4447
}
4548
rename<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise<T> {
4649
if (!options) {

0 commit comments

Comments
 (0)