Skip to content

Commit

Permalink
Merge pull request #2088 from Shopify/ko/tapioca-addon
Browse files Browse the repository at this point in the history
[Tapioca Add-on] Implement boilerplate add-on code
  • Loading branch information
KaanOzkan authored Nov 22, 2024
2 parents d98e8dc + 0bb1961 commit 5434fa6
Show file tree
Hide file tree
Showing 10 changed files with 13,904 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Sorbet/TrueSigil:
Include:
- "**/*.rb"
- "**/*.rake"
Exclude:
- "lib/ruby_lsp/tapioca/server_addon.rb"

Style/CaseEquality:
Enabled: false
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ gem "irb"
gem "rubocop-shopify"
gem "rubocop-sorbet", ">= 0.4.1"
gem "rubocop-rspec" # useful even though we use minitest/spec
gem "ruby-lsp", ">= 0.22.1"
gem "ruby-lsp-rails", ">= 0.3.18"

group :deployment, :development do
gem "rake"
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ GEM
rbi (0.2.1)
prism (~> 1.0)
sorbet-runtime (>= 0.5.9204)
rbs (3.6.1)
logger
rdoc (6.7.0)
psych (>= 4.0.0)
redis (5.0.8)
Expand Down Expand Up @@ -304,6 +306,13 @@ GEM
rubocop (~> 1.51)
rubocop-sorbet (0.8.7)
rubocop (>= 1)
ruby-lsp (0.22.1)
language_server-protocol (~> 3.17.0)
prism (>= 1.2, < 2.0)
rbs (>= 3, < 4)
sorbet-runtime (>= 0.5.10782)
ruby-lsp-rails (0.3.27)
ruby-lsp (>= 0.22.0, < 0.23.0)
ruby-progressbar (1.13.0)
securerandom (0.3.1)
shopify-money (3.0.0)
Expand Down Expand Up @@ -387,6 +396,8 @@ DEPENDENCIES
rubocop-rspec
rubocop-shopify
rubocop-sorbet (>= 0.4.1)
ruby-lsp (>= 0.22.1)
ruby-lsp-rails (>= 0.3.18)
shopify-money
sidekiq
smart_properties
Expand Down
70 changes: 70 additions & 0 deletions lib/ruby_lsp/tapioca/addon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# typed: strict
# frozen_string_literal: true

RubyLsp::Addon.depend_on_ruby_lsp!(">= 0.22.1", "< 0.23")

begin
# The Tapioca add-on depends on the Rails add-on to add a runtime component to the runtime server. We can allow the
# add-on to work outside of a Rails context in the future, but that may require Tapioca spawning its own runtime
# server
require "ruby_lsp/ruby_lsp_rails/runner_client"
rescue LoadError
return
end

module RubyLsp
module Tapioca
class Addon < ::RubyLsp::Addon
extend T::Sig

sig { void }
def initialize
super

@global_state = T.let(nil, T.nilable(RubyLsp::GlobalState))
@rails_runner_client = T.let(nil, T.nilable(RubyLsp::Rails::RunnerClient))
end

sig { override.params(global_state: RubyLsp::GlobalState, outgoing_queue: Thread::Queue).void }
def activate(global_state, outgoing_queue)
@global_state = global_state
return unless @global_state.enabled_feature?(:tapiocaAddon)

Thread.new do
# Get a handle to the Rails add-on's runtime client. The call to `rails_runner_client` will block this thread
# until the server has finished booting, but it will not block the main LSP. This has to happen inside of a
# thread
addon = T.cast(::RubyLsp::Addon.get("Ruby LSP Rails", ">= 0.3.17", "< 0.4"), ::RubyLsp::Rails::Addon)
@rails_runner_client = addon.rails_runner_client
outgoing_queue << Notification.window_log_message("Activating Tapioca add-on v#{version}")
@rails_runner_client.register_server_addon(File.expand_path("server_addon.rb", __dir__))
rescue IncompatibleApiError
# The requested version for the Rails add-on no longer matches. We need to upgrade and fix the breaking
# changes
end
end

sig { override.void }
def deactivate
end

sig { override.returns(String) }
def name
"Tapioca"
end

sig { override.returns(String) }
def version
"0.1.0"
end

sig { params(changes: T::Array[{ uri: String, type: Integer }]).void }
def workspace_did_change_watched_files(changes)
return unless T.must(@global_state).enabled_feature?(:tapiocaAddon)
return unless @rails_runner_client # Client is not ready

nil
end
end
end
end
26 changes: 26 additions & 0 deletions lib/ruby_lsp/tapioca/server_addon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# typed: false
# frozen_string_literal: true

require "tapioca/internal"

module RubyLsp
module Tapioca
class ServerAddon < ::RubyLsp::Rails::ServerAddon
def name
"Tapioca"
end

def execute(request, params)
case request
when "dsl"
dsl(params)
end
end

private

def dsl(params)
end
end
end
end
13 changes: 10 additions & 3 deletions lib/tapioca/helpers/source_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class Source < URI::File
# have the uri gem in their own bundle and thus not use a compatible version.
PARSER = T.let(const_defined?(:RFC2396_PARSER) ? RFC2396_PARSER : DEFAULT_PARSER, RFC2396_Parser)

alias_method(:gem_name, :host)
alias_method(:line_number, :fragment)

sig { returns(T.nilable(String)) }
attr_reader :gem_version

Expand All @@ -54,6 +51,16 @@ def build(gem_name:, gem_version:, path:, line_number:)
end
end

sig { returns(T.nilable(String)) }
def gem_name
host
end

sig { returns(T.nilable(String)) }
def line_number
fragment
end

sig { params(v: T.nilable(String)).void }
def set_path(v) # rubocop:disable Naming/AccessorMethodName
return if v.nil?
Expand Down
Loading

0 comments on commit 5434fa6

Please sign in to comment.