diff --git a/apps/vscode/CHANGELOG.md b/apps/vscode/CHANGELOG.md index 3232b0b7..0f326b55 100644 --- a/apps/vscode/CHANGELOG.md +++ b/apps/vscode/CHANGELOG.md @@ -2,6 +2,8 @@ ## 1.120.0 (unreleased) +- Protect code cell options from formatting (). + ## 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 (). diff --git a/apps/vscode/src/providers/format.ts b/apps/vscode/src/providers/format.ts index acb0c691..f6ab48d5 100644 --- a/apps/vscode/src/providers/format.ts +++ b/apps/vscode/src/providers/format.ts @@ -47,6 +47,7 @@ import { virtualDocUri, withVirtualDocUri, } from "../vdoc/vdoc"; +import { languageOptionComment } from "./option"; export function activateCodeFormatting(engine: MarkdownEngine) { @@ -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( @@ -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; } } diff --git a/apps/vscode/src/providers/option.ts b/apps/vscode/src/providers/option.ts index eff92603..885322ec 100644 --- a/apps/vscode/src/providers/option.ts +++ b/apps/vscode/src/providers/option.ts @@ -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";