## Issue
- [Highlight in "link search"/"backlink searh" · Issue \#201 · tadashi\-aikawa/obsidian\-another\-quick\-switcher](https://github.com/tadashi-aikawa/obsidian-another-quick-switcher/issues/201)
## TODO
- [x] ハイライトのロジック確認
- grep searchを参考にする
- [x] [[Backlink search]]のハイライト
- [*] SuggestionItemにマッチング情報を含める
- 部分一致だから問題ない気がする ([[Backlink search]]の性質上)
- 今が同一行に2回以上出現するケースに対応できていない
- これを対応するのは見送り... かなりキツイので
- 1つ目のだけハイライトするで一旦誤魔化すか...
- いや、renderのときに検索させてもいいのかも?
- 自分でやるのはばかばかしいな...やはり取得したい
- [-] マッチング情報をもとにハイライトする
- ダメだ.. そもそもすべて違うファイルだから、候補ができてから頑張るしかないわ..
- [*] 初回のロードだけ遅くなる問題が若干気になる。。
- 本対応の影響ではない気がするので一旦スルー
## ハイライトロジック
`GrepModal.ts`より
```ts
// 行の残り(未処理)
let restLine = item.line;
// マッチング情報(複数ある)
item.submatches.forEach((x) => {
// マッチした対象テキストの位置を取得
const i = restLine.indexOf(x.match.text);
// マッチした手前のテキストを取得
const before = restLine.slice(0, i);
// 長すぎる場合はtrim
descriptionDiv.createSpan({
text: trimLineByEllipsis(
before,
// 表示する最大文字数
this.settings.maxDisplayLengthAroundMatchedWord
),
});
// マッチしたテキストをハイライト
descriptionDiv.createSpan({
text: x.match.text,
cls: "another-quick-switcher__hit_word",
});
// restlineの調整
restLine = restLine.slice(i + x.match.text.length);
});
// 最後の調整
descriptionDiv.createSpan({
text: trimLineByEllipsis(
restLine,
// 表示する最大文字数
this.settings.maxDisplayLengthAroundMatchedWord
),
});
```
### 変更後のロジック
一部抜粋。
```ts
const descriptionDiv = createDiv({
cls: "another-quick-switcher__backlink__item__description",
});
let restLine = item.line;
let offset = 0;
Array.from(restLine.matchAll(this.originFileBaseNameRegExp))
.map((x) => x.index!)
.forEach((index) => {
const before = restLine.slice(0, index - offset);
descriptionDiv.createSpan({
text: trimLineByEllipsis(
before,
this.settings.maxDisplayLengthAroundMatchedWord
),
});
descriptionDiv.createSpan({
text: this.originFileBaseName,
cls: "another-quick-switcher__hit_word",
});
offset = index - offset + this.originFileBaseName.length;
restLine = restLine.slice(offset);
});
descriptionDiv.createSpan({
text: trimLineByEllipsis(
restLine,
this.settings.maxDisplayLengthAroundMatchedWord
),
});
if (item.order! < 9) {
const hotKeyGuide = createSpan({
cls: "another-quick-switcher__backlink__item__hot-key-guide",
text: `${item.order! + 1}`,
});
descriptionsDiv.appendChild(hotKeyGuide);
}
```
## パフォーマンス
`renderSuggestion`の速度は`0.05ms/1件`程度。たとえば256件表示して`0.1ms/件`かかるとしても、全体で0.2秒程度のためボトルネックにはなりえない。
一方、対象の多い[[ノート]]の[[Backlink search]]をすると数秒固まる事象が発生していた。今回の対応によるものかは未確認。
ファイルIOに関係してそう...? cacheLoadしているので平気と信じたいが...。一旦スルーで。