From 882e749d6e3de140594c49986da89ec5a335b118 Mon Sep 17 00:00:00 2001 From: drzunny Date: Wed, 15 Feb 2017 22:21:04 +0800 Subject: [PATCH 1/2] fix rename error if using 'LF' eol in windows --- src/client/common/utils.ts | 15 +++++++++++++++ src/client/refactor/proxy.ts | 7 +++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/client/common/utils.ts b/src/client/common/utils.ts index 50d90ec3be64..7d7575743434 100644 --- a/src/client/common/utils.ts +++ b/src/client/common/utils.ts @@ -301,4 +301,19 @@ export function getCustomEnvVars(): any { } } return null; +} + +export function getWindowsLineEndingCount(text:String, offset:Number) { + const eolPattern = new RegExp('\r', '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 partialText = text.slice(pos, pos + readBlock); + let cr = partialText.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..789a5e4a4e85 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.getText(), offset); + return offset - winEols; } rename(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise { if (!options) { From 7cdc5f0831ce682d9b1d071e55b389e72ce6038e Mon Sep 17 00:00:00 2001 From: drzunny Date: Thu, 16 Feb 2017 11:31:41 +0800 Subject: [PATCH 2/2] slicing document text by vscode.api --- src/client/common/utils.ts | 13 ++++++++----- src/client/refactor/proxy.ts | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/client/common/utils.ts b/src/client/common/utils.ts index 7d7575743434..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'; @@ -303,16 +303,19 @@ export function getCustomEnvVars(): any { return null; } -export function getWindowsLineEndingCount(text:String, offset:Number) { - const eolPattern = new RegExp('\r', 'g'); +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 partialText = text.slice(pos, pos + readBlock); - let cr = partialText.match(eolPattern); + 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; diff --git a/src/client/refactor/proxy.ts b/src/client/refactor/proxy.ts index 789a5e4a4e85..3f3e0e1daed4 100644 --- a/src/client/refactor/proxy.ts +++ b/src/client/refactor/proxy.ts @@ -41,8 +41,8 @@ export class RefactorProxy extends vscode.Disposable { // So for each line, reduce one characer (for CR) // But Not all Windows users use CRLF const offset = document.offsetAt(position); - - const winEols = getWindowsLineEndingCount(document.getText(), offset); + const winEols = getWindowsLineEndingCount(document, offset); + return offset - winEols; } rename(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise {