My favorites | Sign in
Project Home
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import re

from PyQt4.QtGui import QPlainTextEdit
from PyQt4.QtGui import QFontMetricsF
from PyQt4.QtGui import QToolTip
from PyQt4.QtGui import QAction
from PyQt4.QtGui import QTextCharFormat
from PyQt4.QtGui import QTextOption
from PyQt4.QtGui import QTextEdit
from PyQt4.QtGui import QInputDialog
from PyQt4.QtGui import QTextCursor
from PyQt4.QtGui import QTextDocument
from PyQt4.QtGui import QTextFormat
from PyQt4.QtGui import QFont
#from PyQt4.QtGui import QMenu
from PyQt4.QtGui import QPainter
from PyQt4.QtGui import QColor
from PyQt4.QtCore import SIGNAL
from PyQt4.QtCore import QMimeData
from PyQt4.QtCore import Qt

from ninja_ide import resources
from ninja_ide.core import settings
from ninja_ide.core import file_manager
from ninja_ide.tools import styles
from ninja_ide.gui.main_panel import itab_item
from ninja_ide.gui.editor import highlighter
from ninja_ide.gui.editor import helpers
from ninja_ide.gui.editor import pep8_checker
from ninja_ide.gui.editor import errors_checker
from ninja_ide.gui.editor import sidebar_widget


class Editor(QPlainTextEdit, itab_item.ITabItem):

###############################################################################
# EDITOR SIGNALS
###############################################################################
"""
modificationChanged(bool)
fileSaved(QPlainTextEdit)
locateFunction(QString, QString, bool) [functionName, filePath, isVariable]
openDropFile(QString)
addBackItemNavigation()
warningsFound(QPlainTextEdit)
errorsFound(QPlainTextEdit)
cleanDocument(QPlainTextEdit)
findOcurrences(QString)
cursorPositionChange(int, int) #row, col
"""
###############################################################################

def __init__(self, filename, project):
QPlainTextEdit.__init__(self)
itab_item.ITabItem.__init__(self)
#Config Editor
self.set_flags()
self.__lines_count = None

self._sidebarWidget = sidebar_widget.SidebarWidget(self)
if filename in settings.BREAKPOINTS:
self._sidebarWidget._breakpoints = settings.BREAKPOINTS[filename]
if filename in settings.BOOKMARKS:
self._sidebarWidget._bookmarks = settings.BOOKMARKS[filename]
self.pep8 = pep8_checker.Pep8Checker(self)
self.errors = errors_checker.ErrorsChecker(self)

self.textModified = False
self.newDocument = True
self.highlighter = None
self.syncDocErrorsSignal = False
#Set editor style
styles.set_editor_style(self, resources.CUSTOM_SCHEME)
self.set_font(settings.FONT_FAMILY, settings.FONT_SIZE)
#For Highlighting in document
self._patIsWord = re.compile('\w+')
#Brace matching
self._braces = None
self._mtime = None
#Flag to dont bug the user when answer *the modification dialog*
self.ask_if_externally_modified = True
#Dict functions for KeyPress
self.preKeyPress = {
Qt.Key_Tab: self.__insert_indentation,
Qt.Key_Backspace: self.__backspace,
Qt.Key_Home: self.__home_pressed,
Qt.Key_Enter: self.__ignore_extended_line,
Qt.Key_Return: self.__ignore_extended_line,
Qt.Key_BracketRight: self.__brace_completion,
Qt.Key_BraceRight: self.__brace_completion,
Qt.Key_ParenRight: self.__brace_completion}

self.postKeyPress = {
Qt.Key_Enter: self.__auto_indent,
Qt.Key_Return: self.__auto_indent,
Qt.Key_BracketLeft: self.__complete_braces,
Qt.Key_BraceLeft: self.__complete_braces,
Qt.Key_ParenLeft: self.__complete_braces,
Qt.Key_Apostrophe: self.__complete_braces,
Qt.Key_QuoteDbl: self.__complete_braces}

