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 tryOpen(): view = kate.activeView() assert('View expected to be valid' and view is not None) assert('This action supposed to select some text before' and view.selection()) doc = view.document() doc_url = doc.url() new_url = KUrl(_try_make_url_from_text(view.selectionText())) kate.kDebug('Current document URI: {}'.format(repr(doc_url))) # 0) Make sure it is valid if not new_url.isValid(): kate.ui.popup( i18nc('@title:window', 'Error') , i18nc('@info:tooltip', "Selected text doesn't looks like a valid URI") , 'dialog-error' ) return # 1) Is it have some schema? and current document is not 'Untitled'... if new_url.isRelative() and not doc_url.isEmpty(): # Replace filename from the doc_url w/ the current selection new_url = doc_url.upUrl() new_url.addPath(view.selectionText()) kate.kDebug('Trying URI: {}'.format(repr(new_url))) # Ok, try to open it finally _try_open_show_error(new_url)
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 toggleSelectionSensitiveActions(view): doc_type = view.document().highlightingMode() clnt = kate.getXmlGuiClient() has_selection = view.selection() if doc_type in ['C++', 'C++/Qt4', 'JavaScript'] and not has_selection: clnt.stateChanged('doxygen_actions') else: clnt.stateChanged('doxygen_actions', KXMLGUIClient.StateReverse) if common.isKnownCommentStyle(doc_type) and not has_selection: clnt.stateChanged('comment_actions_no_selection') else: clnt.stateChanged('comment_actions_no_selection', KXMLGUIClient.StateReverse)
def turnFromBlockComment(): document = kate.activeDocument() view = kate.activeView() pos = view.cursorPosition() if view.selection(): sr = view.selectionRange() start = sr.start().line() end = sr.end().line() else: # Try to detect block comment (/* ... */) r = common.getTextBlockAroundCursor( document , pos , [pred.blockCommentStart, neg(pred.startsWith('*'))] , [pred.blockCommentEnd, neg(pred.startsWith('*'))] ) start = r.start().line() - 1 end = r.end().line() + 1 # Replace comments insertionText = list() align = None for i in range(start, end): line = str(document.line(i)) sline = line.lstrip() if align == None: align = ' ' * (len(line) - len(sline)) if sline.startswith('/**') or sline.startswith('*/'): continue if sline.startswith('*'): insertionText.append(align + sline.replace('*', '///', 1)) originRange = KTextEditor.Range(start, 0, end, 0) pos.setPosition(start, len(align) + 3) insertPos = KTextEditor.Cursor(start, 0) # Update the document if bool(insertionText): document.startEditing() # Start edit transaction: document.removeText(originRange) # Remove current line # insert resulting text line by line... document.insertText(insertPos, '\n'.join(insertionText) + '\n') # Move cursor to desired position view.setCursorPosition(pos) document.endEditing() # End transaction
def turnToBlockComment(): document = kate.activeDocument() view = kate.activeView() pos = view.cursorPosition() if view.selection(): sr = view.selectionRange() start = sr.start().line() end = sr.end().line() else: r = common.getTextBlockAroundCursor( document , pos , [neg(any_of(pred.startsWith('///'), pred.startsWith('//!')))] , [neg(any_of(pred.startsWith('///'), pred.startsWith('//!')))] ) start = r.start().line() end = r.end().line() # Replace comments in every line insertionText = list() align = None for i in range(start, end): line = str(document.line(i)) sline = line.lstrip() if align == None: align = ' ' * (len(line) - len(sline)) insertionText.append(align + sline.replace('///', ' *', 1).replace('//!', ' *', 1)) originRange = KTextEditor.Range(start, 0, end, 0) pos.setPosition(start + 1, len(align) + 3) insertPos = KTextEditor.Cursor(start, 0) # Update the document if bool(insertionText): document.startEditing() # Start edit transaction: document.removeText(originRange) # Remove current line # insert resulting text ... document.insertText(insertPos, align + '/**\n' + '\n'.join(insertionText) + '\n' + align + ' */\n'); # Move cursor to desired position view.setCursorPosition(pos) document.endEditing() # End transaction
def toggleSelectionSensitiveActions(view): clnt = kate.getXmlGuiClient() filename = _try_make_url_from_text(view.selectionText()) kate.kDebug('Original new filename: {}'.format(repr(filename))) if view.selection() and len(filename) < _SANE_URI_LENGTH: clnt.stateChanged('has_selection') # Change action text... if _URI_LENGTH_SHOW_THRESHOLD < len(filename): lead_pos = int(2 * _URI_LENGTH_SHOW_THRESHOLD / 3) tail_pos = -int(_URI_LENGTH_SHOW_THRESHOLD / 3) filename = filename[:lead_pos] + '...' + filename[tail_pos:] assert('Sanity check' and hasattr(tryOpen, 'action')) kate.kDebug('New filename: {}'.format(filename)) tryOpen.action.setText( i18nc('@ation:inmenu', 'Open <filename>%1</filename>', filename) ) else: clnt.stateChanged('has_selection', KXMLGUIClient.StateReverse) tryOpen.action.setText(i18nc('@ation:inmenu', 'Open selected document'))
def toggleSelectionSensitiveActions(view): clnt = kate.getXmlGuiClient() if not view.selection(): clnt.stateChanged('has_no_selection') else: clnt.stateChanged('has_no_selection', KXMLGUIClient.StateReverse)