Skip to content
Merged
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
8 changes: 6 additions & 2 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,13 @@ def prefix_search(query, nesting = nil)
end

# Fuzzy searches index entries based on Jaro-Winkler similarity. If no query is provided, all entries are returned
#: (String? query) -> Array[Entry]
def fuzzy_search(query)
#: (String? query) ?{ (Entry) -> bool? } -> Array[Entry]
def fuzzy_search(query, &condition)
unless query
entries = @entries.filter_map do |_name, entries|
next if entries.first.is_a?(Entry::SingletonClass)

entries = entries.select(&condition) if condition
entries
end

Expand All @@ -212,6 +213,9 @@ def fuzzy_search(query)
results = @entries.filter_map do |name, entries|
next if entries.first.is_a?(Entry::SingletonClass)

entries = entries.select(&condition) if condition
next if entries.empty?

similarity = DidYouMean::JaroWinkler.distance(name.gsub("::", "").downcase, normalized_query)
[entries, -similarity] if similarity > ENTRY_SIMILARITY_THRESHOLD
end
Expand Down
32 changes: 20 additions & 12 deletions lib/ruby_lsp/requests/workspace_symbol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,7 @@ def initialize(global_state, query)
# @override
#: -> Array[Interface::WorkspaceSymbol]
def perform
@index.fuzzy_search(@query).filter_map do |entry|
uri = entry.uri
file_path = uri.full_path

# We only show symbols declared in the workspace
in_dependencies = file_path && !not_in_dependencies?(file_path)
next if in_dependencies

# We should never show private symbols when searching the entire workspace
next if entry.private?

fuzzy_search.filter_map do |entry|
kind = kind_for_entry(entry)
loc = entry.location

Expand All @@ -44,7 +34,7 @@ def perform
container_name: container.join("::"),
kind: kind,
location: Interface::Location.new(
uri: uri.to_s,
uri: entry.uri.to_s,
range: Interface::Range.new(
start: Interface::Position.new(line: loc.start_line - 1, character: loc.start_column),
end: Interface::Position.new(line: loc.end_line - 1, character: loc.end_column),
Expand All @@ -53,6 +43,24 @@ def perform
)
end
end

private

#: -> Array[RubyIndexer::Entry]
def fuzzy_search
@index.fuzzy_search(@query) do |entry|
file_path = entry.uri.full_path

# We only show symbols declared in the workspace
in_dependencies = file_path && !not_in_dependencies?(file_path)
next if in_dependencies

# We should never show private symbols when searching the entire workspace
next if entry.private?

true
end
end
end
end
end
Loading