@@ -58,7 +58,19 @@ interface SearchResult {
5858 afterContext : string [ ]
5959}
6060
61+ // Constants
6162const MAX_RESULTS = 300
63+ const MAX_LINE_LENGTH = 500
64+
65+ /**
66+ * Truncates a line if it exceeds the maximum length
67+ * @param line The line to truncate
68+ * @param maxLength The maximum allowed length (defaults to MAX_LINE_LENGTH)
69+ * @returns The truncated line, or the original line if it's shorter than maxLength
70+ */
71+ export function truncateLine ( line : string , maxLength : number = MAX_LINE_LENGTH ) : string {
72+ return line . length > maxLength ? line . substring ( 0 , maxLength ) + " [truncated...]" : line
73+ }
6274
6375async function getBinPath ( vscodeAppRoot : string ) : Promise < string | undefined > {
6476 const checkPath = async ( pkgFolder : string ) => {
@@ -140,7 +152,8 @@ export async function regexSearchFiles(
140152 let output : string
141153 try {
142154 output = await execRipgrep ( rgPath , args )
143- } catch {
155+ } catch ( error ) {
156+ console . error ( "Error executing ripgrep:" , error )
144157 return "No results found"
145158 }
146159 const results : SearchResult [ ] = [ ]
@@ -154,19 +167,28 @@ export async function regexSearchFiles(
154167 if ( currentResult ) {
155168 results . push ( currentResult as SearchResult )
156169 }
170+
171+ // Safety check: truncate extremely long lines to prevent excessive output
172+ const matchText = parsed . data . lines . text
173+ const truncatedMatch = truncateLine ( matchText )
174+
157175 currentResult = {
158176 file : parsed . data . path . text ,
159177 line : parsed . data . line_number ,
160178 column : parsed . data . submatches [ 0 ] . start ,
161- match : parsed . data . lines . text ,
179+ match : truncatedMatch ,
162180 beforeContext : [ ] ,
163181 afterContext : [ ] ,
164182 }
165183 } else if ( parsed . type === "context" && currentResult ) {
184+ // Apply the same truncation logic to context lines
185+ const contextText = parsed . data . lines . text
186+ const truncatedContext = truncateLine ( contextText )
187+
166188 if ( parsed . data . line_number < currentResult . line ! ) {
167- currentResult . beforeContext ! . push ( parsed . data . lines . text )
189+ currentResult . beforeContext ! . push ( truncatedContext )
168190 } else {
169- currentResult . afterContext ! . push ( parsed . data . lines . text )
191+ currentResult . afterContext ! . push ( truncatedContext )
170192 }
171193 }
172194 } catch ( error ) {
0 commit comments