@@ -29,6 +29,7 @@ def initialize(client, response_builder, node_context, global_state, dispatcher)
2929 :on_constant_path_node_enter ,
3030 :on_constant_read_node_enter ,
3131 :on_symbol_node_enter ,
32+ :on_string_node_enter ,
3233 )
3334 end
3435
@@ -56,6 +57,11 @@ def on_symbol_node_enter(node)
5657 handle_possible_dsl ( node )
5758 end
5859
60+ #: (Prism::StringNode node) -> void
61+ def on_string_node_enter ( node )
62+ handle_possible_i18n ( node )
63+ end
64+
5965 private
6066
6167 #: (String name) -> void
@@ -156,6 +162,36 @@ def handle_association(node)
156162 generate_hover ( result [ :name ] )
157163 end
158164
165+ #: (Prism::StringNode node) -> void
166+ def handle_possible_i18n ( node )
167+ call_node = @node_context . call_node
168+ return unless i18n_translate? ( call_node )
169+
170+ first_argument = call_node #: as !nil
171+ . arguments &.arguments &.first
172+ return unless first_argument == node
173+
174+ i18n_key = first_argument . unescaped
175+ return if i18n_key . empty?
176+
177+ result = @client . i18n ( i18n_key )
178+ return unless result
179+
180+ generate_i18n_hover ( result )
181+ end
182+
183+ #: (Prism::CallNode? call_node) -> bool
184+ def i18n_translate? ( call_node )
185+ return false unless call_node
186+
187+ receiver = call_node . receiver
188+ return false unless receiver . is_a? ( Prism ::ConstantReadNode )
189+ return false unless receiver . name == :I18n
190+
191+ message = call_node . message
192+ message == "t" || message == "translate"
193+ end
194+
159195 # Copied from `RubyLsp::Listeners::Hover#generate_hover`
160196 #: (String name) -> void
161197 def generate_hover ( name )
@@ -172,6 +208,16 @@ def generate_hover(name)
172208 @response_builder . push ( content , category : category )
173209 end
174210 end
211+
212+ #: (Hash[Symbol, String] translations) -> void
213+ def generate_i18n_hover ( translations )
214+ content = translations . map { |lang , translation | "#{ lang } : #{ translation } " } . join ( "\n " )
215+ content = "```yaml\n #{ content } \n ```"
216+ @response_builder . push (
217+ content ,
218+ category : :documentation ,
219+ )
220+ end
175221 end
176222 end
177223end
0 commit comments