def comment_block(): '''Wrap selected text (or current line) into a #if0/#endif block''' view = kate.activeView() # This operation have no sense for partly selected lines common.extendSelectionToWholeLine(view) start = -1 end = -1 if view.selection(): sr = view.selectionRange() start = sr.start().line() end = sr.end().line() + 1 else: start = view.cursorPosition().line() end = start + 2 # Do it! document = kate.activeDocument() if start != -1 and end != -1: document.startEditing() # Start edit transaction document.insertLine(start, '#if 0') document.insertLine(end, '#endif') view.removeSelection() document.endEditing() # End transaction
def commentBlock(): '''Wrap selected text (or current line) into a #if0/#endif block''' view = kate.activeView() # This operation have no sense for partly selected lines common.extendSelectionToWholeLine(view) start = -1 end = -1 if view.selection(): sr = view.selectionRange() start = sr.start().line() end = sr.end().line() + 1 else: start = view.cursorPosition().line() end = start + 2 # Do it! document = kate.activeDocument() if start != -1 and end != -1: document.startEditing() # Start edit transaction document.insertLine(start, "#if 0") document.insertLine(end, "#endif") view.removeSelection() document.endEditing() # End transaction
def inline_comment(): ''' Append or align an inlined comment at position 60 for the current line or the selection. Move cursor to the start of a comment, if nothing has changed. If there wasn't any comment aside of #else/#endif put corresponding #if condition as default comment text ''' document = kate.activeDocument() view = kate.activeView() pos = view.cursorPosition() commentCh = common.getCommentStyleForDoc(document) if view.selection(): # If selected smth on a single line... common.extendSelectionToWholeLine(view) selectedText = view.selectionText().split('\n') if not bool(selectedText[-1]): selectedText = selectedText[0:-1] insertionText = [] firstColumn = -1 for textLine in selectedText: (currentLine, column) = processLine(textLine, commentCh) if firstColumn == -1: firstColumn = column insertionText += currentLine # Modify current document if bool(insertionText): document.startEditing() document.removeText(view.selectionRange()) pos = view.cursorPosition() document.insertText(pos, '\n'.join(insertionText) + '\n') pos.setColumn(firstColumn) view.setCursorPosition(pos) view.removeSelection() document.endEditing() else: (text, column) = processLine(document.line(pos.line()), commentCh) # Apply result (if smth really has changed) originalText = document.line(pos.line()) if bool(text) and (len(text) != 1 or originalText != text[0]): document.startEditing() # Start edit transaction: document.removeLine(pos.line()) # Remove current line # insert resulting text line by line... pos.setColumn(0) document.insertText(pos, '\n'.join(text) + '\n') document.endEditing() # End transaction # Move cursor to desired position pos.setColumn(column) view.setCursorPosition(pos)
def commentar(): ''' Append or align an inlined comment at position 60 for the current line or the selection. Move cursor to the start of a comment, if nothing has changed. If there wasn't any comment aside of #else/#endif put corresponding #if condition as default comment text ''' document = kate.activeDocument() view = kate.activeView() pos = view.cursorPosition() commentCh = common.getCommentStyleForDoc(document) if view.selection(): # If selected smth on a single line... common.extendSelectionToWholeLine(view) selectedText = view.selectionText().split('\n') if not bool(selectedText[-1]): selectedText = selectedText[0:-1] insertionText = [] firstColumn = -1 for textLine in selectedText: (currentLine, column) = processLine(textLine, commentCh) if firstColumn == -1: firstColumn = column insertionText += currentLine # Modify current document if bool(insertionText): document.startEditing() document.removeText(view.selectionRange()) pos = view.cursorPosition() document.insertText(pos, '\n'.join(insertionText) + '\n') pos.setColumn(firstColumn) view.setCursorPosition(pos) view.removeSelection() document.endEditing() else: (text, column) = processLine(document.line(pos.line()), commentCh) # Apply result (if smth really has changed) originalText = document.line(pos.line()) if bool(text) and (len(text) != 1 or originalText != text[0]): document.startEditing() # Start edit transaction: document.removeLine(pos.line()) # Remove current line # insert resulting text line by line... pos.setColumn(0) document.insertText(pos, '\n'.join(text) + '\n') document.endEditing() # End transaction # Move cursor to desired position pos.setColumn(column) view.setCursorPosition(pos)
def _wrapBlockWithChar(openCh, closeCh, indentMultiline = True): '''Wrap a current word or selection (if any) into given open and close chars If current selection is multiline, add one indentation level and put open/close chars on separate lines ''' doc = kate.activeDocument() view = kate.activeView() pos = view.cursorPosition() selectedRange = view.selectionRange() if selectedRange.isEmpty(): # No text selected. Ok, lets wrap a word where cursor positioned wordRange = common.getBoundTextRangeSL( common.CXX_IDENTIFIER_BOUNDARIES , common.CXX_IDENTIFIER_BOUNDARIES , pos , doc ) _wrapRange(wordRange, openCh, closeCh, doc) else: if selectedRange.start().line() == selectedRange.end().line() or indentMultiline == False: # single line selection (or no special indentation required) _wrapRange(selectedRange, openCh, closeCh, doc) # extend current selection selectedRange = KTextEditor.Range( selectedRange.start() , KTextEditor.Cursor( selectedRange.end().line() , selectedRange.end().column() + len(openCh) + len(closeCh) ) ) view.setSelection(selectedRange) else: # Try to extend selection to be started from 0 columns at both ends common.extendSelectionToWholeLine(view) selectedRange = view.selectionRange() # multiline selection # 0) extend selection to capture whole lines gap = ' ' * common.getLineIndentation(selectedRange.start().line(), doc) # TODO Get indent width (and char) from config (get rid of hardcocded '4') text = gap + openCh + '\n' \ + '\n'.join([' ' * 4 + line for line in doc.text(selectedRange).split('\n')[:-1]]) \ + '\n' + gap + closeCh + '\n' doc.startEditing() doc.replaceText(selectedRange, text) doc.endEditing() # extend current selection r = KTextEditor.Range(selectedRange.start().line(), 0, selectedRange.end().line() + 2, 0) view.setSelection(r)
def _wrapBlockWithChar(openCh, closeCh, indentMultiline=True): '''Wrap a current word or selection (if any) into given open and close chars If current selection is multiline, add one indentation level and put open/close chars on separate lines ''' doc = kate.activeDocument() view = kate.activeView() pos = view.cursorPosition() selectedRange = view.selectionRange() if selectedRange.isEmpty(): # No text selected. Ok, lets wrap a word where cursor positioned wordRange = common.getBoundTextRangeSL( common.CXX_IDENTIFIER_BOUNDARIES, common.CXX_IDENTIFIER_BOUNDARIES, pos, doc) _wrapRange(wordRange, openCh, closeCh, doc) else: if selectedRange.start().line() == selectedRange.end().line( ) or indentMultiline == False: # single line selection (or no special indentation required) _wrapRange(selectedRange, openCh, closeCh, doc) # extend current selection selectedRange.end().setColumn(selectedRange.end().column() + len(openCh) + len(closeCh)) view.setSelection(selectedRange) else: # Try to extend selection to be started from 0 columns at both ends common.extendSelectionToWholeLine(view) selectedRange = view.selectionRange() # multiline selection # 0) extend selection to capture whole lines gap = ' ' * common.getLineIndentation(selectedRange.start().line(), doc) # TODO Get indent width (and char) from config (get rid of hardcocded '4') text = gap + openCh + '\n' \ + '\n'.join([' ' * 4 + line for line in doc.text(selectedRange).split('\n')[:-1]]) \ + '\n' + gap + closeCh + '\n' doc.startEditing() doc.replaceText(selectedRange, text) doc.endEditing() # extend current selection r = KTextEditor.Range(selectedRange.start().line(), 0, selectedRange.end().line() + 2, 0) view.setSelection(r)