Class: RubyLsp::Ree::HoverHandler

Inherits:
Object
  • Object
show all
Includes:
Requests::Support::Common, ReeLocaleUtils, ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb

Constant Summary collapse

MISSING_LOCALE_PLACEHOLDER =
'_MISSING_LOCALE_'

Constants included from ReeLspUtils

ReeLspUtils::Entry

Instance Method Summary collapse

Methods included from ReeLocaleUtils

#find_locale_key_location, #find_locale_value, #package_locales_folder_path

Methods included from ReeLspUtils

#camelize, #find_local_file_path, #get_range_for_fn_insert, #get_ree_type, #get_uri_path, #package_name_from_spec_uri, #package_name_from_uri, #package_path_from_uri, #parameter_name, #path_from_package_folder, #signature_params_from_node, #spec_relative_file_path_from_uri, #underscore

Constructor Details

#initialize(index, node_context) ⇒ HoverHandler

Returns a new instance of HoverHandler.



16
17
18
19
20
21
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 16

def initialize(index, node_context)
  @index = index
  @node_context = node_context
  @finder = ReeObjectFinder.new(@index)
  @root_node = @node_context.instance_variable_get(:@nesting_nodes).first
end

Instance Method Details

#get_detail_string(ree_object) ⇒ Object



58
59
60
61
62
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 58

def get_detail_string(ree_object)
  return '' if ree_object.signatures.size == 0

  "(#{get_parameters_string(ree_object.signatures.first)})"
end

#get_error_code_hover_items(node) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 103

def get_error_code_hover_items(node)
  parsed_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_ast(@root_node, nil)
  uri = get_uri_from_object(parsed_doc)

  locales_folder = package_locales_folder_path(uri.path)
  return [] unless File.directory?(locales_folder)

  result = []

  key_path_entries = if @node_context.parent.arguments.arguments.size > 1
    [@node_context.parent.arguments.arguments[1].unescaped]
  else
    file_name = File.basename(uri.path, '.rb')

    mod = underscore(parsed_doc.module_name)
    [
      "#{mod}.errors.#{node.unescaped}",
      "#{mod}.errors.#{file_name}.#{node.unescaped}"
    ]
  end

  documentation = ''

  Dir.glob(File.join(locales_folder, '**/*.yml')).each do |locale_file|
    value = key_path_entries.map{ find_locale_value(locale_file, _1) }.compact.first

    loc_key = File.basename(locale_file, '.yml')

    if value
      if value == MISSING_LOCALE_PLACEHOLDER
       # value_location = find_locale_key_location(locale_file, key_path)
        # file_uri = "file://#{locale_file}##{value_location.line+1}"
        # link doesn't work with ssh remote mode
        documentation += "#{loc_key}: #{value}\n\n"
      else
        documentation += "#{loc_key}: #{value}\n\n"
      end
    else
      documentation += "#{loc_key}: [MISSING TRANSLATION](#{locale_file})\n\n"
    end
  end

  [documentation]
end

#get_error_locales_hover_items(node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 70

def get_error_locales_hover_items(node)
  parsed_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_ast(@root_node, nil)
  uri = get_uri_from_object(parsed_doc)

  locales_folder = package_locales_folder_path(uri.path)
  return [] unless File.directory?(locales_folder)

  result = []
  key_path = node.unescaped

  documentation = ''

  Dir.glob(File.join(locales_folder, '**/*.yml')).each do |locale_file|
    value = find_locale_value(locale_file, key_path)
    loc_key = File.basename(locale_file, '.yml')

    if value
      if value == MISSING_LOCALE_PLACEHOLDER
        # value_location = find_locale_key_location(locale_file, key_path)
        # file_uri = "file://#{locale_file}##{value_location.line+1}"
        # link doesn't work with ssh remote mode
        documentation += "#{loc_key}: #{value}\n\n"
      else
        documentation += "#{loc_key}: #{value}\n\n"
      end
    else
      documentation += "#{loc_key}: [MISSING TRANSLATION](#{locale_file})\n\n"
    end
  end

  [documentation]
end

#get_linked_object_hover_items(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 33

def get_linked_object_hover_items(node)
  parent_node = @node_context.parent
  return [] unless parent_node.name == :link

  ree_object = @finder.find_object(node.unescaped)

  return [] unless ree_object

  documentation = get_object_documentation(ree_object)

  [documentation]
end

#get_object_documentation(ree_object) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 46

def get_object_documentation(ree_object)
  <<~DOC
  \`\`\`ruby
  #{ree_object.name}#{get_detail_string(ree_object)}
  \`\`\`
  ---
  #{@finder.object_documentation(ree_object)}

  [#{path_from_package_folder(ree_object.uri)}](#{ree_object.uri})
  DOC
end

#get_parameters_string(signature) ⇒ Object



64
65
66
67
68
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 64

def get_parameters_string(signature)
  return '' unless signature

  signature.parameters.map(&:decorated_name).join(', ')
end

#get_ree_object_hover_items(node) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 23

def get_ree_object_hover_items(node)
  ree_object = @finder.find_object(node.name.to_s)

  return [] unless ree_object

  documentation = get_object_documentation(ree_object)

  [documentation]
end

#get_uri_from_object(parsed_doc) ⇒ Object



148
149
150
151
152
153
# File 'lib/ruby_lsp/ruby_lsp_ree/handlers/hover_handler.rb', line 148

def get_uri_from_object(parsed_doc)
  obj = parsed_doc.links_container_node_name

  ree_obj = @finder.find_object(obj)
  ree_obj.uri
end