Sha256: 1dc229199f6a63a0d01e0bd63ad574c6824c77b7e25ef84a82eef9d9af6ef8f9

Contents?: true

Size: 1.3 KB

Versions: 23

Compression:

Stored size: 1.3 KB

Contents

# typed: strict
# frozen_string_literal: true

module URI
  class Generic
    class << self
      extend T::Sig

      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}")
        elsif path.start_with?("//?/")
          # Some paths on Windows start with "//?/". This is a special prefix that allows for long file paths
          DEFAULT_PARSER.escape(path.delete_prefix("//?"))
        else
          DEFAULT_PARSER.escape(path)
        end

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

    extend T::Sig

    sig { returns(T.nilable(String)) }
    def to_standardized_path
      parsed_path = path
      return unless 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
      if %r{^/[A-Z]:}i.match?(unescaped_path)
        unescaped_path.delete_prefix("/")
      else
        unescaped_path
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

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