Module: Go::Merge
- Defined in:
- lib/go/merge.rb,
lib/go/merge/version.rb,
lib/go/merge/provider.rb,
lib/go/merge/node_wrapper.rb,
lib/go/merge/file_analysis.rb,
sig/go/merge.rbs
Overview
Native Go parsing and source-preserving merge APIs.
Defined Under Namespace
Modules: Version Classes: FileAnalysis, NodeWrapper, Provider
Constant Summary collapse
- PACKAGE_NAME =
'go-merge'- TREE_SITTER_BACKEND =
TreeHaver::KREUZBERG_LANGUAGE_PACK_BACKEND
- DESTINATION_WINS_ARRAY_POLICY =
{ surface: 'array', name: 'destination_wins_array' }.freeze
- BACKEND_REGISTRY =
Struct.new(:registered, :mutex).new(false, Mutex.new)
- VERSION =
Current gem version exposed at the traditional constant location.
Version::VERSION
Class Method Summary collapse
- .available_go_backends ⇒ Object
- .go_backend_available_for_analysis?(backend_id) ⇒ Boolean
- .go_backend_feature_profile(backend: nil) ⇒ Object
- .go_feature_profile ⇒ Object
- .go_plan_context(backend: nil) ⇒ Object
- .match_go_owners(template, destination) ⇒ Object
-
.merge_go(template_source, destination_source, dialect) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity -- legacy merge result assembly remains one compatibility boundary.
- .merge_provider ⇒ Provider
- .parse_go(source, dialect) ⇒ Object
- .register_backend! ⇒ Object
- .register_provider!(replace: false) ⇒ Object
Class Method Details
.available_go_backends ⇒ Object
39 40 41 |
# File 'lib/go/merge.rb', line 39 def available_go_backends go_backend_available_for_analysis?(TREE_SITTER_BACKEND.id) ? [TREE_SITTER_BACKEND] : [] end |
.go_backend_available_for_analysis?(backend_id) ⇒ Boolean
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/go/merge.rb', line 80 def go_backend_available_for_analysis?(backend_id) register_backend! if backend_id.to_s.empty? TreeHaver.parser_for(:go, backend_type: :tree_sitter) else TreeHaver.with_backend(backend_id) { TreeHaver.parser_for(:go, backend_type: :tree_sitter) } end true rescue TreeHaver::Error, ArgumentError false end |
.go_backend_feature_profile(backend: nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/go/merge.rb', line 43 def go_backend_feature_profile(backend: nil) requested = requested_tree_sitter_backend_id(backend) unless go_backend_available_for_analysis?(requested) return unsupported_feature_result("Unsupported Go backend #{requested}.") end go_feature_profile.merge( backend: requested, backend_ref: TREE_SITTER_BACKEND.to_h, supports_dialects: true ) end |
.go_feature_profile ⇒ Object
35 36 37 |
# File 'lib/go/merge.rb', line 35 def go_feature_profile { family: 'go', supported_dialects: ['go'], supported_policies: [DESTINATION_WINS_ARRAY_POLICY] } end |
.go_plan_context(backend: nil) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/go/merge.rb', line 56 def go_plan_context(backend: nil) profile = go_backend_feature_profile(backend: backend) return profile if profile[:ok] == false { family_profile: go_feature_profile, feature_profile: { backend: profile[:backend], supports_dialects: true, supported_policies: profile[:supported_policies] } } end |
.match_go_owners(template, destination) ⇒ Object
101 102 103 |
# File 'lib/go/merge.rb', line 101 def match_go_owners(template, destination) Ast::Merge::OwnerSelection.match_by_path(template, destination) end |
.merge_go(template_source, destination_source, dialect) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity -- legacy merge result assembly remains one compatibility boundary
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 |
# File 'lib/go/merge.rb', line 114 def merge_go(template_source, destination_source, dialect) template = parse_go(template_source, dialect) return { ok: false, diagnostics: template[:diagnostics], policies: [] } unless template[:ok] destination = parse_go(destination_source, dialect) unless destination[:ok] return { ok: false, diagnostics: destination[:diagnostics].map do |diagnostic| diagnostic[:category] == 'parse_error' ? diagnostic.merge(category: 'destination_parse_error') : diagnostic end, policies: [] } end destination_declarations = destination.dig(:analysis, :declarations).to_h { |item| [item[:path], item] } template_additions = template.dig(:analysis, :declarations).reject do |item| destination_declarations[item[:path]] end merged_declaration_texts = destination.dig(:analysis, :declarations).map { |item| item[:text] } + template_additions.map { |item| item[:text] } import_block = destination.dig(:analysis, :imports).map { |item| item[:text] }.join declaration_block = merged_declaration_texts.join("\n").rstrip sections = [import_block.rstrip, declaration_block].reject(&:empty?) { ok: true, diagnostics: [], output: "#{sections.join("\n\n").rstrip}\n", policies: [DESTINATION_WINS_ARRAY_POLICY] } end |
.merge_provider ⇒ Provider
105 106 107 |
# File 'lib/go/merge.rb', line 105 def merge_provider @merge_provider ||= Provider.new end |
.parse_go(source, dialect) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/go/merge.rb', line 70 def parse_go(source, dialect) unless go_backend_available_for_analysis?(nil) diagnostic_backend = TreeHaver.current_backend_id || 'tree-sitter' return unsupported_feature_result("Unsupported Go backend #{diagnostic_backend}.") end return analyze_go_module(source) if dialect == 'go' unsupported_feature_result("Unsupported Go dialect #{dialect}.") end |
.register_backend! ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/go/merge.rb', line 22 def register_backend! BACKEND_REGISTRY.mutex.synchronize do return if BACKEND_REGISTRY.registered TreeHaver::BackendRegistry.register(TREE_SITTER_BACKEND) grammar_finder = TreeHaver::GrammarFinder.new(:go) grammar_finder.register! if grammar_finder.available? BACKEND_REGISTRY.registered = true end end |
.register_provider!(replace: false) ⇒ Object
109 110 111 |
# File 'lib/go/merge.rb', line 109 def register_provider!(replace: false) Ast::Merge.register_provider(merge_provider, replace: replace) end |