## 再現手順 1. [[Obsidian]]を開いて[[ノート]]をpinnedする 2. [[In file search]]を実行する ## エラー内容 ```error Uncaught (in promise) TypeError: Cannot read properties of null (reading 'getValue') at InFileModal.indexingItems (plugin:obsidian-another-quick-switcher:5263:52) at InFileModal.init (plugin:obsidian-another-quick-switcher:5183:16) at showInFileDialog (plugin:obsidian-another-quick-switcher:5540:15) at Object.checkCallback (plugin:obsidian-another-quick-switcher:5624:9) at aK (app.js:1:1988271) at e.executeCommand (app.js:1:1989835) at e.onTrigger (app.js:1:1165525) at e.handleKey (app.js:1:757929) at e.handleKey (app.js:1:758042) ``` 以下の`getCurrentEditor()`がnullになってそう。 ```ts this.appHelper.getCurrentEditor()!.getValue().split("\n"); ``` ## 詳細の確認 pinnedのときだけ`null`になっていることは確認できた。 おそらく `indexingItems` の関数が実行された時点で新しい空ファイルにフォーカスが当たっている。問題は『なぜ新しい空ファイルを開いてしまうのか?』という点になる。 以下の`activeFileLeaf`はちゃんと取得できていた。 ```ts export async function showInFileDialog(app: App, settings: Settings) { if (!app.workspace.getActiveFile()) { return; } const activeFileLeaf = app.workspace.getActiveViewOfType(FileView)?.leaf ?? null; const modal = new InFileModal(app, settings, activeFileLeaf); await modal.init(); modal.open(); } ``` むしろ問題はこっちの方。 ```ts getCurrentEditor(): Editor | null { return this.getMarkdownViewInActiveLeaf()?.editor ?? null; } ``` さらに以下のコード。 ```ts getMarkdownViewInActiveLeaf(): MarkdownView | null { if (!this.unsafeApp.workspace.getActiveViewOfType(MarkdownView)) { return null; } return this.unsafeApp.workspace.getLeaf().view as MarkdownView; } ``` `this.unsafeApp.workspace.getActiveViewOfType(MarkdownView)` が `null` と思われる。pinnedノートだと`MarkdownView`では取得できないのかもしれない...? もしくは... ```diff - return this.unsafeApp.workspace.activeLeaf!.view as FileView; + return this.unsafeApp.workspace.getLeaf().view as FileView; ``` この変更は最近なので、こっちの方が怪しい。。確認したところ **こっちが原因** だった。 ## 原因の深堀 `getLeaf`について。 ```ts /** * Creates a new leaf in a leaf adjacent to the currently active leaf. * If direction is `'vertical'`, the leaf will appear to the right. * If direction is `'horizontal'`, the leaf will appear below the current leaf. * * @public */ getLeaf(newLeaf?: 'split', direction?: SplitDirection): WorkspaceLeaf; ``` `newLeaf`を指定しなくてもPinnedノートだと新しくなってしまうようだ。。以下のsignatureの方を使ってもダメ。 ```ts /** * If newLeaf is false (or not set) then an existing leaf which can be navigated * is returned, or a new leaf will be created if there was no leaf available. * * If newLeaf is `'tab'` or `true` then a new leaf will be created in the preferred * location within the root split and returned. * * If newLeaf is `'split'` then a new leaf will be created adjacent to the currently active leaf. * * If newLeaf is `'window'` then a popout window will be created with a new leaf inside. * * @public */ getLeaf(newLeaf?: PaneType | boolean): WorkspaceLeaf; ``` `activeLeaf`の方はこう書いてあるので... ```ts /** * Indicates the currently focused leaf, if one exists. * * Please avoid using `activeLeaf` directly, especially without checking whether * `activeLeaf` is null. * * The recommended alternatives are: * - If you need information about the current view, use {@link Workspace.getActiveViewOfType}. * - If you need to open a new file or navigate a view, use {@link Workspace.getLeaf}. * * @public * @deprecated - The use of this field is discouraged. */ activeLeaf: WorkspaceLeaf | null; ``` こっちをやるべきだったのか。。 > * - If you need information about the current view, use {@link Workspace.getActiveViewOfType}.