Skip to content

Commit

Permalink
Merge pull request #2 from st0012/use-yarp
Browse files Browse the repository at this point in the history
Use yarp-powered ruby-lsp
  • Loading branch information
st0012 authored Oct 4, 2023
2 parents 77a9cdd + 8d01cf9 commit 6464930
Show file tree
Hide file tree
Showing 13 changed files with 2,725 additions and 5,408 deletions.
58 changes: 26 additions & 32 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
ruby-lsp-rspec (0.1.2)
ruby-lsp (~> 0.10.1)
ruby-lsp (~> 0.11.0)

GEM
remote: https://rubygems.org/
Expand All @@ -22,7 +22,7 @@ GEM
language_server-protocol (3.17.0.3)
netrc (0.11.0)
parallel (1.23.0)
parser (3.2.2.3)
parser (3.2.2.4)
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
Expand All @@ -31,15 +31,13 @@ GEM
racc (1.7.1)
rainbow (3.1.1)
rake (13.0.6)
rbi (0.0.17)
ast
parser (>= 3.0.0)
rbi (0.1.1)
sorbet-runtime (>= 0.5.9204)
unparser (>= 0.5.6)
yarp (>= 0.11.0)
rdoc (6.5.0)
psych (>= 4.0.0)
regexp_parser (2.8.1)
reline (0.3.8)
reline (0.3.9)
io-console (~> 0.5)
rexml (3.2.6)
rspec (3.12.0)
Expand All @@ -51,11 +49,11 @@ GEM
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rubocop (1.56.2)
rspec-support (3.12.1)
rubocop (1.56.4)
base64 (~> 0.1.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand All @@ -73,49 +71,45 @@ GEM
rubocop (~> 1.0)
rubocop-shopify (2.14.0)
rubocop (~> 1.51)
rubocop-sorbet (0.7.3)
rubocop-sorbet (0.7.4)
rubocop (>= 0.90.0)
ruby-lsp (0.10.1)
ruby-lsp (0.11.1)
language_server-protocol (~> 3.17.0)
sorbet-runtime
syntax_tree (>= 6.1.1, < 7)
yarp (>= 0.11, < 0.12)
sorbet-runtime (>= 0.5.5685)
yarp (>= 0.12, < 0.13)
ruby-progressbar (1.13.0)
sorbet (0.5.11010)
sorbet-static (= 0.5.11010)
sorbet-runtime (0.5.11010)
sorbet-static (0.5.11010-universal-darwin)
sorbet-static (0.5.11010-x86_64-linux)
sorbet-static-and-runtime (0.5.11010)
sorbet (= 0.5.11010)
sorbet-runtime (= 0.5.11010)
spoom (1.2.3)
sorbet (0.5.11062)
sorbet-static (= 0.5.11062)
sorbet-runtime (0.5.11062)
sorbet-static (0.5.11062-universal-darwin)
sorbet-static (0.5.11062-x86_64-linux)
sorbet-static-and-runtime (0.5.11062)
sorbet (= 0.5.11062)
sorbet-runtime (= 0.5.11062)
spoom (1.2.4)
erubi (>= 1.10.0)
sorbet-static-and-runtime (>= 0.5.10187)
syntax_tree (>= 6.1.1)
thor (>= 0.19.2)
stringio (3.0.8)
syntax_tree (6.1.1)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
tapioca (0.11.8)
tapioca (0.11.9)
bundler (>= 2.2.25)
netrc (>= 0.11.0)
parallel (>= 1.21.0)
rbi (~> 0.0.0, >= 0.0.16)
rbi (~> 0.1.0, >= 0.1.0)
sorbet-static-and-runtime (>= 0.5.10187)
spoom (~> 1.2.0, >= 1.2.0)
thor (>= 1.2.0)
yard-sorbet
thor (1.2.2)
unicode-display_width (2.4.2)
unparser (0.6.8)
diff-lcs (~> 1.3)
parser (>= 3.2.0)
unicode-display_width (2.5.0)
yard (0.9.34)
yard-sorbet (0.8.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
yarp (0.11.0)
yarp (0.12.0)

PLATFORMS
arm64-darwin-22
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Gem Version](https://badge.fury.io/rb/ruby-lsp-rspec.svg)](https://badge.fury.io/rb/ruby-lsp-rspec)

Ruby LSP RSpec is a [Ruby LSP](https://github.com/Shopify/ruby-lsp) extension for displaying CodeLens for RSpec tests.
Ruby LSP RSpec is a [Ruby LSP](https://github.com/Shopify/ruby-lsp) addon for displaying CodeLens for RSpec tests.

![Screenshot of the code lenses](/misc/example.gif)

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-lsp-rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require "sorbet-runtime"
require "ruby_lsp_rspec/version"
require "ruby_lsp/ruby_lsp_rspec/extension"
require "ruby_lsp/ruby_lsp_rspec/addon"

module RubyLsp
module RSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# typed: strict
# frozen_string_literal: true

require "ruby_lsp/extension"
require "ruby_lsp/addon"
require "ruby_lsp/internal"

require_relative "code_lens"

module RubyLsp
module RSpec
class Extension < ::RubyLsp::Extension
class Addon < ::RubyLsp::Addon
extend T::Sig

sig { override.void }
Expand Down
92 changes: 27 additions & 65 deletions lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CodeLens < ::RubyLsp::Listener
def initialize(uri, emitter, message_queue)
@_response = T.let([], ResponseType)
@path = T.let(uri.to_standardized_path, T.nilable(String))
emitter.register(self, :on_command, :on_command_call, :on_call)
emitter.register(self, :on_call)

@base_command = T.let(
begin
Expand All @@ -40,81 +40,43 @@ def initialize(uri, emitter, message_queue)
super(emitter, message_queue)
end

sig { params(node: SyntaxTree::CallNode).void }
sig { params(node: YARP::CallNode).void }
def on_call(node)
add_group_code_lens(node)
end

sig { params(node: SyntaxTree::CommandCall).void }
def on_command_call(node)
add_group_code_lens(node)
end

sig { params(node: SyntaxTree::Command).void }
def on_command(node)
message_value = node.message.value
message_value = node.message
if message_value == "it"
argument = node.arguments.parts.first

name = case argument
when SyntaxTree::StringLiteral
argument.parts.first.value
when SyntaxTree::VarRef
argument.value.value
end

name = generate_name(node)
add_test_code_lens(node, name: name, kind: :example)
elsif message_value == "describe" || message_value == "context"
add_group_code_lens(node)
end
end

private
return if node.receiver && node.receiver.name.to_s != "RSpec"

sig do
params(node: T.any(
SyntaxTree::StringLiteral,
SyntaxTree::ConstPathRef,
SyntaxTree::ConstRef,
SyntaxTree::TopConstRef,
SyntaxTree::VarRef,
)).returns(T.nilable(String))
end
def get_group_name(node)
case node
when SyntaxTree::StringLiteral
node.parts.first.value
when SyntaxTree::ConstPathRef, SyntaxTree::ConstRef, SyntaxTree::TopConstRef
full_constant_name(node)
when SyntaxTree::VarRef
node.value.value
name = generate_name(node)
add_test_code_lens(node, name: name, kind: :group)
end
end

sig { params(node: T.any(SyntaxTree::CallNode, SyntaxTree::CommandCall, SyntaxTree::Command)).void }
def add_group_code_lens(node)
return unless node.message.value == "describe" || node.message.value == "context"

case node
when SyntaxTree::CommandCall, SyntaxTree::CallNode
return if node.receiver && node.receiver.value.value != "RSpec"
end

argument = case node
when SyntaxTree::CallNode
node.arguments.arguments.parts.first
when SyntaxTree::CommandCall, SyntaxTree::Command
node.arguments.parts.first
sig { params(node: YARP::CallNode).returns(String) }
def generate_name(node)
if node.arguments
argument = node.arguments.arguments.first

case argument
when YARP::StringNode
argument.content
when YARP::CallNode
"<#{argument.name}>"
when nil
""
else
argument.slice
end
else
"<unnamed>"
end

name = get_group_name(argument)

return unless name

add_test_code_lens(node, name: name, kind: :group)
end

sig { params(node: SyntaxTree::Node, name: String, kind: Symbol).void }
private

sig { params(node: YARP::Node, name: String, kind: Symbol).void }
def add_test_code_lens(node, name:, kind:)
return unless @path

Expand Down
6 changes: 3 additions & 3 deletions ruby-lsp-rspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
spec.authors = ["Stan Lo"]
spec.email = ["[email protected]"]

spec.summary = "RSpec extension for ruby-lsp"
spec.description = "RSpec extension for ruby-lsp"
spec.summary = "RSpec addon for ruby-lsp"
spec.description = "RSpec addon for ruby-lsp"
spec.homepage = "https://github.com/st0012/ruby-lsp-rspec"
spec.license = "MIT"
spec.required_ruby_version = ">= 3.0.0"
Expand Down Expand Up @@ -41,5 +41,5 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "ruby-lsp", "~> 0.10.1"
spec.add_dependency "ruby-lsp", "~> 0.11.0"
end
1 change: 1 addition & 0 deletions sorbet/rbi/gems/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.rbi linguist-generated=true
Loading

0 comments on commit 6464930

Please sign in to comment.