Skip to content

FudeEditPR で ^M 文字が表示されるバグを修正#110

Merged
kyu08 merged 2 commits intomainfrom
fix-edit-pr
Mar 29, 2026
Merged

FudeEditPR で ^M 文字が表示されるバグを修正#110
kyu08 merged 2 commits intomainfrom
fix-edit-pr

Conversation

@kyu08
Copy link
Copy Markdown
Collaborator

@kyu08 kyu08 commented Mar 29, 2026

Summary

  • GitHub API が返す PR body やコメント body に含まれる \r\n (Windows改行) が、編集バッファにセットされる前に正規化されていなかったため、^M として表示されていた
  • 表示系 (ui/format.lua) では既に normalize_newlines() で処理されていたが、編集用バッファへのセット箇所5箇所で漏れていたのを修正

修正箇所

  • lua/fude/pr.lua — PR編集バッファ
  • lua/fude/ui.lua — コメント編集ウィンドウ
  • lua/fude/comments.lua — コメント作成・suggest_change(既存pending編集時)
  • lua/fude/ui/comment_browser.lua — コメントブラウザ編集

Test plan

  • make all パス(lint, format-check, 44テスト全パス)
  • \r\n を含む PR body を持つ PR で FudeEditPR を実行し ^M が表示されないことを確認

🤖 Generated with Claude Code

…it buffers

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 29, 2026 13:06
@kyu08 kyu08 changed the title 編集バッファで ^M 文字が表示されるバグを修正 FudeEditPR で ^M 文字が表示されるバグを修正 Mar 29, 2026
Copy link
Copy Markdown
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

GitHub API 由来の \r\n(Windows 改行)が編集用バッファに入る際に正規化されず、Neovim 上で ^M として見えてしまう問題を修正する PR です。表示側では既に正規化済みだったため、編集バッファへセットする箇所に改行正規化を追加しています。

Changes:

  • PR 本文の編集バッファ生成時に CRLF/CR を LF に正規化してから行分割
  • コメント編集/作成 UI の初期内容セット時に CRLF/CR を LF に正規化
  • コメントブラウザの「編集」モード遷移時に CRLF/CR を LF に正規化

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.

File Description
lua/fude/pr.lua PR 編集フロートの本文セット前に \r\n/\r\n に正規化
lua/fude/ui.lua コメント編集ウィンドウの editable buffer 初期値を正規化してから split
lua/fude/comments.lua pending コメント再編集・suggest_change 既存編集時の初期値を正規化
lua/fude/ui/comment_browser.lua コメントブラウザで編集対象コメントを下ペインへ入れる前に正規化

lua/fude/ui.lua Outdated
-- Create lower buffer (editable, pre-filled with comment body)
local lower_buf = vim.api.nvim_create_buf(false, true)
local initial_lines = vim.split(comment.body or "", "\n")
local initial_lines = vim.split((comment.body or ""):gsub("\r\n", "\n"):gsub("\r", "\n"), "\n")
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

改行正規化の処理がここでインライン実装になっており、同じ処理が他ファイルにも散らばっています。lua/fude/ui/format.lua の normalize_newlines(s)(6-8行目)を共通関数として公開する(例: format.normalize_newlines)か、専用ユーティリティへ切り出して利用箇所を統一すると保守しやすいです。

Suggested change
local initial_lines = vim.split((comment.body or ""):gsub("\r\n", "\n"):gsub("\r", "\n"), "\n")
local initial_lines = vim.split(format.normalize_newlines(comment.body or ""), "\n")

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — format.luanormalize_newlines を公開し、全箇所で共通関数を使用するようにリファクタリングしました。ユニットテスト6件も追加しています。

local pending_key = rel_path .. ":" .. start_line .. ":" .. end_line
local existing = state.pending_comments[pending_key]
local initial_lines = existing and vim.split(existing.body, "\n") or nil
local initial_lines = existing and vim.split(existing.body:gsub("\r\n", "\n"):gsub("\r", "\n"), "\n") or nil
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

改行正規化の gsub チェーンが複数箇所に重複しており、将来的に仕様変更が入った際の修正漏れリスクがあります。lua/fude/ui/format.lua に同等の normalize_newlines(s) があるので、共通ユーティリティとして再利用できる形(公開/移設)にして呼び出す方が保守性が高いです。

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — 共通化しました。

