annotate .svn/pristine/66/66ab50d76f6f50210c09955e4a16c4d364df47ff.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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