Class: Highlighter

Inherits:
Qt::Object show all
Defined in:
ext/ruby/qtruby/examples/richtext/syntaxhighlighter/highlighter.rb

Instance Method Summary collapse

Methods inherited from Qt::Base

#%, #&, #*, #**, #+, #-, #-@, #/, #<, #<<, #<=, #==, #>, #>=, #>>, #QCOMPARE, #QEXPECT_FAIL, #QFAIL, #QSKIP, #QTEST, #QVERIFY, #QVERIFY2, #QWARN, #^, ancestors, #is_a?, #methods, private_slots, #protected_methods, #public_methods, q_classinfo, q_signal, q_slot, signals, #singleton_methods, slots, #|, #~

Constructor Details

#initialize(parent = nil) ⇒ Highlighter

Returns a new instance of Highlighter.



31
32
33
34
# File 'ext/ruby/qtruby/examples/richtext/syntaxhighlighter/highlighter.rb', line 31

def initialize(parent = nil)
    super(parent)
	@mappings = {}
end

Instance Method Details

#addMapping(pattern, format) ⇒ Object



41
42
43
# File 'ext/ruby/qtruby/examples/richtext/syntaxhighlighter/highlighter.rb', line 41

def addMapping(pattern, format)
    @mappings[pattern] = format
end

#addToDocument(doc) ⇒ Object



36
37
38
39
# File 'ext/ruby/qtruby/examples/richtext/syntaxhighlighter/highlighter.rb', line 36

def addToDocument(doc)
    connect(doc, SIGNAL('contentsChange(int, int, int)'),
            self, SLOT('highlight(int, int, int)'))
end

#highlight(position, removed, added) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'ext/ruby/qtruby/examples/richtext/syntaxhighlighter/highlighter.rb', line 45

def highlight(position, removed, added)
    doc = sender()

    block = doc.findBlock(position)
    if !block.valid?
        return
	end

    if added > removed
        endBlock = doc.findBlock(position + added)
    else
        endBlock = block
	end

    while block.valid? and !(endBlock < block) do
        highlightBlock(block)
        block = block.next
    end
end

#highlightBlock(block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/ruby/qtruby/examples/richtext/syntaxhighlighter/highlighter.rb', line 65

def highlightBlock(block)
    layout = block.layout
    text = block.text
	if text.nil?
		return
	end

    overrides = []
	@mappings.each do |pattern, value|
        expression = Regexp.new(pattern)
        i = text.index(expression)
        while !i.nil?
            range = Qt::TextLayout::FormatRange.new
            range.start = i
            range.length = $&.length
            range.format = value
            overrides << range

            i = text.index(expression, i + $&.length)
        end
    end

   layout.additionalFormats = overrides
   block.document.markContentsDirty(block.position, block.length)
end