Beispiel #1
0
    def run(self): 
        line_ending = ['\r\n', '\r', '\n'][notepad.getFormatType()]
              
        if editor.getSelectionEmpty():
            editor.setText(line_ending.join(self.__sort(editor.getCharacterPointer())))

        elif editor.getSelectionMode() == 1:
            print '-->> {}'.format(editor.getSelectionNStart(0))
            start = editor.getSelectionNStart(0)
            end = editor.getSelectionNEnd(0)
            start_column = editor.getColumn(start)
            end_column = editor.getColumn(end)
            line_start, line_end = editor.getUserLineSelection()
            start_position_selected_lines = editor.positionFromLine(line_start)
            end_position_selected_lines = editor.getLineEndPosition(line_end)
            
            def sort_as_int_if_possible(text):
                return int(text.strip()) if text.strip().isdigit() else text
            
            if (line_start==0) and (line_end==editor.getLineCount()-1):
                editor.setText(line_ending.join(self.__sort(editor.getCharacterPointer(),lambda x: sort_as_int_if_possible(x[start_column:end_column]))))
            else:
                lines = self.__sort(editor.getTextRange(start_position_selected_lines, end_position_selected_lines),
                                    lambda x: sort_as_int_if_possible(x[start_column:end_column]))
                
                print line_ending.join(lines)
                editor.setTarget(start_position_selected_lines, end_position_selected_lines)
                editor.replaceTarget(line_ending.join(lines))
        else:
            text_of_sel = editor.getTextRange(*editor.getUserCharSelection())
            line_end_of_sel = line_ending if text_of_sel.endswith(line_ending) else ''
            lines = self.__sort(text_of_sel)
            editor.replaceSel(line_ending.join(lines) + line_end_of_sel)

        self.window.destroy()
Beispiel #2
0
    def reformat(separator, area):
        separator = window.clipboard_get() if separator_dict.get(
            separator) == 'clipboard' else separator_dict.get(separator)

        used_eol = format_types_dict.get(notepad.getFormatType())

        editor.beginUndoAction()

        if area == 0:
            rows = editor.getCharacterPointer().splitlines()
            rows = prepare(separator, rows, used_eol)
            editor.setText(used_eol.join(rows))
        elif area == 1:
            if editor.selectionIsRectangle():

                selections = [(editor.getSelectionNStart(i),
                               editor.getSelectionNEnd(i))
                              for i in range(editor.getSelections())]
                rows = [
                    editor.getRangePointer(x, y - x) for x, y in selections
                ]
                rows = prepare(separator, rows, used_eol)

                editor.clearSelections()

                for i, position in zip(reversed(range(len(selections))),
                                       reversed(selections)):
                    editor.setTarget(*position)
                    editor.replaceTarget(rows[i])
            else:
                first_selected_line, last_selected_line = editor.getUserLineSelection(
                )
                startpos = editor.positionFromLine(first_selected_line)
                endpos = editor.positionFromLine(last_selected_line)

                rows = editor.getRangePointer(startpos, endpos - startpos)
                rows = prepare(separator, rows.split(used_eol), used_eol)

                editor.setTarget(startpos, endpos)
                editor.replaceTarget(used_eol.join(rows))
        else:
            first_visible_line = editor.getFirstVisibleLine()
            startpos = editor.positionFromLine(first_visible_line)
            endpos = editor.positionFromLine(editor.linesOnScreen() +
                                             first_visible_line)

            rows = editor.getRangePointer(startpos, endpos - startpos)
            rows = prepare(separator, rows.split(used_eol), used_eol)

            editor.setTarget(startpos, endpos)
            editor.replaceTarget(used_eol.join(rows))
        editor.endUndoAction()
Beispiel #3
0
    def run(self):
        line_ending = ['\r\n', '\r', '\n'][notepad.getFormatType()]

        if editor.getSelectionEmpty():
            editor.setText(
                line_ending.join(self.__sort(editor.getCharacterPointer())))

        elif editor.getSelectionMode() == 1:
            print '-->> {}'.format(editor.getSelectionNStart(0))
            start = editor.getSelectionNStart(0)
            end = editor.getSelectionNEnd(0)
            start_column = editor.getColumn(start)
            end_column = editor.getColumn(end)
            line_start, line_end = editor.getUserLineSelection()
            start_position_selected_lines = editor.positionFromLine(line_start)
            end_position_selected_lines = editor.getLineEndPosition(line_end)

            def sort_as_int_if_possible(text):
                return int(text.strip()) if text.strip().isdigit() else text

            if (line_start == 0) and (line_end == editor.getLineCount() - 1):
                editor.setText(
                    line_ending.join(
                        self.__sort(
                            editor.getCharacterPointer(),
                            lambda x: sort_as_int_if_possible(x[start_column:
                                                                end_column]))))
            else:
                lines = self.__sort(
                    editor.getTextRange(start_position_selected_lines,
                                        end_position_selected_lines), lambda x:
                    sort_as_int_if_possible(x[start_column:end_column]))

                print line_ending.join(lines)
                editor.setTarget(start_position_selected_lines,
                                 end_position_selected_lines)
                editor.replaceTarget(line_ending.join(lines))
        else:
            text_of_sel = editor.getTextRange(*editor.getUserCharSelection())
            line_end_of_sel = line_ending if text_of_sel.endswith(
                line_ending) else ''
            lines = self.__sort(text_of_sel)
            editor.replaceSel(line_ending.join(lines) + line_end_of_sel)

        self.window.destroy()
