diff --git a/share/qtcreator/externaltools/sort.xml b/share/qtcreator/externaltools/sort.xml index cf9e784ca15277d7691266fdc64e710086a471fb..7687fa801b8f9dcc2caace2fbefb927939a50bba 100644 --- a/share/qtcreator/externaltools/sort.xml +++ b/share/qtcreator/externaltools/sort.xml @@ -36,7 +36,7 @@ <displayname xml:lang="de">Selektion Sortieren</displayname> <category>Text</category> <category xml:lang="de">Text</category> - <executable output="showinpane" error="ignore"> + <executable output="replaceselection" error="ignore"> <path>sort</path> <input>%{CurrentSelection}</input> <workingdirectory>%{CurrentPath}</workingdirectory> diff --git a/src/plugins/coreplugin/externaltool.cpp b/src/plugins/coreplugin/externaltool.cpp index b6fa8bdbc1d42d6e3f65623e774f92073ab819fe..f0f50106b0fe4874f97774be49b89625aea69419 100644 --- a/src/plugins/coreplugin/externaltool.cpp +++ b/src/plugins/coreplugin/externaltool.cpp @@ -356,6 +356,10 @@ void ExternalToolRunner::started() void ExternalToolRunner::finished() { + if (m_tool->outputHandling() == ExternalTool::ReplaceSelection + || m_tool->errorHandling() == ExternalTool::ReplaceSelection) { + emit ExternalToolManager::instance()->replaceSelectionRequested(m_processOutput); + } ICore::instance()->messageManager()->printToOutputPane( tr("'%1' finished").arg(m_resolvedExecutable), false); // TODO handle the ReplaceSelection and ReloadDocument flags @@ -379,6 +383,8 @@ void ExternalToolRunner::readStandardOutput() // TODO handle the ReplaceSelection flag if (m_tool->outputHandling() == ExternalTool::ShowInPane) { ICore::instance()->messageManager()->printToOutputPane(output, true); + } else if (m_tool->outputHandling() == ExternalTool::ReplaceSelection) { + m_processOutput.append(output); } } @@ -391,14 +397,19 @@ void ExternalToolRunner::readStandardError() // TODO handle the ReplaceSelection flag if (m_tool->errorHandling() == ExternalTool::ShowInPane) { ICore::instance()->messageManager()->printToOutputPane(output, true); + } else if (m_tool->errorHandling() == ExternalTool::ReplaceSelection) { + m_processOutput.append(output); } } // #pragma mark -- ExternalToolManager +ExternalToolManager *ExternalToolManager::m_instance = 0; + ExternalToolManager::ExternalToolManager(Core::ICore *core) : QObject(core), m_core(core) { + m_instance = this; initialize(); } diff --git a/src/plugins/coreplugin/externaltool.h b/src/plugins/coreplugin/externaltool.h index d89fcdd55c4441512dfebc644bffaacd31327f07..e710b365aeef33e3ffe4f24356b2df6ea6e98076 100644 --- a/src/plugins/coreplugin/externaltool.h +++ b/src/plugins/coreplugin/externaltool.h @@ -31,6 +31,7 @@ #define EXTERNALTOOL_H #include "icore.h" +#include "core_global.h" #include <QtCore/QObject> #include <QtCore/QString> @@ -111,27 +112,38 @@ private: QTextCodec *m_outputCodec; QTextCodec::ConverterState m_outputCodecState; QTextCodec::ConverterState m_errorCodecState; + QString m_processOutput; }; -class ExternalToolManager : public QObject +} // Internal + +class CORE_EXPORT ExternalToolManager : public QObject { Q_OBJECT public: + static ExternalToolManager *instance() { return m_instance; } + ExternalToolManager(Core::ICore *core); ~ExternalToolManager(); - void initialize(); +signals: + void replaceSelectionRequested(const QString &text); private slots: void menuActivated(); private: + void initialize(); + + static ExternalToolManager *m_instance; Core::ICore *m_core; - QMap<QString, ExternalTool *> m_tools; + QMap<QString, Internal::ExternalTool *> m_tools; + + // for sending the replaceSelectionRequested signal + friend class Core::Internal::ExternalToolRunner; }; -} // Internal } // Core #endif // EXTERNALTOOL_H diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index bce94dd65a8ade71fc22bb608cdd35ba1b09a564..692d885d82a14c3713f72a6eeae8a6c8ae8f29c2 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -55,6 +55,7 @@ #include <coreplugin/actionmanager/command.h> #include <coreplugin/editormanager/editormanager.h> #include <coreplugin/uniqueidmanager.h> +#include <coreplugin/externaltool.h> #include <extensionsystem/pluginmanager.h> #include <texteditor/texteditoractionhandler.h> #include <find/searchresultwindow.h> @@ -178,7 +179,8 @@ void TextEditorPlugin::extensionsInitialized() addAutoReleasedObject(new FindInCurrentFile(Find::SearchResultWindow::instance())); connect(Core::VariableManager::instance(), SIGNAL(variableUpdateRequested(QString)), this, SLOT(updateVariable(QString))); - + connect(Core::ExternalToolManager::instance(), SIGNAL(replaceSelectionRequested(QString)), + this, SLOT(updateCurrentSelection(QString))); } void TextEditorPlugin::initializeEditor(PlainTextEditor *editor) @@ -227,4 +229,22 @@ void TextEditorPlugin::updateVariable(const QString &variable) } } +void TextEditorPlugin::updateCurrentSelection(const QString &text) +{ + Core::IEditor *iface = Core::EditorManager::instance()->currentEditor(); + ITextEditable *editor = qobject_cast<ITextEditable *>(iface); + if (editor) { + int pos = editor->position(); + int anchor = editor->position(ITextEditor::Anchor); + int selectionLength = anchor-pos; + if (selectionLength < 0) + selectionLength = -selectionLength; + if (selectionLength == 0) + return; + int start = qMin(pos, anchor); + editor->setCurPos(start); + editor->replace(selectionLength, text); + } +} + Q_EXPORT_PLUGIN(TextEditorPlugin) diff --git a/src/plugins/texteditor/texteditorplugin.h b/src/plugins/texteditor/texteditorplugin.h index d79e1a071634c34971b7da5281751daccb81dc68..21da72096b3442053391365f87e0ee48cf5daffa 100644 --- a/src/plugins/texteditor/texteditorplugin.h +++ b/src/plugins/texteditor/texteditorplugin.h @@ -77,6 +77,7 @@ private slots: void invokeQuickFix(); void updateSearchResultsFont(const TextEditor::FontSettings &); void updateVariable(const QString &variable); + void updateCurrentSelection(const QString &text); private: static TextEditorPlugin *m_instance;