Skip to content

fix: clamp endLine to file length in line-based editor replacement#1066

Open
octo-patch wants to merge 1 commit intocodexu:devfrom
octo-patch:fix/issue-1054-clamp-line-range
Open

fix: clamp endLine to file length in line-based editor replacement#1066
octo-patch wants to merge 1 commit intocodexu:devfrom
octo-patch:fix/issue-1054-clamp-line-range

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #1054

Problem

When the AI uses replace_editor_content with line-based mode (e.g. startLine: 1, endLine: totalLines for a document-wide rewrite), a minor line-count discrepancy between what get_editor_content reported and what editor.getMarkdown() returns at replacement time causes an uncaught error:

Uncaught Error: 无效的行号范围: 1-24,文件共 22 行

This happens because editor.getMarkdown() normalises the content (e.g. strips trailing blank lines) so the actual line count can be slightly lower than totalLines the AI received earlier.

Solution

Instead of throwing when endLine exceeds the file length, clamp endLine to the last line of the file. This preserves the intent of the operation ("replace up to the end of the document") without crashing.

The same fix is applied to deleteLinesInRange for consistency.

Testing

  • Open a markdown file, ask the AI to rewrite the whole document.
  • Previously this would hang/crash with the "无效的行号范围" error.
  • After the fix, the replacement completes successfully even when the AI's endLine is slightly larger than the current line count.

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.

[bug] replace_editor_content 执行卡死问题

1 participant