Module: Docscribe::InlineRewriter
- Defined in:
- lib/docscribe/inline_rewriter.rb,
lib/docscribe/inline_rewriter/collector.rb,
lib/docscribe/inline_rewriter/doc_block.rb,
lib/docscribe/inline_rewriter/tag_sorter.rb,
lib/docscribe/inline_rewriter/doc_builder.rb,
lib/docscribe/inline_rewriter/source_helpers.rb
Overview
Rewrite Ruby source to insert or update inline YARD-style documentation.
Supported strategies:
-
‘:safe`
-
insert missing docs
-
merge into existing doc-like blocks
-
normalize configured sortable tags
-
preserve existing prose and directives where possible
-
-
‘:aggressive`
-
replace existing doc blocks with freshly generated docs
-
Compatibility note:
-
‘merge: true` maps to `strategy: :safe`
-
‘rewrite: true` maps to `strategy: :aggressive`
Defined Under Namespace
Modules: DocBlock, DocBuilder, SourceHelpers, TagSorter Classes: Collector
Class Method Summary collapse
-
.build_rewrite_pipeline(buffer, ast) ⇒ Hash
Build the insertion pipeline: collector, plugin insertions, dedup, rewriter, merge_inserts, changes.
-
.dispatch_attr_insertion(ins, pipeline, buffer, **options) ⇒ void
Dispatch an attr insertion.
-
.dispatch_method_insertion(ins, pipeline, buffer, **options) ⇒ void
Dispatch a method insertion.
-
.dispatch_plugin_insertion(ins, pipeline, buffer, **options) ⇒ void
Dispatch a plugin insertion.
-
.dispatch_rewrite_insertions(pipeline, buffer, **options) ⇒ Object
Dispatch all insertions to the appropriate handler.
-
.insert_comments(code, strategy: nil, rewrite: nil, merge: nil, **options) ⇒ String
Rewrite source and return only the rewritten output string.
-
.rewrite_with_report(code, strategy: nil, rewrite: nil, merge: nil, **options) ⇒ Hash
Rewrite source and return both output and structured change information.
Class Method Details
.build_rewrite_pipeline(buffer, ast) ⇒ Hash
Build the insertion pipeline: collector, plugin insertions, dedup, rewriter, merge_inserts, changes.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/docscribe/inline_rewriter.rb', line 81 def build_rewrite_pipeline(buffer, ast) all = collect_insertions(buffer, ast) method_overrides_by_pos = {} #: Hash[Integer, untyped] all = deduplicate_insertions(all, method_overrides_by_pos: method_overrides_by_pos) rewriter = Parser::Source::TreeRewriter.new(buffer) merge_inserts = Hash.new { |h, k| h[k] = [] } #: Hash[Integer, untyped] changes = [] #: Array[untyped] { all: all, method_overrides_by_pos: method_overrides_by_pos, rewriter: rewriter, merge_inserts: merge_inserts, changes: changes } end |
.dispatch_attr_insertion(ins, pipeline, buffer, **options) ⇒ void
This method returns an undefined value.
Dispatch an attr insertion.
141 142 143 144 145 146 147 |
# File 'lib/docscribe/inline_rewriter.rb', line 141 def dispatch_attr_insertion(ins, pipeline, buffer, **) apply_attr_insertion!( rewriter: pipeline[:rewriter], buffer: buffer, insertion: ins, config: [:config], signature_provider: [:signature_provider], strategy: [:strategy], merge_inserts: pipeline[:merge_inserts] ) end |
.dispatch_method_insertion(ins, pipeline, buffer, **options) ⇒ void
This method returns an undefined value.
Dispatch a method insertion.
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/docscribe/inline_rewriter.rb', line 120 def dispatch_method_insertion(ins, pipeline, buffer, **) pos = plugin_insertion_pos(:method, ins) method_override = pipeline[:method_overrides_by_pos][pos] apply_method_insertion!( rewriter: pipeline[:rewriter], buffer: buffer, insertion: ins, config: [:config], signature_provider: [:signature_provider], core_rbs_provider: [:core_rbs_provider], strategy: [:strategy], changes: pipeline[:changes], file: [:file], method_override: method_override ) end |
.dispatch_plugin_insertion(ins, pipeline, buffer, **options) ⇒ void
This method returns an undefined value.
Dispatch a plugin insertion.
157 158 159 160 161 162 |
# File 'lib/docscribe/inline_rewriter.rb', line 157 def dispatch_plugin_insertion(ins, pipeline, buffer, **) apply_plugin_insertion!( rewriter: pipeline[:rewriter], buffer: buffer, insertion: ins, strategy: [:strategy], config: [:config] ) end |
.dispatch_rewrite_insertions(pipeline, buffer, **options) ⇒ Object
Dispatch all insertions to the appropriate handler.
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/docscribe/inline_rewriter.rb', line 99 def dispatch_rewrite_insertions(pipeline, buffer, **) pipeline[:all].sort_by { |(kind, ins)| plugin_insertion_pos(kind, ins) } .reverse_each do |kind, ins| case kind when :method then dispatch_method_insertion(ins, pipeline, buffer, **) when :attr then dispatch_attr_insertion(ins, pipeline, buffer, **) when :plugin then dispatch_plugin_insertion(ins, pipeline, buffer, **) end end apply_merge_inserts!(rewriter: pipeline[:rewriter], buffer: buffer, merge_inserts: pipeline[:merge_inserts]) end |
.insert_comments(code, strategy: nil, rewrite: nil, merge: nil, **options) ⇒ String
Rewrite source and return only the rewritten output string.
This is the main convenience entry point for library usage.
47 48 49 50 51 |
# File 'lib/docscribe/inline_rewriter.rb', line 47 def insert_comments(code, strategy: nil, rewrite: nil, merge: nil, **) strategy = normalize_strategy(strategy: strategy, rewrite: rewrite, merge: merge) rewrite_with_report(code, strategy: strategy, **)[:output] end |
.rewrite_with_report(code, strategy: nil, rewrite: nil, merge: nil, **options) ⇒ Hash
Rewrite source and return both output and structured change information.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/docscribe/inline_rewriter.rb', line 64 def rewrite_with_report(code, strategy: nil, rewrite: nil, merge: nil, **) strategy = normalize_strategy(strategy: strategy, rewrite: rewrite, merge: merge) validate_strategy!(strategy) parsed = setup_rewrite_env(code, ) pipeline = build_rewrite_pipeline(parsed[:buffer], parsed[:ast]) dispatch_rewrite_insertions(pipeline, parsed[:buffer], config: parsed[:config], signature_provider: parsed[:signature_provider], core_rbs_provider: parsed[:core_rbs_provider], strategy: strategy, file: parsed[:file]) { output: pipeline[:rewriter].process, changes: pipeline[:changes] } end |