Skip to content

Commit 261c8b5

Browse files
Stop using one based math in fourslash.
1 parent 572c550 commit 261c8b5

2 files changed

Lines changed: 17 additions & 39 deletions

File tree

src/harness/fourslash.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,39 +2010,31 @@ module FourSlash {
20102010

20112011
// Get the text of the entire line the caret is currently at
20122012
private getCurrentLineContent() {
2013-
// The current caret position (in line/col terms)
2014-
var line = this.getCurrentCaretFilePosition().line;
2015-
// The line/col of the start of this line
2016-
var pos = this.languageServiceAdapterHost.lineColToPosition(this.activeFile.fileName, line, 1);
2017-
// The index of the current file
2013+
var text = this.getFileContent(this.activeFile.fileName)
20182014

2019-
// The text from the start of the line to the end of the file
2020-
var text = this.getFileContent(this.activeFile.fileName).substring(pos);
2015+
var pos = this.currentCaretPosition;
2016+
var startPos = pos, endPos = pos;
20212017

2022-
// Truncate to the first newline
2023-
var newlinePos = text.indexOf('\n');
2024-
if (newlinePos === -1) {
2025-
return text;
2026-
}
2027-
else {
2028-
if (text.charAt(newlinePos - 1) === '\r') {
2029-
newlinePos--;
2018+
while (startPos > 0) {
2019+
var ch = text.charCodeAt(startPos - 1);
2020+
if (ch === ts.CharacterCodes.carriageReturn || ch === ts.CharacterCodes.lineFeed) {
2021+
break;
20302022
}
2031-
return text.substr(0, newlinePos);
2032-
}
2033-
}
20342023

2035-
private getCurrentCaretFilePosition() {
2036-
var result = this.languageServiceAdapterHost.positionToZeroBasedLineAndCharacter(this.activeFile.fileName, this.currentCaretPosition);
2037-
if (result.line >= 0) {
2038-
result.line++;
2024+
startPos--;
20392025
}
20402026

2041-
if (result.character >= 0) {
2042-
result.character++;
2027+
while (endPos < text.length) {
2028+
var ch = text.charCodeAt(endPos);
2029+
2030+
if (ch === ts.CharacterCodes.carriageReturn || ch === ts.CharacterCodes.lineFeed) {
2031+
break;
2032+
}
2033+
2034+
endPos++;
20432035
}
20442036

2045-
return result;
2037+
return text.substring(startPos, endPos);
20462038
}
20472039

20482040
private assertItemInCompletionList(items: ts.CompletionEntry[], name: string, text?: string, documentation?: string, kind?: string) {

src/harness/harnessLanguageService.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,6 @@ module Harness.LanguageService {
165165
throw new Error("No script with name '" + fileName + "'");
166166
}
167167

168-
/**
169-
* @param line 1 based index
170-
* @param col 1 based index
171-
*/
172-
public lineColToPosition(fileName: string, line: number, col: number): number {
173-
var script: ScriptInfo = this.fileNameToScript[fileName];
174-
assert.isNotNull(script);
175-
assert.isTrue(line >= 1);
176-
assert.isTrue(col >= 1);
177-
178-
return ts.computePositionOfOneBasedLineAndCharacter(script.lineMap, line, col);
179-
}
180-
181168
/**
182169
* @param line 0 based index
183170
* @param col 0 based index
@@ -234,7 +221,6 @@ module Harness.LanguageService {
234221
addScript(fileName: string, content: string): void { this.nativeHost.addScript(fileName, content); }
235222
updateScript(fileName: string, content: string): void { return this.nativeHost.updateScript(fileName, content); }
236223
editScript(fileName: string, minChar: number, limChar: number, newText: string): void { this.nativeHost.editScript(fileName, minChar, limChar, newText); }
237-
lineColToPosition(fileName: string, line: number, col: number): number { return this.nativeHost.lineColToPosition(fileName, line, col); }
238224
positionToZeroBasedLineAndCharacter(fileName: string, position: number): ts.LineAndCharacter { return this.nativeHost.positionToZeroBasedLineAndCharacter(fileName, position); }
239225

240226
getCompilationSettings(): string { return JSON.stringify(this.nativeHost.getCompilationSettings()); }

0 commit comments

Comments
 (0)