2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** If you have questions regarding the use of this file, please contact
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "texteditorplugin.h"
|
|
|
|
|
|
|
|
|
|
#include "findinfiles.h"
|
2009-05-29 14:14:08 +02:00
|
|
|
#include "findincurrentfile.h"
|
2012-01-23 11:49:14 +01:00
|
|
|
#include "findinopenfiles.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "fontsettings.h"
|
|
|
|
|
#include "linenumberfilter.h"
|
|
|
|
|
#include "texteditorconstants.h"
|
|
|
|
|
#include "texteditorsettings.h"
|
|
|
|
|
#include "textfilewizard.h"
|
|
|
|
|
#include "plaintexteditorfactory.h"
|
|
|
|
|
#include "plaintexteditor.h"
|
2010-05-20 13:56:11 +02:00
|
|
|
#include "manager.h"
|
2010-07-08 11:15:26 +02:00
|
|
|
#include "outlinefactory.h"
|
2010-12-02 17:02:23 +01:00
|
|
|
#include "snippets/plaintextsnippetprovider.h"
|
2011-04-15 16:19:23 +02:00
|
|
|
#include "codeassist/assistenums.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-21 18:30:45 +01:00
|
|
|
#include <coreplugin/icore.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/coreconstants.h>
|
|
|
|
|
#include <coreplugin/mimedatabase.h>
|
2010-12-09 14:57:22 +01:00
|
|
|
#include <coreplugin/variablemanager.h>
|
2009-01-13 13:39:31 +01:00
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2009-01-14 13:17:53 +01:00
|
|
|
#include <coreplugin/actionmanager/command.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2011-09-05 16:10:37 +02:00
|
|
|
#include <coreplugin/id.h>
|
2011-08-17 12:54:58 +02:00
|
|
|
#include <coreplugin/externaltoolmanager.h>
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <texteditor/texteditoractionhandler.h>
|
2009-04-08 14:32:31 +02:00
|
|
|
#include <find/searchresultwindow.h>
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <QtCore/QtPlugin>
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtGui/QMainWindow>
|
2009-01-19 12:39:20 +01:00
|
|
|
#include <QtGui/QShortcut>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
using namespace TextEditor::Internal;
|
|
|
|
|
|
2011-05-10 15:19:38 +02:00
|
|
|
static const char kCurrentDocumentSelection[] = "CurrentDocument:Selection";
|
|
|
|
|
static const char kCurrentDocumentRow[] = "CurrentDocument:Row";
|
|
|
|
|
static const char kCurrentDocumentColumn[] = "CurrentDocument:Column";
|
|
|
|
|
static const char kCurrentDocumentRowCount[] = "CurrentDocument:RowCount";
|
|
|
|
|
static const char kCurrentDocumentColumnCount[] = "CurrentDocument:ColumnCount";
|
|
|
|
|
static const char kCurrentDocumentFontSize[] = "CurrentDocument:FontSize";
|
2011-02-10 20:58:17 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
TextEditorPlugin *TextEditorPlugin::m_instance = 0;
|
|
|
|
|
|
2009-01-20 15:31:33 +01:00
|
|
|
TextEditorPlugin::TextEditorPlugin()
|
|
|
|
|
: m_settings(0),
|
2008-12-02 12:01:29 +01:00
|
|
|
m_wizard(0),
|
|
|
|
|
m_editorFactory(0),
|
2009-04-08 14:32:31 +02:00
|
|
|
m_lineNumberFilter(0),
|
|
|
|
|
m_searchResultWindow(0)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2008-12-09 15:25:01 +01:00
|
|
|
QTC_ASSERT(!m_instance, return);
|
2008-12-02 12:01:29 +01:00
|
|
|
m_instance = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditorPlugin::~TextEditorPlugin()
|
|
|
|
|
{
|
|
|
|
|
m_instance = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditorPlugin *TextEditorPlugin::instance()
|
|
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-20 11:52:04 +01:00
|
|
|
// ExtensionSystem::PluginInterface
|
|
|
|
|
bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2009-07-13 17:35:17 +02:00
|
|
|
Q_UNUSED(arguments)
|
2009-01-20 11:52:04 +01:00
|
|
|
|
2012-01-24 15:36:40 +01:00
|
|
|
if (!Core::ICore::mimeDatabase()->addMimeTypes(QLatin1String(":/texteditor/TextEditor.mimetypes.xml"), errorMessage))
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
Core::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);
|
2010-05-12 10:39:49 +02:00
|
|
|
wizardParameters.setDescription(tr("Creates a text file. The default file extension is <tt>.txt</tt>. "
|
|
|
|
|
"You can specify a different extension as part of the filename."));
|
2010-01-07 18:17:24 +01:00
|
|
|
wizardParameters.setDisplayName(tr("Text File"));
|
2010-01-13 18:44:15 +01:00
|
|
|
wizardParameters.setCategory(QLatin1String("U.General"));
|
2010-01-07 18:17:24 +01:00
|
|
|
wizardParameters.setDisplayCategory(tr("General"));
|
2008-12-02 12:01:29 +01:00
|
|
|
m_wizard = new TextFileWizard(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT),
|
|
|
|
|
QLatin1String("text$"),
|
2009-01-20 15:31:33 +01:00
|
|
|
wizardParameters);
|
2008-12-02 12:01:29 +01:00
|
|
|
// Add text file wizard
|
|
|
|
|
addAutoReleasedObject(m_wizard);
|
|
|
|
|
|
2009-01-23 19:13:00 +01:00
|
|
|
m_settings = new TextEditorSettings(this);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Add plain text editor factory
|
|
|
|
|
m_editorFactory = new PlainTextEditorFactory;
|
|
|
|
|
addAutoReleasedObject(m_editorFactory);
|
|
|
|
|
|
|
|
|
|
// Goto line functionality for quick open
|
2009-01-23 19:13:00 +01:00
|
|
|
m_lineNumberFilter = new LineNumberFilter;
|
2008-12-02 12:01:29 +01:00
|
|
|
addAutoReleasedObject(m_lineNumberFilter);
|
|
|
|
|
|
2010-06-25 17:37:59 +02:00
|
|
|
Core::Context context(TextEditor::Constants::C_TEXTEDITOR);
|
2012-01-24 15:36:40 +01:00
|
|
|
Core::ActionManager *am = Core::ICore::actionManager();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
// Add shortcut for invoking automatic completion
|
2012-01-24 15:36:40 +01:00
|
|
|
QShortcut *completionShortcut = new QShortcut(Core::ICore::mainWindow());
|
2008-12-02 12:01:29 +01:00
|
|
|
completionShortcut->setWhatsThis(tr("Triggers a completion in this scope"));
|
|
|
|
|
// Make sure the shortcut still works when the completion widget is active
|
|
|
|
|
completionShortcut->setContext(Qt::ApplicationShortcut);
|
2009-01-14 13:17:53 +01:00
|
|
|
Core::Command *command = am->registerShortcut(completionShortcut, Constants::COMPLETE_THIS, context);
|
2012-01-25 10:25:05 +01:00
|
|
|
#ifndef Q_OS_MAC
|
2008-12-02 12:01:29 +01:00
|
|
|
command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Space")));
|
|
|
|
|
#else
|
2010-03-18 12:18:57 +01:00
|
|
|
command->setDefaultKeySequence(QKeySequence(tr("Meta+Space")));
|
2008-12-02 12:01:29 +01:00
|
|
|
#endif
|
|
|
|
|
connect(completionShortcut, SIGNAL(activated()), this, SLOT(invokeCompletion()));
|
|
|
|
|
|
2009-07-15 12:27:02 +02:00
|
|
|
// Add shortcut for invoking quick fix options
|
2012-01-24 15:36:40 +01:00
|
|
|
QShortcut *quickFixShortcut = new QShortcut(Core::ICore::mainWindow());
|
2009-06-08 14:32:42 +02:00
|
|
|
quickFixShortcut->setWhatsThis(tr("Triggers a quick fix in this scope"));
|
|
|
|
|
// Make sure the shortcut still works when the quick fix widget is active
|
|
|
|
|
quickFixShortcut->setContext(Qt::ApplicationShortcut);
|
|
|
|
|
Core::Command *quickFixCommand = am->registerShortcut(quickFixShortcut, Constants::QUICKFIX_THIS, context);
|
2009-06-09 13:52:27 +02:00
|
|
|
quickFixCommand->setDefaultKeySequence(QKeySequence(tr("Alt+Return")));
|
2009-06-08 14:32:42 +02:00
|
|
|
connect(quickFixShortcut, SIGNAL(activated()), this, SLOT(invokeQuickFix()));
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-05-26 10:58:44 +02:00
|
|
|
// Generic highlighter.
|
|
|
|
|
connect(Core::ICore::instance(), SIGNAL(coreOpened()),
|
|
|
|
|
Manager::instance(), SLOT(registerMimeTypes()));
|
|
|
|
|
|
2010-12-02 17:02:23 +01:00
|
|
|
// Add text snippet provider.
|
|
|
|
|
addAutoReleasedObject(new PlainTextSnippetProvider);
|
|
|
|
|
|
2010-07-08 11:15:26 +02:00
|
|
|
m_outlineFactory = new OutlineFactory;
|
|
|
|
|
addAutoReleasedObject(m_outlineFactory);
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextEditorPlugin::extensionsInitialized()
|
|
|
|
|
{
|
|
|
|
|
m_editorFactory->actionHandler()->initializeActions();
|
2009-04-08 14:32:31 +02:00
|
|
|
|
2010-07-08 11:15:26 +02:00
|
|
|
ExtensionSystem::PluginManager *pluginManager = ExtensionSystem::PluginManager::instance();
|
|
|
|
|
|
2010-07-12 12:46:50 +02:00
|
|
|
m_searchResultWindow = Find::SearchResultWindow::instance();
|
2010-07-08 11:15:26 +02:00
|
|
|
|
|
|
|
|
m_outlineFactory->setWidgetFactories(pluginManager->getObjects<TextEditor::IOutlineWidgetFactory>());
|
2009-04-08 14:32:31 +02:00
|
|
|
|
|
|
|
|
connect(m_settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
|
|
|
|
|
this, SLOT(updateSearchResultsFont(TextEditor::FontSettings)));
|
|
|
|
|
|
|
|
|
|
updateSearchResultsFont(m_settings->fontSettings());
|
2009-05-28 16:42:29 +02:00
|
|
|
|
2011-09-02 11:51:31 +02:00
|
|
|
addAutoReleasedObject(new FindInFiles);
|
|
|
|
|
addAutoReleasedObject(new FindInCurrentFile);
|
2012-01-23 11:49:14 +01:00
|
|
|
addAutoReleasedObject(new FindInOpenFiles);
|
2011-02-10 20:58:17 +01:00
|
|
|
|
|
|
|
|
Core::VariableManager *vm = Core::VariableManager::instance();
|
2011-12-21 11:29:35 +01:00
|
|
|
vm->registerVariable(kCurrentDocumentSelection,
|
2011-02-10 20:58:17 +01:00
|
|
|
tr("Selected text within the current document."));
|
2011-12-21 11:29:35 +01:00
|
|
|
vm->registerVariable(kCurrentDocumentRow,
|
2011-02-10 20:58:17 +01:00
|
|
|
tr("Line number of the text cursor position in current document (starts with 1)."));
|
2011-12-21 11:29:35 +01:00
|
|
|
vm->registerVariable(kCurrentDocumentColumn,
|
2011-02-10 20:58:17 +01:00
|
|
|
tr("Column number of the text cursor position in current document (starts with 0)."));
|
2011-12-21 11:29:35 +01:00
|
|
|
vm->registerVariable(kCurrentDocumentRowCount,
|
2011-02-10 20:58:17 +01:00
|
|
|
tr("Number of lines visible in current document."));
|
2011-12-21 11:29:35 +01:00
|
|
|
vm->registerVariable(kCurrentDocumentColumnCount,
|
2011-02-10 20:58:17 +01:00
|
|
|
tr("Number of columns visible in current document."));
|
2011-12-21 11:29:35 +01:00
|
|
|
vm->registerVariable(kCurrentDocumentFontSize,
|
2011-02-10 20:58:17 +01:00
|
|
|
tr("Current document's font size in points."));
|
2011-12-21 11:29:35 +01:00
|
|
|
connect(vm, SIGNAL(variableUpdateRequested(QByteArray)),
|
|
|
|
|
this, SLOT(updateVariable(QByteArray)));
|
2010-12-09 15:53:43 +01:00
|
|
|
connect(Core::ExternalToolManager::instance(), SIGNAL(replaceSelectionRequested(QString)),
|
|
|
|
|
this, SLOT(updateCurrentSelection(QString)));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-21 16:02:26 +01:00
|
|
|
void TextEditorPlugin::initializeEditor(PlainTextEditorWidget *editor)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
// common actions
|
|
|
|
|
m_editorFactory->actionHandler()->setupActions(editor);
|
|
|
|
|
|
2009-03-17 18:17:51 +01:00
|
|
|
TextEditorSettings::instance()->initializeEditor(editor);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TextEditorPlugin::invokeCompletion()
|
|
|
|
|
{
|
2009-01-21 15:52:34 +01:00
|
|
|
Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
|
2011-04-15 16:19:23 +02:00
|
|
|
if (BaseTextEditorWidget *w = qobject_cast<BaseTextEditorWidget *>(iface->widget()))
|
|
|
|
|
w->invokeAssist(Completion);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-06-08 14:32:42 +02:00
|
|
|
void TextEditorPlugin::invokeQuickFix()
|
|
|
|
|
{
|
|
|
|
|
Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
|
2011-04-15 16:19:23 +02:00
|
|
|
if (BaseTextEditorWidget *w = qobject_cast<BaseTextEditorWidget *>(iface->widget()))
|
|
|
|
|
w->invokeAssist(QuickFix);
|
2009-06-08 14:32:42 +02:00
|
|
|
}
|
|
|
|
|
|
2009-04-08 14:32:31 +02:00
|
|
|
void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
|
|
|
|
|
{
|
|
|
|
|
if (m_searchResultWindow)
|
2009-11-30 19:09:04 +01:00
|
|
|
m_searchResultWindow->setTextEditorFont(QFont(settings.family(),
|
|
|
|
|
settings.fontSize() * settings.fontZoom() / 100));
|
2009-04-08 14:32:31 +02:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-12-21 11:29:35 +01:00
|
|
|
void TextEditorPlugin::updateVariable(const QByteArray &variable)
|
2010-12-09 14:57:22 +01:00
|
|
|
{
|
2011-12-21 11:29:35 +01:00
|
|
|
static QSet<QByteArray> variables = QSet<QByteArray>()
|
|
|
|
|
<< kCurrentDocumentSelection
|
|
|
|
|
<< kCurrentDocumentRow
|
|
|
|
|
<< kCurrentDocumentColumn
|
|
|
|
|
<< kCurrentDocumentRowCount
|
|
|
|
|
<< kCurrentDocumentColumnCount
|
|
|
|
|
<< kCurrentDocumentFontSize;
|
2010-12-13 15:17:33 +01:00
|
|
|
if (variables.contains(variable)) {
|
|
|
|
|
QString value;
|
2010-12-09 14:57:22 +01:00
|
|
|
Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
|
|
|
|
|
ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
|
|
|
|
|
if (editor) {
|
2011-12-21 11:29:35 +01:00
|
|
|
if (variable == kCurrentDocumentSelection) {
|
2010-12-13 15:17:33 +01:00
|
|
|
value = editor->selectedText();
|
|
|
|
|
value.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
|
2011-12-21 11:29:35 +01:00
|
|
|
} else if (variable == kCurrentDocumentRow) {
|
2010-12-13 15:17:33 +01:00
|
|
|
value = QString::number(editor->currentLine());
|
2011-12-21 11:29:35 +01:00
|
|
|
} else if (variable == kCurrentDocumentColumn) {
|
2010-12-13 15:17:33 +01:00
|
|
|
value = QString::number(editor->currentColumn());
|
2011-12-21 11:29:35 +01:00
|
|
|
} else if (variable == kCurrentDocumentRowCount) {
|
2010-12-13 15:17:33 +01:00
|
|
|
value = QString::number(editor->rowCount());
|
2011-12-21 11:29:35 +01:00
|
|
|
} else if (variable == kCurrentDocumentColumnCount) {
|
2010-12-13 15:17:33 +01:00
|
|
|
value = QString::number(editor->columnCount());
|
2011-12-21 11:29:35 +01:00
|
|
|
} else if (variable == kCurrentDocumentFontSize) {
|
2010-12-13 15:17:33 +01:00
|
|
|
value = QString::number(editor->widget()->font().pointSize());
|
|
|
|
|
}
|
2010-12-09 14:57:22 +01:00
|
|
|
}
|
2010-12-13 15:17:33 +01:00
|
|
|
Core::VariableManager::instance()->insert(variable, value);
|
2010-12-09 14:57:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-09 15:53:43 +01:00
|
|
|
void TextEditorPlugin::updateCurrentSelection(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
|
2011-02-21 16:02:26 +01:00
|
|
|
ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
|
2010-12-09 15:53:43 +01:00
|
|
|
if (editor) {
|
|
|
|
|
int pos = editor->position();
|
|
|
|
|
int anchor = editor->position(ITextEditor::Anchor);
|
2011-02-28 14:48:42 +01:00
|
|
|
if (anchor < 0) // no selection
|
|
|
|
|
anchor = pos;
|
2010-12-09 15:53:43 +01:00
|
|
|
int selectionLength = anchor-pos;
|
|
|
|
|
if (selectionLength < 0)
|
|
|
|
|
selectionLength = -selectionLength;
|
|
|
|
|
int start = qMin(pos, anchor);
|
2011-02-21 16:02:26 +01:00
|
|
|
editor->setCursorPosition(start);
|
2010-12-09 15:53:43 +01:00
|
|
|
editor->replace(selectionLength, text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Q_EXPORT_PLUGIN(TextEditorPlugin)
|