Skip to content

fix: protect code cell options from formatting #655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 1.120.0 (unreleased)

- Protect code cell options from formatting (<https://github.com/quarto-dev/quarto/pull/655>).

## 1.119.0 (Release on 2025-03-21)

- Use `QUARTO_VISUAL_EDITOR_CONFIRMED` > `PW_TEST` > `CI` to bypass (`true`) or force (`false`) the Visual Editor confirmation dialogue (<https://github.com/quarto-dev/quarto/pull/654>).
Expand Down
14 changes: 13 additions & 1 deletion apps/vscode/src/providers/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
virtualDocUri,
withVirtualDocUri,
} from "../vdoc/vdoc";
import { languageOptionComment } from "./option";


export function activateCodeFormatting(engine: MarkdownEngine) {
Expand Down Expand Up @@ -205,7 +206,18 @@ async function formatActiveCell(editor: TextEditor, engine: MarkdownEngine) {
}

async function formatBlock(doc: TextDocument, block: TokenMath | TokenCodeBlock, language: EmbeddedLanguage) {
const optionComment = languageOptionComment(language.ids[0]) + "| ";
const blockLines = lines(codeForExecutableLanguageBlock(block));
let optionLines = 0;
if (optionComment) {
for (const line of blockLines) {
if (line.startsWith(optionComment)) {
optionLines++;
} else {
break;
}
}
}
blockLines.push("");
const vdoc = virtualDocForCode(blockLines, language);
const edits = await executeFormatDocumentProvider(
Expand All @@ -226,7 +238,7 @@ async function formatBlock(doc: TextDocument, block: TokenMath | TokenCodeBlock,
);
return new TextEdit(range, edit.newText);
})
.filter(edit => blockRange.contains(edit.range));
.filter(edit => blockRange.contains(edit.range) && edit.range.start.line > block.range.start.line + optionLines);
return adjustedEdits;
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/vscode/src/providers/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function handleOptionEnter(editor: TextEditor, comment: string) {
}
}

function languageOptionComment(language: string) {
export function languageOptionComment(language: string) {
// some mappings
if (language === "ojs") {
language = "js";
Expand Down