MCPcopy Create free account
hub / github.com/claude-code-best/claude-code / segmentTo

Method segmentTo

src/utils/textHighlighting.ts:97–162  ·  view source on GitHub ↗
(targetVisiblePos: number)

Source from the content-addressed store, hash-verified

95 }
96
97 private segmentTo(targetVisiblePos: number): TextSegment | null {
98 if (
99 this.tokenIdx >= this.tokens.length ||
100 targetVisiblePos <= this.visiblePos
101 ) {
102 return null
103 }
104
105 const visibleStart = this.visiblePos
106
107 // Consume leading ANSI codes before first visible char
108 while (this.tokenIdx < this.tokens.length) {
109 const token = this.tokens[this.tokenIdx]!
110 if (token.type !== 'ansi') break
111 this.codes.push(token)
112 this.stringPos += token.code.length
113 this.tokenIdx++
114 }
115
116 const stringStart = this.stringPos
117 const codesStart = [...this.codes]
118
119 // Advance through tokens until we reach target
120 while (
121 this.visiblePos < targetVisiblePos &&
122 this.tokenIdx < this.tokens.length
123 ) {
124 const token = this.tokens[this.tokenIdx]!
125
126 if (token.type === 'ansi') {
127 this.codes.push(token)
128 this.stringPos += token.code.length
129 this.tokenIdx++
130 } else {
131 const charsNeeded = targetVisiblePos - this.visiblePos
132 const charsAvailable = (token as Char).value.length - this.charIdx
133 const charsToTake = Math.min(charsNeeded, charsAvailable)
134
135 this.stringPos += charsToTake
136 this.visiblePos += charsToTake
137 this.charIdx += charsToTake
138
139 if (this.charIdx >= (token as Char).value.length) {
140 this.tokenIdx++
141 this.charIdx = 0
142 }
143 }
144 }
145
146 // Empty segment (can occur when only trailing ANSI codes remain)
147 if (this.stringPos === stringStart) {
148 return null
149 }
150
151 const prefixCodes = reduceCodes(codesStart)
152 const suffixCodes = reduceCodes(this.codes)
153 this.codes = suffixCodes
154

Callers 1

segmentMethod · 0.95

Calls 2

reduceCodesFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected