~ubuntu-branches/ubuntu/wily/spyder/wily

« back to all changes in this revision

Viewing changes to spyderlib/widgets/sourcecode/base.py

  • Committer: Package Import Robot
  • Author(s): Benjamin Drung
  • Date: 2015-01-15 12:20:11 UTC
  • mfrom: (18.1.7 experimental)
  • Revision ID: package-import@ubuntu.com-20150115122011-cc7j5dhy2h9uo13m
Tags: 2.3.2+dfsg-1ubuntu1
Backport patch to support pylint3.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
# pylint: disable=R0911
12
12
# pylint: disable=R0201
13
13
 
 
14
import os
14
15
import re
15
16
import sys
16
17
 
27
28
from spyderlib.widgets.sourcecode.terminal import ANSIEscapeCodeHandler
28
29
from spyderlib.widgets.mixins import BaseEditMixin
29
30
from spyderlib.widgets.calltip import CallTipWidget
30
 
from spyderlib.py3compat import to_text_string, str_lower
31
 
 
32
 
CELL_SEPARATORS = ('#%%', '# %%', '# <codecell>', '# In[')
 
31
from spyderlib.py3compat import to_text_string, str_lower, PY3
33
32
 
34
33
 
35
34
class CompletionWidget(QListWidget):
179
178
class TextEditBaseWidget(QPlainTextEdit, BaseEditMixin):
180
179
    """Text edit base widget"""
181
180
    BRACE_MATCHING_SCOPE = ('sof', 'eof')
 
181
    cell_separators = None
182
182
    
183
183
    def __init__(self, parent=None):
184
184
        QPlainTextEdit.__init__(self, parent)
208
208
        self.codecompletion_enter = False
209
209
        self.calltips = True
210
210
        self.calltip_position = None
211
 
        self.has_cell_separators = False        
 
211
        self.has_cell_separators = False
212
212
        self.completion_text = ""
213
213
        self.calltip_widget = CallTipWidget(self, hide_timer_on=True)
214
214
        
287
287
    #------Highlight current cell
288
288
    def highlight_current_cell(self):
289
289
        """Highlight current cell"""
 
290
        if self.cell_separators is None:
 
291
            return
290
292
        selection = QTextEdit.ExtraSelection()
291
293
        selection.format.setProperty(QTextFormat.FullWidthSelection,
292
294
                                     to_qvariant(True))
435
437
        Copy text to clipboard with correct EOL chars
436
438
        """
437
439
        QApplication.clipboard().setText(self.get_selected_text())
 
440
    
 
441
    def toPlainText(self):
 
442
        """
 
443
        Reimplement Qt method
 
444
        Fix PyQt4 bug on Windows and Python 3
 
445
        """
 
446
        # Fix what appears to be a PyQt4 bug when getting file
 
447
        # contents under Windows and PY3. This bug leads to
 
448
        # corruptions when saving files with certain combinations
 
449
        # of unicode chars on them (like the one attached on
 
450
        # Issue 1546)
 
451
        if os.name == 'nt' and PY3:
 
452
            text = self.get_text('sof', 'eof')
 
453
            return text.replace('\u2028', '\n').replace('\u2029', '\n')\
 
454
                       .replace('\u0085', '\n')
 
455
        else:
 
456
            return super(TextEditBaseWidget, self).toPlainText()
438
457
 
439
458
    #------Text: get, set, ...
440
459
    def get_selection_as_executable_code(self):
530
549
            text = to_text_string(cursor0.selectedText())
531
550
        else:
532
551
            text = to_text_string(block.text())
533
 
        return text.lstrip().startswith(CELL_SEPARATORS)
 
552
        if self.cell_separators is None:
 
553
            return False
 
554
        else:
 
555
            return text.lstrip().startswith(self.cell_separators)
534
556
 
535
557
    def select_current_cell(self):
536
558
        """Select cell under cursor
932
954
        """Reimplement Qt method"""
933
955
        if sys.platform.startswith('linux') and event.button() == Qt.MidButton:
934
956
            self.calltip_widget.hide()
935
 
            if self.has_selected_text():
936
 
                self.remove_selected_text()
937
957
            self.setFocus()
938
958
            event = QMouseEvent(QEvent.MouseButtonPress, event.pos(),
939
959
                                Qt.LeftButton, Qt.LeftButton, Qt.NoModifier)
941
961
            QPlainTextEdit.mouseReleaseEvent(self, event)
942
962
            # Send selection text to clipboard to be able to use
943
963
            # the paste method and avoid the strange Issue 1445
944
 
            #
945
 
            # Note: This issue seems a focusing problem but it
 
964
            # NOTE: This issue seems a focusing problem but it
946
965
            # seems really hard to track
947
966
            mode_clip = QClipboard.Clipboard
948
967
            mode_sel = QClipboard.Selection
959
978
        """Reimplemented to handle focus"""
960
979
        self.emit(SIGNAL("focus_changed()"))
961
980
        self.emit(SIGNAL("focus_in()"))
 
981
        self.highlight_current_cell()
962
982
        QPlainTextEdit.focusInEvent(self, event)
963
983
        
964
984
    def focusOutEvent(self, event):
1234
1254
                              light_background=self.light_background,
1235
1255
                              is_default=style is self.default_style)
1236
1256
        self.ansi_handler.set_base_format(self.default_style.format)
1237