|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright © 2025 TON Studio |
| 3 | +import type {Node as SyntaxNode} from "web-tree-sitter" |
| 4 | +import {RecursiveVisitor} from "@server/languages/tact/psi/visitor" |
| 5 | +import {NamedNode, TlbNode} from "./TlbNode" |
| 6 | +import {TlbReference} from "./TlbReference" |
| 7 | +import type {TlbFile} from "./TlbFile" |
| 8 | + |
| 9 | +/** |
| 10 | + * Describes a scope that contains all possible uses of a certain symbol. |
| 11 | + */ |
| 12 | +export interface SearchScope { |
| 13 | + toString(): string |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Describes the scope described by some AST node; the search for usages will be |
| 18 | + * performed only within this node. |
| 19 | + * |
| 20 | + * For example, the scope for a local variable will be the block in which it is defined. |
| 21 | + */ |
| 22 | +export class LocalSearchScope implements SearchScope { |
| 23 | + public constructor(public node: SyntaxNode) {} |
| 24 | + |
| 25 | + public toString(): string { |
| 26 | + return `LocalSearchScope:\n${this.node.text}` |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Describes a scope consisting of one or more files. |
| 32 | + * |
| 33 | + * For example, the scope of a global function from the standard library is all project files. |
| 34 | + */ |
| 35 | +export class GlobalSearchScope implements SearchScope { |
| 36 | + public constructor(public files: TlbFile[]) {} |
| 37 | + |
| 38 | + public toString(): string { |
| 39 | + return `GlobalSearchScope:\n${this.files.map(f => `- ${f.uri}`).join("\n")}` |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export interface FindTlbReferenceOptions { |
| 44 | + /** |
| 45 | + * if true, the first element of the result contains the definition |
| 46 | + */ |
| 47 | + readonly includeDefinition?: boolean |
| 48 | + /** |
| 49 | + * if true, don't include `self` as usages (for rename) |
| 50 | + */ |
| 51 | + readonly includeSelf?: boolean |
| 52 | + /** |
| 53 | + * if true, only TlbReferences from the same files listed |
| 54 | + */ |
| 55 | + readonly sameFileOnly?: boolean |
| 56 | + /** |
| 57 | + * search stops after `limit` number of TlbReferences are found |
| 58 | + */ |
| 59 | + readonly limit?: number |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Referent encapsulates the logic for finding all TlbReferences to a definition. |
| 64 | + */ |
| 65 | +export class TlbReferent { |
| 66 | + private readonly resolved: NamedNode | null = null |
| 67 | + private readonly file: TlbFile |
| 68 | + |
| 69 | + public constructor(node: SyntaxNode, file: TlbFile) { |
| 70 | + this.file = file |
| 71 | + const element = new NamedNode(node, file) |
| 72 | + this.resolved = TlbReference.resolve(element) |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns a list of nodes that reference the definition. |
| 77 | + */ |
| 78 | + public findReferences({ |
| 79 | + includeDefinition = false, |
| 80 | + sameFileOnly = false, |
| 81 | + limit = Infinity, |
| 82 | + }: FindTlbReferenceOptions): TlbNode[] { |
| 83 | + const resolved = this.resolved |
| 84 | + if (!resolved) return [] |
| 85 | + |
| 86 | + const useScope = this.useScope() |
| 87 | + if (!useScope) return [] |
| 88 | + |
| 89 | + const result: TlbNode[] = [] |
| 90 | + if (includeDefinition && (!sameFileOnly || resolved.file.uri === this.file.uri)) { |
| 91 | + const nameNode = resolved.nameNode() |
| 92 | + if (nameNode) { |
| 93 | + result.push(nameNode) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + this.searchInScope(useScope, sameFileOnly, result, limit) |
| 98 | + return result |
| 99 | + } |
| 100 | + |
| 101 | + private searchInScope( |
| 102 | + scope: SearchScope, |
| 103 | + sameFileOnly: boolean, |
| 104 | + result: TlbNode[], |
| 105 | + limit: number, |
| 106 | + ): void { |
| 107 | + if (!this.resolved) return |
| 108 | + |
| 109 | + if (scope instanceof LocalSearchScope) { |
| 110 | + this.traverseTree(this.resolved.file, scope.node, result, limit) |
| 111 | + } |
| 112 | + |
| 113 | + if (scope instanceof GlobalSearchScope) { |
| 114 | + if (sameFileOnly) { |
| 115 | + this.traverseTree(this.file, this.file.rootNode, result, limit) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + for (const file of scope.files) { |
| 120 | + this.traverseTree(file, file.rootNode, result, limit) |
| 121 | + if (result.length === limit) { |
| 122 | + break |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private traverseTree(file: TlbFile, node: SyntaxNode, result: TlbNode[], limit: number): void { |
| 129 | + const resolved = this.resolved |
| 130 | + if (!resolved) return |
| 131 | + |
| 132 | + // The algorithm for finding TlbReferences is simple: |
| 133 | + // we traverse the node that contains all the uses and resolve |
| 134 | + // each identifier with the same name as searched symbol. |
| 135 | + // If that identifier refers to the definition we are looking for, |
| 136 | + // we add it to the list. |
| 137 | + RecursiveVisitor.visit(node, (node): boolean | "stop" => { |
| 138 | + // fast path, skip non-identifiers |
| 139 | + if (node.type !== "identifier" && node.type !== "type_identifier") { |
| 140 | + return true |
| 141 | + } |
| 142 | + |
| 143 | + // fast path, identifier name doesn't equal to definition name |
| 144 | + const nodeName = node.text |
| 145 | + if (nodeName !== resolved.name()) { |
| 146 | + return true |
| 147 | + } |
| 148 | + |
| 149 | + const parent = node.parent |
| 150 | + if (parent === null) return true |
| 151 | + |
| 152 | + if (parent.type === "combinator") { |
| 153 | + return true |
| 154 | + } |
| 155 | + |
| 156 | + const targets = TlbReference.multiResolve(new NamedNode(node, file)) |
| 157 | + if (targets.length === 0) return true |
| 158 | + |
| 159 | + for (const res of targets) { |
| 160 | + const identifier = res.nameIdentifier() |
| 161 | + if (!identifier) continue |
| 162 | + |
| 163 | + if ( |
| 164 | + res.node.type === resolved.node.type && |
| 165 | + res.file.uri === resolved.file.uri && |
| 166 | + res.node.startPosition.row === resolved.node.startPosition.row && |
| 167 | + (identifier.text === resolved.name() || identifier.text === "self") |
| 168 | + ) { |
| 169 | + // found new TlbReference |
| 170 | + result.push(new TlbNode(node, file)) |
| 171 | + if (result.length === limit) { |
| 172 | + return "stop" // end iteration} |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + return true |
| 177 | + }) |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Returns the effective node in which all possible usages are expected. |
| 182 | + * Outside this node, no usages are assumed to exist. For example, a variable |
| 183 | + * can be used only in an outer block statement where it is defined. |
| 184 | + */ |
| 185 | + public useScope(): SearchScope | null { |
| 186 | + if (!this.resolved) return null |
| 187 | + |
| 188 | + return new GlobalSearchScope([this.file]) |
| 189 | + } |
| 190 | +} |
0 commit comments