Skip to content

Commit

Permalink
Don't range format inside strings (see #720)
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Dec 8, 2021
1 parent 7a2d537 commit 15f9b3c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/calva-fmt/src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export function formatPositionEditableDoc(document: docModel.EditableDocument, o

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
3 changes: 2 additions & 1 deletion src/doc-mirror/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ function processChanges(event: vscode.TextDocumentChangeEvent) {
if (event.contentChanges.length > 0) {
const mirroredDoc = documents.get(event.document);
const model = mirroredDoc.model;
const cursor = mirroredDoc.getTokenCursor();
const parinferOn = formatConfig.getConfig()["infer-parens-as-you-type"];
const formatAsYouTypeOn = formatConfig.getConfig()["format-as-you-type"];
const performFormatAsYouType = formatAsYouTypeOn && event.reason != vscode.TextDocumentChangeReason.Undo;
Expand All @@ -194,7 +195,7 @@ function processChanges(event: vscode.TextDocumentChangeEvent) {
}).then(async _v => {
if (event.document === vscode.window.activeTextEditor?.document) {
if (performFormatAsYouType && !mirroredDoc.rangeFormatted) {
if (event.contentChanges.length === 1 && event.contentChanges[0].text.match(/[\[\](){}]/)) {
if (event.contentChanges.length === 1 && event.contentChanges[0].text.match(/[\[\](){}]/) && !cursor.withinString()) {
const change = event.contentChanges[0];
const start = event.document.offsetAt(change.range.start);
const formatForwardIndex = formatter.indexForFormatForward(mirroredDoc);
Expand Down
3 changes: 3 additions & 0 deletions test-data/parinfer/formatter_sandbox.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
:vig 3.0
:rex 5.0
:lun 4.0}})
"foo
bar"

(defn hello
([world]
Expand Down

0 comments on commit 15f9b3c

Please sign in to comment.