self.connect(self, SIGNAL("updateRequest(const QRect&, int)"),
self._sidebarWidget.update_area)
self.connect(self, SIGNAL("undoAvailable(bool)"), self._file_saved)
self.connect(self, SIGNAL("cursorPositionChanged()"),
self.highlight_current_line)
self.connect(self.pep8, SIGNAL("finished()"), self.show_pep8_errors)
self.connect(self.errors, SIGNAL("finished()"),
self.show_static_errors)
self.connect(self, SIGNAL("blockCountChanged(int)"),
self._update_file_metadata)

#Context Menu Options
self.__actionFindOccurrences = QAction(
self.tr("Find Usages"), self)
self.connect(self.__actionFindOccurrences, SIGNAL("triggered()"),
self._find_occurrences)

def set_flags(self):
self.setWordWrapMode(QTextOption.NoWrap)
self.setMouseTracking(True)
doc = self.document()
option = QTextOption()
if settings.SHOW_TABS_AND_SPACES:
option.setFlags(QTextOption.ShowTabsAndSpaces)
doc.setDefaultTextOption(option)
self.setDocument(doc)
self.setCenterOnScroll(settings.CENTER_ON_SCROLL)

def set_id(self, id_):
super(Editor, self).set_id(id_)
self._mtime = file_manager.get_last_modification(id_)
if settings.CHECK_STYLE:
self.pep8.check_style()
if settings.FIND_ERRORS:
self.errors.check_errors()

def _add_line_increment(self, lines, blockModified, diference):
def _inner_increment(line):
if line < blockModified:
return line
return line + diference
return map(_inner_increment, lines)

def _add_line_increment_for_error(self, lines, blockModified, diference):
def _inner_increment(line):
if line < blockModified:
return line
newLine = line + diference
summary = self.errors.errorsSummary.pop(line)
self.errors.errorsSummary[newLine] = summary
return newLine
return map(_inner_increment, lines)

def _update_file_metadata(self, val):
"""Update the info of bookmarks, breakpoint, pep8 and static errors."""
if self.pep8.pep8lines or self.errors.errorsLines or \
self._sidebarWidget._bookmarks or self._sidebarWidget._breakpoints:
cursor = self.textCursor()
if self.__lines_count:
diference = val - self.__lines_count
else:
diference = 0
blockNumber = cursor.blockNumber() - diference
if self.pep8.pep8lines:
self.pep8.pep8lines = self._add_line_increment(
self.pep8.pep8lines, blockNumber, diference)
self._sidebarWidget._pep8Lines = self.pep8.pep8lines
if self.errors.errorsLines:
self.errors.errorsLines = self._add_line_increment_for_error(
self.errors.errorsLines, blockNumber, diference)
self._sidebarWidget._errorsLines = self.errors.errorsLines
if self._sidebarWidget._breakpoints:
self._sidebarWidget._breakpoints = self._add_line_increment(
self._sidebarWidget._breakpoints, blockNumber, diference)
settings.BREAKPOINTS[self.ID] = self._sidebarWidget._breakpoints
if self._sidebarWidget._bookmarks:
self._sidebarWidget._bookmarks = self._add_line_increment(
self._sidebarWidget._bookmarks, blockNumber, diference)
settings.BOOKMARKS[self.ID] = self._sidebarWidget._bookmarks
self.__lines_count = val
self.highlight_current_line()

def show_pep8_errors(self):
self._sidebarWidget.pep8_check_lines(self.pep8.pep8lines)
if self.syncDocErrorsSignal:
self._sync_tab_icon_notification_signal()
else:
self.syncDocErrorsSignal = True

def show_static_errors(self):
self._sidebarWidget.static_errors_lines(self.errors.errorsLines)
if self.syncDocErrorsSignal:
self._sync_tab_icon_notification_signal()
else:
self.syncDocErrorsSignal = True

def _sync_tab_icon_notification_signal(self):
self.syncDocErrorsSignal = False
if self.errors.errorsLines:
self.emit(SIGNAL("errorsFound(QPlainTextEdit)"), self)
elif self.pep8.pep8lines:
self.emit(SIGNAL("warningsFound(QPlainTextEdit)"), self)
else:
self.emit(SIGNAL("cleanDocument(QPlainTextEdit)"), self)

def check_external_modification(self):
if self.newDocument:
return False
#Saved document we can ask for modification!
return file_manager.check_for_external_modification(
self.get_id(), self._mtime)

