def test_bug_unindent(editor): mode = editor.modes.get(modes.IndenterMode) editor.use_spaces_instead_of_tabs = True editor.setPlainText(' print("foo")', 'text/x-python', 'utf-8') api.TextHelper(editor).goto_line(0, column=len(' print("foo")')) mode.unindent() assert editor.toPlainText() == 'print("foo")'
def get_selected_lines(self): nb_lines = len( api.TextHelper(self.editor).selected_text().splitlines()) cursor = self.editor.textCursor() self._move_cursor_to_selection_start(cursor) lines = [] for i in range(nb_lines): lines.append(cursor.block().text()) cursor.movePosition(cursor.NextBlock) return lines
def _on_cursor_position_changed(self): if self.editor.textCursor().hasSelection(): return new_pos = api.TextHelper(self.editor).cursor_position() if abs(new_pos[0] - self._prev_pos[0]) > 1: # only record when line changed and don't record change if the user # just wen to the previous/next line cmd = MoveCursorCommand(new_pos, self._prev_pos, self.editor) self.undo_stack.push(cmd) self._prev_pos = new_pos
def _move(self, line, column): self._editor().blockSignals(True) api.TextHelper(self._editor()).goto_line(line, column) self._editor().blockSignals(False) try: caret_mode = self._editor().modes.get('CaretLineHighlighterMode') except KeyError: pass else: caret_mode.refresh()
def _update_status_bar(self, editor): if editor: l, c = api.TextHelper(editor).cursor_position() self.lbl_cursor_pos.setText('%d:%d' % (l + 1, c + 1)) self.lbl_encoding.setText(editor.file.encoding) self.lbl_filename.setText(editor.file.path) else: self.lbl_cursor_pos.clear() self.lbl_encoding.clear() self.lbl_filename.clear()
def get_operation(self): lines = self.get_selected_lines() if not lines: lines = [api.TextHelper(self.editor).current_line_text()] min_indent = sys.maxsize comment = False for l in lines: indent = len(l) - len(l.lstrip()) if indent < min_indent and l: min_indent = indent if not l.lstrip().startswith('# ') and l: comment = True return min_indent, comment, len(lines)
def _do_home_key(self, event=None, select=False): """ Performs home key action """ # get nb char to first significative char min_column = self.indenter_mode.min_column text = api.TextHelper(self).current_line_text()[min_column:] indent = len(text) - len(text.lstrip()) delta = (self.textCursor().positionInBlock() - indent - min_column) cursor = self.textCursor() move = QtGui.QTextCursor.MoveAnchor if select: move = QtGui.QTextCursor.KeepAnchor if delta > 0: cursor.movePosition(QtGui.QTextCursor.Left, move, delta) else: cursor.movePosition(QtGui.QTextCursor.StartOfBlock, move) cursor.movePosition(QtGui.QTextCursor.Right, cursor.MoveAnchor, min_column) self.setTextCursor(cursor) if event: event.accept()
def on_cursor_pos_changed(self): if self.tabWidget.current_widget(): editor = self.tabWidget.current_widget() l, c = api.TextHelper(editor).cursor_position() self.lbl_cursor_pos.setText('%d:%d' % (l + 1, c + 1))