class Highlighter

Public Class Methods

new(parent = nil) click to toggle source
Calls superclass method
# File examples/richtext/syntaxhighlighter/highlighter.rb, line 31
def initialize(parent = nil)
    super(parent)
        @mappings = {}
end

Public Instance Methods

addMapping(pattern, format) click to toggle source
# File examples/richtext/syntaxhighlighter/highlighter.rb, line 41
def addMapping(pattern, format)
    @mappings[pattern] = format
end
addToDocument(doc) click to toggle source
# File 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) click to toggle source
# File 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) click to toggle source
# File 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