MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / blankRustCode

Function blankRustCode

src/resolution/rust-scopes.ts:111–185  ·  view source on GitHub ↗
(src: string)

Source from the content-addressed store, hash-verified

109 * character literal.
110 */
111export function blankRustCode(src: string): string {
112 const n = src.length;
113 const parts: string[] = [];
114 let last = 0;
115 // `src[from, to)` as spaces, its newlines kept; the text before it as it is.
116 const blank = (from: number, to: number): void => {
117 if (to <= from) return;
118 parts.push(src.slice(last, from));
119 const chunk = src.slice(from, to);
120 parts.push(chunk.includes('\n') ? chunk.replace(/[^\n]/g, ' ') : ' '.repeat(to - from));
121 last = to;
122 };
123 const special = /[/"']/g;
124 for (let m = special.exec(src); m; m = special.exec(src)) {
125 const i = m.index;
126 const c = src[i];
127 const d = src[i + 1];
128 if (c === '/') {
129 if (d === '/') {
130 const end = src.indexOf('\n', i);
131 const to = end < 0 ? n : end;
132 blank(i, to);
133 special.lastIndex = to;
134 } else if (d === '*') {
135 let depth = 1;
136 let j = i + 2;
137 while (j < n && depth > 0) {
138 if (src[j] === '/' && src[j + 1] === '*') { depth++; j += 2; }
139 else if (src[j] === '*' && src[j + 1] === '/') { depth--; j += 2; }
140 else j++;
141 }
142 blank(i, j);
143 special.lastIndex = j;
144 }
145 continue;
146 }
147 if (c === '"') {
148 // A raw string: `r"…"`, `r#"…"#`, also after a `b` or `c` prefix —
149 // the `r` no end of a longer name.
150 let k = i - 1;
151 while (k >= 0 && src[k] === '#') k--;
152 let p = k - 1;
153 if (src[p] === 'b' || src[p] === 'c') p--;
154 if (src[k] === 'r' && (p < 0 || !isIdentChar(src[p]))) {
155 const close = `"${'#'.repeat(i - 1 - k)}`;
156 const end = src.indexOf(close, i + 1);
157 const to = end < 0 ? n : end;
158 blank(i + 1, to);
159 special.lastIndex = Math.min(n, to + close.length);
160 continue;
161 }
162 let j = i + 1;
163 while (j < n && src[j] !== '"') j += src[j] === '\\' ? 2 : 1;
164 blank(i + 1, Math.min(j, n));
165 special.lastIndex = Math.min(n, j + 1);
166 continue;
167 }
168 // `'\n'`, `'\u{1F600}'`; `'x'`; a character outside the BMP; else a lifetime or a label.

Callers 3

rustModDeclarationFunction · 0.90
inCfgTestModuleFunction · 0.90
readRustScopesFunction · 0.85

Calls 5

isIdentCharFunction · 0.85
pushMethod · 0.80
blankFunction · 0.70
execMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected