diff --git a/package.json b/package.json index 3a27ca361..735f4dbc9 100644 --- a/package.json +++ b/package.json @@ -114,7 +114,7 @@ "editor.autoClosingQuotes": "always", "editor.formatOnType": true, "editor.autoIndent": "full", - "editor.formatOnPaste": true, + "editor.formatOnPaste": false, "files.trimTrailingWhitespace": false, "editor.matchBrackets": "never", "editor.guides.indentation": false, diff --git a/src/calva-fmt/src/format.ts b/src/calva-fmt/src/format.ts index 6f40cb7e1..528e54bf6 100644 --- a/src/calva-fmt/src/format.ts +++ b/src/calva-fmt/src/format.ts @@ -65,7 +65,7 @@ export function formatRangeEdits(document: vscode.TextDocument, range: vscode.Ra const cursor = mirroredDoc.getTokenCursor(startIndex); if (!cursor.withinString()) { const rangeTuple: number[] = [startIndex, endIndex]; - const newText: string = _formatRange(text, document.getText(), rangeTuple, document.eol == 2 ? "\r\n" : "\n"); + const newText: string = _formatRange(text, document.getText(), rangeTuple, document.eol == 2 ? "\r\n" : "\n")['range-text']; if (newText) { return [vscode.TextEdit.replace(range, newText)]; } @@ -122,17 +122,18 @@ export function formatPositionInfoEditableDoc(document: docModel.EditableDocumen } export function formatRangeEditableDoc(document: docModel.EditableDocument, range: [number, number], onType: boolean = false, extraConfig = {}): Thenable { - const formattedInfo = formatRangeInfoEditableDoc(document, range, onType, extraConfig); + const formattedInfo = formatRangeInfoEditableDoc(document, range, onType, { performFormatAsYouType: false, ...extraConfig }); return performFormatEditableDoc(document, formattedInfo, onType, extraConfig); } export function formatPositionEditableDoc(document: docModel.EditableDocument, onType: boolean = false, extraConfig = {}): Thenable { - const formattedInfo = formatPositionInfoEditableDoc(document, onType, extraConfig); + const formattedInfo = formatPositionInfoEditableDoc(document, onType, { performFormatAsYouType: true, ...extraConfig }); return performFormatEditableDoc(document, formattedInfo, onType, extraConfig); } function performFormatEditableDoc(document: docModel.EditableDocument, formattedInfo, onType: boolean, extraConfig = {}): Thenable { const adjustSelection = extraConfig['adjustSelection'] === undefined || extraConfig['adjustSelection']; + const performFormatAsYouType = extraConfig['performFormatAsYouType'] === undefined || extraConfig['performFormatAsYouType']; if (formattedInfo) { const newSelectionConfig = adjustSelection ? { selection: new docModel.ModelEditSelection(formattedInfo.newIndex) } : {}; if (formattedInfo.previousText != formattedInfo.formattedText) { @@ -210,7 +211,7 @@ function _formatIndex(allText: string, range: [number, number], index: number, e } -function _formatRange(rangeText: string, allText: string, range: number[], eol: string): string { +function _formatRange(rangeText: string, allText: string, range: number[], eol: string): { "range-text": string, "range": [number, number], "new-index": number } { const d = { "range-text": rangeText, "all-text": allText, @@ -221,6 +222,6 @@ function _formatRange(rangeText: string, allText: string, range: number[], eol: const cljData = cljify(d); const result = jsify(formatTextAtRange(cljData)); if (!result["error"]) { - return result["range-text"]; + return result; } } diff --git a/src/doc-mirror/index.ts b/src/doc-mirror/index.ts index e3ca4affd..34e9fbafd 100644 --- a/src/doc-mirror/index.ts +++ b/src/doc-mirror/index.ts @@ -193,7 +193,15 @@ function processChanges(event: vscode.TextDocumentChangeEvent) { }).then(async _v => { if (event.document === vscode.window.activeTextEditor?.document) { if (performFormatAsYouType) { - await formatter.formatForward(mirroredDoc); + if (event.contentChanges.length === 1 && event.contentChanges[0].text.match(/[\[\](){}]/)) { + const change = event.contentChanges[0]; + const start = event.document.offsetAt(change.range.start); + const formatForwardIndex = formatter.indexForFormatForward(mirroredDoc); + const end = formatForwardIndex !== mirroredDoc.selection.active ? formatForwardIndex + 1 : mirroredDoc.selection.active; + await formatter.formatRangeEditableDoc(mirroredDoc, [start, end], true); + } else { + await formatter.formatForward(mirroredDoc); + } performHealthCheck = true; } if ((mirroredDoc.model.parinferReadiness.isIndentationHealthy || performHealthCheck) && performInferParens) { @@ -217,7 +225,7 @@ function processChanges(event: vscode.TextDocumentChangeEvent) { model.lineInputModel.dirtyLines = [] model.lineInputModel.insertedLines.clear() model.lineInputModel.deletedLines.clear(); - } + } //else { // model.performInferParens = !vscode.TextDocumentChangeReason.Undo; //} diff --git a/test-data/parinfer/formatter_sandbox.clj b/test-data/parinfer/formatter_sandbox.clj index 1e66ae513..784dc09f5 100644 --- a/test-data/parinfer/formatter_sandbox.clj +++ b/test-data/parinfer/formatter_sandbox.clj @@ -21,6 +21,10 @@ {:pirate-char pirate-char :pirates pirates})) +(map (fn [line] + (if (re-matches #"^ *$" line)) + (str indent-before line))) + (reg-event-fx ::foo (fn []