def find_text(self, text, changed=True, forward=True, case=False, words=False, regexp=False): """Find text""" cursor = self.textCursor() findflag = QTextDocument.FindFlag() if not forward: findflag = findflag | QTextDocument.FindBackward if case: findflag = findflag | QTextDocument.FindCaseSensitively moves = [QTextCursor.NoMove] if forward: moves += [QTextCursor.NextWord, QTextCursor.Start] if changed: if to_text_string(cursor.selectedText()): new_position = min([cursor.selectionStart(), cursor.selectionEnd()]) cursor.setPosition(new_position) else: cursor.movePosition(QTextCursor.PreviousWord) else: moves += [QTextCursor.End] if regexp: text = to_text_string(text) else: text = re.escape(to_text_string(text)) if QT55_VERSION: pattern = QRegularExpression(u"\\b{}\\b".format(text) if words else text) if case: pattern.setPatternOptions( QRegularExpression.CaseInsensitiveOption) else: pattern = QRegExp(u"\\b{}\\b".format(text) if words else text, Qt.CaseSensitive if case else Qt.CaseInsensitive, QRegExp.RegExp2) for move in moves: cursor.movePosition(move) if regexp and '\\n' in text: # Multiline regular expression found_cursor = self.find_multiline_pattern(pattern, cursor, findflag) else: # Single line find: using the QTextDocument's find function, # probably much more efficient than ours found_cursor = self.document().find(pattern, cursor, findflag) if found_cursor is not None and not found_cursor.isNull(): self.setTextCursor(found_cursor) return True return False
def search(self, txt): """Search regular expressions key inside document(from spyder_vim).""" editor = self.get_editor() cursor = QTextCursor(editor.document()) cursor.movePosition(QTextCursor.Start) # Apply the option for search is_ignorecase = CONF.get(CONF_SECTION, 'ignorecase') is_smartcase = CONF.get(CONF_SECTION, 'smartcase') if is_ignorecase is True: option = None self.vim_status.search.ignorecase = True if is_smartcase and txt.lower() != txt: option = QTextDocument.FindCaseSensitively self.vim_status.search.ignorecase = False else: option = QTextDocument.FindCaseSensitively self.vim_status.search.ignorecase = False # Find key in document forward search_stack = [] if option: cursor = editor.document().find(QRegularExpression(txt), options=option) else: cursor = editor.document().find(QRegularExpression(txt)) back = self.vim_status.search.color_bg fore = self.vim_status.search.color_fg while not cursor.isNull(): selection = QTextEdit.ExtraSelection() selection.format.setBackground(back) selection.format.setForeground(fore) selection.cursor = cursor search_stack.append(selection) if option: cursor = editor.document().find(QRegularExpression(txt), cursor, options=option) else: cursor = editor.document().find(QRegularExpression(txt), cursor) self.vim_status.cursor.set_extra_selections('vim_search', [i for i in search_stack]) editor.update_extra_selections() self.vim_status.search.selection_list = search_stack self.vim_status.search.txt_searched = txt
def loadSyntaxFromYAML(self): with open(os.path.join(YAML_DIR, 'gcode_syntax.yml')) as fh: syntax_specs = yaml.load(fh, Loader=yaml.FullLoader) assert isinstance(syntax_specs, dict), \ "Invalid YAML format for language spec, root item must be a dictionary." cio = QRegularExpression.CaseInsensitiveOption for lang_name, language in syntax_specs.items(): definitions = language.get('definitions', {}) default_fmt_spec = definitions.get('default', {}).get('textFormat', {}) for context_name, spec in definitions.items(): base_fmt = default_fmt_spec.copy() fmt_spec = spec.get('textFormat', {}) # update the default fmt spec base_fmt.update(fmt_spec) char_fmt = self.charFormatFromSpec(fmt_spec) patterns = spec.get('match', []) for pattern in patterns: self.rules.append( [QRegularExpression(pattern, cio), char_fmt])
def loadSyntaxFromYAML(self): if INFO.getGcodeSyntaxFile() is not None: YAML_DIR = os.environ['CONFIG_DIR'] gcode_syntax_file = INFO.getGcodeSyntaxFile() else: YAML_DIR = os.path.dirname(DEFAULT_CONFIG_FILE) gcode_syntax_file = 'gcode_syntax.yml' with open(os.path.join(YAML_DIR, gcode_syntax_file)) as fh: syntax_specs = yaml.load(fh, Loader=yaml.FullLoader) cio = QRegularExpression.CaseInsensitiveOption for lang_name, language in list(syntax_specs.items()): definitions = language.get('definitions', {}) default_fmt_spec = definitions.get('default', {}).get('textFormat', {}) for context_name, spec in list(definitions.items()): base_fmt = default_fmt_spec.copy() fmt_spec = spec.get('textFormat', {}) # update the default fmt spec base_fmt.update(fmt_spec) char_fmt = self.charFormatFromSpec(fmt_spec) patterns = spec.get('match', []) for pattern in patterns: self.rules.append( [QRegularExpression(pattern, cio), char_fmt])
def __init__(self, document): super().__init__(document) self.rules = [] # keywords f = QTextCharFormat() f.setFontWeight(QFont.Bold) f.setForeground(Qt.darkBlue) for kw in keyword.kwlist: self.rules.append((QRegularExpression(rf"\b{kw}\b"), f)) # numerals f = QTextCharFormat() f.setForeground(Qt.blue) self.rules.append((QRegularExpression("[0-9]+"), f)) # strings f = QTextCharFormat() f.setForeground(Qt.darkCyan) self.rules.append((QRegularExpression("\".*\""), f)) self.rules.append((QRegularExpression("'.*'"), f))
new_position = min( [cursor.selectionStart(), cursor.selectionEnd()]) cursor.setPosition(new_position) else: cursor.movePosition(QTextCursor.PreviousWord) else: moves += [QTextCursor.End] if regexp: text = to_text_string(text) else: text = re.escape(to_text_string(text)) if QT55_VERSION: pattern = QRegularExpression( u"\\b{}\\b".format(text) if words else text) if case: pattern.setPatternOptions( QRegularExpression.CaseInsensitiveOption) else: pattern = QRegExp(u"\\b{}\\b".format(text) if words else text, Qt.CaseSensitive if case else Qt.CaseInsensitive, QRegExp.RegExp2) for move in moves: cursor.movePosition(move) if regexp and '\\n' in text: # Multiline regular expression found_cursor = self.find_multiline_pattern( pattern, cursor, findflag) else: