diff --git a/addons/externaltools/autotests/externaltooltest.cpp b/addons/externaltools/autotests/externaltooltest.cpp index 82a9944ef..8c320ef73 100644 --- a/addons/externaltools/autotests/externaltooltest.cpp +++ b/addons/externaltools/autotests/externaltooltest.cpp @@ -1,116 +1,116 @@ /* This file is part of the KDE project * * Copyright 2019 Dominik Haumann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "externaltooltest.h" #include "../kateexternaltool.h" #include "../katetoolrunner.h" #include #include #include #include QTEST_MAIN(ExternalToolTest) void ExternalToolTest::initTestCase() { } void ExternalToolTest::cleanupTestCase() { } void ExternalToolTest::testLoadSave() { KConfig config; KConfigGroup cg(&config, "tool"); KateExternalTool tool; tool.category = QStringLiteral("Git Tools"); tool.name = QStringLiteral("git cola"); tool.icon = QStringLiteral("git-cola"); tool.executable = QStringLiteral("git-cola"); tool.arguments = QStringLiteral("none"); tool.input = QStringLiteral("in"); tool.workingDir = QStringLiteral("/usr/bin"); tool.mimetypes = QStringList{ QStringLiteral("everything") }; tool.hasexec = true; tool.actionName = QStringLiteral("asdf"); tool.cmdname = QStringLiteral("git-cola"); tool.saveMode = KateExternalTool::SaveMode::None; tool.save(cg); KateExternalTool clonedTool; clonedTool.load(cg); - QCOMPARE(tool.name, clonedTool.name); + QCOMPARE(tool, clonedTool); } void ExternalToolTest::testRunListDirectory() { auto tool = new KateExternalTool(); tool->category = QStringLiteral("Tools"); tool->name = QStringLiteral("ls"); tool->icon = QStringLiteral("none"); tool->executable = QStringLiteral("ls"); tool->arguments = QStringLiteral("/usr"); tool->workingDir = QStringLiteral("/tmp"); tool->mimetypes = QStringList{}; tool->hasexec = true; tool->actionName = QStringLiteral("ls"); tool->cmdname = QStringLiteral("ls"); tool->saveMode = KateExternalTool::SaveMode::None; // 1. /tmp $ ls /usr KateToolRunner runner1(tool); runner1.run(); runner1.waitForFinished(); QVERIFY(runner1.outputData().contains(QStringLiteral("bin"))); // 2. /usr $ ls auto tool2 = new KateExternalTool(*tool); tool2->arguments.clear(); tool2->workingDir = QStringLiteral("/usr"); KateToolRunner runner2(tool2); runner2.run(); runner2.waitForFinished(); QVERIFY(runner2.outputData().contains(QStringLiteral("bin"))); // 1. and 2. must give the same result QCOMPARE(runner1.outputData(), runner2.outputData()); } void ExternalToolTest::testRunTac() { auto tool = new KateExternalTool(); tool->name = QStringLiteral("tac"); tool->executable = QStringLiteral("tac"); tool->input = QStringLiteral("a\nb\nc\n"); tool->saveMode = KateExternalTool::SaveMode::None; // run tac to reverse order KateToolRunner runner(tool); runner.run(); runner.waitForFinished(); QCOMPARE(runner.outputData(), QStringLiteral("c\nb\na\n")); } // kate: space-indent on; indent-width 4; replace-tabs on; diff --git a/addons/externaltools/kateexternaltool.cpp b/addons/externaltools/kateexternaltool.cpp index 996eb2c8f..cbee19388 100644 --- a/addons/externaltools/kateexternaltool.cpp +++ b/addons/externaltools/kateexternaltool.cpp @@ -1,69 +1,85 @@ /* This file is part of the KDE project * * Copyright 2019 Dominik Haumann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "kateexternaltool.h" #include #include bool KateExternalTool::checkExec() const { return !QStandardPaths::findExecutable(executable).isEmpty(); } bool KateExternalTool::matchesMimetype(const QString& mt) const { return mimetypes.isEmpty() || mimetypes.contains(mt); } void KateExternalTool::load(const KConfigGroup& cg) { category = cg.readEntry("category", ""); name = cg.readEntry("name", ""); icon = cg.readEntry("icon", ""); executable = cg.readEntry("executable", ""); arguments = cg.readEntry("arguments", ""); input = cg.readEntry("input", ""); workingDir = cg.readEntry("workingDir", ""); mimetypes = cg.readEntry("mimetypes", QStringList()); actionName = cg.readEntry("actionName"); cmdname = cg.readEntry("cmdname"); saveMode = static_cast(cg.readEntry("save", 0)); includeStderr = cg.readEntry("includeStderr", false); hasexec = checkExec(); } void KateExternalTool::save(KConfigGroup& cg) const { cg.writeEntry("category", category); cg.writeEntry("name", name); cg.writeEntry("icon", icon); cg.writeEntry("executable", executable); cg.writeEntry("arguments", arguments); cg.writeEntry("input", input); cg.writeEntry("workingDir", workingDir); cg.writeEntry("mimetypes", mimetypes); cg.writeEntry("actionName", actionName); cg.writeEntry("cmdname", cmdname); cg.writeEntry("save", static_cast(saveMode)); cg.writeEntry("includeStderr", includeStderr); } +bool operator==(const KateExternalTool & lhs, const KateExternalTool & rhs) +{ + return lhs.category == rhs.category + && lhs.name == rhs.name + && lhs.icon == rhs.icon + && lhs.executable == rhs.executable + && lhs.arguments == rhs.arguments + && lhs.input == rhs.input + && lhs.workingDir == rhs.workingDir + && lhs.mimetypes == rhs.mimetypes + && lhs.actionName == rhs.actionName + && lhs.cmdname == rhs.cmdname + && lhs.saveMode == rhs.saveMode + && lhs.includeStderr == rhs.includeStderr; +} + // kate: space-indent on; indent-width 4; replace-tabs on; diff --git a/addons/externaltools/kateexternaltool.h b/addons/externaltools/kateexternaltool.h index 23f31398f..e891e1213 100644 --- a/addons/externaltools/kateexternaltool.h +++ b/addons/externaltools/kateexternaltool.h @@ -1,121 +1,127 @@ /* This file is part of the KDE project * * Copyright 2019 Dominik Haumann * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #ifndef KTEXTEDITOR_KATE_EXTERNALTOOL_H #define KTEXTEDITOR_KATE_EXTERNALTOOL_H #include #include #include class KConfigGroup; /** * This class defines a single external tool. */ class KateExternalTool { public: /** * Defines whether any document should be saved before running the tool. */ enum class SaveMode { //! Do not save any document. None, //! Save current document. CurrentDocument, //! Save all documents AllDocuments }; /** * Defines where to redirect stdout from the tool. */ // enum class OutputMode { // Ignore, // InsertAtCursor, // ReplaceSelectedText, // AppendToCurrentDocument, // InsertInNewDocument, // DisplayInPane // } // Q_ENUM(OutputMode) public: /// The category used in the menu to categorize the tool. QString category; /// The name used in the menu. QString name; /// the icon to use in the menu. QString icon; /// The name or path of the executable. QString executable; /// The command line arguments. QString arguments; /// The stdin input. QString input; /// The working directory, if specified. QString workingDir; /// Optional list of mimetypes for which this action is valid. QStringList mimetypes; /// The name for the action. This is generated first time the /// action is is created. QString actionName; /// The name for the commandline. QString cmdname; /// Possibly save documents prior to activating the tool command. SaveMode saveMode = SaveMode::None; /// Possibly redirect the stdout output of the tool. // OutputMode outputMode; /// Include stderr output when running the tool. bool includeStderr = false; public: /// This is set when loading the Tool from disk. bool hasexec = false; /** * @return true if mimetypes is empty, or the @p mimetype matches. */ bool matchesMimetype(const QString& mimetype) const; /** * @return true if "executable" exists and has the executable bit set, or is * empty. * This is run at least once, and the tool is disabled if it fails. */ bool checkExec() const; /** * Load tool data from the config group @p cg. */ void load(const KConfigGroup& cg); /** * Save tool data to the config group @p cg. */ void save(KConfigGroup& cg) const; }; +/** + * Compares for equality. All fields have to match. + */ +bool operator==(const KateExternalTool & lhs, const KateExternalTool & rhs); + +// for use in QVariant (QAction::setData() and QAction::data()) Q_DECLARE_METATYPE(KateExternalTool*) #endif // KTEXTEDITOR_KATE_EXTERNALTOOL_H // kate: space-indent on; indent-width 4; replace-tabs on;