diff --git a/src/client/common/utils.ts b/src/client/common/utils.ts index 50d90ec3be64..c7d59aca2bad 100644 --- a/src/client/common/utils.ts +++ b/src/client/common/utils.ts @@ -8,7 +8,7 @@ import * as path from 'path'; import * as fs from 'fs'; import * as child_process from 'child_process'; import * as settings from './configSettings'; -import { CancellationToken } from 'vscode'; +import { CancellationToken, TextDocument, Range, Position } from 'vscode'; import { isNotInstalledError } from './helpers'; import { mergeEnvVariables, parseEnvFile } from './envFileParser'; @@ -301,4 +301,22 @@ export function getCustomEnvVars(): any { } } return null; +} + +export function getWindowsLineEndingCount(document:TextDocument, offset:Number) { + const eolPattern = new RegExp('\r\n', 'g'); + const readBlock = 1024; + let count = 0; + + // In order to prevent the one-time loading of large files from taking up too much memory + for (let pos = 0; pos < offset; pos += readBlock) { + let startAt = document.positionAt(pos) + let endAt = document.positionAt(pos + readBlock); + + let text = document.getText(new Range(startAt, endAt)); + let cr = text.match(eolPattern); + + count += cr ? cr.length : 0; + } + return count; } \ No newline at end of file diff --git a/src/client/refactor/proxy.ts b/src/client/refactor/proxy.ts index 219e9c21efcc..3f3e0e1daed4 100644 --- a/src/client/refactor/proxy.ts +++ b/src/client/refactor/proxy.ts @@ -6,7 +6,7 @@ import * as child_process from 'child_process'; import { IPythonSettings } from '../common/configSettings'; import { REFACTOR } from '../common/telemetryContracts'; import { sendTelemetryEvent, Delays } from '../common/telemetry'; -import { IS_WINDOWS, getCustomEnvVars } from '../common/utils'; +import { IS_WINDOWS, getCustomEnvVars, getWindowsLineEndingCount } from '../common/utils'; import { mergeEnvVariables } from '../common/envFileParser'; export class RefactorProxy extends vscode.Disposable { @@ -39,8 +39,11 @@ export class RefactorProxy extends vscode.Disposable { // get line count // Rope always uses LF, instead of CRLF on windows, funny isn't it // So for each line, reduce one characer (for CR) + // But Not all Windows users use CRLF const offset = document.offsetAt(position); - return offset - position.line; + const winEols = getWindowsLineEndingCount(document, offset); + + return offset - winEols; } rename(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise { if (!options) {