def has_write_permission(self):
if self.newDocument:
return True
return file_manager.has_write_permission(self.ID)

def restyle(self, syntaxLang=None):
styles.set_editor_style(self, resources.CUSTOM_SCHEME)
if not syntaxLang:
ext = file_manager.get_file_extension(self.ID)
self.highlighter.apply_highlight(
settings.EXTENSIONS.get(ext, 'python'),
resources.CUSTOM_SCHEME)
else:
self.highlighter.apply_highlight(
str(syntaxLang), resources.CUSTOM_SCHEME)

def _file_saved(self, undoAvailable=False):
if not undoAvailable:
self.emit(SIGNAL("fileSaved(QPlainTextEdit)"), self)
self.newDocument = False
self.textModified = False
self.document().setModified(self.textModified)

def register_syntax(self, lang):
if lang in settings.EXTENSIONS:
self.highlighter = highlighter.Highlighter(self.document(),
settings.EXTENSIONS.get(lang, 'python'),
resources.CUSTOM_SCHEME)

def get_text(self):
"""
Returns all the plain text of the editor
"""
return self.toPlainText()

def get_lines_count(self):
"""
Returns the count of lines in the editor
"""
return self.textCursor().document().lineCount()

def set_font(self, family=settings.FONT_FAMILY, size=settings.FONT_SIZE):
font = QFont(family, size)
self.document().setDefaultFont(font)
# Fix for older version of Qt which doens't has ForceIntegerMetrics
if "ForceIntegerMetrics" in dir(QFont):
self.document().defaultFont().setStyleStrategy(
QFont.ForceIntegerMetrics)

def jump_to_line(self, lineno=None):
"""
Jump to a specific line number or ask to the user for the line
"""
"""
try:
lineno = int(lineno)
self.emit(SIGNAL("addBackItemNavigation()"))
self.go_to_line(lineno)
return
except ValueError:
#ask the line and go!
"""
if lineno is not None:
self.emit(SIGNAL("addBackItemNavigation()"))
self.go_to_line(lineno)
return

max = self.blockCount()
line = QInputDialog.getInt(self, self.tr("Jump to Line"),
self.tr("Line:"), 1, 1, max, 1)
if line[1]:
self.emit(SIGNAL("addBackItemNavigation()"))
self.go_to_line(line[0] - 1)

def _find_occurrences(self):
word = self._text_under_cursor()
self.emit(SIGNAL("findOcurrences(QString)"), word)

def go_to_line(self, lineno):
"""
Go to an specific line
"""
cursor = self.textCursor()
cursor.setPosition(self.document().findBlockByLineNumber(
lineno).position())
self.setTextCursor(cursor)

def zoom_in(self):
font = self.document().defaultFont()
size = font.pointSize()
if size < settings.FONT_MAX_SIZE:
size += 2
font.setPointSize(size)
self.setFont(font)

def zoom_out(self):
font = self.document().defaultFont()
size = font.pointSize()
if size > settings.FONT_MIN_SIZE:
size -= 2
font.setPointSize(size)
self.setFont(font)

def get_parent_project(self):
return ''

def get_cursor_position(self):
return self.textCursor().position()

def set_cursor_position(self, pos):
cursor = self.textCursor()
cursor.setPosition(pos)
self.setTextCursor(cursor)

def indent_more(self):
#cursor is a COPY all changes do not affect the QPlainTextEdit's cursor
cursor = self.textCursor()
selectionStart = cursor.selectionStart()
selectionEnd = cursor.selectionEnd()
#line where indent_more should start and end
start = self.document().findBlock(
cursor.selectionStart()).firstLineNumber()
end = self.document().findBlock(
cursor.selectionEnd()).firstLineNumber()
startPosition = self.document().findBlockByLineNumber(start).position()

#Start a undo block
cursor.beginEditBlock()

#Decide which lines will be indented
cursor.setPosition(selectionEnd)
self.setTextCursor(cursor)
#Select one char at left
#If there is a newline \u2029 (\n) then skip it
self.moveCursor(QTextCursor.Left, QTextCursor.KeepAnchor)
if u'\u2029' in self.textCursor().selectedText():
end -= 1

