Class: Troshka::Editor

Inherits:
Qsci::Scintilla
  • Object
show all
Defined in:
lib/troshka/editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEditor

Returns a new instance of Editor.



6
7
8
9
# File 'lib/troshka/editor.rb', line 6

def initialize
  super
  build
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



4
5
6
# File 'lib/troshka/editor.rb', line 4

def app
  @app
end

Instance Method Details

#buildObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/troshka/editor.rb', line 96

def build
  @lexer = Qsci::LexerRuby.new
  self.setAutoIndent true
  self.setIndentationGuides true
  self.setIndentationWidth 2
  self.setFolding Qsci::Scintilla::PlainFoldStyle
  self.setLexer @lexer
  self.setMarginWidth 1, 0
  
  self.user_list_activated do |editor, text|
    editor.select_auto
    editor.replaceSelectedText text
  end
end

#columnObject



70
71
72
73
# File 'lib/troshka/editor.rb', line 70

def column
  #  SCI_GETCOLUMN = 2129
  SendScintilla(2129,position,0)
end

#current_lineObject



80
81
82
# File 'lib/troshka/editor.rb', line 80

def current_line
  text line
end

#current_wordObject



84
85
86
87
88
89
90
# File 'lib/troshka/editor.rb', line 84

def current_word
  word_begin = current_line.rindex(word_separator, column-1)+1 rescue 0
  word_end = current_line.index(word_separator, column)-1 rescue current_line.size-1
  
  word = current_line[word_begin..word_end]
  {word_begin: word_begin, word_end: word_end, word: word}
end

#insert_and_advance(text) ⇒ Object



75
76
77
78
# File 'lib/troshka/editor.rb', line 75

def insert_and_advance(text)
    insert text
    setCursorPosition(line, column + text.length)
end

#keyPressEvent(x) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/troshka/editor.rb', line 15

def keyPressEvent(x)
  if x.key==Qt::Key_Space && x.modifiers==Qt::Control_Modifier
    pos = column
    word = current_word      
    list = @app.complete(word[:word], current_line)
    unless list.empty?
      list.sort!
      
      hint = list.first.dup
      hint.chop! while current_line.rindex(hint).nil?
      @auto = word[:word_end]+1-hint.size, word[:word_end]
      
      setCursorPosition(line, word[:word_end]-hint.size)     
      show_user_list 1, list
      setCursorPosition(line, pos)
    end
  elsif x.key==Qt::Key_Return && x.modifiers==Qt::Control_Modifier
    @app.run text    
  else
    super x
  end
end

#lineObject



65
66
67
68
# File 'lib/troshka/editor.rb', line 65

def line
  # SCI_LINEFROMPOSITION
  SendScintilla(2166,position,0)
end

#line_end_position(line) ⇒ Object



43
44
45
46
# File 'lib/troshka/editor.rb', line 43

def line_end_position(line)
  # SCI_GETLINEENDPOSITION
  SendScintilla(2136, line, 0)
end

#move_to_endObject



48
49
50
# File 'lib/troshka/editor.rb', line 48

def move_to_end
  setCursorPosition lines, line_end_position(lines)
end

#positionObject



38
39
40
41
# File 'lib/troshka/editor.rb', line 38

def position
  # SCI_GETCURRENTPOS
  SendScintilla(2008, 0, 0)
end

#select_autoObject



57
58
59
# File 'lib/troshka/editor.rb', line 57

def select_auto
  setSelection line, @auto.first, line, @auto.last + 1
end

#select_lineObject



61
62
63
# File 'lib/troshka/editor.rb', line 61

def select_line
  setSelection line, 0, line, lineLength(line)
end

#select_wordObject



52
53
54
55
# File 'lib/troshka/editor.rb', line 52

def select_word
  word = current_word
  setSelection line, word[:word_begin], line, word[:word_end]+1
end

#user_list_activated(&block) ⇒ Object



11
12
13
# File 'lib/troshka/editor.rb', line 11

def user_list_activated(&block)
  connect(SIGNAL "userListActivated(int, const QString)") {|id, text| block.(self,text)}
end

#word_separatorObject



92
93
94
# File 'lib/troshka/editor.rb', line 92

def word_separator
  " "
end