Skip to content

Commit fbe9f74

Browse files
4ever2thery
authored andcommitted
Fix error location calculation
Co-Authored-By: Laurent Théry <[email protected]>
1 parent 80defda commit fbe9f74

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

server/src/stm/STM.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as server from '../server';
1616
import {AnnotatedText} from '../util/AnnotatedText'
1717
import * as text from '../util/AnnotatedText'
1818
import {GoalsCache} from './GoalsCache';
19+
import * as semver from 'semver';
1920

2021
import {Settings} from '../protocol';
2122
export {StateStatus} from './State';
@@ -802,11 +803,19 @@ private routeId = 1;
802803
*/
803804
private async handleCallFailure(error: coqtop.CallFailure, command: {range: Range, text: string}) : Promise<proto.FailValue> {
804805
let errorRange : Range = undefined;
805-
if(error.range)
806-
errorRange = vscode.Range.create(
807-
textUtil.positionAtRelativeCNL(command.range.start, command.text, error.range.start)
808-
, textUtil.positionAtRelativeCNL(command.range.start, command.text, error.range.stop)
809-
);
806+
if(error.range) {
807+
const version = this.coqtop.getVersion();
808+
let start : Position;
809+
let end : Position;
810+
if (semver.satisfies(version, ">= 8.15")) {
811+
start = textUtil.positionAtRelative(command.range.start, command.text, error.range.start)
812+
end = textUtil.positionAtRelative(command.range.start, command.text, error.range.stop)
813+
} else {
814+
start = textUtil.positionAtRelativeCNL(command.range.start, command.text, error.range.start)
815+
end = textUtil.positionAtRelativeCNL(command.range.start, command.text, error.range.stop)
816+
}
817+
errorRange = vscode.Range.create(start, end);
818+
}
810819

811820
const diffError = errorParsing.parseError(error.message);
812821
const prettyError = server.project.getPrettifySymbols().prettify(diffError)

server/src/util/text-util.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {Position, Range} from 'vscode-languageserver';
33

44
// 'sticky' flag is not yet supported :()
55
const lineEndingRE = /([^\r\n]*)(\r\n|\r|\n)?/;
6+
const lineEndingRE1 = /([^\r\n]*)((\r\n)*|\r|\n)?/;
67

78
export interface RangeDelta {
89
start: Position;
@@ -223,16 +224,17 @@ export function positionAtRelativeCNL(start: Position, text: string, offset: num
223224
let currentOffset = 0; // offset into text we are current at; <= `offset`
224225
let lineOffset = start.character;
225226
while(true) {
226-
const match = lineEndingRE.exec(text.substring(currentOffset));
227+
const match = lineEndingRE1.exec(text.substring(currentOffset));
227228
// match[0] -- characters plus newline
228229
// match[1] -- characters up to newline
229230
// match[2] -- newline (\n, \r, or \r\n)
231+
const value = match[0].length - match[1].length;
230232
if(!match || match[0].length === 0 || match[1].length >= offset)
231233
return Position.create(line, lineOffset + offset)
232234
currentOffset+= match[0].length;
233-
offset -= match[1].length + (match[2]===undefined ? 0 : 1);
235+
offset -= match[1].length + (Math.floor(value / 2) + 1);
234236
lineOffset = 0;
235-
++line;
237+
line += Math.floor((value + 1) / 2);
236238
}
237239
}
238240

0 commit comments

Comments
 (0)