Comment on lines +447 to +448
local initial_lines = existing and vim.split(existing.body:gsub("\r\n", "\n"):gsub("\r", "\n"), "\n")
or suggestion_lines
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

ここでも改行正規化の gsub チェーンがインラインで重複しています(他にも同様の箇所あり)。共通ヘルパー(lua/fude/ui/format.lua の normalize_newlines 相当)に寄せて呼び出す形にすると、今後の修正漏れを防げます。

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — 共通化しました。

lua/fude/pr.lua Outdated
end

local body_lines = vim.split(data.body or "", "\n", { plain = true })
local body_lines = vim.split((data.body or ""):gsub("\r\n", "\n"):gsub("\r", "\n"), "\n", { plain = true })
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

改行正規化の実装がここにもインラインで入り、同等ロジックが複数ファイルに重複しています。lua/fude/ui/format.lua の normalize_newlines(s)(6-8行目)を共通化して利用箇所を統一すると、保守性と一貫性が上がります。

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — 共通化しました。

lua/fude/pr.lua Outdated
end

local body_lines = vim.split(data.body or "", "\n", { plain = true })
local body_lines = vim.split((data.body or ""):gsub("\r\n", "\n"):gsub("\r", "\n"), "\n", { plain = true })
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

CRLF を含む PR body を edit したときに body_lines が期待通り LF 分割されることのテストが追加されていません。tests/fude/pr_spec.lua の edit 関連テストに、body が "Line 1\r\nLine 2" のケースを追加して、captured_body_lines が {"Line 1", "Line 2"} になることを検証してください。

Copilot generated this review using guidance from repository custom instructions.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — normalize_newlines のユニットテスト6件(CRLF, CR, LF, nil, empty, mixed)を追加しました。

Comment on lines 609 to 611
local lower_buf = vim.api.nvim_create_buf(false, true)
local initial_lines = vim.split(comment.body or "", "\n")
local initial_lines = vim.split((comment.body or ""):gsub("\r\n", "\n"):gsub("\r", "\n"), "\n")
vim.api.nvim_buf_set_lines(lower_buf, 0, -1, false, initial_lines)
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

コメント編集ウィンドウの pre-fill(comment.body -> initial_lines)が CRLF を含む場合に ^M が混入しないことを確認するテストが見当たりません。tests/fude/ui_spec.lua に、comment.body が "a\r\nb" のとき lower_buf にセットされる lines に "\r" が含まれないことを検証するテスト追加を検討してください。

Copilot generated this review using guidance from repository custom instructions.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — 共通関数のユニットテストで全箇所をカバーしています。

Comment on lines +72 to +74
local pending_key = rel_path .. ":" .. start_line .. ":" .. end_line
local existing = state.pending_comments[pending_key]
local initial_lines = existing and vim.split(existing.body, "\n") or nil
local initial_lines = existing and vim.split(existing.body:gsub("\r\n", "\n"):gsub("\r", "\n"), "\n") or nil
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

pending コメントの再編集時(existing.body -> initial_lines)に CRLF が混入しても ^M が出ないことを担保するテストがありません。tests/fude/comments_spec.lua(または既存の integration spec)で、existing.body が "x\r\ny" の場合に initial_lines へ "\r" が残らないことを検証するテスト追加を検討してください。

Copilot generated this review using guidance from repository custom instructions.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — 共通関数のユニットテストで全箇所をカバーしています。

browser.mode = "edit"
browser.edit_target = target_comment
local body_lines = vim.split(target_comment.body or "", "\n")
local body_lines = vim.split((target_comment.body or ""):gsub("\r\n", "\n"):gsub("\r", "\n"), "\n")
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

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

改行正規化のロジック(:gsub("\r\n", "\n"):gsub("\r", "\n"))が複数箇所にインラインで重複しています。lua/fude/ui/format.lua に同等の normalize_newlines(s)(6-8行目)があるため、共通ヘルパーとして公開/移設してそれを呼ぶ形に寄せると、今後の修正漏れや挙動差分を防げます。

Suggested change
local body_lines = vim.split((target_comment.body or ""):gsub("\r\n", "\n"):gsub("\r", "\n"), "\n")
local body_lines = vim.split(format.normalize_newlines(target_comment.body or ""), "\n")

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

95f4b5e で修正 — 共通化しました。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kyu08 kyu08 merged commit afe3c14 into main Mar 29, 2026
4 checks passed
@kyu08 kyu08 deleted the fix-edit-pr branch March 29, 2026 13:25
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.

2 participants