Class: RubyLsp::Ree::SyncDaoColumnsFormatter

Inherits:
BaseFormatter show all
Includes:
ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/formatters/sync_dao_columns_formatter.rb

Constant Summary

Constants included from ReeLspUtils

ReeLspUtils::Entry

Instance Method Summary collapse

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



9
10
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
# File 'lib/ruby_lsp/ruby_lsp_ree/formatters/sync_dao_columns_formatter.rb', line 9

def call(source, uri)
  path = get_uri_path(uri)
  path_parts = path.split('/')
  return source unless path_parts.include?('dao')
  
  parsed_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_source(source, type: :dao)
  return source if !parsed_doc
  
  parsed_doc.parse_class_includes
  return source unless parsed_doc.includes_dao_dsl?
  return source unless parsed_doc.has_schema?

  source = deduplicate_dao_fields(parsed_doc, source)

  dao_folder_index = path_parts.index('dao')
  entities_folder = path_parts.take(dao_folder_index).join('/') + '/entities'

  entity_filename = underscore(parsed_doc.schema_name) + '.rb'

  entity_paths = Dir[File.join(entities_folder, '**', entity_filename)]
  if entity_paths.size > 1
    $stderr.puts("multiple entity paths for #{path}")
    return source
  elsif entity_paths.size == 0
    $stderr.puts("no entity paths #{path}")
    return source
  end

  entity_path = entity_paths.first
  entity_source = File.read(entity_path)
  entity_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_source(entity_source, type: :entity)

  missed_column_names = parsed_doc.dao_fields.map(&:name) - entity_doc.columns.map(&:name)
  missed_columns = parsed_doc.dao_fields.select{ missed_column_names.include?(_1.name) }
  add_columns(entity_path, entity_source, entity_doc, missed_columns)

  source
end