Skip to content

Commit 65f1474

Browse files
amiraliescristianoc
authored andcommitted
Fix formatting textedit range
1 parent 2877f55 commit 65f1474

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

server/src/server.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,9 @@ function format(msg: p.RequestMessage): Array<m.Message> {
398398
extension === c.resiExt
399399
);
400400
if (formattedResult.kind === "success") {
401-
let max = formattedResult.result.length;
402401
let result: p.TextEdit[] = [
403402
{
404-
range: {
405-
start: { line: 0, character: 0 },
406-
end: { line: max, character: max },
407-
},
403+
range: utils.getSourceRange(code),
408404
newText: formattedResult.result,
409405
},
410406
];

server/src/utils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,23 @@ export let parseCompilerLogOutput = (
476476

477477
return { done, result };
478478
};
479+
480+
export let getSourceRange = (source: string): p.Range => {
481+
let start = { line: 0, character: 0 };
482+
483+
// Calculate end position
484+
let len = source.length;
485+
let line = 1;
486+
let colStartIndex = 0;
487+
488+
for (let i = 0; i < len; i++) {
489+
if (source[i] === "\n") {
490+
line++;
491+
colStartIndex = i + 1;
492+
}
493+
}
494+
495+
let end = { line: line, character: len - colStartIndex };
496+
497+
return { start, end };
498+
};

0 commit comments

Comments
 (0)