Skip to content

Commit

Permalink
Merge branch 'main' into yarp
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 29, 2023
2 parents dd22318 + 5cb9407 commit 4bb18a6
Show file tree
Hide file tree
Showing 76 changed files with 1,530 additions and 7,062 deletions.
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,4 @@ group :development do
gem "tapioca", "~> 0.11", require: false, platforms: NON_WINDOWS_PLATFORMS
gem "rdoc", require: false
gem "psych", "~> 5.1", require: false

# The Rails documentation link only activates when railties is detected.
gem "railties", "~> 7.0", require: false
end
91 changes: 20 additions & 71 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,56 +1,29 @@
PATH
remote: .
specs:
ruby-lsp (0.8.1)
ruby-lsp (0.9.1)
language_server-protocol (~> 3.17.0)
sorbet-runtime
syntax_tree (>= 6.1.1, < 7)
yarp (>= 0.8, < 0.10)

yarp (>= 0.9, < 0.10)
d
GEM
remote: https://rubygems.org/
specs:
actionpack (7.0.6)
actionview (= 7.0.6)
activesupport (= 7.0.6)
rack (~> 2.0, >= 2.2.4)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actionview (7.0.6)
activesupport (= 7.0.6)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activesupport (7.0.6)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ansi (1.5.0)
ast (2.4.2)
base64 (0.1.1)
builder (3.2.4)
concurrent-ruby (1.2.2)
crass (1.0.6)
debug (1.8.0)
irb (>= 1.5.0)
reline (>= 0.3.1)
diff-lcs (1.5.0)
erubi (1.12.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
io-console (0.6.0)
irb (1.7.0)
reline (>= 0.3.0)
json (2.6.3)
language_server-protocol (3.17.0.3)
loofah (2.21.3)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
method_source (1.0.0)
mini_portile2 (2.8.2)
minitest (5.19.0)
minitest-reporters (1.6.1)
ansi
Expand All @@ -60,9 +33,6 @@ GEM
mocha (2.1.0)
ruby2_keywords (>= 0.0.5)
netrc (0.11.0)
nokogiri (1.15.2)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand All @@ -71,23 +41,6 @@ GEM
psych (5.1.0)
stringio
racc (1.7.1)
rack (2.2.7)
rack-test (2.1.0)
rack (>= 1.3)
rails-dom-testing (2.1.1)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
rbi (0.0.17)
Expand All @@ -101,7 +54,7 @@ GEM
reline (0.3.5)
io-console (~> 0.5)
rexml (3.2.6)
rubocop (1.56.0)
rubocop (1.56.1)
base64 (~> 0.1.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand All @@ -125,22 +78,22 @@ GEM
rubocop (>= 0.90.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
sorbet (0.5.10978)
sorbet-static (= 0.5.10978)
sorbet-runtime (0.5.10978)
sorbet-static (0.5.10978-universal-darwin-14)
sorbet-static (0.5.10978-universal-darwin-15)
sorbet-static (0.5.10978-universal-darwin-16)
sorbet-static (0.5.10978-universal-darwin-17)
sorbet-static (0.5.10978-universal-darwin-18)
sorbet-static (0.5.10978-universal-darwin-19)
sorbet-static (0.5.10978-universal-darwin-20)
sorbet-static (0.5.10978-universal-darwin-21)
sorbet-static (0.5.10978-universal-darwin-22)
sorbet-static (0.5.10978-x86_64-linux)
sorbet-static-and-runtime (0.5.10978)
sorbet (= 0.5.10978)
sorbet-runtime (= 0.5.10978)
sorbet (0.5.10987)
sorbet-static (= 0.5.10987)
sorbet-runtime (0.5.10987)
sorbet-static (0.5.10987-universal-darwin-14)
sorbet-static (0.5.10987-universal-darwin-15)
sorbet-static (0.5.10987-universal-darwin-16)
sorbet-static (0.5.10987-universal-darwin-17)
sorbet-static (0.5.10987-universal-darwin-18)
sorbet-static (0.5.10987-universal-darwin-19)
sorbet-static (0.5.10987-universal-darwin-20)
sorbet-static (0.5.10987-universal-darwin-21)
sorbet-static (0.5.10987-universal-darwin-22)
sorbet-static (0.5.10987-x86_64-linux)
sorbet-static-and-runtime (0.5.10987)
sorbet (= 0.5.10987)
sorbet-runtime (= 0.5.10987)
spoom (1.2.2)
erubi (>= 1.10.0)
sorbet (>= 0.5.10187)
Expand All @@ -160,8 +113,6 @@ GEM
thor (>= 1.2.0)
yard-sorbet
thor (1.2.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
unparser (0.6.8)
diff-lcs (~> 1.3)
Expand All @@ -171,7 +122,6 @@ GEM
sorbet-runtime (>= 0.5)
yard (>= 0.9)
yarp (0.9.0)
zeitwerk (2.6.11)

PLATFORMS
arm64-darwin
Expand All @@ -187,7 +137,6 @@ DEPENDENCIES
minitest-reporters (~> 1.6)
mocha (~> 2.1)
psych (~> 5.1)
railties (~> 7.0)
rake (~> 13.0)
rdoc
rubocop (~> 1.56)
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.1
0.9.1
3 changes: 0 additions & 3 deletions bin/benchmark
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ require "benchmark"
# Run signature blocks ahead of time
T::Utils.run_all_sig_blocks

# Fetch Rails documents ahead of time
RubyLsp::Requests::Support::RailsDocumentClient.send(:search_index)

# Build gem file paths ahead of time
RubyLsp::Requests::DocumentLink.gem_paths

Expand Down
8 changes: 4 additions & 4 deletions lib/core_ext/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class Generic
class << self
extend T::Sig

sig { params(path: String, scheme: String).returns(URI::Generic) }
def from_path(path:, scheme: "file")
sig { params(path: String, fragment: T.nilable(String), scheme: String).returns(URI::Generic) }
def from_path(path:, fragment: nil, scheme: "file")
# On Windows, if the path begins with the disk name, we need to add a leading slash to make it a valid URI
escaped_path = if /^[A-Z]:/i.match?(path)
DEFAULT_PARSER.escape("/#{path}")
else
DEFAULT_PARSER.escape(path)
end

build(scheme: scheme, path: escaped_path)
build(scheme: scheme, path: escaped_path, fragment: fragment)
end
end

Expand All @@ -26,7 +26,7 @@ def to_standardized_path
parsed_path = path
return unless parsed_path

unescaped_path = CGI.unescape(parsed_path)
unescaped_path = DEFAULT_PARSER.unescape(parsed_path)

# On Windows, when we're getting the file system path back from the URI, we need to remove the leading forward
# slash
Expand Down
10 changes: 5 additions & 5 deletions lib/ruby_indexer/lib/ruby_indexer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ def files_to_index
end

# Add the locked gems to the list of files to be indexed
locked_gems&.each do |spec|
next if excluded_gems.include?(spec.name)
locked_gems&.each do |lazy_spec|
next if excluded_gems.include?(lazy_spec.name)

full_gem_path = Gem::Specification.find_by_name(spec.name).full_gem_path
spec = Gem::Specification.find_by_name(lazy_spec.name)

# When working on a gem, it will be included in the locked_gems list. Since these are the project's own files,
# we have already included and handled exclude patterns for it and should not re-include or it'll lead to
# duplicates or accidentally ignoring exclude patterns
next if full_gem_path == Dir.pwd
next if spec.full_gem_path == Dir.pwd

files_to_index.concat(Dir.glob("#{full_gem_path}/**/*.rb"))
files_to_index.concat(Dir.glob("#{spec.full_gem_path}/{#{spec.require_paths.join(",")}}/**/*.rb"))
rescue Gem::MissingSpecError
# If a gem is scoped only to some specific platform, then its dependencies may not be installed either, but they
# are still listed in locked_gems. We can't index them because they are not installed for the platform, so we
Expand Down
7 changes: 7 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def index_single(path, source = nil)
content = source || File.read(path)
visitor = IndexVisitor.new(self, YARP.parse(content), path)
visitor.run
rescue Errno::EISDIR
# If `path` is a directory, just ignore it and continue indexing
end

class Entry
Expand All @@ -120,6 +122,11 @@ def initialize(name, file_path, location, comments)
@comments = comments
end

sig { returns(String) }
def file_name
File.basename(@file_path)
end

class Namespace < Entry
sig { returns(String) }
def short_name
Expand Down
14 changes: 14 additions & 0 deletions lib/ruby_indexer/test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ def test_load_configuration_executes_configure_block
assert(files_to_index.none? { |path| path == __FILE__ })
end

def test_files_to_index_only_includes_gem_require_paths
@config.load_config
files_to_index = @config.files_to_index

Bundler.locked_gems.specs.each do |lazy_spec|
next if lazy_spec.name == "ruby-lsp"

spec = Gem::Specification.find_by_name(lazy_spec.name)
assert(files_to_index.none? { |path| path.start_with?("#{spec.full_gem_path}/test/") })
rescue Gem::MissingSpecError
# Transitive dependencies might be missing when running tests on Windows
end
end

def test_files_to_index_does_not_include_default_gem_path_when_in_bundle
@config.load_config
files_to_index = @config.files_to_index
Expand Down
7 changes: 7 additions & 0 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,12 @@ class Something
assert_equal(4, result.length)
assert_equal(["Foo::Baz", "Foo::Bar", "Foo", "Foo::Baz::Something"], result.map(&:name))
end

def test_index_single_ignores_directories
FileUtils.mkdir("lib/this_is_a_dir.rb")
@index.index_single("lib/this_is_a_dir.rb")
ensure
FileUtils.rm_r("lib/this_is_a_dir.rb")
end
end
end
6 changes: 6 additions & 0 deletions lib/ruby_lsp/event_emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def emit_for_target(node)
@listeners[:on_constant_path]&.each { |l| T.unsafe(l).on_constant_path(node) }
when YARP::StringNode
@listeners[:on_string]&.each { |l| T.unsafe(l).on_string(node) }
when YARP::ClassNode
@listeners[:on_class]&.each { |l| T.unsafe(l).on_class(node) }
when YARP::ModuleNode
@listeners[:on_module]&.each { |l| T.unsafe(l).on_module(node) }
when YARP::ConstantWriteNode
@listeners[:on_constant_write]&.each { |l| T.unsafe(l).on_constant_write(node) }
end
end

Expand Down
46 changes: 34 additions & 12 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ def run(request)
warn(errored_extensions.map(&:backtraces).join("\n\n"))
end

if @store.experimental_features
# The begin progress invocation happens during `initialize`, so that the notification is sent before we are
# stuck indexing files
RubyIndexer.configuration.load_config
@index.index_all
end_progress("indexing-progress")
end

perform_initial_indexing
check_formatter_is_available

warn("Ruby LSP is ready")
Expand Down Expand Up @@ -209,6 +202,31 @@ def did_change_watched_files(changes)
VOID
end

sig { void }
def perform_initial_indexing
return unless @store.experimental_features

# The begin progress invocation happens during `initialize`, so that the notification is sent before we are
# stuck indexing files
RubyIndexer.configuration.load_config

begin
@index.index_all
rescue StandardError => error
@message_queue << Notification.new(
message: "window/showMessage",
params: Interface::ShowMessageParams.new(
type: Constant::MessageType::ERROR,
message: "Error while indexing: #{error.message}",
),
)
end

# Always end the progress notification even if indexing failed or else it never goes away and the user has no way
# of dismissing it
end_progress("indexing-progress")
end

sig { params(query: T.nilable(String)).returns(T::Array[Interface::WorkspaceSymbol]) }
def workspace_symbol(query)
Requests::WorkspaceSymbol.new(query, @index).run
Expand Down Expand Up @@ -259,16 +277,20 @@ def hover(uri, position)
document = @store.get(uri)
return if document.syntax_error?

target, parent = document.locate_node(position)
target, parent, nesting = document.locate_node(
position,
node_types: Requests::Hover::ALLOWED_TARGETS,
)

if !Requests::Hover::ALLOWED_TARGETS.include?(target.class) &&
Requests::Hover::ALLOWED_TARGETS.include?(parent.class)
if (Requests::Hover::ALLOWED_TARGETS.include?(parent.class) &&
!Requests::Hover::ALLOWED_TARGETS.include?(target.class)) ||
(parent.is_a?(SyntaxTree::ConstPathRef) && target.is_a?(SyntaxTree::Const))
target = parent
end

# Instantiate all listeners
emitter = EventEmitter.new
hover = Requests::Hover.new(emitter, @message_queue)
hover = Requests::Hover.new(@index, nesting, emitter, @message_queue)

# Emit events for all listeners
emitter.emit_for_target(target)
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_lsp/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module RubyLsp
# - [CodeLens](rdoc-ref:RubyLsp::Requests::CodeLens)
# - [Definition](rdoc-ref:RubyLsp::Requests::Definition)
# - [ShowSyntaxTree](rdoc-ref:RubyLsp::Requests::ShowSyntaxTree)
# - [WorkspaceSymbol](rdoc-ref:RubyLsp::Requests::WorkspaceSymbol)

module Requests
autoload :BaseRequest, "ruby_lsp/requests/base_request"
Expand Down
Loading

0 comments on commit 4bb18a6

Please sign in to comment.