Skip to content

Commit

Permalink
Merge pull request #2009 from SillyCoon/fix-deftype-formatter-case
Browse files Browse the repository at this point in the history
Fix deftype formatter case
  • Loading branch information
PEZ authored Jan 10, 2023
2 parents 21304f0 + 17f8000 commit 8ad6f5f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes to Calva.

## [Unreleased]

- Fix (formatter): [Indenter and formatter fails while typing out body of deftype method](https://github.com/BetterThanTomorrow/calva/issues/1957)

## [2.0.323] - 2023-01-07

- Fix: [Provider completions not handling errors gracefully](https://github.com/BetterThanTomorrow/calva/issues/2006)
Expand Down
55 changes: 38 additions & 17 deletions src/calva-fmt/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {
} from '../../../out/cljs-lib/cljs-lib';
import * as util from '../../utilities';
import { isUndefined, cloneDeep } from 'lodash';
import { LispTokenCursor } from '../../cursor-doc/token-cursor';

const FormatDepthDefaults = {
deftype: 2,
};

export async function indentPosition(position: vscode.Position, document: vscode.TextDocument) {
const editor = util.getActiveTextEditor();
Expand Down Expand Up @@ -67,7 +72,7 @@ export async function formatRangeEdits(
text,
document.getText(),
rangeTuple,
document.eol == 2 ? '\r\n' : '\n'
_convertEolNumToStringNotation(document.eol)
);
if (newText) {
return [vscode.TextEdit.replace(range, newText)];
Expand All @@ -94,20 +99,14 @@ export async function formatPositionInfo(
extraConfig = {}
) {
const doc: vscode.TextDocument = editor.document;
const pos: vscode.Position = editor.selection.active;
const index = doc.offsetAt(pos);
const mirroredDoc: MirroredDocument = getDocument(doc);
const cursor = mirroredDoc.getTokenCursor(index);
const formatDepth = extraConfig['format-depth'] ? extraConfig['format-depth'] : 1;
const isComment = cursor.getFunctionName() === 'comment';
const config = { ...extraConfig, 'comment-form?': isComment };
let formatRange = cursor.rangeForList(formatDepth);
if (!formatRange) {
formatRange = cursor.rangeForCurrentForm(index);
if (!formatRange || !formatRange.includes(index)) {
return;
}
const index = doc.offsetAt(editor.selection.active);
const cursor = getDocument(doc).getTokenCursor(index);

const formatRange = _calculateFormatRange(extraConfig, cursor, index);
if (!formatRange?.includes(index)) {
return;
}

const formatted: {
'range-text': string;
range: number[];
Expand All @@ -116,9 +115,12 @@ export async function formatPositionInfo(
doc.getText(),
formatRange,
index,
doc.eol == 2 ? '\r\n' : '\n',
_convertEolNumToStringNotation(doc.eol),
onType,
config
{
...extraConfig,
'comment-form?': cursor.getFunctionName() === 'comment',
}
);
const range: vscode.Range = new vscode.Range(
doc.positionAt(formatted.range[0]),
Expand All @@ -135,6 +137,21 @@ export async function formatPositionInfo(
};
}

function _calculateFormatRange(
config: { 'format-depth'?: number },
cursor: LispTokenCursor,
index: number
) {
const formatDepth = config?.['format-depth'] ?? _formatDepth(cursor);
return cursor.rangeForList(formatDepth) ?? cursor.rangeForCurrentForm(index);
}

function _formatDepth(cursor: LispTokenCursor) {
const cursorClone = cursor.clone();
cursorClone.backwardFunction(1);
return FormatDepthDefaults?.[cursorClone.getFunctionName()] ?? 1;
}

export async function formatPosition(
editor: vscode.TextEditor,
onType: boolean = false,
Expand Down Expand Up @@ -195,7 +212,7 @@ export function trimWhiteSpacePositionCommand(editor: vscode.TextEditor) {
export async function formatCode(code: string, eol: number) {
const d = {
'range-text': code,
eol: eol == 2 ? '\r\n' : '\n',
eol: _convertEolNumToStringNotation(eol),
config: await config.getConfig(),
};
const result = jsify(formatText(d));
Expand Down Expand Up @@ -249,3 +266,7 @@ async function _formatRange(
return result['range-text'];
}
}

function _convertEolNumToStringNotation(eol: vscode.EndOfLine) {
return eol == 2 ? '\r\n' : '\n';
}
5 changes: 5 additions & 0 deletions src/cljs-lib/test/calva/fmt/formatter_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
baz)")

(def deftype-all-text
"(deftype MyType [arg1 arg2]\n IMyProto\n (method1 [this]\n (smth)))")

(deftest format-text-at-idx
(is (= "(defn bar
[x]
Expand All @@ -28,6 +31,8 @@ baz)")
(:range (sut/format-text-at-idx {:eol "\n" :all-text "(\n\n,)" :range [0 5] :idx 2}))))
(is (= "()"
(:range-text (sut/format-text-at-idx {:eol "\n" :all-text "(\n\n,)" :range [0 5] :idx 2}))))
(is (= "(deftype MyType [arg1 arg2]\n IMyProto\n (method1 [this]\n (smth)))"
(:range-text (sut/format-text-at-idx {:eol "\n" :all-text deftype-all-text :range [0 76] :idx 68}))))
;; TODO: Figure out why the extra space is not removed
#_(is (= "a c"
(:range-text (sut/format-text-at-idx {:eol "\n" :all-text "a c" :range [0 4] :idx 2})))))
Expand Down

0 comments on commit 8ad6f5f

Please sign in to comment.