Class: Solargraph::Parser::CommentRipper

Inherits:
Ripper::SexpBuilder
  • Object
show all
Defined in:
lib/solargraph/parser/comment_ripper.rb

Instance Method Summary collapse

Constructor Details

#initialize(src, filename = '(ripper)', lineno = 0) ⇒ CommentRipper

Returns a new instance of CommentRipper.



6
7
8
9
10
# File 'lib/solargraph/parser/comment_ripper.rb', line 6

def initialize src, filename = '(ripper)', lineno = 0
  super
  @buffer = src
  @buffer_lines = @buffer.lines
end

Instance Method Details

#on_comment(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/solargraph/parser/comment_ripper.rb', line 12

def on_comment *args
  result = super
  if @buffer_lines[result[2][0]][0..result[2][1]].strip =~ /^#/
    chomped = result[1].chomp
    if result[2][0] == 0 && chomped.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').match(/^#\s*frozen_string_literal:/)
      chomped = '#'
    end
    @comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
  end
  result
end

#on_embdoc(*args) ⇒ Object



31
32
33
34
35
36
# File 'lib/solargraph/parser/comment_ripper.rb', line 31

def on_embdoc *args
  result = super
  chomped = result[1].chomp
  @comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
  result
end

#on_embdoc_beg(*args) ⇒ Object



24
25
26
27
28
29
# File 'lib/solargraph/parser/comment_ripper.rb', line 24

def on_embdoc_beg *args
  result = super
  chomped = result[1].chomp
  @comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
  result
end

#on_embdoc_end(*args) ⇒ Object



38
39
40
41
42
43
# File 'lib/solargraph/parser/comment_ripper.rb', line 38

def on_embdoc_end *args
  result = super
  chomped = result[1].chomp
  @comments[result[2][0]] = Snippet.new(Range.from_to(result[2][0], result[2][1], result[2][0], result[2][1] + chomped.length), chomped)
  result
end

#parseObject



45
46
47
48
49
# File 'lib/solargraph/parser/comment_ripper.rb', line 45

def parse
  @comments = {}
  super
  @comments
end