Module: MilkTea::LSP::Workspace::WorkspaceUtilities
- Included in:
- MilkTea::LSP::Workspace
- Defined in:
- lib/milk_tea/lsp/workspace/utilities.rb
Instance Method Summary collapse
- #dependency_resolution_mode ⇒ Object
- #dependency_resolution_mode=(mode) ⇒ Object
-
#find_call_context(uri, lsp_line, lsp_char) ⇒ Object
Scan text up to the cursor to find the innermost open function call context.
-
#find_definition_token(uri, name, before_line: nil, before_char: nil) ⇒ Object
Find the name identifier token immediately after a definition keyword (def, struct, union, enum, flags, variant, type, const, var) for the given name.
-
#find_dot_receiver(uri, lsp_line, lsp_char) ⇒ Object
Returns the receiver name before '.' if the cursor is in a dot-access context, e.g.
- #find_dot_receiver_path(uri, lsp_line, lsp_char) ⇒ Object
-
#find_token_at(uri, lsp_line, lsp_character) ⇒ Object
Find the identifier/keyword token under the cursor.
- #platform_override ⇒ Object
- #platform_override=(platform) ⇒ Object
- #shared_module_cache ⇒ Object
- #strict_current_root_diagnostics_enabled ⇒ Object
- #strict_current_root_diagnostics_enabled=(enabled) ⇒ Object
- #token_contains_position?(token, target_line, target_char) ⇒ Boolean
- #workspace_root_path ⇒ Object
- #workspace_root_path=(path) ⇒ Object
Instance Method Details
#dependency_resolution_mode ⇒ Object
37 38 39 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 37 def dependency_resolution_mode @dependency_resolution_mode end |
#dependency_resolution_mode=(mode) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 19 def dependency_resolution_mode=(mode) normalized = DependencyResolution.normalize_mode(mode) return if @dependency_resolution_mode == normalized @dependency_resolution_mode = normalized @facts_state_mutex.synchronize do @facts_cache_mutex.synchronize do @shared_module_cache.clear @facts_cache.clear @tooling_snapshot_cache.clear @diagnostics_cache.clear @last_good_facts_cache.clear @last_good_tooling_snapshot_cache.clear clear_dependency_index end end end |
#find_call_context(uri, lsp_line, lsp_char) ⇒ Object
Scan text up to the cursor to find the innermost open function call context. Returns { name:, active_parameter: } or nil if not inside a call.
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 81 def find_call_context(uri, lsp_line, lsp_char) content = get_content(uri) return nil if content.empty? lines = content.split("\n", -1) cursor_line = lines[lsp_line] || '' prefix = lsp_line.positive? ? lines[0...lsp_line].join("\n") + "\n" : '' text = prefix + cursor_line[0...lsp_char] depth = 0 active_param = 0 i = text.length - 1 paren_pos = nil while i >= 0 ch = text[i] case ch when ')', ']' depth += 1 when '[' return nil if depth.zero? depth -= 1 when '(' if depth.zero? paren_pos = i break end depth -= 1 when ',' active_param += 1 if depth.zero? end i -= 1 end return nil unless paren_pos # Find identifier immediately before the '(' j = paren_pos - 1 j -= 1 while j >= 0 && (text[j] == ' ' || text[j] == "\t") return nil if j < 0 || text[j] !~ /[A-Za-z0-9_]/ end_j = j j -= 1 while j > 0 && text[j - 1] =~ /[A-Za-z0-9_]/ name = text[j..end_j] return nil if name.empty? { name: name, active_parameter: active_param } rescue StandardError => e warn "LSP call context error #{uri}: #{e.}" nil end |
#find_definition_token(uri, name, before_line: nil, before_char: nil) ⇒ Object
Find the name identifier token immediately after a definition keyword (def, struct, union, enum, flags, variant, type, const, var) for the given name. Returns the identifier Token, or nil if not found.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 224 def find_definition_token(uri, name, before_line: nil, before_char: nil) tokens = get_tokens(uri) return nil if tokens.nil? nearest = nil tokens.each_cons(2) do |kw_tok, id_tok| next unless DEFINITION_KEYWORDS.include?(kw_tok.type) next unless id_tok.type == :identifier && id_tok.lexeme == name if before_line next if id_tok.line > before_line next if id_tok.line == before_line && before_char && id_tok.column >= before_char end if nearest.nil? || id_tok.line > nearest.line || (id_tok.line == nearest.line && id_tok.column > nearest.column) nearest = id_tok end end return nearest if nearest tokens.each_cons(2) do |kw_tok, id_tok| next unless DEFINITION_KEYWORDS.include?(kw_tok.type) next unless id_tok.type == :identifier && id_tok.lexeme == name return id_tok end nil end |
#find_dot_receiver(uri, lsp_line, lsp_char) ⇒ Object
Returns the receiver name before '.' if the cursor is in a dot-access context, e.g. for "vec.len|" returns "vec". Returns nil otherwise. lsp_char is the 0-based cursor character position (LSP convention).
171 172 173 174 175 176 177 178 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 171 def find_dot_receiver(uri, lsp_line, lsp_char) receiver_path = find_dot_receiver_path(uri, lsp_line, lsp_char) return nil unless receiver_path receiver_path.split('.').last rescue StandardError nil end |
#find_dot_receiver_path(uri, lsp_line, lsp_char) ⇒ Object
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 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 180 def find_dot_receiver_path(uri, lsp_line, lsp_char) content = get_content(uri) lines = content.split("\n", -1) line_str = lines[lsp_line] || '' idx = [lsp_char - 1, line_str.length - 1].min # Walk back past the hovered identifier to its dot idx -= 1 while idx >= 0 && line_str[idx] =~ /[A-Za-z0-9_]/ return nil if idx < 0 || line_str[idx] != '.' segments = [] left = idx - 1 loop do # Skip brackets and their contents, then collect the identifier before them if line_str[left] == ']' depth = 1 left -= 1 while left >= 0 && depth > 0 depth += 1 if line_str[left] == ']' depth -= 1 if line_str[left] == '[' left -= 1 if depth > 0 && left > 0 end left -= 1 # skip past opening bracket end # Walk back over identifier chars start = left start -= 1 while start >= 0 && line_str[start] =~ /[A-Za-z0-9_]/ segments.unshift(line_str[start + 1..left]) if start < left break unless start >= 0 && line_str[start] == '.' left = start - 1 end segments.any? ? segments.join('.') : nil rescue StandardError nil end |
#find_token_at(uri, lsp_line, lsp_character) ⇒ Object
Find the identifier/keyword token under the cursor. lsp_line and lsp_character are 0-based (LSP convention).
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 137 def find_token_at(uri, lsp_line, lsp_character) tokens = get_tokens(uri) return nil if tokens.nil? # Tokens use 1-based line/column target_line = lsp_line + 1 target_char = lsp_character + 1 tokens.find do |tok| next false if [:newline, :indent, :dedent, :eof].include?(tok.type) token_contains_position?(tok, target_line, target_char) end end |
#platform_override ⇒ Object
59 60 61 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 59 def platform_override @platform_override end |
#platform_override=(platform) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 41 def platform_override=(platform) normalized = platform.nil? ? nil : ModuleLoader.normalize_platform_name(platform) return if @platform_override == normalized @platform_override = normalized @facts_state_mutex.synchronize do @facts_cache_mutex.synchronize do @shared_module_cache.clear @facts_cache.clear @tooling_snapshot_cache.clear @diagnostics_cache.clear @last_good_facts_cache.clear @last_good_tooling_snapshot_cache.clear clear_dependency_index end end end |
#shared_module_cache ⇒ Object
7 8 9 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 7 def shared_module_cache @shared_module_cache end |
#strict_current_root_diagnostics_enabled ⇒ Object
75 76 77 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 75 def strict_current_root_diagnostics_enabled @strict_current_root_diagnostics_enabled end |
#strict_current_root_diagnostics_enabled=(enabled) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 63 def strict_current_root_diagnostics_enabled=(enabled) normalized = !!enabled return if @strict_current_root_diagnostics_enabled == normalized @strict_current_root_diagnostics_enabled = normalized @facts_state_mutex.synchronize do @facts_cache_mutex.synchronize do @diagnostics_cache.clear end end end |
#token_contains_position?(token, target_line, target_char) ⇒ Boolean
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 152 def token_contains_position?(token, target_line, target_char) segments = token.lexeme.split("\n", -1) end_line = token.line + segments.length - 1 return false if target_line < token.line || target_line > end_line if segments.length == 1 return token.column <= target_char && target_char < (token.column + segments.first.length) end if target_line == token.line return token.column <= target_char && target_char <= (token.column + segments.first.length - 1) end target_char <= segments.fetch(target_line - token.line).length end |
#workspace_root_path ⇒ Object
15 16 17 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 15 def workspace_root_path @workspace_root_path end |
#workspace_root_path=(path) ⇒ Object
11 12 13 |
# File 'lib/milk_tea/lsp/workspace/utilities.rb', line 11 def workspace_root_path=(path) @workspace_root_path = normalize_workspace_root_path(path) end |