Class: RubyLsp::Ree::SortLinksFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- RubyLsp::Ree::SortLinksFormatter
- Defined in:
- lib/ruby_lsp/ruby_lsp_ree/formatters/sort_links_formatter.rb
Instance Method Summary collapse
Methods inherited from BaseFormatter
Constructor Details
This class inherits a constructor from RubyLsp::Ree::BaseFormatter
Instance Method Details
#call(source, _uri) ⇒ Object
8 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 |
# File 'lib/ruby_lsp/ruby_lsp_ree/formatters/sort_links_formatter.rb', line 8 def call(source, _uri) parsed_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_source(source) return source if !parsed_doc || !parsed_doc.link_nodes&.any? # Order of groups: # - links from the current package without options # - links from the current package with options # - links from other packages # - links with filenames # - import links editor = RubyLsp::Ree::ReeSourceEditor.new(source) renderer = RubyLsp::Ree::LinkRenderer.new # cleanup old links parsed_doc.link_nodes.each do |link_node| editor.remove_link(link_node) end editor.cleanup_blank_lines(parsed_doc.link_nodes.first.location.start_line-1, parsed_doc.link_nodes.last.location.end_line-1) link_groups = [ parsed_doc.link_nodes.select(&:object_name_type?).select{ !_1.has_kwargs? }.sort_by(&:name), parsed_doc.link_nodes.select(&:object_name_type?).select(&:has_kwargs?).select{ !_1.from_arg_value }.sort_by(&:name), parsed_doc.link_nodes.select(&:object_name_type?).select{ !!_1.from_arg_value }.sort_by(&:link_package_name), parsed_doc.link_nodes.select(&:file_path_type?).sort_by(&:name), parsed_doc.link_nodes.select(&:import_link_type?).sort_by(&:link_package_name), ] link_groups_texts = link_groups.map do |link_group| link_group.map{ renderer.render(_1) }.join('') end editor.insert_link_block(parsed_doc, link_groups_texts.select{ _1.size > 0 }.join('')) editor.source end |