1- # typed: true
1+ # typed: strict
22# frozen_string_literal: true
33
44require "uri/file"
55
6- module Tapioca
7- class SourceURI < URI :: File
8- extend T :: Sig
6+ # Don't redefine this class if RubyLSP already defined it.
7+ # This also prevents us from registering two different classes for the `SOURCE` scheme.
8+ return if defined? ( URI :: Source )
99
10+ module URI
11+ # Must be kept in sync with the one in Ruby LSP
12+ # https://github.com/Shopify/ruby-lsp/blob/main/lib/ruby_lsp/requests/support/source_uri.rb
13+ class Source < ::URI ::File
1014 COMPONENT = [
1115 :scheme ,
1216 :gem_name ,
@@ -22,12 +26,17 @@ class SourceURI < URI::File
2226 # have the uri gem in their own bundle and thus not use a compatible version.
2327 PARSER = const_defined? ( :RFC2396_PARSER ) ? RFC2396_PARSER : DEFAULT_PARSER #: RFC2396_Parser
2428
29+ # We need to hide these aliased methods from Sorbet, because it would otherwise complain about these
30+ # being redefinitions of the existing methods it knows about from RubyLSP.
31+ self #: as untyped # rubocop:disable Style/RedundantSelf
32+ . alias_method ( :gem_name , :host )
33+ self #: as untyped # rubocop:disable Style/RedundantSelf
34+ . alias_method ( :line_number , :fragment )
35+
2536 #: String?
2637 attr_reader :gem_version
2738
2839 class << self
29- extend T ::Sig
30-
3140 #: (gem_name: String, gem_version: String?, path: String, line_number: String?) -> instance
3241 def build ( gem_name :, gem_version :, path :, line_number :)
3342 super (
@@ -41,21 +50,14 @@ def build(gem_name:, gem_version:, path:, line_number:)
4150 end
4251 end
4352
44- #: -> String?
45- def gem_name
46- host
47- end
48-
49- #: -> String?
50- def line_number
51- fragment
52- end
53-
5453 #: (String? v) -> void
5554 def set_path ( v ) # rubocop:disable Naming/AccessorMethodName
5655 return if v . nil?
56+
57+ gem_version , path = v . delete_prefix ( "/" ) . split ( "/" , 2 )
5758
58- @gem_version , @path = v . split ( "/" , 2 )
59+ @gem_version = gem_version #: String?
60+ @path = path #: String?
5961 end
6062
6163 #: (String? v) -> bool
@@ -76,8 +78,11 @@ def to_s
7678 end
7779
7880 if URI . respond_to? ( :register_scheme )
81+ # Handle URI 0.11.0 and newer https://github.com/ruby/uri/pull/26
7982 URI . register_scheme ( "SOURCE" , self )
8083 else
84+ # Fallback for URI <0.11.0
85+ @@schemes = @@schemes #: Hash[String, untyped] # rubocop:disable Style/ClassVars
8186 @@schemes [ "SOURCE" ] = self
8287 end
8388 end
0 commit comments