Skip to content

Commit

Permalink
Special treatment for formatting top-level forms
Browse files Browse the repository at this point in the history
Fixes #640
  • Loading branch information
PEZ committed May 10, 2020
1 parent 130d202 commit 73fb795
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changes to Calva.

## [Unreleased]
- Fix [Formatting top-level form stopped working](https://github.com/BetterThanTomorrow/calva/issues/640)

## [2.0.98] - 2020-05-04
- Fix [Problems Editing a Bare file (instead of directory)](https://github.com/BetterThanTomorrow/calva/issues/622)
Expand Down
26 changes: 18 additions & 8 deletions src/calva-fmt/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ export function formatPositionInfo(editor: vscode.TextEditor, onType: boolean =
const mirroredDoc: MirroredDocument = getDocument(doc);
const cursor = mirroredDoc.getTokenCursor(index);
const formatDepth = extraConfig["format-depth"] ? extraConfig["format-depth"] : 1;
const formatRange = cursor.rangeForList(formatDepth);
let formatRange = cursor.rangeForList(formatDepth);
if (!formatRange) {
formatRange = cursor.rangeForCurrentForm(index);
if (!formatRange.includes(index)) {
return;
}
}
const formatted: {
"range-text": string,
"range": number[],
Expand All @@ -61,20 +67,24 @@ export function formatPositionInfo(editor: vscode.TextEditor, onType: boolean =
export function formatPosition(editor: vscode.TextEditor, onType: boolean = false, extraConfig = {}): Thenable<boolean> {
const doc: vscode.TextDocument = editor.document,
formattedInfo = formatPositionInfo(editor, onType, extraConfig);
if (formattedInfo.previousText != formattedInfo.formattedText) {
if (formattedInfo && formattedInfo.previousText != formattedInfo.formattedText) {
return editor.edit(textEditorEdit => {
textEditorEdit.replace(formattedInfo.range, formattedInfo.formattedText);
}, { undoStopAfter: false, undoStopBefore: false }).then((onFulfilled: boolean) => {
editor.selection = new vscode.Selection(doc.positionAt(formattedInfo.newIndex), doc.positionAt(formattedInfo.newIndex))
return onFulfilled;
});
} else {
return new Promise((resolve, _reject) => {
if (formattedInfo.newIndex != formattedInfo.previousIndex) {
editor.selection = new vscode.Selection(doc.positionAt(formattedInfo.newIndex), doc.positionAt(formattedInfo.newIndex));
}
resolve(true);
});
if (formattedInfo) {
return new Promise((resolve, _reject) => {
if (formattedInfo.newIndex != formattedInfo.previousIndex) {
editor.selection = new vscode.Selection(doc.positionAt(formattedInfo.newIndex), doc.positionAt(formattedInfo.newIndex));
}
resolve(true);
});
} else {
return formatRange(doc, new vscode.Range(doc.positionAt(0), doc.positionAt(doc.getText().length)))
}
}
}

Expand Down

0 comments on commit 73fb795

Please sign in to comment.