Sha256: ef4e5d029de69637d868dff37a376ba3994e8bbeb783e300f7466fc38fc9d6af

Contents?: true

Size: 1.95 KB

Versions: 75

Compression:

Stored size: 1.95 KB

Contents

# typed: strict
# frozen_string_literal: true

require "uri/file"

module URI
  # Must be kept in sync with the one in Tapioca
  class Source < URI::File
    extend T::Sig

    COMPONENT = T.let(
      [
        :scheme,
        :gem_name,
        :gem_version,
        :path,
        :line_number,
      ].freeze,
      T::Array[Symbol],
    )

    T.unsafe(self).alias_method(:gem_name, :host)
    T.unsafe(self).alias_method(:line_number, :fragment)

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

    class << self
      extend T::Sig

      sig do
        params(
          gem_name: String,
          gem_version: T.nilable(String),
          path: String,
          line_number: T.nilable(String),
        ).returns(URI::Source)
      end
      def build(gem_name:, gem_version:, path:, line_number:)
        super(
          {
            scheme: "source",
            host: gem_name,
            path: DEFAULT_PARSER.escape("/#{gem_version}/#{path}"),
            fragment: line_number,
          }
        )
      end
    end

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

      gem_version, path = v.delete_prefix("/").split("/", 2)

      @gem_version = T.let(gem_version, T.nilable(String))
      @path = T.let(path, T.nilable(String))
    end

    sig { params(v: T.nilable(String)).returns(T::Boolean) }
    def check_host(v)
      return true unless v

      if /[A-Za-z][A-Za-z0-9\-_]*/ !~ v
        raise InvalidComponentError,
          "bad component(expected gem name): #{v}"
      end

      true
    end

    sig { returns(String) }
    def to_s
      "source://#{gem_name}/#{gem_version}#{path}##{line_number}"
    end

    if URI.respond_to?(:register_scheme)
      URI.register_scheme("SOURCE", self)
    else
      @@schemes = T.let(@@schemes, T::Hash[String, T.untyped]) # rubocop:disable Style/ClassVars
      @@schemes["SOURCE"] = self
    end
  end
end

Version data entries

75 entries across 75 versions & 2 rubygems

Version Path
ruby-lsp-0.18.4 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.18.3 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.18.2 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.18.1 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.18.0 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.17 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.16 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.15 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.14 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.13 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.12 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.11 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.10 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.9 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.8 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.7 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.6 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.5 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.4 lib/ruby_lsp/requests/support/source_uri.rb
ruby-lsp-0.17.3 lib/ruby_lsp/requests/support/source_uri.rb