Class: Solargraph::Parser::CommentRipper

Inherits:
Ripper::SexpBuilderPP
  • 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.

Parameters:

  • src (String)
  • filename (String) (defaults to: '(ripper)')
  • lineno (Integer) (defaults to: 0)


9
10
11
12
13
# File 'lib/solargraph/parser/comment_ripper.rb', line 9

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

Instance Method Details

#on_comment(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/solargraph/parser/comment_ripper.rb', line 15

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



34
35
36
37
38
39
# File 'lib/solargraph/parser/comment_ripper.rb', line 34

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



27
28
29
30
31
32
# File 'lib/solargraph/parser/comment_ripper.rb', line 27

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



41
42
43
44
45
46
# File 'lib/solargraph/parser/comment_ripper.rb', line 41

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

#parseHash{Integer => String}

Returns:

  • (Hash{Integer => String})


49
50
51
52
53
# File 'lib/solargraph/parser/comment_ripper.rb', line 49

def parse
  @comments = {}
  super
  @comments
end