cursor.setPosition(selectionStart)
self.setTextCursor(cursor)
self.moveCursor(QTextCursor.StartOfLine)
#Indent loop; line by line
for i in xrange(start, end + 1):
self.textCursor().insertText(' ' * settings.INDENT)
self.moveCursor(QTextCursor.Down, QTextCursor.MoveAnchor)

#Restore the user selection
cursor.setPosition(startPosition)
selectionEnd = selectionEnd + \
(settings.INDENT * (end - start + 1))
cursor.setPosition(selectionEnd, QTextCursor.KeepAnchor)
self.setTextCursor(cursor)
#End a undo block
cursor.endEditBlock()

def indent_less(self):
#save the total of movements made after indent_less
totalIndent = 0
#cursor is a COPY all changes do not affect the QPlainTextEdit's cursor
cursor = self.textCursor()
if not cursor.hasSelection():
cursor.movePosition(QTextCursor.EndOfLine)
selectionEnd = cursor.selectionEnd()
#line where indent_less should start and end
start = self.document().findBlock(
cursor.selectionStart()).firstLineNumber()
end = self.document().findBlock(
cursor.selectionEnd()).firstLineNumber()
startPosition = self.document().findBlockByLineNumber(start).position()

#Start a undo block
cursor.beginEditBlock()

#Decide which lines will be indented_less
cursor.setPosition(selectionEnd)
self.setTextCursor(cursor)
#Select one char at left
self.moveCursor(QTextCursor.Left, QTextCursor.KeepAnchor)
#If there is a newline \u2029 (\n) then dont indent this line; skip it!
if u'\u2029' in self.textCursor().selectedText():
end -= 1

cursor.setPosition(startPosition)
self.setTextCursor(cursor)
self.moveCursor(QTextCursor.StartOfLine)
#Indent_less loop; line by line
for i in xrange(start, end + 1):
#Select Settings.indent chars from the current line
for j in xrange(settings.INDENT):
self.moveCursor(QTextCursor.Right, QTextCursor.KeepAnchor)

text = self.textCursor().selectedText()
if text == ' ' * settings.INDENT:
self.textCursor().removeSelectedText()
totalIndent += settings.INDENT
elif u'\u2029' in text:
#\u2029 is the unicode char for \n
#if there is a newline, rollback the selection made above.
for j in xrange(settings.INDENT):
self.moveCursor(QTextCursor.Left, QTextCursor.KeepAnchor)

#Go Down to the next line!
self.moveCursor(QTextCursor.Down)
#Restore the user selection
cursor.setPosition(startPosition)
cursor.setPosition(selectionEnd - totalIndent, QTextCursor.KeepAnchor)
self.setTextCursor(cursor)
#End a undo block
cursor.endEditBlock()

def find_match(self, word, flags, findNext=False):
flags = QTextDocument.FindFlags(flags)
if findNext:
self.moveCursor(QTextCursor.NoMove, QTextCursor.KeepAnchor)
else:
self.moveCursor(QTextCursor.StartOfWord, QTextCursor.KeepAnchor)
found = self.find(word, flags)
if not found:
cursor = self.textCursor()
self.moveCursor(QTextCursor.Start)
found = self.find(word, flags)
if not found:
self.setTextCursor(cursor)

def replace_match(self, wordOld, wordNew, flags, all=False):
flags = QTextDocument.FindFlags(flags)
self.moveCursor(QTextCursor.NoMove, QTextCursor.KeepAnchor)

cursor = self.textCursor()
cursor.beginEditBlock()

self.moveCursor(QTextCursor.Start)
replace = True
while (replace or all):
result = False
result = self.find(wordOld, flags)

if result:
tc = self.textCursor()
if tc.hasSelection():
tc.insertText(wordNew)
else:
break
replace = False

cursor.endEditBlock()

def focusInEvent(self, event):
super(Editor, self).focusInEvent(event)
#use parent().parent() to Access QTabWidget
#First parent() = QStackedWidget, Second parent() = TabWidget
self.parent().parent().focusInEvent(event)
#Check for modifications

def keyReleaseEvent(self, event):
if event.key() == Qt.Key_Control:
self.highlight_current_line()
QPlainTextEdit.keyReleaseEvent(self, event)

