Skip to content

Commit

Permalink
Format pasted text as-you-type
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Nov 21, 2021
1 parent 21a76be commit de643b4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions src/calva-fmt/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}
Expand Down Expand Up @@ -122,17 +122,18 @@ export function formatPositionInfoEditableDoc(document: docModel.EditableDocumen
}

export function formatRangeEditableDoc(document: docModel.EditableDocument, range: [number, number], onType: boolean = false, extraConfig = {}): Thenable<boolean> {
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<boolean> {
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<boolean> {
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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}
12 changes: 10 additions & 2 deletions src/doc-mirror/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
//}
Expand Down
4 changes: 4 additions & 0 deletions test-data/parinfer/formatter_sandbox.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down

0 comments on commit de643b4

Please sign in to comment.