Module: MilkTea::LSP::Server::ServerReferences
- Included in:
- MilkTea::LSP::Server
- Defined in:
- lib/milk_tea/lsp/server/references.rb
Instance Method Summary collapse
- #declaration_name_range_fallback(node, uri, name) ⇒ Object
- #find_member_column_in_line(line, receiver_start_col, member_name) ⇒ Object
- #get_content_line(uri, lsp_line) ⇒ Object
- #handle_document_highlight(params) ⇒ Object
- #handle_document_link(params) ⇒ Object
- #handle_document_link_resolve(params) ⇒ Object
- #handle_references(params) ⇒ Object
- #handle_workspace_symbol(params) ⇒ Object
- #module_level_ast_name?(ast, name) ⇒ Boolean
- #module_level_declaration_node?(node) ⇒ Boolean
- #module_level_name?(facts, name) ⇒ Boolean
- #module_level_name_kind(facts, name) ⇒ Object
- #module_level_reference_locations(uri, name, facts, include_declaration:, cross_file_allowed: true) ⇒ Object
- #path_like_string_literal?(literal) ⇒ Boolean
- #resolve_document_link_path(source_path, literal) ⇒ Object
- #resource_document_link(uri, file_path, token) ⇒ Object
Instance Method Details
#declaration_name_range_fallback(node, uri, name) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/milk_tea/lsp/server/references.rb', line 281 def declaration_name_range_fallback(node, uri, name) return nil unless node.line row = get_content_line(uri, node.line - 1) return nil unless row col = row.index(/\b#{Regexp.escape(name)}\b/) return nil unless col { name: name, line: node.line, column: col + 1, length: name.length } rescue StandardError nil end |
#find_member_column_in_line(line, receiver_start_col, member_name) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/milk_tea/lsp/server/references.rb', line 249 def find_member_column_in_line(line, receiver_start_col, member_name) idx = receiver_start_col while idx < line.length && line[idx] =~ /[A-Za-z0-9_.]/ idx += 1 end dot_idx = line.index('.', receiver_start_col) return nil unless dot_idx && dot_idx < idx member_start = dot_idx + 1 match = line[member_start, member_name.length] return nil unless match == member_name member_start end |
#get_content_line(uri, lsp_line) ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'lib/milk_tea/lsp/server/references.rb', line 240 def get_content_line(uri, lsp_line) content = @workspace.get_content(uri) return nil unless content content.split("\n", -1)[lsp_line] rescue StandardError nil end |
#handle_document_highlight(params) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/milk_tea/lsp/server/references.rb', line 295 def handle_document_highlight(params) uri = params['textDocument']['uri'] lsp_line = params['position']['line'] lsp_char = params['position']['character'] token = @workspace.find_token_at(uri, lsp_line, lsp_char) return [] unless token&.type == :identifier if (facts = @workspace.get_facts(uri)) scoped = begin scoped_local_reference_locations(uri, token, lsp_line, lsp_char, facts, include_declaration: true) rescue StandardError nil end unless scoped.nil? return scoped.map { |entry| { range: entry[:range], kind: 1 } } end binding_id = begin rename_target_binding_id(uri, token, lsp_line, lsp_char, facts) rescue StandardError nil end if binding_id ranges = begin scoped_binding_occurrence_ranges(uri, token.lexeme, facts, binding_id, include_declaration: true) rescue StandardError [] end unless ranges.empty? return ranges.map do |range| { range: { start: { line: range[:line] - 1, character: range[:column] - 1 }, end: { line: range[:line] - 1, character: range[:column] - 1 + range[:length] }, }, kind: 1, } end end end end toks = @workspace.get_tokens(uri) || [] toks.select { |t| t.type == :identifier && t.lexeme == token.lexeme } .map { |t| { range: token_to_range(t), kind: 1 } } rescue StandardError => e warn "Error in documentHighlight handler: #{e.}" [] end |
#handle_document_link(params) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/milk_tea/lsp/server/references.rb', line 106 def handle_document_link(params) uri = params['textDocument']['uri'] file_path = uri_to_path(uri) return [] unless file_path tokens = @workspace.get_tokens(uri) return [] unless tokens tokens.filter_map do |token| resource_document_link(uri, file_path, token) end rescue StandardError => e warn "Error in documentLink handler: #{e.}" [] end |
#handle_document_link_resolve(params) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/milk_tea/lsp/server/references.rb', line 122 def handle_document_link_resolve(params) target = params['target'] if target && target.start_with?('file://') path = uri_to_path(target) if path && File.file?(path) first_line = File.open(path, &:readline).strip rescue nil params.merge( 'tooltip' => first_line ? "#{path}\n#{first_line}" : path ) else params end else params end rescue StandardError => e warn "Error in documentLink/resolve handler: #{e.}" params end |
#handle_references(params) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/milk_tea/lsp/server/references.rb', line 7 def handle_references(params) stages = new_perf_stages total_start = stages ? monotonic_time : nil uri = params['textDocument']['uri'] lsp_line = params['position']['line'] lsp_char = params['position']['character'] result_count = 0 result_state = 'miss' token = measure_perf_stage(stages, 'token') { @workspace.find_token_at(uri, lsp_line, lsp_char) } unless token&.type == :identifier result_state = 'not-identifier' return [] end include_declaration = params.dig('context', 'includeDeclaration') != false context = measure_perf_stage(stages, 'token_context') { token_context_at(uri, lsp_line, lsp_char) } if context tokens = context[:tokens] token_index = context[:token_index] named_arg_refs = named_argument_label_references(uri, token.lexeme, tokens, token_index, include_declaration:, stages:) return named_arg_refs if named_arg_refs end facts = measure_perf_stage(stages, 'facts') { @workspace.get_facts(uri) } target = facts ? measure_perf_stage(stages, 'static_target') { resolve_static_type_reference_target(uri, token, facts) } : nil if target refs = measure_perf_stage(stages, 'static_refs') { static_type_method_references(target, include_declaration: include_declaration) } result_count = refs.length result_state = refs.empty? ? 'miss' : 'hit' return refs end if facts scoped_refs = measure_perf_stage(stages, 'scoped_refs') do scoped_local_reference_locations(uri, token, lsp_line, lsp_char, facts, include_declaration: include_declaration) end unless scoped_refs.nil? result_count = scoped_refs.length result_state = scoped_refs.empty? ? 'miss' : 'hit' return scoped_refs end end level_kind = facts ? module_level_name?(facts, token.lexeme) : false if level_kind cross_file = true refs = measure_perf_stage(stages, 'module_refs') { module_level_reference_locations(uri, token.lexeme, facts, include_declaration: include_declaration, cross_file_allowed: cross_file) } result_count = refs.length result_state = refs.empty? ? 'miss' : 'hit' return refs end unless facts ast = @workspace.get_ast(uri) if ast && module_level_ast_name?(ast, token.lexeme) refs = measure_perf_stage(stages, 'module_refs') { module_level_reference_locations(uri, token.lexeme, nil, include_declaration: include_declaration) } result_count = refs.length result_state = refs.empty? ? 'miss' : 'hit' return refs end end refs = measure_perf_stage(stages, 'refs_scan') { @workspace.find_all_references(token.lexeme) } if include_declaration result_count = refs.length result_state = refs.empty? ? 'miss' : 'hit' return refs end found = measure_perf_stage(stages, 'definition_lookup') { @workspace.find_definition_token_global(token.lexeme, preferred_uri: uri) } unless found result_count = refs.length result_state = refs.empty? ? 'miss' : 'hit' return refs end def_uri = found[:uri] def_line = found[:token].line - 1 def_char = found[:token].column - 1 filtered = measure_perf_stage(stages, 'filter') do refs.reject do |r| r[:uri] == def_uri && r[:range][:start][:line] == def_line && r[:range][:start][:character] == def_char end end result_count = filtered.length result_state = filtered.empty? ? 'miss' : 'hit' filtered rescue StandardError => e result_state = 'error' warn "Error in references handler: #{e.}" [] ensure log_request_stage_breakdown('textDocument/references', total_start, uri: uri, stages: stages, summary: "result=#{result_state} refs=#{result_count}") end |
#handle_workspace_symbol(params) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/milk_tea/lsp/server/references.rb', line 346 def handle_workspace_symbol(params) query = (params['query'] || '').downcase @workspace.index_workspace(@root_uri) if @root_uri results = [] @workspace.all_documents.each do |uri| @workspace.get_symbols(uri).each do |sym| next unless query.empty? || sym[:name].downcase.include?(query) results << format_symbol(sym, uri) end end results rescue StandardError => e warn "Error in workspace/symbol handler: #{e.}" [] end |
#module_level_ast_name?(ast, name) ⇒ Boolean
163 164 165 166 167 168 |
# File 'lib/milk_tea/lsp/server/references.rb', line 163 def module_level_ast_name?(ast, name) each_ast_node(ast) do |node| return true if module_level_declaration_node?(node) && node.respond_to?(:name) && node.name == name end false end |
#module_level_declaration_node?(node) ⇒ Boolean
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/milk_tea/lsp/server/references.rb', line 265 def module_level_declaration_node?(node) node.is_a?(AST::FunctionDef) || node.is_a?(AST::MethodDef) || node.is_a?(AST::InterfaceMethodDecl) || node.is_a?(AST::ExternFunctionDecl) || node.is_a?(AST::ForeignFunctionDecl) || node.is_a?(AST::ConstDecl) || node.is_a?(AST::VarDecl) || node.is_a?(AST::TypeAliasDecl) || node.is_a?(AST::StructDecl) || node.is_a?(AST::UnionDecl) || node.is_a?(AST::EnumDecl) || node.is_a?(AST::FlagsDecl) || node.is_a?(AST::OpaqueDecl) end |
#module_level_name?(facts, name) ⇒ Boolean
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/milk_tea/lsp/server/references.rb', line 142 def module_level_name?(facts, name) return :function if facts.functions.key?(name) || facts.types.key?(name) || facts.values.key?(name) facts.methods.each_value do |methods| return :method if methods.key?(name) || methods.key?("static:#{name}") end facts.interfaces.each_value do |interface| if interface.respond_to?(:methods) && interface.methods.key?(name) return :interface_method end end false end |
#module_level_name_kind(facts, name) ⇒ Object
158 159 160 161 |
# File 'lib/milk_tea/lsp/server/references.rb', line 158 def module_level_name_kind(facts, name) result = module_level_name?(facts, name) result.is_a?(Symbol) ? result : nil end |
#module_level_reference_locations(uri, name, facts, include_declaration:, cross_file_allowed: true) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/milk_tea/lsp/server/references.rb', line 170 def module_level_reference_locations(uri, name, facts, include_declaration:, cross_file_allowed: true) ast = @workspace.get_ast(uri) return [] unless ast results = [] binding_ids = facts ? facts.binding_resolution&.identifier_binding_ids : nil local_bid_set = nil if binding_ids && facts&.binding_resolution resolution = facts.binding_resolution local_bid_set = Set.new each_ast_node(ast) do |node| next unless local_binding_declaration_node?(node) bid = resolution.declaration_binding_ids[node.object_id] local_bid_set << bid if bid end end each_ast_node(ast) do |node| if node.is_a?(AST::Identifier) && node.name == name if binding_ids&.key?(node.object_id) bid = binding_ids[node.object_id] next if local_bid_set&.include?(bid) end range = ast_name_range(node.name, node.line, node.column) next unless range results << { uri: uri, range: { start: { line: range[:line] - 1, character: range[:column] - 1 }, end: { line: range[:line] - 1, character: range[:column] - 1 + name.length }, }, } elsif include_declaration && node.respond_to?(:name) && node.name == name && module_level_declaration_node?(node) range = declaration_name_range(node) unless range range = declaration_name_range_fallback(node, uri, name) end next unless range results << { uri: uri, range: { start: { line: range[:line] - 1, character: range[:column] - 1 }, end: { line: range[:line] - 1, character: range[:column] - 1 + name.length }, }, } elsif node.is_a?(AST::MemberAccess) && node.member == name && node.line && node.column member_col = node.column - 1 results << { uri: uri, range: { start: { line: node.line - 1, character: member_col }, end: { line: node.line - 1, character: member_col + name.length }, }, } end end if cross_file_allowed && facts&.module_name importing_uris = @workspace.reverse_import_dependents_for(facts.module_name) if importing_uris && !importing_uris.empty? cross_refs = @workspace.find_all_references_in(name, importing_uris.to_a - [uri]) results.concat(cross_refs) if cross_refs end end results.uniq { |r| [r[:uri], r.dig(:range, :start, :line), r.dig(:range, :start, :character)] } end |
#path_like_string_literal?(literal) ⇒ Boolean
383 384 385 |
# File 'lib/milk_tea/lsp/server/references.rb', line 383 def path_like_string_literal?(literal) literal.include?('/') || literal.include?(File::SEPARATOR) end |
#resolve_document_link_path(source_path, literal) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/milk_tea/lsp/server/references.rb', line 387 def resolve_document_link_path(source_path, literal) base_dir = File.dirname(source_path) candidate = if Pathname.new(literal).absolute? literal else File.(literal, base_dir) end return nil unless File.exist?(candidate) candidate rescue StandardError nil end |
#resource_document_link(uri, file_path, token) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/milk_tea/lsp/server/references.rb', line 366 def resource_document_link(uri, file_path, token) return nil unless [:string, :cstring].include?(token.type) literal = token.literal return nil unless literal.is_a?(String) return nil unless path_like_string_literal?(literal) resolved_path = resolve_document_link_path(file_path, literal) return nil unless resolved_path { range: token_to_range(token), target: path_to_uri(resolved_path), tooltip: "Open linked file" } end |