Search in sources :

Example 1 with LineRange

use of com.maddyhome.idea.vim.ex.LineRange in project ideavim by JetBrains.

the class SortHandler method execute.

@Override
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException {
    final String arg = cmd.getArgument();
    final boolean nonEmptyArg = arg.trim().length() > 0;
    final boolean reverse = nonEmptyArg && arg.contains("!");
    final boolean ignoreCase = nonEmptyArg && arg.contains("i");
    final boolean number = nonEmptyArg && arg.contains("n");
    final LineRange range = getLineRange(editor, context, cmd);
    final Comparator<String> lineComparator = new LineComparator(ignoreCase, number, reverse);
    return VimPlugin.getChange().sortRange(editor, range, lineComparator);
}
Also used : LineRange(com.maddyhome.idea.vim.ex.LineRange)

Example 2 with LineRange

use of com.maddyhome.idea.vim.ex.LineRange in project ideavim by JetBrains.

the class SortHandler method getLineRange.

@NotNull
private LineRange getLineRange(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) {
    final LineRange range = cmd.getLineRange(editor, context);
    final LineRange normalizedRange;
    // Something like "30,20sort" gets converted to "20,30sort"
    if (range.getEndLine() < range.getStartLine()) {
        normalizedRange = new LineRange(range.getEndLine(), range.getStartLine());
    } else {
        normalizedRange = range;
    }
    // If we don't have a range, we either have "sort", a selection, or a block
    if (normalizedRange.getEndLine() - normalizedRange.getStartLine() == 0) {
        // If we have a selection.
        final SelectionModel selectionModel = editor.getSelectionModel();
        if (selectionModel.hasSelection()) {
            final int start = selectionModel.getSelectionStart();
            final int end = selectionModel.getSelectionEnd();
            final int startLine = editor.offsetToLogicalPosition(start).line;
            final int endLine = editor.offsetToLogicalPosition(end).line;
            return new LineRange(startLine, endLine);
        } else // If we have a generic selection, i.e. "sort" entire document
        {
            return new LineRange(0, editor.getDocument().getLineCount() - 1);
        }
    }
    return normalizedRange;
}
Also used : SelectionModel(com.intellij.openapi.editor.SelectionModel) LineRange(com.maddyhome.idea.vim.ex.LineRange) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LineRange (com.maddyhome.idea.vim.ex.LineRange)2 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 NotNull (org.jetbrains.annotations.NotNull)1