def apply_smart_comment(editor, opening='/*', closing='*/', line_comment=None): doc = editor.document() c = QTextCursor(editor.textCursor()) c.clearSelection() before_opening = doc.find( opening, c, QTextDocument.FindFlag.FindBackward | QTextDocument.FindFlag.FindCaseSensitively) before_closing = doc.find( closing, c, QTextDocument.FindFlag.FindBackward | QTextDocument.FindFlag.FindCaseSensitively) after_opening = doc.find(opening, c, QTextDocument.FindFlag.FindCaseSensitively) after_closing = doc.find(closing, c, QTextDocument.FindFlag.FindCaseSensitively) in_block_comment = (not before_opening.isNull() and (before_closing.isNull() or before_opening.position() >= before_closing.position())) and \ (not after_closing.isNull() and (after_opening.isNull() or after_closing.position() <= after_opening.position())) if in_block_comment: before_opening.removeSelectedText(), after_closing.removeSelectedText() return c = QTextCursor(editor.textCursor()) left, right = min(c.position(), c.anchor()), max(c.position(), c.anchor()) c.beginEditBlock() c.setPosition(right), c.insertText(closing) c.setPosition(left), c.insertText(opening) c.endEditBlock()
def set_document(self, doc, doc_name=None): old_doc = self.doc if old_doc is not None: old_doc.contentsChange.disconnect(self.reformat_blocks) c = QTextCursor(old_doc) c.beginEditBlock() blk = old_doc.begin() while blk.isValid(): blk.layout().clearAdditionalFormats() blk = blk.next() c.endEditBlock() self.doc = self.doc_name = None if doc is not None: self.doc = doc self.doc_name = doc_name doc.contentsChange.connect(self.reformat_blocks) self.rehighlight()
def resized(self): ' Resize images to fit in new view size and adjust all line number references accordingly ' for v in (self.left, self.right): changes = [] for i, (top, bot, kind) in enumerate(v.changes): if top in v.images: img, oldw, oldlines = v.images[top] lines, w = self.get_lines_for_image(img, v) if lines != oldlines: changes.append((i, lines, lines - oldlines, img, w)) for i, lines, delta, img, w in changes: top, bot, kind = v.changes[i] c = QTextCursor(v.document().findBlockByNumber(top+1)) c.beginEditBlock() c.movePosition(QTextCursor.MoveOperation.StartOfBlock) if delta > 0: for _ in range(delta): c.insertBlock() else: c.movePosition(QTextCursor.MoveOperation.NextBlock, QTextCursor.MoveMode.KeepAnchor, -delta) c.removeSelectedText() c.endEditBlock() v.images[top] = (img, w, lines) def mapnum(x): return x if x <= top else x + delta lnm = LineNumberMap() lnm.max_width = v.line_number_map.max_width for x, val in iteritems(v.line_number_map): dict.__setitem__(lnm, mapnum(x), val) v.line_number_map = lnm v.changes = [(mapnum(t), mapnum(b), k) for t, b, k in v.changes] v.headers = [(mapnum(x), name) for x, name in v.headers] v.images = OrderedDict((mapnum(x), v) for x, v in iteritems(v.images)) v.viewport().update()