def resizeEvent(self, event):
QPlainTextEdit.resizeEvent(self, event)
self._sidebarWidget.setFixedHeight(self.height())

def __insert_indentation(self, event):
if self.textCursor().hasSelection():
self.indent_more()
else:
self.textCursor().insertText(' ' * settings.INDENT)
return True

def __backspace(self, event):
if self.textCursor().hasSelection():
return False
for i in xrange(settings.INDENT):
self.moveCursor(QTextCursor.Left, QTextCursor.KeepAnchor)
text = self.textCursor().selection()
if unicode(text.toPlainText()) == ' ' * settings.INDENT:
self.textCursor().removeSelectedText()
return True
else:
for i in xrange(text.toPlainText().size()):
self.moveCursor(QTextCursor.Right)

def __home_pressed(self, event):
if event.modifiers() == Qt.ShiftModifier:
move = QTextCursor.KeepAnchor
else:
move = QTextCursor.MoveAnchor
if self.textCursor().atBlockStart():
self.moveCursor(QTextCursor.WordRight, move)
return True

def __ignore_extended_line(self, event):
if event.modifiers() == Qt.ShiftModifier:
return True

def __brace_completion(self, event):
if unicode(event.text()) in \
(set(settings.BRACES.values()) - set(["'", '"'])):
cursor = self.textCursor()
cursor.setPosition(cursor.position() + 1)
cursor.setPosition(cursor.position() - 2, QTextCursor.KeepAnchor)
brace = unicode(cursor.selectedText())
if brace == ('%s%s' % (brace[0],
settings.BRACES.get(brace[0], ''))) and brace[1] == event.text():
self.moveCursor(QTextCursor.Right)
return True

def __auto_indent(self, event):
text = unicode(self.textCursor().block().previous().text())
spaces = helpers.get_indentation(text)
self.textCursor().insertText(spaces)
if text != '' and text == ' ' * len(text):
self.moveCursor(QTextCursor.Up)
self.moveCursor(QTextCursor.EndOfLine, QTextCursor.KeepAnchor)
self.textCursor().removeSelectedText()
self.moveCursor(QTextCursor.Down)
else:
helpers.check_for_assistance_completion(self, text)

def __complete_braces(self, event):
cursor = self.textCursor()
cursor.movePosition(QTextCursor.StartOfLine,
QTextCursor.KeepAnchor)
# if not settings.ENABLE_COMPLETION_IN_COMMENTS and \
# cursor.selectedText().contains("#"):
# return
self.textCursor().insertText(settings.BRACES[unicode(event.text())])
self.moveCursor(QTextCursor.Left)
self.textCursor().insertText(self.selected_text)

def keyPressEvent(self, event):
#On Return == True stop the execution of this method
if self.preKeyPress.get(event.key(), lambda x: False)(event):
#emit a signal then plugings can do something
self.emit(SIGNAL("keyPressEvent(QEvent)"), event)
return
self.selected_text = self.textCursor().selectedText()

QPlainTextEdit.keyPressEvent(self, event)

self.postKeyPress.get(event.key(), lambda x: False)(event)

#emit a signal then plugings can do something
self.emit(SIGNAL("keyPressEvent(QEvent)"), event)

def _text_under_cursor(self):
tc = self.textCursor()
tc.select(QTextCursor.WordUnderCursor)
return tc.selectedText()

def paintEvent(self, event):
super(Editor, self).paintEvent(event)
if settings.SHOW_MARGIN_LINE:
painter = QPainter()
painter.begin(self.viewport())
painter.setPen(QColor('#FE9E9A'))
posX = QFontMetricsF(self.document().defaultFont()).width(' ') \
* settings.MARGIN_LINE
offset = self.contentOffset()
painter.drawLine(posX + offset.x(), 0, \
posX + offset.x(), self.viewport().height())
painter.end()

def wheelEvent(self, event, forward=True):
if event.modifiers() == Qt.ControlModifier:
if event.delta() == 120:
self.zoom_in()
elif event.delta() == -120:
self.zoom_out()
event.ignore()
QPlainTextEdit.wheelEvent(self, event)
#parent().parent() == TabWidget
if forward:
self.parent().parent().wheelScroll(event)

def contextMenuEvent(self, event):
popup_menu = self.createStandardContextMenu()

