Skip to content

Fix formatting on paste behavior #501

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 5 commits into from
Feb 14, 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
40 changes: 28 additions & 12 deletions src/features/DocumentFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
DocumentFormattingEditProvider,
DocumentRangeFormattingEditProvider,
Range,
TextEditor
TextEditor,
TextLine
} from 'vscode';
import { LanguageClient, RequestType } from 'vscode-languageclient';
import Window = vscode.window;
Expand Down Expand Up @@ -134,6 +135,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
private static documentLocker = new DocumentLocker();
private static statusBarTracker = new Object();
private languageClient: LanguageClient;
private lineDiff: number;

// The order in which the rules will be executed starting from the first element.
private readonly ruleOrder: string[] = [
Expand All @@ -153,6 +155,7 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider

constructor(aggregateUndoStop = true) {
this.aggregateUndoStop = aggregateUndoStop;
this.lineDiff = 0;
}

provideDocumentFormattingEdits(
Expand Down Expand Up @@ -195,6 +198,12 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
PSDocumentFormattingEditProvider.disposeAllStatusBars();
}

private snapRangeToEdges(range: Range, document: TextDocument): Range {
return range.with({
start: range.start.with({ character: 0 }),
end: document.lineAt(range.end.line).range.end });
}

private getEditor(document: TextDocument): TextEditor {
return Window.visibleTextEditors.find((e, n, obj) => { return e.document === document; });
}
Expand Down Expand Up @@ -248,12 +257,18 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
// we need to update the range as the edits might
// have changed the original layout
if (range !== null) {
let tempRange: Range = this.getSelectionRange(document);
if (tempRange !== null) {
range = tempRange;
if (this.lineDiff !== 0) {
range = range.with({ end: range.end.translate({ lineDelta: this.lineDiff }) });
}

// extend the range such that it starts at the first character of the
// start line of the range.
range = this.snapRangeToEdges(range, document);
}

// reset line difference to 0
this.lineDiff = 0;

// 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
Expand Down Expand Up @@ -292,7 +307,13 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
edit.startColumnNumber - 1,
edit.endLineNumber - 1,
edit.endColumnNumber - 1);
if (range === null || range.contains(editRange)) {

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,
Expand All @@ -310,13 +331,8 @@ class PSDocumentFormattingEditProvider implements DocumentFormattingEditProvider
}
}

private getSelectionRange(document: TextDocument): Range {
let editor = vscode.window.visibleTextEditors.find(editor => editor.document === document);
if (editor !== undefined) {
return editor.selection as Range;
}

return null;
private getNumLines(text: string): number {
return text.split(/\r?\n/).length;
}

private getSettings(rule: string): any {
Expand Down