Module: MilkTea::LSP::Server::ServerUtilities
- Included in:
- MilkTea::LSP::Server
- Defined in:
- lib/milk_tea/lsp/server/utilities.rb
Constant Summary collapse
- MESSAGE_TYPES =
{ error: 1, warning: 2, info: 3, log: 4, }.freeze
Instance Method Summary collapse
- #clear_cancelled_request(id) ⇒ Object
- #collect_call_argument_starts(tokens, lparen_index) ⇒ Object
- #current_word_prefix(uri, lsp_line, lsp_char) ⇒ Object
- #decode_client_char(uri, lsp_line, client_char) ⇒ Object
- #diagnostics_fingerprint(content, diagnostics) ⇒ Object
- #elapsed_ms(start_time) ⇒ Object
- #encode_char_for_client(lsp_line, internal_char) ⇒ Object
- #format_document_symbol(sym) ⇒ Object
- #format_symbol(sym, uri) ⇒ Object
- #handle_did_change_watched_files(params) ⇒ Object
- #hget(hash, key) ⇒ Object
- #invalidate_document_caches(uri) ⇒ Object
- #invalidate_document_caches_for(uris) ⇒ Object
- #library_uri?(uri) ⇒ Boolean
- #log_message(type, message) ⇒ Object
- #log_perf_breakdown(method_name, elapsed_ms_value, detail) ⇒ Object
- #log_request_stage_breakdown(method_name, total_start, uri: nil, stages: nil, summary: nil) ⇒ Object
- #measure_perf_stage(stages, name) ⇒ Object
- #monotonic_time ⇒ Object
- #new_perf_stages ⇒ Object
- #next_diagnostic_result_id(uri, fingerprint) ⇒ Object
- #next_non_trivia_token(tokens, index) ⇒ Object
- #path_to_uri(path) ⇒ Object
- #perf_breakdown_logging?(elapsed_ms) ⇒ Boolean
- #perf_log_context(method_name, params, verbose: false) ⇒ Object
- #perf_logging? ⇒ Boolean
- #perf_verbose? ⇒ Boolean
- #position_in_range?(line, char, start_line, start_char, end_line, end_char) ⇒ Boolean
- #refresh_open_document_dependency_state(changed_uri, previous_content: nil, current_content: nil) ⇒ Object
- #request_cancelled?(id) ⇒ Boolean
- #self_describing_argument_expression?(tokens, arg_tok) ⇒ Boolean
- #shorten_uri(uri) ⇒ Object
- #show_message(type, message) ⇒ Object
- #show_message_request(type, message, actions:, &callback) ⇒ Object
- #simple_identifier_like_argument_expression?(tokens, start_index) ⇒ Boolean
- #skip_expensive_source_fix_all?(uri, content) ⇒ Boolean
- #skip_expensive_work_reason(uri, content) ⇒ Object
- #summarize_lsp_params(method_name, params) ⇒ Object
- #symbol_kind(kind) ⇒ Object
- #token_end_position(token) ⇒ Object
- #token_to_range(token) ⇒ Object
- #uri_to_path(uri) ⇒ Object
- #utf32_to_utf16_char(line_text, utf32_offset) ⇒ Object
- #utf8_to_utf16_char(line_text, utf8_offset) ⇒ Object
Instance Method Details
#clear_cancelled_request(id) ⇒ Object
232 233 234 235 236 237 238 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 232 def clear_cancelled_request(id) return if id.nil? @cancelled_requests_mutex.synchronize do @cancelled_request_ids.delete(id) end end |
#collect_call_argument_starts(tokens, lparen_index) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 385 def collect_call_argument_starts(tokens, lparen_index) starts = [] depth = 1 j = lparen_index + 1 first = next_non_trivia_token(tokens, j) starts << first if first && first.type != :rparen while j < tokens.length tok = tokens[j] case tok.type when :lparen depth += 1 when :rparen depth -= 1 return [starts, j] if depth.zero? when :comma if depth == 1 next_tok = next_non_trivia_token(tokens, j + 1) starts << next_tok if next_tok && next_tok.type != :rparen end end j += 1 end [starts, nil] end |
#current_word_prefix(uri, lsp_line, lsp_char) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 293 def current_word_prefix(uri, lsp_line, lsp_char) content = @workspace.get_content(uri) lines = content.split("\n", -1) line = lines[lsp_line] || '' # Walk backwards from cursor to find start of current word char_idx = [lsp_char - 1, line.length - 1].min return '' if char_idx < 0 start = char_idx start -= 1 while start >= 0 && line[start] =~ /[A-Za-z0-9_]/ line[(start + 1)..char_idx] || '' end |
#decode_client_char(uri, lsp_line, client_char) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 333 def decode_client_char(uri, lsp_line, client_char) return client_char if @position_encoding == 'utf-16' content = @workspace.get_content(uri) return client_char unless content lines = content.split("\n", -1) line_text = lines[lsp_line] return client_char unless line_text if @position_encoding == 'utf-8' utf8_to_utf16_char(line_text, client_char) elsif @position_encoding == 'utf-32' utf32_to_utf16_char(line_text, client_char) else client_char end end |
#diagnostics_fingerprint(content, diagnostics) ⇒ Object
372 373 374 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 372 def diagnostics_fingerprint(content, diagnostics) [content, diagnostics].hash.to_s(16) end |
#elapsed_ms(start_time) ⇒ Object
23 24 25 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 23 def elapsed_ms(start_time) ((monotonic_time - start_time) * 1000).round(1) end |
#encode_char_for_client(lsp_line, internal_char) ⇒ Object
327 328 329 330 331 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 327 def encode_char_for_client(lsp_line, internal_char) return internal_char if @position_encoding == 'utf-16' internal_char end |
#format_document_symbol(sym) ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 257 def format_document_symbol(sym) line = sym[:line].to_i col = sym[:column].to_i { name: sym[:name], kind: symbol_kind(sym[:kind]), range: { start: { line: line - 1, character: col - 1 }, end: { line: line - 1, character: col - 1 + sym[:name].length } }, selectionRange: { start: { line: line - 1, character: col - 1 }, end: { line: line - 1, character: col - 1 + sym[:name].length } } } end |
#format_symbol(sym, uri) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 240 def format_symbol(sym, uri) line = sym[:line].to_i col = sym[:column].to_i { name: sym[:name], kind: symbol_kind(sym[:kind]), location: { uri: uri, range: { start: { line: line - 1, character: col - 1 }, end: { line: line - 1, character: col - 1 + sym[:name].length } } } } end |
#handle_did_change_watched_files(params) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 178 def handle_did_change_watched_files(params) changes = params['changes'] || [] affected_uris = Set.new changes.each do |change| uri = change['uri'] type = change['type'] next unless uri affected_uris.merge(@workspace.apply_watched_file_change(uri, type)) end invalidate_document_caches_for(affected_uris) affected_uris.each do |affected_uri| schedule_diagnostics(affected_uri, force: true, lint_tier: :full) unless @workspace.background_document?(affected_uri) end refresh_client_semantic_tokens if affected_uris.any? nil end |
#hget(hash, key) ⇒ Object
113 114 115 116 117 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 113 def hget(hash, key) return nil unless hash.is_a?(Hash) hash[key] || hash[key.to_sym] end |
#invalidate_document_caches(uri) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 197 def invalidate_document_caches(uri) @semantic_tokens_cache.delete(uri) @semantic_tokens_delta_cache.delete(uri) @fixall_cache.delete(uri) path = uri_to_path(uri) if path prefix = "#{path}:" @definition_file_token_cache.delete_if { |key, _| key.start_with?(prefix) } @definition_file_ast_cache.delete_if { |key, _| key.start_with?(prefix) } end end |
#invalidate_document_caches_for(uris) ⇒ Object
209 210 211 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 209 def invalidate_document_caches_for(uris) uris.each { |uri| invalidate_document_caches(uri) } end |
#library_uri?(uri) ⇒ Boolean
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 148 def library_uri?(uri) return false unless @root_uri file_path = uri_to_path(uri) root_path = uri_to_path(@root_uri) return false unless file_path && root_path !file_path.start_with?(root_path) rescue StandardError false end |
#log_message(type, message) ⇒ Object
470 471 472 473 474 475 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 470 def (type, ) @protocol.write_notification("window/logMessage", { type: MESSAGE_TYPES[type] || type, message: , }) end |
#log_perf_breakdown(method_name, elapsed_ms_value, detail) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 27 def log_perf_breakdown(method_name, elapsed_ms_value, detail) return unless perf_breakdown_logging?(elapsed_ms_value) id_detail = @current_request_id ? " id=#{@current_request_id}" : '' warn "[LSP perf] breakdown #{method_name} #{elapsed_ms_value}ms#{id_detail} #{detail}" end |
#log_request_stage_breakdown(method_name, total_start, uri: nil, stages: nil, summary: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 47 def log_request_stage_breakdown(method_name, total_start, uri: nil, stages: nil, summary: nil) return unless total_start detail = [] detail << "uri=#{shorten_uri(uri) || uri}" if uri detail << summary if summary && !summary.empty? unless stages.nil? || stages.empty? detail << "stages_ms=#{stages.map { |name, ms| "#{name}:#{ms}" }.join(',')}" end log_perf_breakdown(method_name, elapsed_ms(total_start), detail.join(' ')) end |
#measure_perf_stage(stages, name) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 38 def measure_perf_stage(stages, name) return yield unless stages start_time = monotonic_time result = yield stages << [name, elapsed_ms(start_time)] result end |
#monotonic_time ⇒ Object
19 20 21 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 19 def monotonic_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
#new_perf_stages ⇒ Object
34 35 36 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 34 def new_perf_stages perf_logging? ? [] : nil end |
#next_diagnostic_result_id(uri, fingerprint) ⇒ Object
376 377 378 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 376 def next_diagnostic_result_id(uri, fingerprint) "#{uri}:#{fingerprint}" end |
#next_non_trivia_token(tokens, index) ⇒ Object
447 448 449 450 451 452 453 454 455 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 447 def next_non_trivia_token(tokens, index) i = index while i < tokens.length tok = tokens[i] return tok unless [:newline, :indent, :dedent].include?(tok.type) i += 1 end nil end |
#path_to_uri(path) ⇒ Object
380 381 382 383 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 380 def path_to_uri(path) escaped_path = path.split('/').map { |seg| CGI.escape(seg).gsub('+', '%20') }.join('/') "file://#{escaped_path}" end |
#perf_breakdown_logging?(elapsed_ms) ⇒ Boolean
15 16 17 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 15 def perf_breakdown_logging?(elapsed_ms) perf_logging? && (perf_verbose? || elapsed_ms > Workspace::PERF_LOG_THRESHOLD_MS) end |
#perf_log_context(method_name, params, verbose: false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 60 def perf_log_context(method_name, params, verbose: false) return "" unless params.is_a?(Hash) summary = summarize_lsp_params(method_name, params) return summary.empty? ? "" : " #{summary}" if verbose text_document = hget(params, 'textDocument') uri = text_document.is_a?(Hash) ? hget(text_document, 'uri') : nil bits = [] bits << "uri=#{shorten_uri(uri) || uri}" if uri if method_name == 'textDocument/didChange' changes = hget(params, 'contentChanges') bits << "changes=#{changes.length}" if changes.respond_to?(:length) end bits.empty? ? "" : " #{bits.join(' ')}" rescue StandardError "" end |
#perf_logging? ⇒ Boolean
7 8 9 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 7 def perf_logging? @perf_logging ||= !ENV.fetch('MILK_TEA_LSP_PERF', nil).to_s.empty? end |
#perf_verbose? ⇒ Boolean
11 12 13 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 11 def perf_verbose? @perf_verbose ||= ENV.fetch('MILK_TEA_LSP_PERF', nil).to_s == 'verbose' end |
#position_in_range?(line, char, start_line, start_char, end_line, end_char) ⇒ Boolean
457 458 459 460 461 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 457 def position_in_range?(line, char, start_line, start_char, end_line, end_char) after_start = (line > start_line) || (line == start_line && char >= start_char) before_end = (line < end_line) || (line == end_line && char <= end_char) after_start && before_end end |
#refresh_open_document_dependency_state(changed_uri, previous_content: nil, current_content: nil) ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 213 def refresh_open_document_dependency_state(changed_uri, previous_content: nil, current_content: nil) return [] unless dependency_refresh_required_for_edit?(changed_uri, previous_content, current_content) affected_uris = @workspace.refresh_open_document_dependency_caches(changed_uri) invalidate_document_caches_for(affected_uris) affected_uris.each do |affected_uri| schedule_diagnostics(affected_uri, force: true, lint_tier: :full) unless @workspace.background_document?(affected_uri) end affected_uris end |
#request_cancelled?(id) ⇒ Boolean
224 225 226 227 228 229 230 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 224 def request_cancelled?(id) return false if id.nil? @cancelled_requests_mutex.synchronize do @cancelled_request_ids.include?(id) end end |
#self_describing_argument_expression?(tokens, arg_tok) ⇒ Boolean
413 414 415 416 417 418 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 413 def self_describing_argument_expression?(tokens, arg_tok) arg_index = tokens.index(arg_tok) return false unless arg_index simple_identifier_like_argument_expression?(tokens, arg_index) end |
#shorten_uri(uri) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 119 def shorten_uri(uri) return nil unless uri return uri unless uri.is_a?(String) && uri.start_with?('file://') file_path = uri_to_path(uri) return uri unless file_path root_path = uri_to_path(@root_uri) return uri unless root_path begin relative = Pathname.new(file_path).relative_path_from(Pathname.new(root_path)).to_s return relative unless relative.start_with?('..') rescue StandardError # Keep the original URI if path normalization fails. end uri end |
#show_message(type, message) ⇒ Object
463 464 465 466 467 468 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 463 def (type, ) @protocol.write_notification("window/showMessage", { type: MESSAGE_TYPES[type] || type, message: , }) end |
#show_message_request(type, message, actions:, &callback) ⇒ Object
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 477 def (type, , actions:, &callback) if @protocol.respond_to?(:send_request) @protocol.send_request('window/showMessageRequest', { type: MESSAGE_TYPES[type] || type, message: , actions: actions.map { |title| { title: title } } }) do |result, error| if error callback.call(nil) elsif result.is_a?(Hash) && result['title'] callback.call(result['title']) else callback.call(nil) end end else Protocol.send_request('window/showMessageRequest', { type: MESSAGE_TYPES[type] || type, message: , actions: actions.map { |title| { title: title } } }) do |result, error| if error callback.call(nil) elsif result.is_a?(Hash) && result['title'] callback.call(result['title']) else callback.call(nil) end end end end |
#simple_identifier_like_argument_expression?(tokens, start_index) ⇒ Boolean
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 420 def simple_identifier_like_argument_expression?(tokens, start_index) saw_identifier = false expect_identifier = true i = start_index while i < tokens.length tok = tokens[i] break if [:comma, :rparen].include?(tok.type) return false if [:newline, :indent, :dedent].include?(tok.type) if expect_identifier return false unless tok.type == :identifier saw_identifier = true expect_identifier = false else return false unless tok.type == :dot expect_identifier = true end i += 1 end saw_identifier && !expect_identifier end |
#skip_expensive_source_fix_all?(uri, content) ⇒ Boolean
160 161 162 163 164 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 160 def skip_expensive_source_fix_all?(uri, content) !skip_expensive_work_reason(uri, content).nil? rescue StandardError false end |
#skip_expensive_work_reason(uri, content) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 166 def skip_expensive_work_reason(uri, content) return 'library-uri' if library_uri?(uri) # Heuristic thresholds to avoid expensive full-file lint-fix runs. return 'large-bytes' if content.bytesize > 200_000 return 'large-lines' if content.count("\n") > 1200 nil rescue StandardError nil end |
#summarize_lsp_params(method_name, params) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 81 def summarize_lsp_params(method_name, params) return "" unless params.is_a?(Hash) text_document = hget(params, 'textDocument') uri = text_document.is_a?(Hash) ? hget(text_document, 'uri') : nil file_path = uri_to_path(uri) short_uri = shorten_uri(uri) position = hget(params, 'position') line = position.is_a?(Hash) ? hget(position, 'line') : nil char = position.is_a?(Hash) ? hget(position, 'character') : nil pos = "#{line}:#{char}" if line && char query = hget(params, 'query') bits = [] bits << "uri=#{short_uri || uri}" if uri bits << "pos=#{pos}" if pos if file_path && line.is_a?(Integer) && char.is_a?(Integer) bits << "loc=#{file_path}:#{line + 1}:#{char + 1}" end bits << "query=#{query.inspect}" if query bits << "keys=#{params.keys.map(&:to_s).sort.join(',')}" unless params.empty? if method_name == 'textDocument/didChange' changes = hget(params, 'contentChanges') bits << "changes=#{changes.length}" if changes.respond_to?(:length) end bits.join(' ') rescue StandardError "" end |
#symbol_kind(kind) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 275 def symbol_kind(kind) case kind when 'function' then 12 # Function when 'interface' then 11 # Interface when 'struct' then 23 # Struct when 'union' then 23 # Struct (union is a struct variant) when 'enum' then 10 # Enum when 'flags' then 10 # Enum (flags is an enum variant) when 'variant' then 23 # Struct (variant is a struct variant) when 'type_alias' then 5 # Class when 'constant' then 14 # Constant when 'variable' then 13 # Variable when 'event' then 24 # Event when 'type_param' then 26 # TypeParameter else 1 # File end end |
#token_end_position(token) ⇒ Object
318 319 320 321 322 323 324 325 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 318 def token_end_position(token) segments = token.lexeme.split("\n", -1) if segments.length == 1 [token.line - 1, token.column - 1 + segments.first.length] else [token.line - 1 + segments.length - 1, segments.last.length] end end |
#token_to_range(token) ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 306 def token_to_range(token) end_line, end_character = token_end_position(token) start_line = token.line - 1 start_char = encode_char_for_client(start_line, token.column - 1) end_char = encode_char_for_client(end_line, end_character) { start: { line: start_line, character: start_char }, end: { line: end_line, character: end_char } } end |
#uri_to_path(uri) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 139 def uri_to_path(uri) parsed = URI.parse(uri) return nil unless parsed.scheme == 'file' CGI.unescape(parsed.path) rescue URI::InvalidURIError nil end |
#utf32_to_utf16_char(line_text, utf32_offset) ⇒ Object
363 364 365 366 367 368 369 370 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 363 def utf32_to_utf16_char(line_text, utf32_offset) utf16_count = 0 line_text.each_char.with_index do |ch, idx| break if idx >= utf32_offset utf16_count += ch.ord > 0xFFFF ? 2 : 1 end utf16_count end |
#utf8_to_utf16_char(line_text, utf8_offset) ⇒ Object
352 353 354 355 356 357 358 359 360 361 |
# File 'lib/milk_tea/lsp/server/utilities.rb', line 352 def utf8_to_utf16_char(line_text, utf8_offset) bytes_seen = 0 utf16_count = 0 line_text.each_char do |ch| break if bytes_seen >= utf8_offset bytes_seen += ch.bytesize utf16_count += ch.ord > 0xFFFF ? 2 : 1 end utf16_count end |