Skip to content

Commit adcf152

Browse files
author
Kapil Borle
authored
Merge pull request #636 from PowerShell/kapilmb/format-on-type-hang
Fix hang caused when format on type is enabled
2 parents 0def0af + 0347539 commit adcf152

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

src/features/DocumentFormatter.ts

+33-35
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,6 @@ class PSDocumentFormattingEditProvider implements
310310
return -1 * editComparer(left, right);
311311
});
312312

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

326314
// we need to update the range as the edits might
327315
// have changed the original layout
@@ -333,6 +321,22 @@ class PSDocumentFormattingEditProvider implements
333321
// extend the range such that it starts at the first character of the
334322
// start line of the range.
335323
range = this.snapRangeToEdges(range, document);
324+
325+
// filter edits that are contained in the input range
326+
edits = edits.filter(edit => range.contains(toRange(edit).start));
327+
}
328+
329+
// We cannot handle multiple edits at the same point hence we
330+
// filter the markers so that there is only one edit per region
331+
if (edits.length > 0) {
332+
uniqueEdits.push(edits[0]);
333+
for (let edit of edits.slice(1)) {
334+
let lastEdit: ScriptRegion = uniqueEdits[uniqueEdits.length - 1];
335+
if (lastEdit.startLineNumber !== edit.startLineNumber
336+
|| (edit.startColumnNumber + edit.text.length) < lastEdit.startColumnNumber) {
337+
uniqueEdits.push(edit);
338+
}
339+
}
336340
}
337341

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

376-
if (range === null || range.contains(editRange.start)) {
377-
378-
// accumulate the changes in number of lines
379-
// get the difference between the number of lines in the replacement text and
380-
// that of the original text
381-
this.lineDiff += this.getNumLines(edit.text) - (editRange.end.line - editRange.start.line + 1);
382-
return editor.edit((editBuilder) => {
383-
editBuilder.replace(
384-
editRange,
385-
edit.text);
386-
},
387-
{
388-
undoStopAfter: undoStopAfter,
389-
undoStopBefore: undoStopBefore
390-
}).then((isEditApplied) => {
391-
return this.applyEdit(editor, edits, range, markerIndex + 1, ruleIndex);
392-
}); // TODO handle rejection
393-
}
394-
else {
395-
return this.applyEdit(editor, edits, range, markerIndex + 1, ruleIndex);
396-
}
379+
380+
// accumulate the changes in number of lines
381+
// get the difference between the number of lines in the replacement text and
382+
// that of the original text
383+
this.lineDiff += this.getNumLines(edit.text) - (editRange.end.line - editRange.start.line + 1);
384+
return editor.edit((editBuilder) => {
385+
editBuilder.replace(
386+
editRange,
387+
edit.text);
388+
},
389+
{
390+
undoStopAfter: undoStopAfter,
391+
undoStopBefore: undoStopBefore
392+
}).then((isEditApplied) => {
393+
return this.applyEdit(editor, edits, markerIndex + 1, ruleIndex);
394+
}); // TODO handle rejection
397395
}
398396

399397
private getNumLines(text: string): number {

0 commit comments

Comments
 (0)