Example #1
0
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)
Example #2
0
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)
Example #3
0
def toggleDocTypeSeisitiveActions():
    view = kate.activeView()
    if view is None:
        return
    doc_type = view.document().highlightingMode()
    kate.kDebug('toggleDocTypeSeisitiveActions: doc_type={}'.format(doc_type))
    clnt = kate.getXmlGuiClient()

    if doc_type in ['C++', 'C++/Qt4', 'C']:
        clnt.stateChanged('if0_actions')
    else:
        clnt.stateChanged('if0_actions', KXMLGUIClient.StateReverse)

    if common.isKnownCommentStyle(doc_type):
        clnt.stateChanged('comment_actions')
    else:
        clnt.stateChanged('comment_actions', KXMLGUIClient.StateReverse)
Example #4
0
def changeParagraphWidth(step):
    view = kate.activeView()
    doc = kate.activeDocument()
    pos = view.cursorPosition()

    originRange, isBlock = getParagraphRange(doc, pos)
    if originRange.isEmpty():
        kate.ui.popup(
            i18nc('@title:window', 'Alert')
          , i18nc(
                '@info:tooltip'
              , 'Unable to detect a commented paragraph at cursor...'
              )
          , 'dialog-information'
          )
        return                                              # Dunno what to do on empty range!

    indent = common.getCurrentLineIndentation(view)         # detect current align
    # Processing:
    # 0) split text into left stripped lines
    originalText = view.document().text(originRange)
    lines = [line.lstrip() for line in originalText.split('\n')]
    # 1) detect comment style
    comment = [c.strip() for c in lines[0].split(' ')][0]
    # 2) strip leading comments (and possible left spaces) from each line
    lines = [line[len(comment):].lstrip() for line in lines]
    # 3) get a desired width of the current paragraph
    if step == -1:
        # 3.1) For shrink it is really simple: we just want to fit last word
        # to the next line, and it is enough to specify max(line size) - 1
        newSize = len(max(lines, key=len)) - 1
    elif step == 1:
        # 3.2) To extend paragraph we just want to append a first word from the next
        # after longest line.
        currentMax = 0
        prevLineWasLongest = False
        delta = 0
        for line in lines:
            # 3.2.1) if current maximum was changed on prevoius iteration,
            # get length of a first word on a line
            if prevLineWasLongest:
                # NOTE +1 for one space
                delta = len([word.strip() for word in line.split(' ')][0]) + 1
            # 3.2.2) is current line longer than we've seen before?
            lineSize = len(line)
            prevLineWasLongest = bool(currentMax < lineSize)
            if prevLineWasLongest:
                currentMax = lineSize
        newSize = currentMax + delta
    else:
        assert(not 'Incorrect step specified')

    # 4) wrap the text
    res = textwrap.wrap(' '.join(lines), newSize, break_long_words=False)
    # 5) form a text from the result list
    align = ' ' * indent + comment + ' '
    text = align + ('\n' + align).join(res) + '\n'

    # Return text only if smth really changed
    if originalText != text:                                # Update document only if smth really has changed
        doc.startEditing()                                  # Start edit transaction:
        doc.removeText(originRange)                         # Remove the origin range
        doc.insertText(originRange.start(), text)           # Insert modified text
        view.setCursorPosition(originRange.start())         # Move cursor to the start of the origin range
        doc.endEditing()                                    # End transaction