# menuRefactor = QMenu(self.tr("Refactor"))
# extractMethodAction = menuRefactor.addAction(
# self.tr("Extract as Method"))
# organizeImportsAction = menuRefactor.addAction(
# self.tr("Organize Imports"))
# removeUnusedAction = menuRefactor.addAction(
# self.tr("Remove Unused Imports"))
#TODO
# self.connect(organizeImportsAction, SIGNAL("triggered()"),
# self.organize_imports)
# self.connect(removeUnusedAction, SIGNAL("triggered()"),
# self.remove_unused_imports)
# self.connect(extractMethodAction, SIGNAL("triggered()"),
# self.extract_method)
popup_menu.insertSeparator(popup_menu.actions()[0])
# popup_menu.insertMenu(popup_menu.actions()[0], menuRefactor)
popup_menu.insertAction(popup_menu.actions()[0],
self.__actionFindOccurrences)
#add extra menus (from Plugins)
lang = file_manager.get_file_extension(self.ID)
extra_menus = self.EXTRA_MENU.get(lang, None)
if extra_menus:
popup_menu.addSeparator()
for menu in extra_menus:
popup_menu.addMenu(menu)
#show menu
popup_menu.exec_(event.globalPos())

def mouseMoveEvent(self, event):
position = event.pos()
cursor = self.cursorForPosition(position)
block = cursor.block()
if (block.blockNumber() + 1) in self.pep8.pep8lines:
index = self.pep8.pep8lines.index(block.blockNumber() + 1)
QToolTip.showText(self.mapToGlobal(position),
self.pep8.pep8checks[index], self)
if (block.blockNumber() + 1) in self.errors.errorsLines:
QToolTip.showText(self.mapToGlobal(position),
self.errors.errorsSummary[(block.blockNumber() + 1)], self)
if event.modifiers() == Qt.ControlModifier:
cursor.select(QTextCursor.WordUnderCursor)
cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor)
if cursor.selectedText().endsWith('(') or \
cursor.selectedText().endsWith('.'):
cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor)
self.extraSelections = []
selection = QTextEdit.ExtraSelection()
lineColor = QColor(resources.CUSTOM_SCHEME.get('linkNavigate',
resources.COLOR_SCHEME['linkNavigate']))
selection.format.setForeground(lineColor)
selection.format.setFontUnderline(True)
selection.cursor = cursor
self.extraSelections.append(selection)
self.setExtraSelections(self.extraSelections)
else:
self.extraSelections = []
self.setExtraSelections(self.extraSelections)
QPlainTextEdit.mouseMoveEvent(self, event)

def mousePressEvent(self, event):
if event.modifiers() == Qt.ControlModifier:
cursor = self.cursorForPosition(event.pos())
self.setTextCursor(cursor)
self.go_to_definition(cursor)
elif event.button() == Qt.RightButton and \
not self.textCursor().hasSelection():
cursor = self.cursorForPosition(event.pos())
self.setTextCursor(cursor)
QPlainTextEdit.mousePressEvent(self, event)

def dropEvent(self, event):
if len(event.mimeData().urls()) > 0:
path = event.mimeData().urls()[0].path()
self.emit(SIGNAL("openDropFile(QString)"), path)
event.ignore()
event.mimeData = QMimeData()
QPlainTextEdit.dropEvent(self, event)
self.undo()

def go_to_definition(self, cursor=None):
if not cursor:
cursor = self.textCursor()
cursor.select(QTextCursor.WordUnderCursor)
cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor)
if cursor.selectedText().endsWith('('):
cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor)
self.emit(SIGNAL("locateFunction(QString, QString, bool)"),
cursor.selectedText(), self.ID, False)
elif cursor.selectedText().endsWith('.'):
cursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor)
self.emit(SIGNAL("locateFunction(QString, QString, bool)"),
cursor.selectedText(), self.ID, True)

def get_selection(self, posStart, posEnd):
cursor = self.textCursor()
cursor.setPosition(posStart)
cursor2 = self.textCursor()
if posEnd == QTextCursor.End:
cursor2.movePosition(posEnd)
cursor.setPosition(cursor2.position(), QTextCursor.KeepAnchor)
else:
cursor.setPosition(posEnd, QTextCursor.KeepAnchor)
text = cursor.selectedText()
return unicode(text)

