MCPcopy Create free account
hub / github.com/experdot/pointer / extractSnippet

Function extractSnippet

src/renderer/src/utils/globalSearchUtils.ts:100–126  ·  view source on GitHub ↗

* 提取匹配文本的上下文片段

(
  content: string,
  matchIndex: number,
  matchLength: number
)

Source from the content-addressed store, hash-verified

98 * 提取匹配文本的上下文片段
99 */
100function extractSnippet(
101 content: string,
102 matchIndex: number,
103 matchLength: number
104): { snippet: string; matchStart: number; matchEnd: number } {
105 const start = Math.max(0, matchIndex - CONTEXT_LENGTH)
106 const end = Math.min(content.length, matchIndex + matchLength + CONTEXT_LENGTH)
107
108 let snippet = content.slice(start, end)
109
110 // 添加省略号
111 if (start > 0) {
112 snippet = '...' + snippet
113 }
114 if (end < content.length) {
115 snippet = snippet + '...'
116 }
117
118 // 计算匹配在 snippet 中的位置
119 const matchStart = matchIndex - start + (start > 0 ? 3 : 0)
120 const matchEnd = matchStart + matchLength
121
122 // 替换换行符为空格
123 snippet = snippet.replace(/\n/g, ' ')
124
125 return { snippet, matchStart, matchEnd }
126}
127
128function countOccurrencesBefore(content: string, matchText: string, currentOffset: number): number {
129 let count = 0

Callers 1

searchInMessagesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected