* The class the nearest routine header at or above a line names * (`procedure TFoo.Bar;` → `TFoo`), or null when it is a free routine's, or * the unit's code resumes there: after an `end;` that starts its line, or at * `initialization`.
(filePath: string, line: number, context: ResolutionContext)
| 295 | * `initialization`. |
| 296 | */ |
| 297 | function headerClassAt(filePath: string, line: number, context: ResolutionContext): string | null { |
| 298 | const memo = memos(context).headerClasses; |
| 299 | let headers = memo.get(filePath); |
| 300 | if (!headers) { |
| 301 | headers = []; |
| 302 | const lines = fileLines(filePath, context); |
| 303 | const header = /^(?:class\s+)?(?:procedure|function|constructor|destructor|operator)\s+([A-Za-z_][\w.<>, ]*?)\s*[(:;]/i; |
| 304 | for (let i = 0; i < lines.length; i++) { |
| 305 | const text = lines[i]!; |
| 306 | // Each starts its line with one of c, d, e, f, i, o, p (either case). |
| 307 | const first = text.charCodeAt(0) | 0x20; |
| 308 | if (first !== 0x63 && first !== 0x64 && first !== 0x65 && first !== 0x66 && first !== 0x69 && first !== 0x6f && first !== 0x70) continue; |
| 309 | if (/^(?:end;|(?:initialization|finalization)\b)/i.test(text)) { |
| 310 | headers.push({ line: i + 1, classQn: null, routine: false }); |
| 311 | continue; |
| 312 | } |
| 313 | const m = header.exec(text); |
| 314 | if (!m) continue; |
| 315 | const parts = m[1]!.replace(/<[^>]*>/g, '').replace(/\s+/g, '').split('.'); |
| 316 | headers.push({ line: i + 1, classQn: parts.length > 1 ? parts.slice(0, -1).join('::') : null, routine: true }); |
| 317 | } |
| 318 | memo.set(filePath, headers); |
| 319 | } |
| 320 | let lo = 0; |
| 321 | let hi = headers.length - 1; |
| 322 | let hit: string | null = null; |
| 323 | while (lo <= hi) { |
| 324 | const mid = (lo + hi) >> 1; |
| 325 | if (headers[mid]!.line <= line) { |
| 326 | hit = headers[mid]!.classQn; |
| 327 | lo = mid + 1; |
| 328 | } else { |
| 329 | hi = mid - 1; |
| 330 | } |
| 331 | } |
| 332 | return hit; |
| 333 | } |
| 334 | |
| 335 | // --------------------------------------------------------------------------- |
| 336 | // Scope lookup |