To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 66 / 66ab50d76f6f50210c09955e4a16c4d364df47ff.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (2.4 KB)

1
module CodeRay
2
module Encoders
3
  
4
  load :filter
5
  
6
  # A Filter that selects tokens based on their token kind.
7
  # 
8
  # == Options
9
  # 
10
  # === :exclude
11
  # 
12
  # One or many symbols (in an Array) which shall be excluded.
13
  # 
14
  # Default: []
15
  # 
16
  # === :include
17
  # 
18
  # One or many symbols (in an array) which shall be included.
19
  # 
20
  # Default: :all, which means all tokens are included.
21
  # 
22
  # Exclusion wins over inclusion.
23
  # 
24
  # See also: CommentFilter
25
  class TokenKindFilter < Filter
26
    
27
    register_for :token_kind_filter
28
    
29
    DEFAULT_OPTIONS = {
30
      :exclude => [],
31
      :include => :all
32
    }
33
    
34
  protected
35
    def setup options
36
      super
37
      
38
      @group_excluded = false
39
      @exclude = options[:exclude]
40
      @exclude = Array(@exclude) unless @exclude == :all
41
      @include = options[:include]
42
      @include = Array(@include) unless @include == :all
43
    end
44
    
45
    def include_text_token? text, kind
46
      include_group? kind
47
    end
48
    
49
    def include_group? kind
50
       (@include == :all || @include.include?(kind)) &&
51
      !(@exclude == :all || @exclude.include?(kind))
52
    end
53
    
54
  public
55
    
56
    # Add the token to the output stream if +kind+ matches the conditions.
57
    def text_token text, kind
58
      super if !@group_excluded && include_text_token?(text, kind)
59
    end
60
    
61
    # Add the token group to the output stream if +kind+ matches the
62
    # conditions.
63
    # 
64
    # If it does not, all tokens inside the group are excluded from the
65
    # stream, even if their kinds match.
66
    def begin_group kind
67
      if @group_excluded
68
        @group_excluded += 1
69
      elsif include_group? kind
70
        super
71
      else
72
        @group_excluded = 1
73
      end
74
    end
75
    
76
    # See +begin_group+.
77
    def begin_line kind
78
      if @group_excluded
79
        @group_excluded += 1
80
      elsif include_group? kind
81
        super
82
      else
83
        @group_excluded = 1
84
      end
85
    end
86
    
87
    # Take care of re-enabling the delegation of tokens to the output stream
88
    # if an exluded group has ended.
89
    def end_group kind
90
      if @group_excluded
91
        @group_excluded -= 1
92
        @group_excluded = false if @group_excluded.zero?
93
      else
94
        super
95
      end
96
    end
97
    
98
    # See +end_group+.
99
    def end_line kind
100
      if @group_excluded
101
        @group_excluded -= 1
102
        @group_excluded = false if @group_excluded.zero?
103
      else
104
        super
105
      end
106
    end
107
    
108
  end
109
  
110
end
111
end