def _match_braces(self, position, brace, forward):
"""based on: http://gitorious.org/khteditor"""
if forward:
braceMatch = {'(': ')', '[': ']', '{': '}'}
text = self.get_selection(position, QTextCursor.End)
braceOpen, braceClose = 1, 1
else:
braceMatch = {')': '(', ']': '[', '}': '{'}
text = self.get_selection(QTextCursor.Start, position)
braceOpen, braceClose = len(text) - 1, len(text) - 1
while True:
if forward:
posClose = text.find(braceMatch[brace], braceClose)
else:
posClose = text.rfind(braceMatch[brace], 0, braceClose + 1)
if posClose > -1:
if forward:
braceClose = posClose + 1
posOpen = text.find(brace, braceOpen, posClose)
else:
braceClose = posClose - 1
posOpen = text.rfind(brace, posClose, braceOpen + 1)
if posOpen > -1:
if forward:
braceOpen = posOpen + 1
else:
braceOpen = posOpen - 1
else:
if forward:
return position + posClose
else:
return position - (len(text) - posClose)
else:
return

def highlight_current_line(self):
self.emit(SIGNAL("cursorPositionChange(int, int)"),
self.textCursor().blockNumber() + 1,
self.textCursor().columnNumber())
self.extraSelections = []

if not self.isReadOnly():
selection = QTextEdit.ExtraSelection()
lineColor = QColor(resources.CUSTOM_SCHEME.get('current-line',
resources.COLOR_SCHEME['current-line']))
lineColor.setAlpha(20)
selection.format.setBackground(lineColor)
selection.format.setProperty(QTextFormat.FullWidthSelection, True)
selection.cursor = self.textCursor()
selection.cursor.clearSelection()
self.extraSelections.append(selection)
self.setExtraSelections(self.extraSelections)

#Highlight selected variable
if not self.isReadOnly() and settings.HIGHLIGHT_VARIABLES:
word = self._text_under_cursor()
if self._patIsWord.match(word):
lineColor = QColor(resources.CUSTOM_SCHEME.get('selected-word',
resources.COLOR_SCHEME['selected-word']))
lineColor.setAlpha(100)
if settings.HIGHLIGHT_ALL_VARIABLES:
block = self.document().findBlock(0)
else:
block = self.firstVisibleBlock()
cursor = self.document().find(word, block.position(),
QTextDocument.FindCaseSensitively or \
QTextDocument.FindWholeWords)
while block.isValid() and \
block.blockNumber() <= self._sidebarWidget.highest_line and \
cursor.position() != -1:
selection = QTextEdit.ExtraSelection()
selection.format.setBackground(lineColor)
selection.cursor = cursor
self.extraSelections.append(selection)
cursor = self.document().find(word, cursor.position(),
QTextDocument.FindCaseSensitively or \
QTextDocument.FindWholeWords)
block = block.next()
self.setExtraSelections(self.extraSelections)

#Find Errors
if settings.ERRORS_HIGHLIGHT_LINE and \
len(self.errors.errorsLines) < settings.MAX_HIGHLIGHT_ERRORS:
cursor = self.textCursor()
for lineno in self.errors.errorsLines:
block = self.document().findBlockByLineNumber(lineno - 1)
if not block.isValid():
continue
cursor.setPosition(block.position())
selection = QTextEdit.ExtraSelection()
selection.format.setUnderlineColor(QColor(
resources.CUSTOM_SCHEME.get('error-underline',
resources.COLOR_SCHEME['error-underline'])))
selection.format.setUnderlineStyle(
QTextCharFormat.WaveUnderline)
selection.cursor = cursor
selection.cursor.movePosition(QTextCursor.EndOfBlock,
QTextCursor.KeepAnchor)
self.extraSelections.append(selection)
if self.errors.errorsLines:
self.setExtraSelections(self.extraSelections)

