Class: RubyLsp::Ree::MissingErrorLocalesFormatter

Inherits:
BaseFormatter
  • Object
show all
Includes:
ReeLocaleUtils, ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/formatters/missing_error_locales_formatter.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

Methods inherited from BaseFormatter

call, #initialize

Constructor Details

This class inherits a constructor from RubyLsp::Ree::BaseFormatter

Instance Method Details

#call(source, uri) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_lsp/ruby_lsp_ree/formatters/missing_error_locales_formatter.rb', line 11

def call(source, uri)
  parsed_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_source(source)
  return source if !parsed_doc || !parsed_doc.has_root_class?

  locales_folder = package_locales_folder_path(get_uri_path(uri))
  return source if !locales_folder || !File.directory?(locales_folder)

  file_name = File.basename(uri.to_s, '.rb')

  result = []
  key_paths = []
  parsed_doc.parse_error_definitions
  parsed_doc.error_definitions.each do |error_definition|
    key_path_entries = if error_definition.value.arguments.arguments.size > 1
      [error_definition.value.arguments.arguments[1].unescaped]
    else
      mod = underscore(parsed_doc.module_name)
      [
        "#{mod}.errors.#{error_definition.value.arguments.arguments[0].unescaped}",
        "#{mod}.errors.#{file_name}.#{error_definition.value.arguments.arguments[0].unescaped}"
      ]
    end

    key_paths << key_path_entries
  end

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

        add_locale_placeholder(locale_file, key_path)    
        send_message(locale_file, key_path)      
      end
    end
  end

  source
end