1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "texteditorplugin.h"
36 #include "completionsupport.h"
37 #include "findinfiles.h"
38 #include "findincurrentfile.h"
39 #include "fontsettings.h"
40 #include "linenumberfilter.h"
41 #include "texteditorconstants.h"
42 #include "texteditorsettings.h"
43 #include "textfilewizard.h"
44 #include "plaintexteditorfactory.h"
45 #include "plaintexteditor.h"
46 #include "storagesettings.h"
48 #include "outlinefactory.h"
49 #include "snippets/plaintextsnippetprovider.h"
51 #include <coreplugin/icore.h>
52 #include <coreplugin/coreconstants.h>
53 #include <coreplugin/mimedatabase.h>
54 #include <coreplugin/variablemanager.h>
55 #include <coreplugin/actionmanager/actionmanager.h>
56 #include <coreplugin/actionmanager/command.h>
57 #include <coreplugin/editormanager/editormanager.h>
58 #include <coreplugin/uniqueidmanager.h>
59 #include <coreplugin/externaltool.h>
60 #include <extensionsystem/pluginmanager.h>
61 #include <texteditor/texteditoractionhandler.h>
62 #include <find/searchresultwindow.h>
63 #include <utils/qtcassert.h>
65 #include <QtCore/QtPlugin>
66 #include <QtGui/QMainWindow>
67 #include <QtGui/QShortcut>
69 using namespace TextEditor;
70 using namespace TextEditor::Internal;
72 static const char * const kCurrentDocumentSelection = "CurrentDocument:Selection";
73 static const char * const kCurrentDocumentRow = "CurrentDocument:Row";
74 static const char * const kCurrentDocumentColumn = "CurrentDocument:Column";
75 static const char * const kCurrentDocumentRowCount = "CurrentDocument:RowCount";
76 static const char * const kCurrentDocumentColumnCount = "CurrentDocument:ColumnCount";
77 static const char * const kCurrentDocumentFontSize = "CurrentDocument:FontSize";
79 TextEditorPlugin *TextEditorPlugin::m_instance = 0;
81 TextEditorPlugin::TextEditorPlugin()
85 m_lineNumberFilter(0),
86 m_searchResultWindow(0)
88 QTC_ASSERT(!m_instance, return);
92 TextEditorPlugin::~TextEditorPlugin()
97 TextEditorPlugin *TextEditorPlugin::instance()
102 // ExtensionSystem::PluginInterface
103 bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
107 if (!Core::ICore::instance()->mimeDatabase()->addMimeTypes(QLatin1String(":/texteditor/TextEditor.mimetypes.xml"), errorMessage))
110 Core::BaseFileWizardParameters wizardParameters(Core::IWizard::FileWizard);
111 wizardParameters.setDescription(tr("Creates a text file. The default file extension is <tt>.txt</tt>. "
112 "You can specify a different extension as part of the filename."));
113 wizardParameters.setDisplayName(tr("Text File"));
114 wizardParameters.setCategory(QLatin1String("U.General"));
115 wizardParameters.setDisplayCategory(tr("General"));
116 m_wizard = new TextFileWizard(QLatin1String(TextEditor::Constants::C_TEXTEDITOR_MIMETYPE_TEXT),
117 QLatin1String("text$"),
119 // Add text file wizard
120 addAutoReleasedObject(m_wizard);
122 m_settings = new TextEditorSettings(this);
124 // Add plain text editor factory
125 m_editorFactory = new PlainTextEditorFactory;
126 addAutoReleasedObject(m_editorFactory);
128 // Goto line functionality for quick open
129 Core::ICore *core = Core::ICore::instance();
130 m_lineNumberFilter = new LineNumberFilter;
131 addAutoReleasedObject(m_lineNumberFilter);
133 Core::Context context(TextEditor::Constants::C_TEXTEDITOR);
134 Core::ActionManager *am = core->actionManager();
136 // Add shortcut for invoking automatic completion
137 QShortcut *completionShortcut = new QShortcut(core->mainWindow());
138 completionShortcut->setWhatsThis(tr("Triggers a completion in this scope"));
139 // Make sure the shortcut still works when the completion widget is active
140 completionShortcut->setContext(Qt::ApplicationShortcut);
141 Core::Command *command = am->registerShortcut(completionShortcut, Constants::COMPLETE_THIS, context);
143 command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Space")));
145 command->setDefaultKeySequence(QKeySequence(tr("Meta+Space")));
147 connect(completionShortcut, SIGNAL(activated()), this, SLOT(invokeCompletion()));
149 // Add shortcut for invoking quick fix options
150 QShortcut *quickFixShortcut = new QShortcut(core->mainWindow());
151 quickFixShortcut->setWhatsThis(tr("Triggers a quick fix in this scope"));
152 // Make sure the shortcut still works when the quick fix widget is active
153 quickFixShortcut->setContext(Qt::ApplicationShortcut);
154 Core::Command *quickFixCommand = am->registerShortcut(quickFixShortcut, Constants::QUICKFIX_THIS, context);
155 quickFixCommand->setDefaultKeySequence(QKeySequence(tr("Alt+Return")));
156 connect(quickFixShortcut, SIGNAL(activated()), this, SLOT(invokeQuickFix()));
158 // Generic highlighter.
159 connect(Core::ICore::instance(), SIGNAL(coreOpened()),
160 Manager::instance(), SLOT(registerMimeTypes()));
162 // Add text snippet provider.
163 addAutoReleasedObject(new PlainTextSnippetProvider);
165 m_outlineFactory = new OutlineFactory;
166 addAutoReleasedObject(m_outlineFactory);
171 void TextEditorPlugin::extensionsInitialized()
173 m_editorFactory->actionHandler()->initializeActions();
175 ExtensionSystem::PluginManager *pluginManager = ExtensionSystem::PluginManager::instance();
177 m_searchResultWindow = Find::SearchResultWindow::instance();
179 m_outlineFactory->setWidgetFactories(pluginManager->getObjects<TextEditor::IOutlineWidgetFactory>());
181 connect(m_settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
182 this, SLOT(updateSearchResultsFont(TextEditor::FontSettings)));
184 updateSearchResultsFont(m_settings->fontSettings());
186 addAutoReleasedObject(new FindInFiles(Find::SearchResultWindow::instance()));
187 addAutoReleasedObject(new FindInCurrentFile(Find::SearchResultWindow::instance()));
189 Core::VariableManager *vm = Core::VariableManager::instance();
190 vm->registerVariable(QLatin1String(kCurrentDocumentSelection),
191 tr("Selected text within the current document."));
192 vm->registerVariable(QLatin1String(kCurrentDocumentRow),
193 tr("Line number of the text cursor position in current document (starts with 1)."));
194 vm->registerVariable(QLatin1String(kCurrentDocumentColumn),
195 tr("Column number of the text cursor position in current document (starts with 0)."));
196 vm->registerVariable(QLatin1String(kCurrentDocumentRowCount),
197 tr("Number of lines visible in current document."));
198 vm->registerVariable(QLatin1String(kCurrentDocumentColumnCount),
199 tr("Number of columns visible in current document."));
200 vm->registerVariable(QLatin1String(kCurrentDocumentFontSize),
201 tr("Current document's font size in points."));
202 connect(vm, SIGNAL(variableUpdateRequested(QString)),
203 this, SLOT(updateVariable(QString)));
204 connect(Core::ExternalToolManager::instance(), SIGNAL(replaceSelectionRequested(QString)),
205 this, SLOT(updateCurrentSelection(QString)));
208 void TextEditorPlugin::initializeEditor(PlainTextEditorWidget *editor)
211 m_editorFactory->actionHandler()->setupActions(editor);
213 TextEditorSettings::instance()->initializeEditor(editor);
216 void TextEditorPlugin::invokeCompletion()
218 Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
219 ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
221 CompletionSupport::instance()->complete(editor, SemanticCompletion, true);
224 void TextEditorPlugin::invokeQuickFix()
226 Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
227 ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
229 CompletionSupport::instance()->complete(editor, QuickFixCompletion, true);
232 void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
234 if (m_searchResultWindow)
235 m_searchResultWindow->setTextEditorFont(QFont(settings.family(),
236 settings.fontSize() * settings.fontZoom() / 100));
239 void TextEditorPlugin::updateVariable(const QString &variable)
241 static QSet<QString> variables = QSet<QString>()
242 << QString::fromLatin1(kCurrentDocumentSelection)
243 << QString::fromLatin1(kCurrentDocumentRow)
244 << QString::fromLatin1(kCurrentDocumentColumn)
245 << QString::fromLatin1(kCurrentDocumentRowCount)
246 << QString::fromLatin1(kCurrentDocumentColumnCount)
247 << QString::fromLatin1(kCurrentDocumentFontSize);
248 if (variables.contains(variable)) {
250 Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
251 ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
253 if (variable == QLatin1String(kCurrentDocumentSelection)) {
254 value = editor->selectedText();
255 value.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
256 } else if (variable == QLatin1String(kCurrentDocumentRow)) {
257 value = QString::number(editor->currentLine());
258 } else if (variable == QLatin1String(kCurrentDocumentColumn)) {
259 value = QString::number(editor->currentColumn());
260 } else if (variable == QLatin1String(kCurrentDocumentRowCount)) {
261 value = QString::number(editor->rowCount());
262 } else if (variable == QLatin1String(kCurrentDocumentColumnCount)) {
263 value = QString::number(editor->columnCount());
264 } else if (variable == QLatin1String(kCurrentDocumentFontSize)) {
265 value = QString::number(editor->widget()->font().pointSize());
268 Core::VariableManager::instance()->insert(variable, value);
272 void TextEditorPlugin::updateCurrentSelection(const QString &text)
274 Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
275 ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
277 int pos = editor->position();
278 int anchor = editor->position(ITextEditor::Anchor);
279 if (anchor < 0) // no selection
281 int selectionLength = anchor-pos;
282 if (selectionLength < 0)
283 selectionLength = -selectionLength;
284 int start = qMin(pos, anchor);
285 editor->setCursorPosition(start);
286 editor->replace(selectionLength, text);
290 Q_EXPORT_PLUGIN(TextEditorPlugin)