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 insertColor(): ''' Insert/edit #color using color chooser dialog If cursor positioned in a #color 'word', this action will edit it, otherwise a new #color will be inserted into a document. ''' document = kate.activeDocument() view = kate.activeView() cursor = view.cursorPosition() if not view.selection(): # If no selection, try to get a #color under cursor colorRange = common.getBoundTextRangeSL(_LEFT_COLOR_BOUNDARY, _RIGHT_COLOR_BOUNDARY, cursor, document) else: # Some text selected, just use it as input... colorRange = view.selectionRange() if colorRange.isValid(): currentColor = document.text(colorRange) else: currentColor = kate.configuration[_INSERT_COLOR_LCC] color = QtGui.QColor(currentColor) # If no text selected (i.e. user don't want to override selection) # and (guessed) #color string is not valid, entered color will # be inserted at the current cursor position. if not color.isValid(): color = QtGui.QColor(kate.configuration[_INSERT_COLOR_LCC]) if not view.selection(): colorRange = KTextEditor.Range( cursor, cursor) # Will not override the text under cursor... # Choose a #color via dialog result = kdeui.KColorDialog.getColor(color) if result == kdeui.KColorDialog.Accepted: # Is user pressed Ok? colorStr = color.name() # Get it as #color text # Remember for future use kate.configuration[_INSERT_COLOR_LCC] = colorStr document.startEditing() document.replaceText( colorRange, colorStr) # Replace selected/found range w/ a new text document.endEditing() # Select just entered #color, if smth was selected before if view.selection(): startPos = colorRange.start() endPos = KTextEditor.Cursor(startPos.line(), startPos.column() + len(colorStr)) view.setSelection(KTextEditor.Range(startPos, endPos))
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)
def _get_color_range_under_cursor(view): assert(view is not None) if view.selection(): # Some text selected, just use it as input... color_range = view.selectionRange() else: # If no selection, try to get a #color under cursor color_range = common.getBoundTextRangeSL( common.IDENTIFIER_BOUNDARIES - {'#'} , common.IDENTIFIER_BOUNDARIES , view.cursorPosition() , view.document() ) # Check if a word under cursor is a valid #color color = QColor(view.document().text(color_range)) if not color.isValid(): color_range = KTextEditor.Range(view.cursorPosition(), view.cursorPosition()) return color_range
def insertColor(): """Insert/edit color string using color chooser dialog If cursor positioned in a color string, this action will edit it, otherwise a new color string will be inserted into a document. """ document = kate.activeDocument() view = kate.activeView() cursor = view.cursorPosition() if view.selection(): # Some text selected, just use it as input... color_range = view.selectionRange() else: # If no selection, try to get a #color under cursor color_range = common.getBoundTextRangeSL( common.IDENTIFIER_BOUNDARIES - {'#'}, common.IDENTIFIER_BOUNDARIES, cursor, document) if color_range.isValid(): current_color = document.text(color_range) else: current_color = kate.configuration[_INSERT_COLOR_LCC] color = QColor(current_color) # If no text selected (i.e. user doesn’t want to override selection) # and (guessed) color string is not valid, entered color will # be inserted at the current cursor position. if not color.isValid(): color = QColor(kate.configuration[_INSERT_COLOR_LCC]) if not view.selection(): color_range = KTextEditor.Range( cursor, 0) # Will not override the text under cursor… # Choose a color via dialog result = KColorDialog.getColor(color) if result == KColorDialog.Accepted: # Did user press OK? color_str = color.name() # Get it as color string # Remember for future use kate.configuration[_INSERT_COLOR_LCC] = color_str document.startEditing() document.replaceText( color_range, color_str) # Replace selected/found range w/ a new text document.endEditing() # Select just entered #color, if something was selected before if view.selection(): start_pos = color_range.start() view.setSelection(KTextEditor.Range(start_pos, len(color_str)))
def wordAndArgumentAtCursorRanges(document, cursor): line = document.line(cursor.line()) argument_range = None # Handle some special cases: if cursor.column() > 0: if cursor.column() < len(line) and line[cursor.column()] == ')': # special case: cursor just right before a closing brace argument_end = kate.KTextEditor.Cursor(cursor.line(), cursor.column()) argument_start = matchingParenthesisPosition(document, argument_end, opening=')') argument_end.setColumn(argument_end.column() + 1) argument_range = kate.KTextEditor.Range(argument_start, argument_end) cursor = argument_start # NOTE Reassign if line[cursor.column() - 1] == ')': # one more special case: cursor past end of arguments argument_end = kate.KTextEditor.Cursor(cursor.line(), cursor.column() - 1) argument_start = matchingParenthesisPosition(document, argument_end, opening=')') argument_end.setColumn(argument_end.column() + 1) argument_range = kate.KTextEditor.Range(argument_start, argument_end) cursor = argument_start # NOTE Reassign word_range = common.getBoundTextRangeSL(common.IDENTIFIER_BOUNDARIES, common.IDENTIFIER_BOUNDARIES, cursor, document) end = word_range.end().column() if argument_range is None and word_range.isValid(): if end < len(line) and line[end] == '(': # ruddy lack of attribute type access from the KTextEditor # interfaces. argument_start = kate.KTextEditor.Cursor(cursor.line(), end) argument_end = matchingParenthesisPosition(document, argument_start, opening='(') argument_range = kate.KTextEditor.Range(argument_start, argument_end) return word_range, argument_range
def insertColor(): ''' Insert/edit #color using color chooser dialog If cursor positioned in a #color 'word', this action will edit it, otherwise a new #color will be inserted into a document. ''' document = kate.activeDocument() view = kate.activeView() cursor = view.cursorPosition() if not view.selection(): # If no selection, try to get a #color under cursor colorRange = common.getBoundTextRangeSL(_LEFT_COLOR_BOUNDARY, _RIGHT_COLOR_BOUNDARY, cursor, document) else: # Some text selected, just use it as input... colorRange = view.selectionRange() if colorRange.isValid(): currentColor = document.text(colorRange) else: currentColor = kate.configuration[_INSERT_COLOR_LCC] color = QtGui.QColor(currentColor) # If no text selected (i.e. user don't want to override selection) # and (guessed) #color string is not valid, entered color will # be inserted at the current cursor position. if not color.isValid(): color = QtGui.QColor(kate.configuration[_INSERT_COLOR_LCC]) if not view.selection(): colorRange = KTextEditor.Range(cursor, cursor) # Will not override the text under cursor... # Choose a #color via dialog result = kdeui.KColorDialog.getColor(color) if result == kdeui.KColorDialog.Accepted: # Is user pressed Ok? colorStr = color.name() # Get it as #color text # Remember for future use kate.configuration[_INSERT_COLOR_LCC] = colorStr document.startEditing() document.replaceText(colorRange, colorStr) # Replace selected/found range w/ a new text document.endEditing() # Select just entered #color, if smth was selected before if view.selection(): startPos = colorRange.start() endPos = KTextEditor.Cursor(startPos.line(), startPos.column() + len(colorStr)) view.setSelection(KTextEditor.Range(startPos, endPos))
def _wordAndArgumentAtCursorRanges(document, cursor): line = document.line(cursor.line()) argument_range = None # Handle some special cases: if cursor.column() > 0: if cursor.column() < len(line) and line[cursor.column()] == ')': # special case: cursor just right before a closing brace argument_end = KTextEditor.Cursor(cursor.line(), cursor.column()) argument_start = _matchingParenthesisPosition(document, argument_end, opening=')') argument_end.setColumn(argument_end.column() + 1) argument_range = KTextEditor.Range(argument_start, argument_end) cursor = argument_start # NOTE Reassign if line[cursor.column() - 1] == ')': # one more special case: cursor past end of arguments argument_end = KTextEditor.Cursor(cursor.line(), cursor.column() - 1) argument_start = _matchingParenthesisPosition(document, argument_end, opening=')') argument_end.setColumn(argument_end.column() + 1) argument_range = KTextEditor.Range(argument_start, argument_end) cursor = argument_start # NOTE Reassign word_range = common.getBoundTextRangeSL( common.IDENTIFIER_BOUNDARIES , common.IDENTIFIER_BOUNDARIES , cursor , document ) end = word_range.end().column() if argument_range is None and word_range.isValid(): if end < len(line) and line[end] == '(': # ruddy lack of attribute type access from the KTextEditor # interfaces. argument_start = KTextEditor.Cursor(cursor.line(), end) argument_end = _matchingParenthesisPosition(document, argument_start, opening='(') argument_range = KTextEditor.Range(argument_start, argument_end) return word_range, argument_range
def openCMakeList(): # TODO Handle unnamed docs # First of all check the document's MIME-type document = kate.activeDocument() cur_dir = os.path.dirname(document.url().toLocalFile()) mimetype = document.mimeType() if mimetype != _CMAKE_MIME_TYPE: # Ok, current document is not a CMakeLists.txt, lets try to open # it in a current directory if not _try_open_CMakeLists(cur_dir): # Ok, no CMakeLists.txt found, lets ask a user _ask_for_CMakeLists_location_and_try_open(cur_dir, cur_dir) return # Ok, we are in some CMake module... # Check if there smth selected by user? view = kate.activeView() selectedRange = view.selectionRange() if not selectedRange.isEmpty(): selected_dir = document.text(selectedRange) else: # Ok, nothing selected. Lets check the context: are we inside a command? cursor = view.cursorPosition() command, in_a_string, in_a_var, in_a_comment, fn_params_range = _find_current_context(document, cursor) kate.kDebug('CMakeHelper: command="{}", in_a_string={}, in_a_var={}'.format(command, in_a_string, in_a_var)) selected_dir = cur_dir if command == 'add_subdirectory': # Check if the command have some parameters already entered if fn_params_range.isValid(): # Ok, get the arg right under cursor wordRange = common.getBoundTextRangeSL(' \n()', ' \n()', cursor, document) selected_dir = document.text(wordRange) kate.kDebug('CMakeHelper: word@cursor={}'.format(selected_dir)) # TODO Try to handle cursor pointed in `include()` command? else: # Huh, lets find a nearest add_subdirectory command # and get its first arg if cursor.line() == 0: back_line = -1 fwd_line = cursor.line() elif cursor.line() == (document.lines() - 1): back_line = cursor.line() fwd_line = document.lines() else: back_line = cursor.line() fwd_line = cursor.line() + 1 found = None while not found and (back_line != -1 or fwd_line != document.lines()): # Try to match a line towards a document's start if 0 <= back_line: found = _get_1st_arg_if_add_subdirectory_cmd(document, back_line) back_line -= 1 # Try to match a line towards a document's end if not found and fwd_line < document.lines(): found = _get_1st_arg_if_add_subdirectory_cmd(document, fwd_line) fwd_line += 1 # Let to user specify anything he wants. if found: selected_dir = found _ask_for_CMakeLists_location_and_try_open(selected_dir, cur_dir) return if not os.path.isabs(selected_dir): selected_dir = os.path.join(cur_dir, selected_dir) # Try to open if not _try_open_CMakeLists(selected_dir): # Yep, show a location dialog w/ an initial dir equal to a selected text _ask_for_CMakeLists_location_and_try_open(selected_dir, cur_dir)