Class: SorbetView::Lsp::PositionTranslator
- Inherits:
-
Object
- Object
- SorbetView::Lsp::PositionTranslator
- Extended by:
- T::Sig
- Defined in:
- lib/sorbet_view/lsp/position_translator.rb
Overview
Translates positions between template and generated Ruby using SourceMaps
Instance Method Summary collapse
-
#initialize ⇒ PositionTranslator
constructor
A new instance of PositionTranslator.
- #register(template_path, source_map) ⇒ Object
- #registered_paths ⇒ Object
- #ruby_range_to_template(template_path, lsp_range) ⇒ Object
- #ruby_to_template(template_path, lsp_position) ⇒ Object
- #source_map_for(template_path) ⇒ Object
- #template_to_ruby(template_path, lsp_position) ⇒ Object
- #unregister(template_path) ⇒ Object
Constructor Details
#initialize ⇒ PositionTranslator
Returns a new instance of PositionTranslator.
11 12 13 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 11 def initialize @source_maps = T.let({}, T::Hash[String, SourceMap::SourceMap]) end |
Instance Method Details
#register(template_path, source_map) ⇒ Object
16 17 18 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 16 def register(template_path, source_map) @source_maps[template_path] = source_map end |
#registered_paths ⇒ Object
31 32 33 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 31 def registered_paths @source_maps.keys end |
#ruby_range_to_template(template_path, lsp_range) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 86 def ruby_range_to_template(template_path, lsp_range) start_pos = ruby_to_template(template_path, lsp_range['start'] || lsp_range[:start]) end_pos = ruby_to_template(template_path, lsp_range['end'] || lsp_range[:end]) return nil unless start_pos && end_pos { 'start' => start_pos, 'end' => end_pos } end |
#ruby_to_template(template_path, lsp_position) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 64 def ruby_to_template(template_path, lsp_position) sm = @source_maps[template_path] return nil unless sm pos = SourceMap::Position.new( line: lsp_position['line'] || lsp_position[:line] || 0, column: lsp_position['character'] || lsp_position[:character] || 0 ) template_pos = sm.ruby_to_template(pos) return nil unless template_pos template_pos.to_lsp end |
#source_map_for(template_path) ⇒ Object
26 27 28 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 26 def source_map_for(template_path) @source_maps[template_path] end |
#template_to_ruby(template_path, lsp_position) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 42 def template_to_ruby(template_path, lsp_position) sm = @source_maps[template_path] return nil unless sm pos = SourceMap::Position.new( line: lsp_position['line'] || lsp_position[:line] || 0, column: lsp_position['character'] || lsp_position[:character] || 0 ) ruby_pos = sm.template_to_ruby(pos) return nil unless ruby_pos ruby_pos.to_lsp end |
#unregister(template_path) ⇒ Object
21 22 23 |
# File 'lib/sorbet_view/lsp/position_translator.rb', line 21 def unregister(template_path) @source_maps.delete(template_path) end |