-
Notifications
You must be signed in to change notification settings - Fork 40
Fix cell formatting issues #754
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
lionel-
wants to merge
3
commits into
main
Choose a base branch
from
bugfix/vscode-format-cell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,9 +138,13 @@ class FormatCellCommand implements Command { | |
const edits = await formatActiveCell(editor, this.engine_); | ||
if (edits) { | ||
editor.edit((editBuilder) => { | ||
edits.forEach((edit) => { | ||
editBuilder.replace(edit.range, edit.newText); | ||
}); | ||
// Sort edits by descending start position to avoid range shifting issues | ||
edits | ||
.slice() | ||
.sort((a, b) => b.range.start.compareTo(a.range.start)) | ||
.forEach((edit) => { | ||
editBuilder.replace(edit.range, edit.newText); | ||
}); | ||
}); | ||
} else { | ||
window.showInformationMessage( | ||
|
@@ -204,15 +208,20 @@ async function formatActiveCell(editor: TextEditor, engine: MarkdownEngine) { | |
} | ||
|
||
async function formatBlock(doc: TextDocument, block: TokenMath | TokenCodeBlock, language: EmbeddedLanguage) { | ||
const blockLines = lines(codeForExecutableLanguageBlock(block)); | ||
blockLines.push(""); | ||
// Create virtual document containing the block | ||
const blockLines = lines(codeForExecutableLanguageBlock(block, false)); | ||
const vdoc = virtualDocForCode(blockLines, language); | ||
|
||
const edits = await executeFormatDocumentProvider( | ||
vdoc, | ||
doc, | ||
formattingOptions(doc.uri, vdoc.language) | ||
); | ||
|
||
if (edits) { | ||
// Because we format with the block code copied in an empty virtual | ||
// document, we need to adjust the ranges to match the edits to the block | ||
// cell in the original file. | ||
const blockRange = new Range( | ||
new Position(block.range.start.line, block.range.start.character), | ||
new Position(block.range.end.line, block.range.end.character) | ||
|
@@ -224,8 +233,17 @@ async function formatBlock(doc: TextDocument, block: TokenMath | TokenCodeBlock, | |
new Position(edit.range.end.line + block.range.start.line + 1, edit.range.end.character) | ||
); | ||
return new TextEdit(range, edit.newText); | ||
}) | ||
.filter(edit => blockRange.contains(edit.range)); | ||
}); | ||
|
||
// Bail if any edit is out of range. We used to filter these edits out but | ||
// this could bork the cell. | ||
if (edits.some(edit => !blockRange.contains(edit.range))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated but I'm pretty sure applying edits partially can bork our cells 😬 |
||
window.showInformationMessage( | ||
"Formatting edits were out of range and could not be applied to the code cell." | ||
); | ||
return []; | ||
} | ||
|
||
return adjustedEdits; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated but I thought we should make this robust to providers returning unsorted edits.