Fixes #5273. Code block partial selection should not include fence delimiters#5280
Closed
YourRobotOverlord wants to merge 2 commits into
Closed
Conversation
…nce delimiters When copying a partial selection that starts or ends inside a fenced code block, the copied text should not include the fence delimiters unless the selection actually crosses from non-code content into the code block. Root cause: GetSelectedText() unconditionally injected the opening fence whenever it first encountered a code-block line, and unconditionally injected the closing fence at the end of the loop if still inside a code block. This meant any selection touching a code block line would include fences, even if the selection was entirely within the code block. Fix: Replace the unconditional fence injection with two tracking flags: - selectionHasNonCodeContent: set true when any non-code line is processed. Opening fence is only emitted when transitioning from non-code -> code. - codeOpenFenceEmitted: tracks whether an opening fence was actually emitted for the current code block. Closing fence is only emitted when the matching opening fence was emitted. This ensures: - Selection entirely within a code block -> no fences (regardless of position) - Selection starting before a code block -> opening fence included - Selection ending after a code block -> closing fence included Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes #5273 by adjusting MarkdownView partial-selection copy logic so fenced code-block delimiters are only included when the selection actually crosses into/out of a code-block boundary, matching expected clipboard behavior.
Changes:
- Reworked
GetSelectedText()fence injection to avoid emitting fences for selections entirely inside a code block. - Removed the unconditional trailing closing fence for selections ending mid-code-block.
- Added/updated unit tests covering the four selection scenarios described in the issue.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| Terminal.Gui/Views/Markdown/MarkdownView.Selection.cs | Updates fence injection logic for partial selections across code-block boundaries. |
| Tests/UnitTestsParallelizable/Views/Markdown/MarkdownViewSelectionTests.cs | Adds regression tests for partial selection behavior inside/around fenced code blocks. |
Comment on lines
+860
to
+868
| // Regression guard: selecting all lines of a code block starting from its first line should | ||
| // produce NO fence delimiters — the selection is entirely within the fenced region. | ||
| [Fact] | ||
| public void PartialSelection_AllLinesOfCodeBlock_FromFirstLine_NoFences () | ||
| { | ||
| // Three code lines; select only the first two to avoid triggering IsFullDocumentSelected(). | ||
| string md = "```csharp\nline A\nline B\nline C\n```"; | ||
| (IApplication app, Runnable window, Terminal.Gui.Views.Markdown mv) = CreateMv (md, width: 60, height: 10); | ||
|
|
Collaborator
Author
There was a problem hiding this comment.
@copilot apply changes based on this feedback
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Collaborator
Author
|
Moving this to #5282 - overlapping changes. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Copilot Session
cc36ca80-1ae3-4e1c-a9f7-6230152a804e
Summary
Fixes #5273. When copying a partial selection from inside a fenced code block in
MarkdownView, the copied text should not include fence delimiters (```lang/```) unless the selection actually crosses a code-block boundary.Behavior
Root Cause
In
GetSelectedText(), the opening fence was unconditionally injected when first entering a code block, and a trailing closing fence was always appended at the end. This caused partial selections inside a block to include unwanted fence delimiters.Fix
Rewrote the fence-injection logic in
MarkdownView.Selection.cs:selectionHasNonCodeContentflag: the opening fence is only emitted when entering a code block after non-code content has been seen.Tests
Added/updated tests in
MarkdownViewSelectionTests.cscovering all four scenarios:PartialSelection_InsideCodeBlock_DoesNotIncludeFenceDelimitersPartialSelection_StartBeforeCodeBlock_EndInside_HasOpeningFenceOnlyPartialSelection_StartInsideCodeBlock_EndAfter_HasClosingFenceOnlyPartialSelection_AllLinesOfCodeBlock_FromFirstLine_NoFences