#Check Style
if settings.CHECK_HIGHLIGHT_LINE and \
len(self.pep8.pep8lines) < settings.MAX_HIGHLIGHT_ERRORS:
cursor = self.textCursor()
for xline, line in enumerate(self.pep8.pep8lines):
block = self.document().findBlockByLineNumber(line - 1)
if not block.isValid():
continue
cursor.setPosition(block.position())
selection = QTextEdit.ExtraSelection()
selection.format.setToolTip(self.pep8.pep8checks[xline])
selection.format.setUnderlineColor(QColor(
resources.CUSTOM_SCHEME.get('pep8-underline',
resources.COLOR_SCHEME['pep8-underline'])))
selection.format.setUnderlineStyle(
QTextCharFormat.WaveUnderline)
selection.cursor = cursor
selection.cursor.movePosition(QTextCursor.EndOfBlock,
QTextCursor.KeepAnchor)
self.extraSelections.append(selection)
if self.pep8.pep8lines:
self.setExtraSelections(self.extraSelections)

if self._braces is not None:
self._braces = None
cursor = self.textCursor()
if cursor.position() == 0:
self.setExtraSelections(self.extraSelections)
return
cursor.movePosition(QTextCursor.PreviousCharacter,
QTextCursor.KeepAnchor)
text = unicode(cursor.selectedText())
pos1 = cursor.position()
if text in (')', ']', '}'):
pos2 = self._match_braces(pos1, text, forward=False)
elif text in ('(', '[', '{'):
pos2 = self._match_braces(pos1, text, forward=True)
else:
self.setExtraSelections(self.extraSelections)
return
if pos2 is not None:
self._braces = (pos1, pos2)
selection = QTextEdit.ExtraSelection()
selection.format.setForeground(QColor(
resources.CUSTOM_SCHEME.get('brace-foreground',
resources.COLOR_SCHEME.get('brace-foreground'))))
selection.format.setBackground(QColor(
resources.CUSTOM_SCHEME.get('brace-background',
resources.COLOR_SCHEME.get('brace-background'))))
selection.cursor = cursor
self.extraSelections.append(selection)
selection = QTextEdit.ExtraSelection()
selection.format.setForeground(QColor(
resources.CUSTOM_SCHEME.get('brace-foreground',
resources.COLOR_SCHEME.get('brace-foreground'))))
selection.format.setBackground(QColor(
resources.CUSTOM_SCHEME.get('brace-background',
resources.COLOR_SCHEME.get('brace-background'))))
selection.cursor = self.textCursor()
selection.cursor.setPosition(pos2)
selection.cursor.movePosition(QTextCursor.NextCharacter,
QTextCursor.KeepAnchor)
self.extraSelections.append(selection)
else:
self._braces = (pos1,)
selection = QTextEdit.ExtraSelection()
selection.format.setBackground(QColor(
resources.CUSTOM_SCHEME.get('brace-background',
resources.COLOR_SCHEME.get('brace-background'))))
selection.format.setForeground(QColor(
resources.CUSTOM_SCHEME.get('brace-foreground',
resources.COLOR_SCHEME.get('brace-foreground'))))
selection.cursor = cursor
self.extraSelections.append(selection)
self.setExtraSelections(self.extraSelections)


def create_editor(fileName='', project=None, syntax=None):
editor = Editor(fileName, project)
#if syntax is specified, use it
if syntax:
editor.register_syntax(syntax)
else:
#try to set a syntax based on the file extension
ext = file_manager.get_file_extension(fileName)
if ext not in settings.EXTENSIONS and fileName == '':
#by default use python syntax
editor.register_syntax('py')
else:
editor.register_syntax(ext)
return editor

Change log

3d44214559f1 by Diego Sarmentero <diego.sarmentero> on Oct 24, 2011   Diff
Fixed  Issue 240 
Fixed image in StartPage
Fixed bug in _update_file_metadata for
errors.
Go to: 
Project members, sign in to write a code review

Older revisions

0b8c0ddb8caa by Diego Sarmentero <diego.sarmentero> on Oct 24, 2011   Diff
Fixed  Issue 306 
45401262b5e3 by Diego Sarmentero <diego.sarmentero> on Oct 24, 2011   Diff
Fixed  Issue 248 
5fcf38d75687 by Diego Sarmentero <diego.sarmentero> on Oct 22, 2011   Diff
merge
All revisions of this file

File info

Size: 36796 bytes, 893 lines
Powered by Google Project Hosting