def find(self, changed=True, forward=True, rehighlight=True, start_highlight_timer=False, multiline_replace_check=True): """Call the find function""" # When several lines are selected in the editor and replace box is # activated, dynamic search is deactivated to prevent changing the # selection. Otherwise we show matching items. if multiline_replace_check and self.replace_widgets[0].isVisible(): sel_text = self.editor.get_selected_text() if len(to_text_string(sel_text).splitlines()) > 1: return None text = self.search_text.currentText() if len(text) == 0: self.search_text.lineEdit().setStyleSheet("") if not self.is_code_editor: # Clears the selection for WebEngine self.editor.find_text('') self.change_number_matches() return None else: case = self.case_button.isChecked() word = self.words_button.isChecked() regexp = self.re_button.isChecked() found = self.editor.find_text(text, changed, forward, case=case, word=word, regexp=regexp) stylesheet = self.STYLE[found] tooltip = self.TOOLTIP[found] if not found and regexp: error_msg = regexp_error_msg(text) if error_msg: # special styling for regexp errors stylesheet = self.STYLE['regexp_error'] tooltip = self.TOOLTIP['regexp_error'] + ': ' + error_msg self.search_text.lineEdit().setStyleSheet(stylesheet) self.search_text.setToolTip(tooltip) if self.is_code_editor and found: cursor = QTextCursor(self.editor.textCursor()) TextHelper(self.editor).unfold_if_colapsed(cursor) if rehighlight or not self.editor.found_results: self.highlight_timer.stop() if start_highlight_timer: self.highlight_timer.start() else: self.highlight_matches() else: self.clear_matches() number_matches = self.editor.get_number_matches(text, case=case, regexp=regexp, word=word) if hasattr(self.editor, 'get_match_number'): match_number = self.editor.get_match_number(text, case=case, regexp=regexp, word=word) else: match_number = 0 self.change_number_matches(current_match=match_number, total_matches=number_matches) return found
def find(self, changed=True, forward=True, rehighlight=True, start_highlight_timer=False, multiline_replace_check=True): """Call the find function""" # When several lines are selected in the editor and replace box is activated, # dynamic search is deactivated to prevent changing the selection. Otherwise # we show matching items. if multiline_replace_check and self.replace_widgets[0].isVisible() and \ len(to_text_string(self.editor.get_selected_text()).splitlines())>1: return None text = self.search_text.currentText() if len(text) == 0: self.search_text.lineEdit().setStyleSheet("") if not self.is_code_editor: # Clears the selection for WebEngine self.editor.find_text('') self.change_number_matches() return None else: case = self.case_button.isChecked() words = self.words_button.isChecked() regexp = self.re_button.isChecked() found = self.editor.find_text(text, changed, forward, case=case, words=words, regexp=regexp) stylesheet = self.STYLE[found] tooltip = self.TOOLTIP[found] if not found and regexp: error_msg = regexp_error_msg(text) if error_msg: # special styling for regexp errors stylesheet = self.STYLE['regexp_error'] tooltip = self.TOOLTIP['regexp_error'] + ': ' + error_msg self.search_text.lineEdit().setStyleSheet(stylesheet) self.search_text.setToolTip(tooltip) if self.is_code_editor and found: block = self.editor.textCursor().block() TextHelper(self.editor).unfold_if_colapsed(block) if rehighlight or not self.editor.found_results: self.highlight_timer.stop() if start_highlight_timer: self.highlight_timer.start() else: self.highlight_matches() else: self.clear_matches() number_matches = self.editor.get_number_matches(text, case=case, regexp=regexp) if hasattr(self.editor, 'get_match_number'): match_number = self.editor.get_match_number(text, case=case, regexp=regexp) else: match_number = 0 self.change_number_matches(current_match=match_number, total_matches=number_matches) return found
def find(self, changed=True, forward=True, rehighlight=False, start_highlight_timer=False, multiline_replace_check=False): """Call the find function""" text = self.search_text.currentText() if len(text) == 0: self.search_text.lineEdit().setStyleSheet("") return None else: case = self.case_button.isChecked() word = self.words_button.isChecked() regexp = self.re_button.isChecked() if forward: found = self.parent.search_next(text, case=case, regex=regexp, word=word) else: found = self.parent.search_previous(text, case=case, regex=regexp, word=word) found = False if found == -1 else True stylesheet = self.STYLE[found] tooltip = self.TOOLTIP[found] if not found and regexp: error_msg = regexp_error_msg(text) if error_msg: # special styling for regexp errors stylesheet = self.STYLE['regexp_error'] tooltip = self.TOOLTIP['regexp_error'] + ': ' + error_msg self.search_text.lineEdit().setStyleSheet(stylesheet) self.search_text.setToolTip(tooltip) return found
def _get_options(self): """ Get search options. """ text_re = self.search_regexp_action.isChecked() exclude_re = self.exclude_regexp_action.isChecked() case_sensitive = self.case_action.isChecked() # Clear fields self.search_text_edit.lineEdit().setStyleSheet("") self.exclude_pattern_edit.lineEdit().setStyleSheet("") self.exclude_pattern_edit.setToolTip("") self.search_text_edit.setToolTip("") utext = str(self.search_text_edit.currentText()) if not utext: return try: texts = [(utext.encode('utf-8'), 'utf-8')] except UnicodeEncodeError: texts = [] for enc in self.supported_encodings: try: texts.append((utext.encode(enc), enc)) except UnicodeDecodeError: pass exclude = str(self.exclude_pattern_edit.currentText()) if not case_sensitive: texts = [(text[0].lower(), text[1]) for text in texts] file_search = self.path_selection_combo.is_file_search() path = self.path_selection_combo.get_current_searchpath() if not exclude_re: items = [ fnmatch.translate(item.strip()) for item in exclude.split(",") if item.strip() != '' ] exclude = '|'.join(items) # Validate exclude regular expression if exclude: error_msg = regexp_error_msg(exclude) if error_msg: exclude_edit = self.exclude_pattern_edit.lineEdit() exclude_edit.setStyleSheet(self.REGEX_INVALID) tooltip = self.REGEX_ERROR + ': ' + str(error_msg) self.exclude_pattern_edit.setToolTip(tooltip) return None else: exclude = re.compile(exclude) # Validate text regular expression if text_re: error_msg = regexp_error_msg(texts[0][0]) if error_msg: self.search_text_edit.lineEdit().setStyleSheet( self.REGEX_INVALID) tooltip = self.REGEX_ERROR + ': ' + str(error_msg) self.search_text_edit.setToolTip(tooltip) return None else: texts = [(re.compile(x[0]), x[1]) for x in texts] return (path, file_search, exclude, texts, text_re, case_sensitive)
def get_options(self, to_save=False): """Get options""" text_re = self.edit_regexp.isChecked() exclude_re = self.exclude_regexp.isChecked() case_sensitive = self.case_button.isChecked() # Return current options for them to be saved when closing # Spyder. if to_save: search_text = [to_text_string(self.search_text.itemText(index)) for index in range(self.search_text.count())] exclude = [to_text_string(self.exclude_pattern.itemText(index)) for index in range(self.exclude_pattern.count())] exclude_idx = self.exclude_pattern.currentIndex() path_history = self.path_selection_combo.get_external_paths() more_options = self.more_options.isChecked() return (search_text, text_re, exclude, exclude_idx, exclude_re, more_options, case_sensitive, path_history) # Clear fields self.search_text.lineEdit().setStyleSheet("") self.exclude_pattern.lineEdit().setStyleSheet("") self.search_text.setToolTip("") self.exclude_pattern.setToolTip("") utext = to_text_string(self.search_text.currentText()) if not utext: return try: texts = [(utext.encode('utf-8'), 'utf-8')] except UnicodeEncodeError: texts = [] for enc in self.supported_encodings: try: texts.append((utext.encode(enc), enc)) except UnicodeDecodeError: pass exclude = to_text_string(self.exclude_pattern.currentText()) if not case_sensitive: texts = [(text[0].lower(), text[1]) for text in texts] file_search = self.path_selection_combo.is_file_search() path = self.path_selection_combo.get_current_searchpath() if not exclude_re: items = [fnmatch.translate(item.strip()) for item in exclude.split(",") if item.strip() != ''] exclude = '|'.join(items) # Validate exclude regular expression if exclude: error_msg = regexp_error_msg(exclude) if error_msg: exclude_edit = self.exclude_pattern.lineEdit() exclude_edit.setStyleSheet(self.REGEX_INVALID) tooltip = self.REGEX_ERROR + u': ' + to_text_string(error_msg) self.exclude_pattern.setToolTip(tooltip) return None else: exclude = re.compile(exclude) # Validate text regular expression if text_re: error_msg = regexp_error_msg(texts[0][0]) if error_msg: self.search_text.lineEdit().setStyleSheet(self.REGEX_INVALID) tooltip = self.REGEX_ERROR + u': ' + to_text_string(error_msg) self.search_text.setToolTip(tooltip) return None else: texts = [(re.compile(x[0]), x[1]) for x in texts] return (path, file_search, exclude, texts, text_re, case_sensitive)