Class: SorbetView::Lsp::UriMapper
- Inherits:
-
Object
- Object
- SorbetView::Lsp::UriMapper
- Extended by:
- T::Sig
- Defined in:
- lib/sorbet_view/lsp/uri_mapper.rb
Overview
Maps between template file URIs and generated Ruby file URIs
Constant Summary collapse
- TEMPLATE_EXTENSIONS =
T.let(%w[.erb .haml .slim].freeze, T::Array[String])
Instance Method Summary collapse
- #component_to_ruby_uri(component_uri) ⇒ Object
- #generated_ruby_uri?(uri) ⇒ Boolean
-
#initialize(config:) ⇒ UriMapper
constructor
A new instance of UriMapper.
- #path_to_uri(path) ⇒ Object
- #ruby_to_template_uri(ruby_uri) ⇒ Object
- #set_roots(editor_root:, local_root:) ⇒ Object
- #template_to_ruby_uri(template_uri) ⇒ Object
- #template_uri?(uri) ⇒ Boolean
- #uri_to_path(uri) ⇒ Object
Constructor Details
#initialize(config:) ⇒ UriMapper
Returns a new instance of UriMapper.
15 16 17 18 19 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 15 def initialize(config:) @output_dir = config.output_dir @editor_root = T.let(nil, T.nilable(String)) @local_root = T.let(nil, T.nilable(String)) end |
Instance Method Details
#component_to_ruby_uri(component_uri) ⇒ Object
52 53 54 55 56 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 52 def component_to_ruby_uri(component_uri) path = uri_to_path(component_uri) ruby_path = File.join(@output_dir, "#{path}__erb_template.rb") path_to_uri(ruby_path) end |
#generated_ruby_uri?(uri) ⇒ Boolean
37 38 39 40 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 37 def generated_ruby_uri?(uri) path = uri_to_path(uri) path.start_with?(@output_dir) || path.include?("/#{@output_dir}/") end |
#path_to_uri(path) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 98 def path_to_uri(path) absolute = File.(path) # Map local root -> editor root if @editor_root && @local_root && absolute.start_with?(@local_root) absolute = absolute.sub(@local_root, @editor_root) end "file://#{URI.encode_www_form_component(absolute).gsub('%2F', '/')}" end |
#ruby_to_template_uri(ruby_uri) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 60 def ruby_to_template_uri(ruby_uri) path = uri_to_path(ruby_uri) # Remove output_dir prefix and .rb suffix relative = path.sub(%r{^.*/#{Regexp.escape(@output_dir)}/}, '') .sub(%r{^#{Regexp.escape(@output_dir)}/}, '') return nil unless relative.end_with?('.rb') # Component heredoc: output is foo.rb__erb_template.rb → source is foo.rb if relative.end_with?('__erb_template.rb') template_path = relative.chomp('__erb_template.rb') return path_to_uri(template_path) end template_path = relative.chomp('.rb') path_to_uri(template_path) end |
#set_roots(editor_root:, local_root:) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 23 def set_roots(editor_root:, local_root:) return if editor_root == local_root @editor_root = editor_root @local_root = local_root end |
#template_to_ruby_uri(template_uri) ⇒ Object
44 45 46 47 48 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 44 def template_to_ruby_uri(template_uri) path = uri_to_path(template_uri) ruby_path = File.join(@output_dir, "#{path}.rb") path_to_uri(ruby_path) end |
#template_uri?(uri) ⇒ Boolean
31 32 33 34 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 31 def template_uri?(uri) path = uri_to_path(uri) TEMPLATE_EXTENSIONS.any? { |ext| path.end_with?(ext) } end |
#uri_to_path(uri) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/sorbet_view/lsp/uri_mapper.rb', line 79 def uri_to_path(uri) path = if uri.start_with?('file://') URI.decode_www_form_component(URI.parse(uri).path || '') else uri end # Map editor root -> local root if @editor_root && @local_root && path.start_with?(@editor_root) path = path.sub(@editor_root, @local_root) end # Make relative to CWD cwd = Dir.pwd if path.start_with?("#{cwd}/") path = path.delete_prefix("#{cwd}/") end path end |