annotate vendor/plugins/coderay-0.9.2/lib/coderay/scanners/diff.rb @ 889:e124b1258c0b bug_83

Close obsolete branch bug_83
author Chris Cannam
date Sat, 19 Feb 2011 09:58:02 +0000
parents 513646585e45
children
rev   line source
Chris@0 1 module CodeRay
Chris@0 2 module Scanners
Chris@0 3
Chris@0 4 class Diff < Scanner
Chris@0 5
Chris@0 6 register_for :diff
Chris@0 7 title 'diff output'
Chris@0 8
Chris@0 9 def scan_tokens tokens, options
Chris@0 10
Chris@0 11 line_kind = nil
Chris@0 12 state = :initial
Chris@0 13
Chris@0 14 until eos?
Chris@0 15 kind = match = nil
Chris@0 16
Chris@0 17 if match = scan(/\n/)
Chris@0 18 if line_kind
Chris@0 19 tokens << [:end_line, line_kind]
Chris@0 20 line_kind = nil
Chris@0 21 end
Chris@0 22 tokens << [match, :space]
Chris@0 23 next
Chris@0 24 end
Chris@0 25
Chris@0 26 case state
Chris@0 27
Chris@0 28 when :initial
Chris@0 29 if match = scan(/--- |\+\+\+ |=+|_+/)
Chris@0 30 tokens << [:begin_line, line_kind = :head]
Chris@0 31 tokens << [match, :head]
Chris@0 32 next unless match = scan(/.+/)
Chris@0 33 kind = :plain
Chris@0 34 elsif match = scan(/Index: |Property changes on: /)
Chris@0 35 tokens << [:begin_line, line_kind = :head]
Chris@0 36 tokens << [match, :head]
Chris@0 37 next unless match = scan(/.+/)
Chris@0 38 kind = :plain
Chris@0 39 elsif match = scan(/Added: /)
Chris@0 40 tokens << [:begin_line, line_kind = :head]
Chris@0 41 tokens << [match, :head]
Chris@0 42 next unless match = scan(/.+/)
Chris@0 43 kind = :plain
Chris@0 44 state = :added
Chris@0 45 elsif match = scan(/\\ /)
Chris@0 46 tokens << [:begin_line, line_kind = :change]
Chris@0 47 tokens << [match, :change]
Chris@0 48 next unless match = scan(/.+/)
Chris@0 49 kind = :plain
Chris@0 50 elsif scan(/(@@)((?>[^@\n]*))(@@)/)
Chris@0 51 tokens << [:begin_line, line_kind = :change]
Chris@0 52 tokens << [self[1], :change]
Chris@0 53 tokens << [self[2], :plain]
Chris@0 54 tokens << [self[3], :change]
Chris@0 55 next unless match = scan(/.+/)
Chris@0 56 kind = :plain
Chris@0 57 elsif match = scan(/\+/)
Chris@0 58 tokens << [:begin_line, line_kind = :insert]
Chris@0 59 tokens << [match, :insert]
Chris@0 60 next unless match = scan(/.+/)
Chris@0 61 kind = :plain
Chris@0 62 elsif match = scan(/-/)
Chris@0 63 tokens << [:begin_line, line_kind = :delete]
Chris@0 64 tokens << [match, :delete]
Chris@0 65 next unless match = scan(/.+/)
Chris@0 66 kind = :plain
Chris@0 67 elsif scan(/ .*/)
Chris@0 68 kind = :comment
Chris@0 69 elsif scan(/.+/)
Chris@0 70 tokens << [:begin_line, line_kind = :head]
Chris@0 71 kind = :plain
Chris@0 72 else
Chris@0 73 raise_inspect 'else case rached'
Chris@0 74 end
Chris@0 75
Chris@0 76 when :added
Chris@0 77 if match = scan(/ \+/)
Chris@0 78 tokens << [:begin_line, line_kind = :insert]
Chris@0 79 tokens << [match, :insert]
Chris@0 80 next unless match = scan(/.+/)
Chris@0 81 kind = :plain
Chris@0 82 else
Chris@0 83 state = :initial
Chris@0 84 next
Chris@0 85 end
Chris@0 86 end
Chris@0 87
Chris@0 88 match ||= matched
Chris@0 89 if $CODERAY_DEBUG and not kind
Chris@0 90 raise_inspect 'Error token %p in line %d' %
Chris@0 91 [[match, kind], line], tokens
Chris@0 92 end
Chris@0 93 raise_inspect 'Empty token', tokens unless match
Chris@0 94
Chris@0 95 tokens << [match, kind]
Chris@0 96 end
Chris@0 97
Chris@0 98 tokens << [:end_line, line_kind] if line_kind
Chris@0 99 tokens
Chris@0 100 end
Chris@0 101
Chris@0 102 end
Chris@0 103
Chris@0 104 end
Chris@0 105 end