def unindent_selection(self, cursor): """ Un-indents selected text :param cursor: QTextCursor """ doc = self.editor.document() tab_len = self.editor.tab_length nb_lines = len(cursor.selection().toPlainText().splitlines()) if nb_lines == 0: nb_lines = 1 block = doc.findBlock(cursor.selectionStart()) assert isinstance(block, QtGui.QTextBlock) i = 0 debug('unindent selection: %d lines', nb_lines) while i < nb_lines: txt = block.text() debug('line to unindent: %s', txt) debug('self.editor.use_spaces_instead_of_tabs: %r', self.editor.use_spaces_instead_of_tabs) if self.editor.use_spaces_instead_of_tabs: indentation = (len(txt) - len(txt.lstrip())) else: indentation = len(txt) - len(txt.replace('\t', '')) debug('unindent line %d: %d spaces', i, indentation) if indentation > 0: c = QtGui.QTextCursor(block) c.movePosition(c.StartOfLine, cursor.MoveAnchor) for _ in range(tab_len): txt = block.text() if len(txt) and txt[0] == ' ': c.deleteChar() block = block.next() i += 1 return cursor
def indent_selection(self, cursor): """ Indent selected text :param cursor: QTextCursor """ doc = self.editor.document() tab_len = self.editor.tab_length cursor.beginEditBlock() nb_lines = len(cursor.selection().toPlainText().splitlines()) if (cursor.atBlockStart() and cursor.position() == cursor.selectionEnd()): nb_lines += 1 block = doc.findBlock(cursor.selectionStart()) i = 0 # indent every lines while i < nb_lines: nb_space_to_add = tab_len cursor = QtGui.QTextCursor(block) indentation = TextHelper(self.editor).line_indent(block) if indentation < self.min_column: # align with at least 7th char nb_space_to_add = self.min_column if block.text().strip().startswith('*') and indentation == 0: nb_space_to_add -= 1 cursor.movePosition(cursor.StartOfLine, cursor.MoveAnchor) if self.editor.use_spaces_instead_of_tabs: for _ in range(nb_space_to_add): cursor.insertText(" ") else: cursor.insertText('\t') block = block.next() i += 1 cursor.endEditBlock()
def indent_selection(self, cursor): """ Indent selected text :param cursor: QTextCursor """ doc = self.editor.document() tab_len = self.editor.tab_length cursor.beginEditBlock() nb_lines = len(cursor.selection().toPlainText().splitlines()) c = self.editor.textCursor() if c.atBlockStart() and c.position() == c.selectionEnd(): nb_lines += 1 block = doc.findBlock(cursor.selectionStart()) i = 0 # indent every lines while i < nb_lines: nb_space_to_add = tab_len cursor = QtGui.QTextCursor(block) cursor.movePosition(cursor.StartOfLine, cursor.MoveAnchor) if self.editor.use_spaces_instead_of_tabs: for _ in range(nb_space_to_add): cursor.insertText(" ") else: cursor.insertText('\t') block = block.next() i += 1 cursor.endEditBlock()
def _on_key_pressed(self, event): no_modifiers = int(event.modifiers()) == QtCore.Qt.NoModifier if event.key() == QtCore.Qt.Key_Backspace and no_modifiers: if self.editor.textCursor().atBlockStart(): return tab_len = self.editor.tab_length tab_len = (self.editor.textCursor().positionInBlock() - self.editor.indenter_mode.min_column) % tab_len if tab_len == 0: tab_len = self.editor.tab_length # count the number of spaces deletable, stop at tab len spaces = 0 cursor = QtGui.QTextCursor(self.editor.textCursor()) while (spaces < tab_len and cursor.positionInBlock() > self.editor.indenter_mode.min_column): pos = cursor.position() cursor.movePosition(cursor.Left, cursor.KeepAnchor) char = cursor.selectedText() if char == " ": spaces += 1 else: break cursor.setPosition(pos - 1) cursor = self.editor.textCursor() if spaces == 0: return cursor.beginEditBlock() for _ in range(spaces): cursor.deletePreviousChar() cursor.endEditBlock() self.editor.setTextCursor(cursor) event.accept()
def __init__(self, cursor_or_bloc_or_doc, start_pos=None, end_pos=None, start_line=None, end_line=None, draw_order=0, tooltip=None, full_width=False): """ Creates a text decoration. .. note:: start_pos/end_pos and start_line/end_line pairs let you easily specify the selected text. You should use one pair or the other or they will conflict between each others. If you don't specify any values, the selection will be based on the cursor. :param cursor_or_bloc_or_doc: Reference to a valid QTextCursor/QTextBlock/QTextDocument :param start_pos: Selection start position :param end_pos: Selection end position :param start_line: Selection start line. :param end_line: Selection end line. :param draw_order: The draw order of the selection, highest values will appear on top of the lowest values. :param tooltip: An optional tooltips that will be automatically shown when the mouse cursor hover the decoration. :param full_width: True to select the full line width. .. note:: Use the cursor selection if startPos and endPos are none. """ super(TextDecoration, self).__init__() self.signals = self.Signals() self.draw_order = draw_order self.tooltip = tooltip self.cursor = QtGui.QTextCursor(cursor_or_bloc_or_doc) if full_width: self.set_full_width(full_width) if start_pos is not None: self.cursor.setPosition(start_pos) if end_pos is not None: self.cursor.setPosition(end_pos, QtGui.QTextCursor.KeepAnchor) if start_line is not None: self.cursor.movePosition(self.cursor.Start, self.cursor.MoveAnchor) self.cursor.movePosition(self.cursor.Down, self.cursor.MoveAnchor, start_line) if end_line is not None: self.cursor.movePosition(self.cursor.Down, self.cursor.KeepAnchor, end_line - start_line)
def selection_range(self): """ Returns the selected lines boundaries (start line, end line) :return: tuple(int, int) """ editor = self._editor doc = editor.document() start = doc.findBlock( editor.textCursor().selectionStart()).blockNumber() end = doc.findBlock(editor.textCursor().selectionEnd()).blockNumber() text_cursor = QtGui.QTextCursor(editor.textCursor()) text_cursor.setPosition(editor.textCursor().selectionEnd()) if text_cursor.columnNumber() == 0 and start != end: end -= 1 return start, end
def count_deletable_spaces(self, cursor, max_spaces): # count the number of spaces deletable, stop at tab len max_spaces = abs(max_spaces) if max_spaces > self.editor.tab_length: max_spaces = self.editor.tab_length spaces = 0 trav_cursor = QtGui.QTextCursor(cursor) while spaces < max_spaces or trav_cursor.atBlockStart(): pos = trav_cursor.position() trav_cursor.movePosition(cursor.Left, cursor.KeepAnchor) char = trav_cursor.selectedText() if char == " ": spaces += 1 else: break trav_cursor.setPosition(pos - 1) return spaces
def unindent_selection(self, cursor): """ Un-indents selected text :param cursor: QTextCursor """ doc = self.editor.document() tab_len = self.editor.tab_length nb_lines = len(cursor.selection().toPlainText().splitlines()) if nb_lines == 0: nb_lines = 1 block = doc.findBlock(cursor.selectionStart()) assert isinstance(block, QtGui.QTextBlock) i = 0 _logger().debug('unindent selection: %d lines', nb_lines) while i < nb_lines: txt = block.text()[self.min_column:] _logger().debug('line to unindent: %s', txt) _logger().debug('self.editor.use_spaces_instead_of_tabs: %r', self.editor.use_spaces_instead_of_tabs) if self.editor.use_spaces_instead_of_tabs: indentation = len(txt) - len(txt.lstrip()) else: indentation = len(txt) - len(txt.replace('\t', '')) _logger().debug('unindent line %d: %d spaces (min indent=%d)', i, indentation, self.min_column) if indentation > 0: c = QtGui.QTextCursor(block) c.movePosition(c.StartOfLine, cursor.MoveAnchor) c.movePosition(c.Right, cursor.MoveAnchor, indentation + self.min_column) max_spaces = indentation % tab_len if max_spaces == 0: max_spaces = tab_len spaces = self.count_deletable_spaces(c, max_spaces) for _ in range(spaces): c.deletePreviousChar() block = block.next() i += 1 return cursor
def indent_selection(self, cursor): """ Indent selected text :param cursor: QTextCursor """ doc = self.editor.document() cursor.beginEditBlock() nb_lines = len(cursor.selection().toPlainText().splitlines()) c = self.editor.textCursor() if c.atBlockStart() and c.position() == c.selectionEnd(): nb_lines += 1 block = doc.findBlock(cursor.selectionStart()) i = 0 # indent every lines while i < nb_lines: cursor = QtGui.QTextCursor(block) cursor.movePosition(cursor.StartOfLine, cursor.MoveAnchor) cursor.insertText(self._single_indent) block = block.next() i += 1 cursor.endEditBlock()
def list_symbols(editor, block, character): """ Retuns a list of symbols found in the block text :param editor: code edit instance :param block: block to parse :param character: character to look for. """ text = block.text() symbols = [] cursor = QtGui.QTextCursor(block) cursor.movePosition(cursor.StartOfBlock) pos = text.find(character, 0) cursor.movePosition(cursor.Right, cursor.MoveAnchor, pos) while pos != -1: if not TextHelper(editor).is_comment_or_string(cursor): # skips symbols in string literal or comment info = ParenthesisInfo(pos, character) symbols.append(info) pos = text.find(character, pos + 1) cursor.movePosition(cursor.StartOfBlock) cursor.movePosition(cursor.Right, cursor.MoveAnchor, pos) return symbols