Sha256: 9fc55098b67b4f482c97e16b4887389d954055d98150c474aeba41254ece40a2

Contents?: true

Size: 1.16 KB

Versions: 25

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require "benchmark"
require "uri"
require "cgi"

module PlatformosCheck
  module LanguageServer
    module URIHelper
      # Will URI.encode a string the same way VS Code would. There are two things
      # to watch out for:
      #
      # 1. VS Code still uses the outdated '%20' for spaces
      # 2. VS Code prefixes Windows paths with / (so /C:/Users/... is expected)
      #
      # Exists because of https://github.com/Platform-OS/platformos-lsp/issues/360
      def file_uri(absolute_path)
        return if absolute_path.nil?

        "file://" + absolute_path
                    .to_s
                    .split('/')
                    .map { |x| CGI.escape(x).gsub('+', '%20') }
                    .join('/')
                    .sub(%r{^/?}, '/') # Windows paths should be prefixed by /c:
      end

      def file_path(uri_string)
        return if uri_string.nil?

        uri = URI(uri_string)
        path = CGI.unescape(uri.path)
        # On Windows, VS Code sends the URLs as file:///C:/...
        # /C:/1234 is not a valid path in ruby. So we strip the slash.
        path.sub(%r{^/([a-z]:/)}i, '\1')
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.4.14 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.13 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.12 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.11 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.10 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.9 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.8 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.7 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.6 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.5 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.4 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.3 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.2 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.1 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.4.0 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.3.3 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.3.1 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.3.0 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.2.2 lib/platformos_check/language_server/uri_helper.rb
platformos-check-0.2.1 lib/platformos_check/language_server/uri_helper.rb