<table><tr><td style="">dhaumann created this revision.<br />dhaumann added reviewers: cullmann, mwolff, kfunk.<br />Restricted Application added projects: Kate, Frameworks.<br />Restricted Application added a subscriber: Frameworks.
</td><a style="text-decoration: none; padding: 4px 8px; margin: 0 8px 8px; float: right; color: #464C5C; font-weight: bold; border-radius: 3px; background-color: #F7F7F9; background-image: linear-gradient(to bottom,#fff,#f1f0f1); display: inline-block; border: 1px solid rgba(71,87,120,.2);" href="https://phabricator.kde.org/D9247" rel="noreferrer">View Revision</a></tr></table><br /><div><strong>REVISION SUMMARY</strong><div><p>This patch adds the following JavaScript API:</p>

<p>object view.executeCommand(String command,</p>

<div class="remarkup-code-block" style="margin: 12px 0;" data-code-lang="text" data-sigil="remarkup-code-block"><pre class="remarkup-code" style="font: 11px/15px "Menlo", "Consolas", "Monaco", monospace; padding: 12px; margin: 0; background: rgba(71, 87, 120, 0.08);">String args = String(),
Range range = Rante::invalid())</pre></div>

<p>The returned object indicates whether execution of the command<br />
was successful:</p>

<ul class="remarkup-list">
<li class="remarkup-list-item">object.ok [bool] is true on success, otherwise false.</li>
<li class="remarkup-list-item">object.status [String] contains the result of the command (e.g. "31 replacements made", or an error message)</li>
</ul>

<p>Examples:</p>

<div class="remarkup-code-block" style="margin: 12px 0;" data-code-lang="text" data-sigil="remarkup-code-block"><pre class="remarkup-code" style="font: 11px/15px "Menlo", "Consolas", "Monaco", monospace; padding: 12px; margin: 0; background: rgba(71, 87, 120, 0.08);">view.executeCommand("sort"); // all, or seletcion
view.executeCommand("sort", "", new Range(1, 0, 4, 0); // sort lines 1-4
view.executeCommand("set-indent-width", "4");</pre></div>

<p>Unfortunately, we do not have any getters, e.g. "indent-width".<br />
This is a shortcoming of the KTextEditor::Command API. We should<br />
consider to change KTextEditor::Command as follows:</p>

<div class="remarkup-code-block" style="margin: 12px 0;" data-code-lang="text" data-sigil="remarkup-code-block"><pre class="remarkup-code" style="font: 11px/15px "Menlo", "Consolas", "Monaco", monospace; padding: 12px; margin: 0; background: rgba(71, 87, 120, 0.08);">// old:
virtual bool exec(KTextEditor::View *view, const QString &cmd, QString &msg,
                  const KTextEditor::Range &range = KTextEditor::Range::invalid()) = 0;

// new:
virtual QVariant exec(const QVariant & args,
                  const KTextEditor::Range &range = KTextEditor::Range::invalid(),
                  KTextEditor::View * view = nullptr) = 0;</pre></div>

<p>or similar. This way, Commands would be much more usabe in general<br />
(and also better chainable, which could result in: command 1 | command 2 | ...).</p>

<p>What is missing in this patch:</p>

<ul class="remarkup-list">
<li class="remarkup-list-item">infinite recursion is very easily possible</li>
<li class="remarkup-list-item">guard against some special commands: reload-scripts, anything else?</li>
<li class="remarkup-list-item">should the API be more smart? E.g. we could support both:<ul class="remarkup-list">
<li class="remarkup-list-item">view.executeCommand("sort", "", new Range(1,0, 3, 0))</li>
<li class="remarkup-list-item">view.executeCommand("sort", new Range(1,0, 3, 0)) -> we'd have to lookup the type of the passed QJSValues (would work)...</li>
</ul></li>
<li class="remarkup-list-item">Maybe only return a bool instead of object with "ok" and "status" ? -> The status could be helpful as a return value, but to be honest, this is a hack (see shortcoming of KTextEditor::Command above). We could also add KTextEditor::CommandExtension, provide an improved interface, and then do this properly here... Comments?</li>
</ul></div></div><br /><div><strong>TEST PLAN</strong><div><p>make test, manual testing</p></div></div><br /><div><strong>REPOSITORY</strong><div><div>R39 KTextEditor</div></div></div><br /><div><strong>BRANCH</strong><div><div>CommandScript (branched from master)</div></div></div><br /><div><strong>REVISION DETAIL</strong><div><a href="https://phabricator.kde.org/D9247" rel="noreferrer">https://phabricator.kde.org/D9247</a></div></div><br /><div><strong>AFFECTED FILES</strong><div><div>src/script/katescriptview.cpp<br />
src/script/katescriptview.h</div></div></div><br /><div><strong>To: </strong>dhaumann, cullmann, mwolff, kfunk<br /><strong>Cc: </strong>Frameworks, kevinapavew, ngraham, demsking, cullmann, sars, dhaumann<br /></div>