Class: Ast::Merge::Comment::Style
- Inherits:
-
Object
- Object
- Ast::Merge::Comment::Style
- Defined in:
- lib/ast/merge/comment/style.rb
Overview
Configuration for different comment syntax styles.
Supports multiple comment syntax patterns used across programming languages:
:hash_comment- Ruby/Python/YAML/Shell style (# comment):html_comment- HTML/XML/Markdown style (<!-- comment -->):c_style_line- C/JavaScript/Go line comments (// comment):c_style_block- C/JavaScript/CSS block comments (/* comment */):semicolon_comment- Lisp/Clojure/Assembly style (; comment):double_dash_comment- SQL/Haskell/Lua style (-- comment)
Constant Summary collapse
- STYLES =
Predefined comment styles. Mutable to allow runtime registration of custom styles.
{ hash_comment: { line_start: '#', line_end: nil, block_start: nil, block_end: nil, line_pattern: /^\s*#/, block_start_pattern: nil, block_end_pattern: nil }, html_comment: { line_start: '<!--', line_end: '-->', block_start: '<!--', block_end: '-->', line_pattern: /^\s*<!--.*-->\s*$/, block_start_pattern: /^\s*<!--/, block_end_pattern: /-->\s*$/ }, c_style_line: { line_start: '//', line_end: nil, block_start: nil, block_end: nil, line_pattern: %r{^\s*//}, block_start_pattern: nil, block_end_pattern: nil }, c_style_block: { line_start: nil, line_end: nil, block_start: '/*', block_end: '*/', line_pattern: nil, block_start_pattern: %r{^\s*/\*}, block_end_pattern: %r{\*/\s*$} }, semicolon_comment: { line_start: ';', line_end: nil, block_start: nil, block_end: nil, line_pattern: /^\s*;/, block_start_pattern: nil, block_end_pattern: nil }, double_dash_comment: { line_start: '--', line_end: nil, block_start: nil, block_end: nil, line_pattern: /^\s*--/, block_start_pattern: nil, block_end_pattern: nil } }.freeze
- DEFAULT_STYLE =
Default style when none specified
:hash_comment
Instance Attribute Summary collapse
-
#block_end ⇒ String?
readonly
Block comment end delimiter (e.g., "*/").
-
#block_end_pattern ⇒ Regexp?
readonly
Pattern to match block comment end.
-
#block_start ⇒ String?
readonly
Block comment start delimiter (e.g., "/*").
-
#block_start_pattern ⇒ Regexp?
readonly
Pattern to match block comment start.
-
#line_end ⇒ String?
readonly
Line comment end delimiter (for HTML-style: "-->").
-
#line_pattern ⇒ Regexp
readonly
Pattern to match a single comment line.
-
#line_start ⇒ String?
readonly
Line comment start delimiter (e.g., "#", "//").
-
#name ⇒ Symbol
readonly
The style identifier.
Class Method Summary collapse
-
.available_styles ⇒ Array<Symbol>
List all registered style names.
-
.for(name) ⇒ Style
Get a Style instance for a given style name.
-
.register(name, line_start: nil, line_end: nil, block_start: nil, block_end: nil, line_pattern: nil, block_start_pattern: nil, block_end_pattern: nil) ⇒ Hash
Register a custom comment style.
-
.supports_block_comments?(name) ⇒ Boolean
Check if a style supports block comments.
-
.supports_line_comments?(name) ⇒ Boolean
Check if a style supports line comments.
Instance Method Summary collapse
-
#extract_line_content(line) ⇒ String
Extract content from a line comment, removing the delimiter.
-
#initialize(name, line_start: nil, line_end: nil, block_start: nil, block_end: nil, line_pattern: nil, block_start_pattern: nil, block_end_pattern: nil) ⇒ Style
constructor
Initialize a new Style.
-
#inspect ⇒ String
Human-readable representation.
-
#match_block_end?(line) ⇒ Boolean
Check if a line ends a block comment.
-
#match_block_start?(line) ⇒ Boolean
Check if a line starts a block comment.
-
#match_line?(line) ⇒ Boolean
Check if a line matches this style's line comment pattern.
-
#supports_block_comments? ⇒ Boolean
Check if this style supports block comments.
-
#supports_line_comments? ⇒ Boolean
Check if this style supports line comments.
Constructor Details
#initialize(name, line_start: nil, line_end: nil, block_start: nil, block_end: nil, line_pattern: nil, block_start_pattern: nil, block_end_pattern: nil) ⇒ Style
Initialize a new Style.
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/ast/merge/comment/style.rb', line 203 def initialize(name, line_start: nil, line_end: nil, block_start: nil, block_end: nil, line_pattern: nil, block_start_pattern: nil, block_end_pattern: nil) @name = name @line_start = line_start @line_end = line_end @block_start = block_start @block_end = block_end @line_pattern = line_pattern @block_start_pattern = block_start_pattern @block_end_pattern = block_end_pattern end |
Instance Attribute Details
#block_end ⇒ String? (readonly)
Returns Block comment end delimiter (e.g., "*/").
41 42 43 |
# File 'lib/ast/merge/comment/style.rb', line 41 def block_end @block_end end |
#block_end_pattern ⇒ Regexp? (readonly)
Returns Pattern to match block comment end.
50 51 52 |
# File 'lib/ast/merge/comment/style.rb', line 50 def block_end_pattern @block_end_pattern end |
#block_start ⇒ String? (readonly)
Returns Block comment start delimiter (e.g., "/*").
38 39 40 |
# File 'lib/ast/merge/comment/style.rb', line 38 def block_start @block_start end |
#block_start_pattern ⇒ Regexp? (readonly)
Returns Pattern to match block comment start.
47 48 49 |
# File 'lib/ast/merge/comment/style.rb', line 47 def block_start_pattern @block_start_pattern end |
#line_end ⇒ String? (readonly)
Returns Line comment end delimiter (for HTML-style: "-->").
35 36 37 |
# File 'lib/ast/merge/comment/style.rb', line 35 def line_end @line_end end |
#line_pattern ⇒ Regexp (readonly)
Returns Pattern to match a single comment line.
44 45 46 |
# File 'lib/ast/merge/comment/style.rb', line 44 def line_pattern @line_pattern end |
#line_start ⇒ String? (readonly)
Returns Line comment start delimiter (e.g., "#", "//").
32 33 34 |
# File 'lib/ast/merge/comment/style.rb', line 32 def line_start @line_start end |
#name ⇒ Symbol (readonly)
Returns The style identifier.
29 30 31 |
# File 'lib/ast/merge/comment/style.rb', line 29 def name @name end |
Class Method Details
.available_styles ⇒ Array<Symbol>
List all registered style names.
170 171 172 |
# File 'lib/ast/merge/comment/style.rb', line 170 def available_styles STYLES.keys end |
.for(name) ⇒ Style
Get a Style instance for a given style name.
122 123 124 125 126 127 128 |
# File 'lib/ast/merge/comment/style.rb', line 122 def for(name) name = name&.to_sym || DEFAULT_STYLE config = STYLES[name] raise ArgumentError, "Unknown comment style: #{name}" unless config new(name, **config) end |
.register(name, line_start: nil, line_end: nil, block_start: nil, block_end: nil, line_pattern: nil, block_start_pattern: nil, block_end_pattern: nil) ⇒ Hash
Register a custom comment style.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ast/merge/comment/style.rb', line 142 def register(name, line_start: nil, line_end: nil, block_start: nil, block_end: nil, line_pattern: nil, block_start_pattern: nil, block_end_pattern: nil) name = name.to_sym raise ArgumentError, "Style :#{name} already registered" if STYLES.key?(name) config = { line_start: line_start, line_end: line_end, block_start: block_start, block_end: block_end, line_pattern: line_pattern, block_start_pattern: block_start_pattern, block_end_pattern: block_end_pattern } # Modify STYLES (it's frozen, so we need to work around) STYLES.dup.tap do |styles| styles[name] = config remove_const(:STYLES) const_set(:STYLES, styles.freeze) end config end |
.supports_block_comments?(name) ⇒ Boolean
Check if a style supports block comments.
187 188 189 190 |
# File 'lib/ast/merge/comment/style.rb', line 187 def supports_block_comments?(name) config = STYLES[name.to_sym] config && config[:block_start_pattern] end |
.supports_line_comments?(name) ⇒ Boolean
Check if a style supports line comments.
178 179 180 181 |
# File 'lib/ast/merge/comment/style.rb', line 178 def supports_line_comments?(name) config = STYLES[name.to_sym] config && config[:line_pattern] end |
Instance Method Details
#extract_line_content(line) ⇒ String
Extract content from a line comment, removing the delimiter.
249 250 251 252 253 254 255 256 257 |
# File 'lib/ast/merge/comment/style.rb', line 249 def extract_line_content(line) return line.to_s unless line_start content = line.to_s.sub(/^\s*#{Regexp.escape(line_start)}\s?/, '') content = content.sub(/\s*#{Regexp.escape(line_end)}\s*$/, '') if line_end # Content extraction normalizes trailing spaces for comparison/parsing. # Callers that need source-preserving output should use the raw line. content.rstrip end |
#inspect ⇒ String
Returns Human-readable representation.
274 275 276 |
# File 'lib/ast/merge/comment/style.rb', line 274 def inspect "#<Comment::Style:#{name}>" end |
#match_block_end?(line) ⇒ Boolean
Check if a line ends a block comment.
239 240 241 242 243 |
# File 'lib/ast/merge/comment/style.rb', line 239 def match_block_end?(line) return false unless block_end_pattern block_end_pattern.match?(line.to_s) end |
#match_block_start?(line) ⇒ Boolean
Check if a line starts a block comment.
229 230 231 232 233 |
# File 'lib/ast/merge/comment/style.rb', line 229 def match_block_start?(line) return false unless block_start_pattern block_start_pattern.match?(line.to_s) end |
#match_line?(line) ⇒ Boolean
Check if a line matches this style's line comment pattern.
219 220 221 222 223 |
# File 'lib/ast/merge/comment/style.rb', line 219 def match_line?(line) return false unless line_pattern line_pattern.match?(line.to_s) end |
#supports_block_comments? ⇒ Boolean
Check if this style supports block comments.
269 270 271 |
# File 'lib/ast/merge/comment/style.rb', line 269 def supports_block_comments? !block_start_pattern.nil? end |
#supports_line_comments? ⇒ Boolean
Check if this style supports line comments.
262 263 264 |
# File 'lib/ast/merge/comment/style.rb', line 262 def supports_line_comments? !line_pattern.nil? end |