Beispiel #4
0
    def reformat(separator,area):
        separator = window.clipboard_get() if separator_dict.get(separator) == 'clipboard' else separator_dict.get(separator)

        used_eol = format_types_dict.get(notepad.getFormatType())

        editor.beginUndoAction()

        if area == 0:
            rows = editor.getCharacterPointer().splitlines()
            rows = prepare(separator, rows, used_eol)
            editor.setText(used_eol.join(rows))
        elif area == 1:
            if editor.selectionIsRectangle():

                selections = [(editor.getSelectionNStart(i),editor.getSelectionNEnd(i)) for i in range(editor.getSelections())]
                rows = [editor.getRangePointer(x, y-x) for x,y in selections]
                rows = prepare(separator, rows, used_eol)

                editor.clearSelections()

                for i, position in zip(reversed(range(len(selections))), reversed(selections)):
                    editor.setTarget(*position)
                    editor.replaceTarget(rows[i])
            else:
                first_selected_line, last_selected_line = editor.getUserLineSelection()
                startpos = editor.positionFromLine(first_selected_line)
                endpos = editor.positionFromLine(last_selected_line)

                rows = editor.getRangePointer(startpos, endpos-startpos)
                rows = prepare(separator, rows.split(used_eol), used_eol)

                editor.setTarget(startpos, endpos)
                editor.replaceTarget(used_eol.join(rows))
        else:
            first_visible_line = editor.getFirstVisibleLine()
            startpos = editor.positionFromLine(first_visible_line)
            endpos = editor.positionFromLine(editor.linesOnScreen() + first_visible_line)

            rows = editor.getRangePointer(startpos, endpos-startpos)
            rows = prepare(separator, rows.split(used_eol), used_eol)

            editor.setTarget(startpos, endpos)
            editor.replaceTarget(used_eol.join(rows))
        editor.endUndoAction()
Beispiel #5
0
def main():
    if editor.getSelectionEmpty():
        # user hasn't selected anything, hide cursor line
        start_line = end_line = editor.lineFromPosition(editor.getCurrentPos())
    else:
        start_line, end_line = editor.getUserLineSelection()

    total_number_less_one_line = editor.getLineCount() - 1  # zero-based

    # recalculate which lines to hide as first and last line cannot be hide
    start_line = start_line if start_line > 0 else 1
    end_line = end_line if total_number_less_one_line > end_line else end_line - 1

    # calculate at which lines marker need to be placed
    marker_start_line = start_line - 1 if start_line > 0 else start_line
    marker_end_line = end_line + 1 if end_line < total_number_less_one_line else end_line

    # recalculate in case that marker(start/end)lines are not visible
    # either because they are part of a folding tree or already hidden
    while not editor.getLineVisible(marker_start_line):
        marker_start_line -= 1

    if not editor.getLineVisible(marker_end_line):
        visible_line = editor.visibleFromDocLine(marker_end_line)
        marker_end_line = editor.docLineFromVisible(visible_line)

    # check if there is already a marker set at those lines
    marker_at_marker_start_line = editor.markerGet(marker_start_line)
    marker_at_marker_end_line = editor.markerGet(marker_end_line)

    marker_already_set = False
    if (marker_at_marker_start_line
            & MARK_HIDELINESBEGIN_MASK) == MARK_HIDELINESBEGIN_MASK:
        marker_type = 'start'
        marker_already_set = True

    elif (marker_at_marker_end_line
          & MARK_HIDELINESEND_MASK) == MARK_HIDELINESEND_MASK:
        marker_type = 'end'
        marker_already_set = True

    # already markers set - inform user
    if marker_already_set:

        if EXTEND_AUTOMATICALLY is False:
            answer = notepad.messageBox((
                'There can only be one {} marker per line\r\n'
                'Should it be extended instead?\r\n'
                "If it shouldn't, it doesn't do anything").format(marker_type),
                                        'Info!', 4)

        if EXTEND_AUTOMATICALLY or answer == MESSAGEBOXFLAGS.RESULTYES:
            if marker_type == 'start':
                _matching_marker_line = find_correspondig_marker(
                    marker_start_line, MARK_HIDELINESEND_MASK)
                _start_marker_line_to_delete = marker_start_line
                _end_marker_line_to_delete = _matching_marker_line
            else:
                _matching_marker_line = find_correspondig_marker(
                    marker_end_line, MARK_HIDELINESBEGIN_MASK)
                _start_marker_line_to_delete = _matching_marker_line
                _end_marker_line_to_delete = marker_end_line

            editor.markerDelete(_start_marker_line_to_delete,
                                MARK_HIDELINESBEGIN)
            editor.markerDelete(_start_marker_line_to_delete,
                                MARK_HIDELINESUNDERLINE)
            editor.markerDelete(_end_marker_line_to_delete, MARK_HIDELINESEND)

        else:
            return

    editor.hideLines(start_line, end_line)
    editor.markerAdd(marker_start_line, MARK_HIDELINESBEGIN)
    editor.markerAdd(marker_start_line, MARK_HIDELINESUNDERLINE)
    editor.markerAdd(marker_end_line, MARK_HIDELINESEND)
    editor.gotoLine(marker_start_line)