feat(neovim): ターミナル内でシェルコマンドを使って親Neovimでファイルを開く機能を実装(OSC 51エスケープシーケンス方式) #80
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.
概要
<leader>vで開いたターミナル内でe filenameやnvim filenameを実行すると、ネストしたNeovimを起動せず、親のNeovimでファイルが開かれるようになりました。問題の背景
これまで、Neovim内のターミナルバッファで
nvim filenameを実行すると、新しいNeovimインスタンスが起動してしまい、ネストした状態になっていました。これはワークフローを妨げ、不便でした。既存の
:ParentEditコマンドは動作していましたが、シェルコマンドとしてのeやnvimは実装されていませんでした。実装した解決策
アーキテクチャ
OSC 51エスケープシーケンス方式を採用し、以下の流れで動作します:
この実装は、Vimの
Tapi_関数と同じアプローチで、直接的かつ信頼性の高い方法です。主要コンポーネント
Tapi_OpenFileInParent グローバル関数
_G.Tapi_OpenFileInParent()として定義自動生成シェルスクリプト (
~/.cache/nvim/nvim_parent_open_<job_id>.sh)nvim_parent_edit()関数を定義eとnvimのエイリアスを設定自動クリーンアップ
BufDeleteautocmdでリソースを解放従来方式との比較
初回実装ではファイル監視方式を試みましたが、動作しなかったため、ユーザーからのフィードバックに基づきOSC 51エスケープシーケンス方式に変更しました。
使い方
技術的詳細
OSC 51エスケープシーケンス
OSC (Operating System Command) 51は、ターミナルとアプリケーション間で通信するための標準的なエスケープシーケンスです。
フォーマット:
実装コード(Neovim側):
実装コード(シェル側):
タイミング
パス処理
/path/to/file): そのまま使用relative/file):$(pwd)/relative/fileに変換エラーハンドリング
セキュリティ
vim.fn.fnameescape())Tapi_プレフィックスで名前空間を分離既存機能との互換性
以下の既存機能は変更なく動作します:
:ParentEdit <filename>コマンド<leader>V,<leader><leader>vなど)変更内容
.config/nvim/after/ftplugin/terminal-shell.lua(130行)Tapi_OpenFileInParentグローバル関数を追加トラブルシューティング
もし動作しない場合:
:lua print(type(_G.Tapi_OpenFileInParent))でfunctionと表示されるかls ~/.cache/nvim/nvim_parent_open_*.shalias e:messagesecho $SHELLでBashであることを確認(他のシェルは未サポート)Fixes #51
Original prompt
Fixes #51
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.