MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / buildWhitespaceTolerantRegex

Function buildWhitespaceTolerantRegex

src/core/tools/EditFileTool.ts:87–116  ·  view source on GitHub ↗
(oldLF: string)

Source from the content-addressed store, hash-verified

85}
86
87function buildWhitespaceTolerantRegex(oldLF: string): RegExp {
88 if (oldLF === "") {
89 // Never match empty string
90 return new RegExp("(?!)", "g")
91 }
92
93 const parts = oldLF.match(/(\s+|\S+)/g) ?? []
94 const whitespacePatternForRun = (run: string): string => {
95 // If the whitespace run includes a newline, allow matching any whitespace (including newlines)
96 // to tolerate wrapping changes across lines.
97 if (run.includes("\n")) {
98 return "\\s+"
99 }
100
101 // Otherwise, limit matching to horizontal whitespace so we don't accidentally consume
102 // line breaks that precede indentation.
103 return "[\\t ]+"
104 }
105
106 const pattern = parts
107 .map((part) => {
108 if (/^\s+$/.test(part)) {
109 return whitespacePatternForRun(part)
110 }
111 return escapeRegExp(part)
112 })
113 .join("")
114
115 return new RegExp(pattern, "g")
116}
117
118function buildTokenRegex(oldLF: string): RegExp {
119 const tokens = oldLF.split(/\s+/).filter(Boolean)

Callers 1

executeMethod · 0.85

Calls 2

whitespacePatternForRunFunction · 0.85
escapeRegExpFunction · 0.70

Tested by

no test coverage detected