From ae560fcc493f3be3aa4783bc2354104b433bb3ac Mon Sep 17 00:00:00 2001 From: hjk Date: Mon, 12 Mar 2018 14:13:07 +0100 Subject: [PATCH] FakeVim: Implement :sor[t][!] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit None of the other options yet. Change-Id: Iabf18c1be4d228c97d2de9cb17e71c307e9ec5a2 Task-number: QTCREATORBUG-20022 Reviewed-by: Lukas Holecek Reviewed-by: André Hartmann --- src/plugins/fakevim/fakevimhandler.cpp | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp index 45139a5bfdd..66ee7fc7b4a 100644 --- a/src/plugins/fakevim/fakevimhandler.cpp +++ b/src/plugins/fakevim/fakevimhandler.cpp @@ -2139,6 +2139,7 @@ public: bool handleExReadCommand(const ExCommand &cmd); bool handleExUndoRedoCommand(const ExCommand &cmd); bool handleExSetCommand(const ExCommand &cmd); + bool handleExSortCommand(const ExCommand &cmd); bool handleExShiftCommand(const ExCommand &cmd); bool handleExSourceCommand(const ExCommand &cmd); bool handleExSubstituteCommand(const ExCommand &cmd); @@ -6077,6 +6078,39 @@ bool FakeVimHandler::Private::handleExShiftCommand(const ExCommand &cmd) return true; } +bool FakeVimHandler::Private::handleExSortCommand(const ExCommand &cmd) +{ + // :[range]sor[t][!] [b][f][i][n][o][r][u][x] [/{pattern}/] + // FIXME: Only the ! for reverse is implemented. + if (!cmd.matches("sor", "sort")) + return false; + + // Force operation on full lines, and full document if only + // one line (the current one...) is specified + int beginLine = lineForPosition(cmd.range.beginPos); + int endLine = lineForPosition(cmd.range.endPos); + if (beginLine == endLine) { + beginLine = 0; + endLine = lineForPosition(lastPositionInDocument()); + } + Range range(firstPositionInLine(beginLine), + firstPositionInLine(endLine), RangeLineMode); + + QString input = selectText(range); + if (input.endsWith('\n')) // It should always... + input.chop(1); + + QStringList lines = input.split('\n'); + lines.sort(); + if (cmd.hasBang) + std::reverse(lines.begin(), lines.end()); + QString res = lines.join('\n') + '\n'; + + replaceText(range, res); + + return true; +} + bool FakeVimHandler::Private::handleExNohlsearchCommand(const ExCommand &cmd) { // :noh, :nohl, ..., :nohlsearch @@ -6230,6 +6264,7 @@ bool FakeVimHandler::Private::handleExCommandHelper(ExCommand &cmd) || handleExUndoRedoCommand(cmd) || handleExSetCommand(cmd) || handleExShiftCommand(cmd) + || handleExSortCommand(cmd) || handleExSourceCommand(cmd) || handleExSubstituteCommand(cmd) || handleExTabNextCommand(cmd)