Class: Ast::Merge::Comment::Style

Inherits:
Object
  • Object
show all
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`)

Examples:

Using a predefined style

style = Style.for(:hash_comment)
style.line_start #=> "#"
style.match_line?("# hello") #=> true

Registering a custom style

Style.register(:percent_comment,
  line_start: "%",
  line_pattern: /^\s*%/
)

Constant Summary collapse

STYLES =

Predefined comment styles. Mutable to allow runtime registration of custom styles.

Returns:

  • (Hash{Symbol => Hash})

    Registered comment 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

Returns:

  • (Symbol)
:hash_comment

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • name (Symbol)

    Style identifier

  • line_start (String, nil) (defaults to: nil)

    Line comment start delimiter

  • line_end (String, nil) (defaults to: nil)

    Line comment end delimiter

  • block_start (String, nil) (defaults to: nil)

    Block comment start delimiter

  • block_end (String, nil) (defaults to: nil)

    Block comment end delimiter

  • line_pattern (Regexp, nil) (defaults to: nil)

    Pattern to match comment lines

  • block_start_pattern (Regexp, nil) (defaults to: nil)

    Pattern to match block start

  • block_end_pattern (Regexp, nil) (defaults to: nil)

    Pattern to match block end



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ast/merge/comment/style.rb', line 205

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_endString? (readonly)

Returns Block comment end delimiter (e.g., “*/”).

Returns:

  • (String, nil)

    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_patternRegexp? (readonly)

Returns Pattern to match block comment end.

Returns:

  • (Regexp, nil)

    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_startString? (readonly)

Returns Block comment start delimiter (e.g., “/*”).

Returns:

  • (String, nil)

    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_patternRegexp? (readonly)

Returns Pattern to match block comment start.

Returns:

  • (Regexp, nil)

    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_endString? (readonly)

Returns Line comment end delimiter (for HTML-style: “–>”).

Returns:

  • (String, nil)

    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_patternRegexp (readonly)

Returns Pattern to match a single comment line.

Returns:

  • (Regexp)

    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_startString? (readonly)

Returns Line comment start delimiter (e.g., “#”, “//”).

Returns:

  • (String, nil)

    Line comment start delimiter (e.g., “#”, “//”)



32
33
34
# File 'lib/ast/merge/comment/style.rb', line 32

def line_start
  @line_start
end

#nameSymbol (readonly)

Returns The style identifier.

Returns:

  • (Symbol)

    The style identifier



29
30
31
# File 'lib/ast/merge/comment/style.rb', line 29

def name
  @name
end

Class Method Details

.available_stylesArray<Symbol>

List all registered style names.

Returns:

  • (Array<Symbol>)

    Available style names



172
173
174
# File 'lib/ast/merge/comment/style.rb', line 172

def available_styles
  STYLES.keys
end

.for(name) ⇒ Style

Get a Style instance for a given style name.

Parameters:

  • name (Symbol)

    Style name (e.g., :hash_comment, :c_style_line)

Returns:

  • (Style)

    The style configuration

Raises:

  • (ArgumentError)

    if style name is not registered



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.

Parameters:

  • name (Symbol)

    Style identifier

  • line_start (String, nil) (defaults to: nil)

    Line comment start delimiter

  • line_end (String, nil) (defaults to: nil)

    Line comment end delimiter

  • block_start (String, nil) (defaults to: nil)

    Block comment start delimiter

  • block_end (String, nil) (defaults to: nil)

    Block comment end delimiter

  • line_pattern (Regexp, nil) (defaults to: nil)

    Pattern to match comment lines

  • block_start_pattern (Regexp, nil) (defaults to: nil)

    Pattern to match block start

  • block_end_pattern (Regexp, nil) (defaults to: nil)

    Pattern to match block end

Returns:

  • (Hash)

    The registered style configuration

Raises:

  • (ArgumentError)

    if name already exists



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# 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
  if STYLES.key?(name)
    raise ArgumentError, "Style :#{name} already registered"
  end

  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.

Parameters:

  • name (Symbol)

    Style name

Returns:

  • (Boolean)

    true if style has block comment support



189
190
191
192
# File 'lib/ast/merge/comment/style.rb', line 189

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.

Parameters:

  • name (Symbol)

    Style name

Returns:

  • (Boolean)

    true if style has line comment support



180
181
182
183
# File 'lib/ast/merge/comment/style.rb', line 180

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.

Parameters:

  • line (String)

    The comment line

Returns:

  • (String)

    The comment content without delimiters



251
252
253
254
255
256
257
258
259
# File 'lib/ast/merge/comment/style.rb', line 251

def extract_line_content(line)
  return line.to_s unless line_start

  content = line.to_s.sub(/^\s*#{Regexp.escape(line_start)}\s?/, "")
  if line_end
    content = content.sub(/\s*#{Regexp.escape(line_end)}\s*$/, "")
  end
  content.rstrip
end

#inspectString

Returns Human-readable representation.

Returns:

  • (String)

    Human-readable representation



276
277
278
# File 'lib/ast/merge/comment/style.rb', line 276

def inspect
  "#<Comment::Style:#{name}>"
end

#match_block_end?(line) ⇒ Boolean

Check if a line ends a block comment.

Parameters:

  • line (String)

    The line to check

Returns:

  • (Boolean)

    true if line ends a block comment



241
242
243
244
245
# File 'lib/ast/merge/comment/style.rb', line 241

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.

Parameters:

  • line (String)

    The line to check

Returns:

  • (Boolean)

    true if line starts a block comment



231
232
233
234
235
# File 'lib/ast/merge/comment/style.rb', line 231

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.

Parameters:

  • line (String)

    The line to check

Returns:

  • (Boolean)

    true if line is a comment in this style



221
222
223
224
225
# File 'lib/ast/merge/comment/style.rb', line 221

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.

Returns:

  • (Boolean)

    true if block comments are supported



271
272
273
# File 'lib/ast/merge/comment/style.rb', line 271

def supports_block_comments?
  !block_start_pattern.nil?
end

#supports_line_comments?Boolean

Check if this style supports line comments.

Returns:

  • (Boolean)

    true if line comments are supported



264
265
266
# File 'lib/ast/merge/comment/style.rb', line 264

def supports_line_comments?
  !line_pattern.nil?
end