Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when the client sends multiple contentChanges at once #1131

Merged
merged 2 commits into from
Dec 22, 2023
Merged
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
74 changes: 53 additions & 21 deletions src/common/providers/astProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
import { URI } from "vscode-uri";
import Parser, { Edit, Point, SyntaxNode } from "web-tree-sitter";
import { ElmWorkspaceMatcher } from "../util/elmWorkspaceMatcher";
import { Position, Range } from "vscode-languageserver-textdocument";
import {
Position,
Range,
TextDocumentContentChangeEvent,
} from "vscode-languageserver-textdocument";
import { TextDocumentEvents } from "../util/textDocumentEvents";
import { TreeUtils } from "../util/treeUtils";
import { ISourceFile } from "../../compiler/forest";
Expand Down Expand Up @@ -107,26 +111,9 @@ export class ASTProvider {
let hasContentChanges = false;
if ("contentChanges" in params && params.contentChanges) {
hasContentChanges = true;
for (const change of params.contentChanges) {
if ("range" in change) {
tree?.edit(this.getEditFromChange(change, tree.rootNode.text));
} else {
const currentText = tree?.rootNode.text;
if (currentText) {
const regex = new RegExp(/\r\n|\r|\n/);
const lines = currentText.split(regex);
const range = {
start: { line: 0, character: 0 },
end: { line: lines.length, character: 0 },
};
tree?.edit(
this.getEditFromChange(
{ text: change.text, range: range },
currentText,
),
);
}
}

if (tree) {
this.applyChangesToTree(tree, params.contentChanges);
}
}

Expand Down Expand Up @@ -227,6 +214,51 @@ export class ASTProvider {
}
};

private applyChangesToTree(
tree: Parser.Tree,
changes: TextDocumentContentChangeEvent[],
): void {
let text = tree.rootNode.text;

if (!text) {
return;
}

const multipleChanges = changes.length > 1;
for (const change of changes) {
const changeRecord = this.getChangeWithRange(change, text);
const edit = this.getEditFromChange(changeRecord, text);

tree?.edit(edit);

// If there are multiple changes, we also need to take care to apply each change to the
// rootNode text. Otherwise, the startIndex and endIndex for later changes will be off.
if (multipleChanges) {
text =
text.substring(0, edit.startIndex) +
change.text +
text.substring(edit.oldEndIndex);
}
}
}

private getChangeWithRange(
change: TextDocumentContentChangeEvent,
text: string,
): { text: string; range: Range } {
if ("range" in change) {
return change;
} else {
const regex = new RegExp(/\r\n|\r|\n/);
const lines = text.split(regex);
const range = {
start: { line: 0, character: 0 },
end: { line: lines.length, character: 0 },
};
return { text: change.text, range: range };
}
}

private getEditFromChange(
change: { text: string; range: Range },
text: string,
Expand Down
Loading