Module: Kapusta::LSP::Rename
- Defined in:
- lib/kapusta/lsp/rename.rb
Defined Under Namespace
Classes: Target
Constant Summary collapse
- RESPONSE_REQUEST_FAILED =
-32_803
Class Method Summary collapse
- .classify(walker, sym, binding, reference, seg) ⇒ Object
- .collect_local_targets(walker, binding) ⇒ Object
- .collect_macro_changes(def_uri, def_binding, new_name, workspace_index) ⇒ Object
- .conflict_local?(scope, new_name, targets, _walker, binding) ⇒ Boolean
- .constant_target(walker, binding, seg, sym: nil) ⇒ Object
- .error(message) ⇒ Object
- .local_target(walker, binding, seg, sym: nil) ⇒ Object
- .locate(text, line_zero, character) ⇒ Object
- .locate_macro_definition(uri, binding, workspace_index) ⇒ Object
- .lsp_range(line, start_col, end_col) ⇒ Object
- .macro_defined_in_file?(entry, name, except:) ⇒ Boolean
- .parse(text) ⇒ Object
- .path_segments(sym) ⇒ Object
- .path_sym?(sym) ⇒ Boolean
- .perform(uri, text, line_zero, character, new_name, workspace_index: nil) ⇒ Object
- .placeholder_for(target) ⇒ Object
- .prepare(text, line_zero, character) ⇒ Object
- .rename_constant(target, new_name, workspace_index) ⇒ Object
- .rename_local(uri, target, new_name) ⇒ Object
- .rename_macro(uri, target, new_name, workspace_index) ⇒ Object
- .rename_method(uri, target, new_name) ⇒ Object
- .rename_toplevel(target, new_name, workspace_index) ⇒ Object
- .segment_at_column(sym, col) ⇒ Object
- .segment_range(sym, segment_index) ⇒ Object
- .shadowed_in_chain?(ref_scope, new_name, target_scope) ⇒ Boolean
- .sym_at_cursor(walker, line, col) ⇒ Object
- .synthetic?(sym) ⇒ Boolean
- .text_edit_first_segment(occurrence, new_name) ⇒ Object
- .text_edit_full(occurrence, new_name) ⇒ Object
Class Method Details
.classify(walker, sym, binding, reference, seg) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/kapusta/lsp/rename.rb', line 126 def classify(walker, sym, binding, reference, seg) return if sym.colonized? && seg[:index].positive? if sym.dotted? && seg[:index].positive? segment_text = sym.segments[seg[:index]] return if segment_text.match?(/\A[a-z]/) end if binding return constant_target(walker, binding, seg) if Compiler::Language.header_scope?(binding.kind) return local_target(walker, binding, seg) end if reference target = reference.target if target return constant_target(walker, target, seg, sym:) if Compiler::Language.header_scope?(target.kind) return local_target(walker, target, seg, sym:) end end first_seg = path_sym?(sym) ? path_segments(sym).first : sym.name if first_seg.match?(/\A[A-Z]/) Target.new( kind: :free_constant, sym:, name: sym.name, segment_index: seg[:index], segment_prefix: (sym.dotted? ? sym.segments[0..seg[:index]] : [first_seg]), seg_start: seg[:start], seg_end: seg[:end], walker: ) else Target.new( kind: :free_toplevel, sym:, name: sym.name, segment_index: seg[:index], seg_start: seg[:start], seg_end: seg[:end], walker: ) end end |
.collect_local_targets(walker, binding) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/kapusta/lsp/rename.rb', line 245 def collect_local_targets(walker, binding) scope = binding.scope results = [] walker.bindings.each do |b| results << b if b.name == binding.name && b.scope.equal?(scope) end walker.references.each do |r| results << r if r.target.equal?(binding) end results end |
.collect_macro_changes(def_uri, def_binding, new_name, workspace_index) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/kapusta/lsp/rename.rb', line 332 def collect_macro_changes(def_uri, def_binding, new_name, workspace_index) changes = {} def_entry = workspace_index.entry(def_uri) if def_entry targets = def_entry.walker.bindings.select { |b| b.equal?(def_binding) } targets += def_entry.walker.references.select do |r| r.target.equal?(def_binding) || (r.target.nil? && r.name == def_binding.name) end changes[def_uri] = targets.map { |t| text_edit_first_segment(t, new_name) } unless targets.empty? end workspace_index.each_entry do |uri, entry| next if uri == def_uri imports = entry.walker.bindings.select do |b| next false unless b.kind == :macro_import next false unless b.import_key.to_s.tr('_', '-') == def_binding.name workspace_index.import_resolves_to?(uri, b.import_module, def_uri) end next if imports.empty? refs = entry.walker.references.select do |r| imports.any? { |imp| imp.equal?(r.target) } end changes[uri] = (imports + refs).map { |t| text_edit_first_segment(t, new_name) } end changes end |
.conflict_local?(scope, new_name, targets, _walker, binding) ⇒ Boolean
257 258 259 260 261 262 263 264 265 |
# File 'lib/kapusta/lsp/rename.rb', line 257 def conflict_local?(scope, new_name, targets, _walker, binding) return true if scope.bindings[new_name] && !scope.bindings[new_name].equal?(binding) targets.any? do |t| next false unless t.is_a?(ScopeWalker::Reference) shadowed_in_chain?(t.scope, new_name, scope) end end |
.constant_target(walker, binding, seg, sym: nil) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/kapusta/lsp/rename.rb', line 183 def constant_target(walker, binding, seg, sym: nil) the_sym = sym || binding.sym prefix = the_sym.dotted? ? the_sym.segments[0..seg[:index]] : [the_sym.name] Target.new( kind: :constant, sym: the_sym, name: binding.name, segment_index: seg[:index], segment_prefix: prefix, seg_start: seg[:start], seg_end: seg[:end], binding:, walker: ) end |
.error(message) ⇒ Object
432 433 434 |
# File 'lib/kapusta/lsp/rename.rb', line 432 def error() { error: { code: RESPONSE_REQUEST_FAILED, message: } } end |
.local_target(walker, binding, seg, sym: nil) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/kapusta/lsp/rename.rb', line 164 def local_target(walker, binding, seg, sym: nil) kind = case binding.kind when :toplevel_fn then :toplevel_fn when :macro, :macro_import then :macro when :method then :method else :local end Target.new( kind:, sym: sym || binding.sym, name: binding.name, segment_index: seg[:index], seg_start: seg[:start], seg_end: seg[:end], binding:, walker: ) end |
.locate(text, line_zero, character) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/kapusta/lsp/rename.rb', line 54 def locate(text, line_zero, character) forms = parse(text) return unless forms walker = ScopeWalker.analyze(forms) line = line_zero + 1 col = character + 1 sym = sym_at_cursor(walker, line, col) return unless sym return if synthetic?(sym) seg = segment_at_column(sym, col) return unless seg && seg[:index] != :on_delimiter binding = walker.bindings.find { |b| b.sym.equal?(sym) } reference = walker.references.find { |r| r.sym.equal?(sym) } classify(walker, sym, binding, reference, seg) end |
.locate_macro_definition(uri, binding, workspace_index) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/kapusta/lsp/rename.rb', line 311 def locate_macro_definition(uri, binding, workspace_index) case binding.kind when :macro entry = workspace_index.entry(uri) return unless entry indexed = entry.walker.bindings.find { |b| b.kind == :macro && b.name == binding.name } indexed ? [uri, indexed] : nil when :macro_import workspace_index.find_macro_definition(uri, binding.import_module, binding.import_key) end end |
.lsp_range(line, start_col, end_col) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/kapusta/lsp/rename.rb', line 205 def lsp_range(line, start_col, end_col) { start: { line: line - 1, character: start_col - 1 }, end: { line: line - 1, character: end_col - 1 } } end |
.macro_defined_in_file?(entry, name, except:) ⇒ Boolean
324 325 326 327 328 329 330 |
# File 'lib/kapusta/lsp/rename.rb', line 324 def macro_defined_in_file?(entry, name, except:) return false unless entry entry.walker.bindings.any? do |b| b.kind == :macro && b.name == name && !b.equal?(except) end end |
.parse(text) ⇒ Object
75 76 77 78 79 |
# File 'lib/kapusta/lsp/rename.rb', line 75 def parse(text) Reader.read_all(text) rescue Kapusta::Error nil end |
.path_segments(sym) ⇒ Object
440 441 442 |
# File 'lib/kapusta/lsp/rename.rb', line 440 def path_segments(sym) sym.dotted? ? sym.segments : sym.colon_segments end |
.path_sym?(sym) ⇒ Boolean
436 437 438 |
# File 'lib/kapusta/lsp/rename.rb', line 436 def path_sym?(sym) sym.dotted? || sym.colonized? end |
.perform(uri, text, line_zero, character, new_name, workspace_index: nil) ⇒ Object
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 |
# File 'lib/kapusta/lsp/rename.rb', line 28 def perform(uri, text, line_zero, character, new_name, workspace_index: nil) target = locate(text, line_zero, character) return error('rename not available at this position') unless target case target.kind when :local rename_local(uri, target, new_name) when :method rename_method(uri, target, new_name) when :toplevel_fn, :free_toplevel return error('cross-file rename requires a workspace') unless workspace_index rename_toplevel(target, new_name, workspace_index) when :constant, :free_constant return error('cross-file rename requires a workspace') unless workspace_index rename_constant(target, new_name, workspace_index) when :macro return error('cross-file rename requires a workspace') unless workspace_index rename_macro(uri, target, new_name, workspace_index) else error("rename not supported for #{target.kind}") end end |
.placeholder_for(target) ⇒ Object
199 200 201 202 203 |
# File 'lib/kapusta/lsp/rename.rb', line 199 def placeholder_for(target) return target.segment_prefix.last if target.segment_prefix target.name end |
.prepare(text, line_zero, character) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/kapusta/lsp/rename.rb', line 18 def prepare(text, line_zero, character) target = locate(text, line_zero, character) return unless target { range: lsp_range(target.sym.line, target.seg_start, target.seg_end), placeholder: placeholder_for(target) } end |
.rename_constant(target, new_name, workspace_index) ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/kapusta/lsp/rename.rb', line 364 def rename_constant(target, new_name, workspace_index) unless Identifier.valid_constant_segment?(new_name) return error("class and module names must start with an uppercase letter (got #{new_name.inspect})") end prefix = target.segment_prefix seg_index = target.segment_index per_uri = workspace_index.constant_occurrences(prefix) return error('no occurrences found') if per_uri.empty? new_prefix = prefix.dup new_prefix[seg_index] = new_name if workspace_index.constant_definition_with_prefix?(new_prefix, except_prefix: prefix) return error("rename conflict: constant '#{new_prefix.join('.')}' is already defined") end changes = {} per_uri.each do |uri, occs| changes[uri] = occs.map do |occ| seg_start, seg_end = segment_range(occ.sym, seg_index) { range: { start: { line: occ.line - 1, character: seg_start - 1 }, end: { line: occ.line - 1, character: seg_end - 1 } }, newText: new_name } end end { changes: } end |
.rename_local(uri, target, new_name) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/kapusta/lsp/rename.rb', line 228 def rename_local(uri, target, new_name) return error("invalid identifier: #{new_name}") unless Identifier.valid_local?(new_name) return error('cannot resolve binding') unless target.binding walker = target.walker binding = target.binding scope = binding.scope edits_targets = collect_local_targets(walker, binding) if conflict_local?(scope, new_name, edits_targets, walker, binding) return error("rename conflict: '#{new_name}' is already in scope") end edits = edits_targets.map { |t| text_edit_first_segment(t, new_name) } { changes: { uri => edits } } end |
.rename_macro(uri, target, new_name, workspace_index) ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/kapusta/lsp/rename.rb', line 293 def rename_macro(uri, target, new_name, workspace_index) return error("invalid identifier: #{new_name}") unless Identifier.valid_local?(new_name) return error('cannot resolve binding') unless target.binding def_uri, def_binding = locate_macro_definition(uri, target.binding, workspace_index) return error('macro definition not found') unless def_uri && def_binding if workspace_index.macro_definition_anywhere?(new_name, except_uri: def_uri) || macro_defined_in_file?(workspace_index.entry(def_uri), new_name, except: def_binding) return error("rename conflict: macro '#{new_name}' is already defined in the workspace") end changes = collect_macro_changes(def_uri, def_binding, new_name, workspace_index) return error('no occurrences found') if changes.empty? { changes: } end |
.rename_method(uri, target, new_name) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/kapusta/lsp/rename.rb', line 212 def rename_method(uri, target, new_name) return error("invalid identifier: #{new_name}") unless Identifier.valid_local?(new_name) return error('cannot resolve binding') unless target.binding walker = target.walker binding = target.binding occs = [binding] walker.references.each do |r| occs << r if r.target.equal?(binding) end edits = occs.map { |o| text_edit_first_segment(o, new_name) } { changes: { uri => edits } } end |
.rename_toplevel(target, new_name, workspace_index) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/kapusta/lsp/rename.rb', line 277 def rename_toplevel(target, new_name, workspace_index) return error("invalid identifier: #{new_name}") unless Identifier.valid_local?(new_name) per_uri = workspace_index.toplevel_fn_occurrences(target.name) return error('no occurrences found') if per_uri.empty? if workspace_index.toplevel_definition?(new_name, except_name: target.name) return error("rename conflict: '#{new_name}' is already defined in the workspace") end changes = per_uri.transform_values do |occs| occs.map { |o| text_edit_first_segment(o, new_name) } end { changes: } end |
.segment_at_column(sym, col) ⇒ Object
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 |
# File 'lib/kapusta/lsp/rename.rb', line 100 def segment_at_column(sym, col) unless path_sym?(sym) start_col = sym.column end_col = sym.column + sym.name.length return unless col.between?(start_col, end_col) return { index: 0, start: start_col, end: end_col } end pos = sym.column segments = path_segments(sym) segments.each_with_index do |seg, k| seg_start = pos seg_end = pos + seg.length return { index: k, start: seg_start, end: seg_end } if col >= seg_start && col < seg_end if k < segments.length - 1 return { index: :on_delimiter, start: seg_end, end: seg_end + 1 } if col == seg_end elsif col == seg_end return { index: k, start: seg_start, end: seg_end } end pos = seg_end + 1 end nil end |
.segment_range(sym, segment_index) ⇒ Object
396 397 398 399 400 401 402 403 |
# File 'lib/kapusta/lsp/rename.rb', line 396 def segment_range(sym, segment_index) return [sym.column, sym.column + sym.name.length] unless path_sym?(sym) segments = path_segments(sym) prior = segments[0...segment_index].sum { |s| s.length + 1 } start_col = sym.column + prior [start_col, start_col + segments[segment_index].length] end |
.shadowed_in_chain?(ref_scope, new_name, target_scope) ⇒ Boolean
267 268 269 270 271 272 273 274 275 |
# File 'lib/kapusta/lsp/rename.rb', line 267 def shadowed_in_chain?(ref_scope, new_name, target_scope) s = ref_scope while s && !s.equal?(target_scope) return true if s.bindings.key?(new_name) s = s.parent end false end |
.sym_at_cursor(walker, line, col) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/kapusta/lsp/rename.rb', line 81 def sym_at_cursor(walker, line, col) candidates = [] walker.bindings.each do |b| candidates << b.sym if b.line == line && col >= b.column && col <= b.end_column end walker.references.each do |r| candidates << r.sym if r.line == line && col >= r.column && col <= r.end_column end candidates.first end |
.synthetic?(sym) ⇒ Boolean
92 93 94 95 96 97 98 |
# File 'lib/kapusta/lsp/rename.rb', line 92 def synthetic?(sym) return true if sym.is_a?(MacroSym) || sym.is_a?(AutoGensym) name = sym.name name == '_' || name == '&' || name == '...' || name == '$' || name == '$...' || name.match?(/\A\$\d\z/) end |
.text_edit_first_segment(occurrence, new_name) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/kapusta/lsp/rename.rb', line 418 def text_edit_first_segment(occurrence, new_name) sym = occurrence.sym return text_edit_full(occurrence, new_name) unless sym.is_a?(Sym) && path_sym?(sym) seg_start, seg_end = segment_range(sym, 0) { range: { start: { line: occurrence.line - 1, character: seg_start - 1 }, end: { line: occurrence.line - 1, character: seg_end - 1 } }, newText: new_name } end |
.text_edit_full(occurrence, new_name) ⇒ Object
405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/kapusta/lsp/rename.rb', line 405 def text_edit_full(occurrence, new_name) line = occurrence.line - 1 start_col = occurrence.column - 1 end_col = occurrence.end_column - 1 { range: { start: { line:, character: start_col }, end: { line:, character: end_col } }, newText: new_name } end |