Skip to content

Commit

Permalink
Complete global methods from a file inside namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
lekemula committed May 25, 2024
1 parent 524c94e commit 580ce02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/solargraph/source_map/clip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize api_map, cursor
def define
return [] if cursor.comment? || cursor.chain.literal?
result = cursor.chain.define(api_map, block, locals)
result.concat file_global_methods
result.concat((source_map.pins + source_map.locals).select{ |p| p.name == cursor.word && p.location.range.contain?(cursor.position) }) if result.empty?
result
end
Expand Down Expand Up @@ -214,6 +215,7 @@ def code_complete
return package_completions(api_map.get_global_variable_pins)
end
result.concat locals
result.concat file_global_methods unless block.binder.namespace.empty?
result.concat api_map.get_constants(context_pin.context.namespace, *gates)
result.concat api_map.get_methods(block.binder.namespace, scope: block.binder.scope, visibility: [:public, :private, :protected])
result.concat api_map.get_methods('Kernel')
Expand All @@ -224,6 +226,13 @@ def code_complete
end
package_completions(result)
end

def file_global_methods
return [] if cursor.word.empty?
source_map.pins.select do |pin|
pin.is_a?(Pin::Method) && pin.namespace == '' && pin.name.start_with?(cursor.word)
end
end
end
end
end
18 changes: 18 additions & 0 deletions spec/source_map/clip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1478,4 +1478,22 @@ def foo; end
string_names = api_map.clip_at('test.rb', [6, 22]).complete.pins.map(&:name)
expect(string_names).to eq(["upcase", "upcase!", "upto"])
end

it 'completes global methods defined in top level scope inside class when referenced inside a namespace' do
source = Solargraph::Source.load_string(%(
def some_method;end
class Thing
def foo
some_
end
end
some_
), 'test.rb')
api_map = Solargraph::ApiMap.new.map(source)
pin_names = api_map.clip_at('test.rb', [5, 15]).complete.pins.map(&:name)
expect(pin_names).to eq(["some_method"])
pin_names = api_map.clip_at('test.rb', [8, 5]).complete.pins.map(&:name)
expect(pin_names).to include("some_method")
end
end

0 comments on commit 580ce02

Please sign in to comment.