Skip to content

Fix hang caused when format on type is enabled #636

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

Merged
merged 2 commits into from
Mar 30, 2017
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
68 changes: 33 additions & 35 deletions src/features/DocumentFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,6 @@ class PSDocumentFormattingEditProvider implements
return -1 * editComparer(left, right);
});

// We cannot handle multiple edits at the same point hence we
// filter the markers so that there is only one edit per region
if (edits.length > 0) {
uniqueEdits.push(edits[0]);
for (let edit of edits.slice(1)) {
let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1];
if (lastEdit.startLineNumber !== edit.startLineNumber
|| (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) {
uniqueEdits.push(edit);
}
}
}

// we need to update the range as the edits might
// have changed the original layout
Expand All @@ -333,6 +321,22 @@ class PSDocumentFormattingEditProvider implements
// extend the range such that it starts at the first character of the
// start line of the range.
range = this.snapRangeToEdges(range, document);

// filter edits that are contained in the input range
edits = edits.filter(edit => range.contains(toRange(edit).start));
}

// We cannot handle multiple edits at the same point hence we
// filter the markers so that there is only one edit per region
if (edits.length > 0) {
uniqueEdits.push(edits[0]);
for (let edit of edits.slice(1)) {
let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1];
if (lastEdit.startLineNumber !== edit.startLineNumber
|| (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) {
uniqueEdits.push(edit);
}
}
}

// reset line difference to 0
Expand All @@ -341,7 +345,7 @@ class PSDocumentFormattingEditProvider implements
// we do not return a valid array because our text edits
// need to be executed in a particular order and it is
// easier if we perform the edits ourselves
return this.applyEdit(editor, uniqueEdits, range, 0, index);
return this.applyEdit(editor, uniqueEdits, 0, index);
})
.then(() => {
// execute the same rule again if we left out violations
Expand All @@ -361,7 +365,6 @@ class PSDocumentFormattingEditProvider implements
private applyEdit(
editor: TextEditor,
edits: ScriptRegion[],
range: Range,
markerIndex: number,
ruleIndex: number): Thenable<void> {
if (markerIndex >= edits.length) {
Expand All @@ -373,27 +376,22 @@ class PSDocumentFormattingEditProvider implements
let edit: ScriptRegion = edits[markerIndex];
let editRange: Range = toRange(edit);

if (range === null || range.contains(editRange.start)) {

// accumulate the changes in number of lines
// get the difference between the number of lines in the replacement text and
// that of the original text
this.lineDiff += this.getNumLines(edit.text) - (editRange.end.line - editRange.start.line + 1);
return editor.edit((editBuilder) => {
editBuilder.replace(
editRange,
edit.text);
},
{
undoStopAfter: undoStopAfter,
undoStopBefore: undoStopBefore
}).then((isEditApplied) => {
return this.applyEdit(editor, edits, range, markerIndex + 1, ruleIndex);
}); // TODO handle rejection
}
else {
return this.applyEdit(editor, edits, range, markerIndex + 1, ruleIndex);
}

// accumulate the changes in number of lines
// get the difference between the number of lines in the replacement text and
// that of the original text
this.lineDiff += this.getNumLines(edit.text) - (editRange.end.line - editRange.start.line + 1);
return editor.edit((editBuilder) => {
editBuilder.replace(
editRange,
edit.text);
},
{
undoStopAfter: undoStopAfter,
undoStopBefore: undoStopBefore
}).then((isEditApplied) => {
return this.applyEdit(editor, edits, markerIndex + 1, ruleIndex);
}); // TODO handle rejection
}

private getNumLines(text: string): number {
Expand Down