Sha256: b45c71fadf4281598f67d5017f0aa863256c477477538b5fc569a854fcbe266b

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# typed: strict
# frozen_string_literal: true

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

      sig { params(path: String, scheme: String).returns(URI::Generic) }
      def from_path(path:, 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}")
        else
          DEFAULT_PARSER.escape(path)
        end

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

    extend T::Sig

    sig { returns(T.nilable(String)) }
    def to_standardized_path
      parsed_path = path
      return unless parsed_path

      unescaped_path = CGI.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

1 entries across 1 versions & 1 rubygems

Version Path
ruby-lsp-0.8.1 lib/core_ext/uri.rb