Class: Cosmos::RubyEditor::RubySyntax

Inherits:
Qt::SyntaxHighlighter
  • Object
show all
Defined in:
lib/cosmos/gui/text/ruby_editor.rb

Overview

This works but slows down the GUI significantly when pasting a large (10k line) block of code into the editor

Constant Summary collapse

RUBY_KEYWORDS =

Ruby keywords - www.ruby-doc.org/docs/keywords/1.9/ Also include some common methods that are typically called by themselves like puts, print, sleep

%w(BEGIN END __ENCODING__ __END__ __FILE__ __LINE__ \
alias and begin break case class def defined? do \
else elsif end ensure false for if in module \
next nil not or raise redo rescue retry return \
self super then true undef unless until when while yield \
puts print sleep)
STYLES =

Syntax styles - refer to the ruby.properties in Scite

{
  'normal' => create_format('black'),
  'ruby_keyword' => create_format('darkBlue','bold'),
  'cosmos_keyword' => create_format('blue','bold'),
  'operator' => create_format('red'),
  'brace' => create_format('black','bold'),
  'class' => create_format('blue', 'bold'),
  'method' => create_format(Cosmos::getColor(0, 127, 127), 'bold'),
  'string' => create_format(Cosmos::getColor(127, 0, 127)),
  'symbol' => create_format(Cosmos::getColor(192,160,48)),
  'comment' => create_format('green'),
}
BRACES =

Ruby braces

['\{', '\}', '\(', '\)', '\[', '\]','\|']
RULES =
[]
RULES_INFO =

Build a RegExp for each pattern

[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_format(color = Cosmos::BLACK, style = '') ⇒ Object

common stand alone methods



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 45

def self.create_format(color = Cosmos::BLACK, style='')
  color = Cosmos::getColor(color)
  brush = Cosmos::getBrush(color)

  format = Qt::TextCharFormat.new
  format.setForeground(brush)
  if style == 'bold'
    format.setFontWeight(Qt::Font::Bold)
  end
  if style == 'italic'
    format.setFontItalic(true)
  end
  format
end

Instance Method Details

#highlightBlock(text) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cosmos/gui/text/ruby_editor.rb', line 123

def highlightBlock(text)
  # Do other syntax formatting
  RULES_INFO.each do |expression, format|
    match = text.scan(expression).flatten.compact
    next unless match && !match.empty?
    i = 0 # initial string match offset
    match.each do |part|
      # Index the match part starting at the offset
      index = text.index(part, i)
      self.setFormat(index, part.length, format)
      # Increment the index so we can match the same string
      # in two different locations in the text, e.g. "#{x} #{y}"
      i = index + part.length
    end
  end
end