Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete global methods from a file inside modules/classes #714

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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