~ubuntu-branches/ubuntu/trusty/spyder/trusty-proposed

« back to all changes in this revision

Viewing changes to spyderlib/widgets/findreplace.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2013-05-08 21:12:32 UTC
  • mfrom: (1.2.1) (18.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20130508211232-r867h9xoenknvuxf
Tags: 2.2.0+dfsg-1
* Imported Upstream version 2.2.0+dfsg
* Depends on ipython-qtconsole (< 1.0)
* Removed the obsolte DM-Upload-Allowed

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
# pylint: disable=R0201
13
13
 
14
14
from spyderlib.qt.QtGui import (QHBoxLayout, QGridLayout, QCheckBox, QLabel,
15
 
                                QWidget, QSizePolicy, QShortcut, QKeySequence)
 
15
                                QWidget, QSizePolicy, QShortcut, QKeySequence,
 
16
                                QTextCursor)
16
17
from spyderlib.qt.QtCore import SIGNAL, Qt, QTimer
17
18
 
 
19
import re
 
20
 
18
21
# Local imports
19
 
from spyderlib.utils.qthelpers import get_std_icon, create_toolbutton
 
22
from spyderlib.utils.qthelpers import (get_icon, get_std_icon,
 
23
                                       create_toolbutton)
20
24
from spyderlib.widgets.comboboxes import PatternComboBox
21
25
from spyderlib.baseconfig import _
22
 
from spyderlib.config import get_icon
 
26
 
 
27
 
 
28
def is_position_sup(pos1, pos2):
 
29
    """Return True is pos1 > pos2"""
 
30
    return pos1 > pos2
 
31
    
 
32
def is_position_inf(pos1, pos2):
 
33
    """Return True is pos1 < pos2"""
 
34
    return pos1 < pos2
23
35
 
24
36
 
25
37
class FindReplace(QWidget):
48
60
        # Find layout
49
61
        self.search_text = PatternComboBox(self, tip=_("Search string"),
50
62
                                           adjust_to_minimum=False)
 
63
        self.connect(self.search_text, SIGNAL('valid(bool)'),
 
64
                     lambda state:
 
65
                     self.find(changed=False, forward=True, rehighlight=False))
51
66
        self.connect(self.search_text.lineEdit(),
52
67
                     SIGNAL("textEdited(QString)"), self.text_has_been_edited)
53
68
        
264
279
        state = self.find(changed=False, forward=False, rehighlight=False)
265
280
        self.editor.setFocus()
266
281
        return state
267
 
        
 
282
 
268
283
    def text_has_been_edited(self, text):
269
284
        """Find text has been edited (this slot won't be triggered when 
270
285
        setting the search pattern combo box text programmatically"""
298
313
            found = self.editor.find_text(text, changed, forward, case=case,
299
314
                                          words=words, regexp=regexp)
300
315
            self.search_text.lineEdit().setStyleSheet(self.STYLE[found])
301
 
            if found:
 
316
            if self.is_code_editor and found:
302
317
                if rehighlight or not self.editor.found_results:
303
318
                    self.highlight_timer.stop()
304
319
                    if start_highlight_timer:
312
327
    def replace_find(self):
313
328
        """Replace and find"""
314
329
        if (self.editor is not None):
315
 
            replace_text = self.replace_text.currentText()
316
 
            search_text = self.search_text.currentText()
 
330
            replace_text = unicode(self.replace_text.currentText())
 
331
            search_text = unicode(self.search_text.currentText())
317
332
            pattern = search_text if self.re_button.isChecked() else None
 
333
            case = self.case_button.isChecked()
318
334
            first = True
 
335
            cursor = None
319
336
            while True:
320
337
                if first:
321
338
                    # First found
322
 
                    if self.editor.has_selected_text() and \
323
 
                       self.editor.get_selected_text() == unicode(search_text):
 
339
                    seltxt = unicode(self.editor.get_selected_text())
 
340
                    cmptxt1 = search_text if case else search_text.lower()
 
341
                    cmptxt2 = seltxt if case else seltxt.lower()
 
342
                    if self.editor.has_selected_text() and cmptxt1 == cmptxt2:
324
343
                        # Text was already found, do nothing
325
344
                        pass
326
345
                    else:
331
350
                    wrapped = False
332
351
                    position = self.editor.get_position('cursor')
333
352
                    position0 = position
 
353
                    cursor = self.editor.textCursor()
 
354
                    cursor.beginEditBlock()
334
355
                else:
335
356
                    position1 = self.editor.get_position('cursor')
336
357
                    if wrapped:
337
358
                        if position1 == position or \
338
 
                           self.editor.is_position_sup(position1, position):
 
359
                           is_position_sup(position1, position):
339
360
                            # Avoid infinite loop: replace string includes
340
361
                            # part of the search string
341
362
                            break
342
363
                    if position1 == position0:
343
364
                        # Avoid infinite loop: single found occurence
344
365
                        break
345
 
                    if self.editor.is_position_inf(position1, position0):
 
366
                    if is_position_inf(position1, position0):
346
367
                        wrapped = True
347
368
                    position0 = position1
348
 
                self.editor.replace(replace_text, pattern=pattern)
349
 
                if not self.find_next():
 
369
                if pattern is None:
 
370
                    cursor.removeSelectedText()
 
371
                    cursor.insertText(replace_text)
 
372
                else:
 
373
                    seltxt = unicode(cursor.selectedText())
 
374
                    cursor.removeSelectedText()
 
375
                    cursor.insertText(re.sub(pattern, replace_text, seltxt))
 
376
                if self.find_next():
 
377
                    found_cursor = self.editor.textCursor()
 
378
                    cursor.setPosition(found_cursor.selectionStart(),
 
379
                                       QTextCursor.MoveAnchor)
 
380
                    cursor.setPosition(found_cursor.selectionEnd(),
 
381
                                       QTextCursor.KeepAnchor)
 
382
                else:
350
383
                    break
351
384
                if not self.all_check.isChecked():
352
385
                    break
353
386
            self.all_check.setCheckState(Qt.Unchecked)
354
 
            
 
387
            if cursor is not None:
 
388
                cursor.endEditBlock()