Skip to content

fix: moving header sections when inserting snippets#328

Merged
MIchaelMainer merged 2 commits intoOneDrive:masterfrom
gavinbarron:fix/snippet-injection-index-out-of-range
Mar 19, 2026
Merged

fix: moving header sections when inserting snippets#328
MIchaelMainer merged 2 commits intoOneDrive:masterfrom
gavinbarron:fix/snippet-injection-index-out-of-range

Conversation

@gavinbarron
Copy link
Contributor

When a markdown file's tab section terminator '---' is the very last line with no trailing blank line, two issues occurred:

  1. Accessing originalFileContents[currentIndex + 1] without bounds checking caused an IndexOutOfRangeException crash.
  2. The '---' line was not included in the removable splice range, leaving a stray terminator in the output.

Fix by setting snippetsTabSectionEndLine to currentIndex + 1 as an exclusive end so the '---' line is always included in the splice range. Remove the trailing blank line peek which was only needed as a workaround for the old inclusive index and which, when combined with the +1 fix, would over-remove and displace subsequent headers like '### Response' into the tab section.

Clamp the FileSplicer offset when insertionLine falls past the end of the array after the splice (EOF edge case).

…fRangeException

When a markdown file's tab section terminator '---' is the very last
line with no trailing blank line, two issues occurred:

1. Accessing originalFileContents[currentIndex + 1] without bounds
   checking caused an IndexOutOfRangeException crash.
2. The '---' line was not included in the removable splice range,
   leaving a stray terminator in the output.

Fix by setting snippetsTabSectionEndLine to currentIndex + 1 as an
exclusive end so the '---' line is always included in the splice
range. Remove the trailing blank line peek which was only needed as
a workaround for the old inclusive index and which, when combined
with the +1 fix, would over-remove and displace subsequent headers
like '### Response' into the tab section.

Clamp the FileSplicer offset when insertionLine falls past the end
of the array after the splice (EOF edge case).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 18, 2026 17:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes edge cases in markdown snippet-tab replacement when the tab terminator (---) appears at EOF (no trailing blank line), preventing crashes and ensuring the terminator is properly removed without displacing subsequent headers.

Changes:

  • Adjust tab-section end handling to treat the --- line as part of the removable splice range (exclusive end index) and remove a bounds-unsafe “peek” for a trailing blank line.
  • Add/adjust unit tests for EOF terminator behavior and for ensuring following headers (e.g., ### Response) are not moved into the tab section.
  • Update the “comment on first line” test to assert current behavior (no injection) instead of only asserting “no throw”.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
ApiDoctor.Console/Program.cs Updates tab-section end detection to avoid out-of-range access and ensure --- is included in the splice range.
ApiDoctor.Console.UnitTests/ProcessSnippetInjectionTests.cs Refines and expands tests around EOF terminators and header displacement scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@MIchaelMainer MIchaelMainer merged commit eb5043e into OneDrive:master Mar 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants