* The declared type, as written, of a parameter or a local the calling * routine declares above a reference, the nearest one (`Writer: TWriter;`, * `A, Writer: TWriter`, `var Writer := TWriter.Create;`); not one a routine * nested in it declares for itself. Undefined when it declares none.
(lower: string, ref: UnresolvedRef, context: ResolutionContext)
| 1708 | * nested in it declares for itself. Undefined when it declares none. |
| 1709 | */ |
| 1710 | function localTypeText(lower: string, ref: UnresolvedRef, context: ResolutionContext): string | undefined { |
| 1711 | const lines = fileLines(ref.filePath!, context); |
| 1712 | const from = routineHeaderAt(ref.filePath!, ref.line, context); |
| 1713 | const name = escapeRegExp(lower); |
| 1714 | const mentions = new RegExp(`\\b${name}\\b`, 'i'); |
| 1715 | const declares = new RegExp(`(?:^|[^.\\w])${name}(?:\\s*,\\s*&?[A-Za-z_]\\w*)*\\s*:(?!=)\\s*([^;=)]*)`, 'i'); |
| 1716 | const created = new RegExp(`\\bvar\\s+${name}\\s*:=\\s*&?([A-Za-z_][\\w.]*)\\s*\\.\\s*create\\b`, 'i'); |
| 1717 | const elsewhere = nestedRoutineSpans(ref.filePath!, context).filter(([start, end]) => ref.line < start || ref.line > end); |
| 1718 | let found: string | undefined; |
| 1719 | for (let i = Math.max(1, from); i <= ref.line && i <= lines.length; i++) { |
| 1720 | const raw = lines[i - 1]!; |
| 1721 | if (!mentions.test(raw) || elsewhere.some(([start, end]) => i >= start && i <= end)) continue; |
| 1722 | const text = raw.replace(/\/\/.*$|\{[^}]*\}|\(\*.*?\*\)|'(?:[^']|'')*'/g, ' '); |
| 1723 | const m = declares.exec(text) ?? created.exec(text); |
| 1724 | if (m && m[1]!.trim()) found = m[1]!.trim(); |
| 1725 | } |
| 1726 | return found; |
| 1727 | } |
| 1728 | |
| 1729 | /** The lines (first, last) of the routines a file nests in others. */ |
| 1730 | function nestedRoutineSpans(filePath: string, context: ResolutionContext): Array<[number, number]> { |
no test coverage detected