Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/client/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
7 changes: 5 additions & 2 deletions src/client/refactor/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<T>(document: vscode.TextDocument, name: string, filePath: string, range: vscode.Range, options?: vscode.TextEditorOptions): Promise<T> {
if (!options) {
Expand Down