-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Pin::Search to query workspace symbols (#560)
- Loading branch information
Showing
5 changed files
with
101 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'jaro_winkler' | ||
|
||
module Solargraph | ||
module Pin | ||
class Search | ||
class Result | ||
# @return [Float] | ||
attr_reader :match | ||
|
||
# @return [Pin::Base] | ||
attr_reader :pin | ||
|
||
def initialize match, pin | ||
@match = match | ||
@pin = pin | ||
end | ||
end | ||
|
||
# @param pins [Array<Pin::Base>] | ||
# @param query [String] | ||
def initialize pins, query | ||
@pins = pins | ||
@query = query | ||
end | ||
|
||
# @return [Array<Pin::Base>] | ||
def results | ||
@results ||= do_query | ||
end | ||
|
||
private | ||
|
||
# @return [Array<Pin::Base>] | ||
def do_query | ||
return @pins if @query.nil? || @query.empty? | ||
@pins.map do |pin| | ||
match = [fuzzy_string_match(pin.path, @query), fuzzy_string_match(pin.name, @query)].max | ||
Result.new(match, pin) if match > 0.6 | ||
end | ||
.compact | ||
.sort do |a, b| | ||
if a.match == b.match | ||
a.pin.path <=> b.pin.path | ||
else | ||
b.match <=> a.match | ||
end | ||
end | ||
.map(&:pin) | ||
end | ||
|
||
# @param str1 [String] | ||
# @param str2 [String] | ||
# @return [Float] | ||
def fuzzy_string_match str1, str2 | ||
JaroWinkler.distance(str1, str2) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
describe Solargraph::Pin::Search do | ||
it 'returns ordered matches on paths' do | ||
example_class = Solargraph::Pin::Namespace.new(name: 'Example') | ||
pins = [ | ||
example_class, | ||
Solargraph::Pin::Method.new(name: 'foobar', closure: example_class), | ||
Solargraph::Pin::Method.new(name: 'foo_bar', closure: example_class) | ||
] | ||
search = Solargraph::Pin::Search.new(pins, 'example') | ||
expect(search.results.map(&:path)).to eq(['Example']) | ||
end | ||
|
||
it 'returns ordered matches on names' do | ||
example_class = Solargraph::Pin::Namespace.new(name: 'Example') | ||
pins = [ | ||
example_class, | ||
Solargraph::Pin::Method.new(name: 'foobar', closure: example_class), | ||
Solargraph::Pin::Method.new(name: 'foo_bar', closure: example_class) | ||
] | ||
search = Solargraph::Pin::Search.new(pins, 'foobar') | ||
expect(search.results.map(&:path)).to eq(['Example.foobar', 'Example.foo_bar']) | ||
end | ||
|
||
it 'filters insufficient matches' do | ||
example_class = Solargraph::Pin::Namespace.new(name: 'Example') | ||
pins = [ | ||
example_class, | ||
Solargraph::Pin::Method.new(name: 'foobar', closure: example_class), | ||
Solargraph::Pin::Method.new(name: 'bazquz', closure: example_class) | ||
] | ||
search = Solargraph::Pin::Search.new(pins, 'foobar') | ||
expect(search.results.map(&:path)).to eq(['Example.foobar']) | ||
end | ||
end |