Skip to content

fix: restore Insert After matching for table separator rows (Fixes #970)#983

Merged
chhoumann merged 1 commit intomasterfrom
fix/insert-after-table-separator-matching
Nov 14, 2025
Merged

fix: restore Insert After matching for table separator rows (Fixes #970)#983
chhoumann merged 1 commit intomasterfrom
fix/insert-after-table-separator-matching

Conversation

@chhoumann
Copy link
Copy Markdown
Owner

@chhoumann chhoumann commented Nov 14, 2025

Problem

In QuickAdd v1.18.1, Insert After could match table separator rows when the selector appeared at the end of the line. For example, a selector like | ----- | could successfully match a table separator row like:

| ------------- | -------- | ----- |

However, in newer versions (including v2.6.0), this functionality was broken. The matching logic only checked for:

  1. Exact matches
  2. Prefix matches (line starts with target)

This meant that selectors appearing at the end of lines (like in table separator rows) would fail to match, causing the error:

Insert After Error: Unable to find insert after line in file

Solution

This PR restores the legacy matching behavior by adding substring matching support. The findInsertAfterIndex function now checks three scenarios in priority order:

  1. Exact match: Line exactly equals the target
  2. Prefix match: Line starts with target followed by only whitespace
  3. Substring match: Target appears anywhere in the line followed by only whitespace (NEW)

The substring match restores the ability to match table separator rows where the selector appears at the end of the line, matching the behavior from v1.18.1.

Changes

  • Updated findInsertAfterIndex in captureChoiceFormatter.ts to support substring matching
  • Added comprehensive comments explaining the matching logic and priority order
  • Updated test helper function to match the new implementation
  • Added regression tests for table separator row matching scenarios

Testing

Fixes #970

Summary by CodeRabbit

  • Bug Fixes
    • Improved insertion logic to accurately handle content insertion when targets appear mid-line, not just at line starts.
    • Enhanced detection for table structures, ensuring correct row insertion in markdown tables.

Restore legacy behavior where Insert After selectors can match when
the target appears anywhere in a line (not just at the start), as
long as only whitespace follows it. This fixes the case where
selectors like '| ----- |' can match table separator rows like
'| ------------- | -------- | ----- |' where the target appears
at the end of the line.

The matching logic now checks three scenarios in priority order:
1. Exact match (line equals target)
2. Prefix match (line starts with target + whitespace)
3. Substring match (target appears anywhere + whitespace after)

This restores functionality that worked in v1.18.1 but was broken
in newer versions.

Fixes #970
@vercel
Copy link
Copy Markdown

vercel Bot commented Nov 14, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
quickadd Ready Ready Preview Nov 14, 2025 11:54am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Nov 14, 2025

Walkthrough

The pull request enhances the findInsertAfterIndex function in the formatter to support matching targets that appear mid-line with whitespace-only suffixes. This fixes table separator row matching (e.g., | ----- |), extending the matching strategy beyond exact and prefix matches to include substring matches with trailing whitespace conditions.

Changes

Cohort / File(s) Summary
Test Coverage for Table Separator Matching
src/formatters/capture-insert-after-linkcurrent.test.ts
Added tests validating insertion after markdown table separators when target appears at end-of-line and when matching exact table separator rows.
Enhanced Insert-After Matching Logic
src/formatters/captureChoiceFormatter.ts
Restructured findInsertAfterIndex matching rules: prefix match now explicitly continues to next iteration; introduced new substring-match rule that accepts matches only when suffix is whitespace-only; stores broader substring matches as fallback. Minor JSDoc formatting adjustment.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Caller
    participant findInsertAfterIndex
    participant LineEval as Line Evaluation

    Caller->>findInsertAfterIndex: findInsertAfterIndex(lines, target)
    
    loop For each line
        findInsertAfterIndex->>LineEval: Check exact match or line starts with target
        alt Exact/Prefix Match
            LineEval-->>findInsertAfterIndex: Match found
            findInsertAfterIndex->>findInsertAfterIndex: continue to next iteration
        else No Exact/Prefix Match
            LineEval->>LineEval: Search for target substring in line
            alt Substring found
                LineEval->>LineEval: Extract suffix after target
                alt Suffix is whitespace-only
                    LineEval-->>findInsertAfterIndex: Valid match (return this index)
                else Suffix has non-whitespace
                    LineEval->>LineEval: Store as fallback (first broad match)
                    LineEval-->>findInsertAfterIndex: Continue searching
                end
            else Substring not found
                LineEval-->>findInsertAfterIndex: Continue to next line
            end
        end
    end
    
    findInsertAfterIndex-->>Caller: Return matched index or fallback index
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • The change is localized to a single function with a clear, iterative matching strategy.
  • The logic follows a consistent pattern: exact → prefix → substring match, each with appropriate conditions.
  • Test additions directly validate the new substring-matching behavior and provide clear usage examples.
  • Primary attention: verify the whitespace-suffix detection logic is correct for edge cases (table rows, trailing spaces) and ensure the fallback mechanism doesn't mask legitimate match failures.

Poem

🐰 A rabbit's table row delight!
Where separators hide in plain sight,
Mid-line matches now take their place,
With whitespace trailing—the perfect grace. 🥕
Insertion flows where pipes align,
In markdown gardens, tables shine!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: restoring Insert After matching for table separator rows, and directly references the fixed issue (#970).
Linked Issues check ✅ Passed All coding objectives from issue #970 are met: the PR restores substring matching in findInsertAfterIndex to match table separators, adds comprehensive tests, and fixes the regression preventing end-of-line selector matching.
Out of Scope Changes check ✅ Passed All changes are directly related to restoring Insert After matching for table separators. The modifications to findInsertAfterIndex and test additions align with the scope defined in issue #970.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/insert-after-table-separator-matching

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f195c06 and f61b0b8.

📒 Files selected for processing (2)
  • src/formatters/capture-insert-after-linkcurrent.test.ts (2 hunks)
  • src/formatters/captureChoiceFormatter.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/formatters/capture-insert-after-linkcurrent.test.ts (1)
src/formatters/captureChoiceFormatter.ts (2)
  • findInsertAfterIndex (155-198)
  • insertTextAfterPositionInBody (349-366)
🔇 Additional comments (4)
src/formatters/captureChoiceFormatter.ts (2)

22-26: LGTM: Improved JSDoc formatting.

The reformatted comment enhances readability while preserving the explanation about preventing double Templater execution.


166-194: Excellent implementation of substring matching to restore legacy behavior.

The three-tier matching priority (exact → prefix → substring) is well-designed and clearly documented. The continue statement on line 178 is critical—it prevents redundant substring checking when a prefix match is already handled, making the control flow explicit and efficient.

The substring matching logic correctly handles the reported issue where "| ----- |" needs to match table separator rows like "| ------------- | -------- | ----- |" by finding the target anywhere in the line and verifying the suffix contains only whitespace.

src/formatters/capture-insert-after-linkcurrent.test.ts (2)

42-50: Test helper correctly mirrors the implementation.

The updated helper function accurately reflects the changes in captureChoiceFormatter.ts, including the critical continue statement and the new substring matching logic. This ensures tests validate the actual behavior.


160-201: Excellent regression test coverage for issue #970.

The test suite directly validates the fix with two well-designed scenarios:

  1. Substring matching at line end (lines 162-185): Tests the core regression where "| ----- |" matches a separator row where it appears at the end, verifying both the match index and the insertion output.

  2. Exact matching baseline (lines 187-200): Ensures exact matching still works when the line is literally "| ----- |", confirming the priority ordering remains correct.

These tests provide strong protection against future regressions.


Comment @coderabbitai help to get the list of available commands and usage tips.

@chhoumann chhoumann merged commit 1393e6a into master Nov 14, 2025
4 checks passed
@chhoumann chhoumann deleted the fix/insert-after-table-separator-matching branch November 14, 2025 12:02
github-actions Bot pushed a commit that referenced this pull request Nov 14, 2025
# [2.8.0](2.7.0...2.8.0) (2025-11-14)

### Bug Fixes

* restore Insert After matching for table separator rows ([#983](#983)) ([1393e6a](1393e6a)), closes [#970](#970)
* support frontmatter tags in getFieldValues filtering ([#980](#980)) ([c9de468](c9de468)), closes [#927](#927)

### Features

* Add embed replacement option for link placement ([#984](#984)) ([06a77a3](06a77a3)), closes [#893](#893)
* add provider-native model discovery ([#982](#982)) ([f195c06](f195c06))
* add textarea type for advanced script settings ([#981](#981)) ([dc9a650](dc9a650))
* add update modal settings for major releases only ([#985](#985)) ([d63f8c9](d63f8c9)), closes [#447](#447)
* add write to top of file switch for capture to active file ([#986](#986)) ([5361e6c](5361e6c)), closes [#248](#248) [#248](#248)
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 2.8.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Insert After can't match | ----- | to append new table row

1 participant