OSDN Git Service

0e4ef7052996cd96540e5b793c195d9e5ed06583
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / texteditorplugin.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "texteditorplugin.h"
35
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"
47 #include "manager.h"
48 #include "outlinefactory.h"
49 #include "snippets/plaintextsnippetprovider.h"
50
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>
64
65 #include <QtCore/QtPlugin>
66 #include <QtGui/QMainWindow>
67 #include <QtGui/QShortcut>
68
69 using namespace TextEditor;
70 using namespace TextEditor::Internal;
71
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";
78
79 TextEditorPlugin *TextEditorPlugin::m_instance = 0;
80
81 TextEditorPlugin::TextEditorPlugin()
82   : m_settings(0),
83     m_wizard(0),
84     m_editorFactory(0),
85     m_lineNumberFilter(0),
86     m_searchResultWindow(0)
87 {
88     QTC_ASSERT(!m_instance, return);
89     m_instance = this;
90 }
91
92 TextEditorPlugin::~TextEditorPlugin()
93 {
94     m_instance = 0;
95 }
96
97 TextEditorPlugin *TextEditorPlugin::instance()
98 {
99     return m_instance;
100 }
101
102 // ExtensionSystem::PluginInterface
103 bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
104 {
105     Q_UNUSED(arguments)
106
107     if (!Core::ICore::instance()->mimeDatabase()->addMimeTypes(QLatin1String(":/texteditor/TextEditor.mimetypes.xml"), errorMessage))
108         return false;
109
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$"),
118                                   wizardParameters);
119     // Add text file wizard
120     addAutoReleasedObject(m_wizard);
121
122     m_settings = new TextEditorSettings(this);
123
124     // Add plain text editor factory
125     m_editorFactory = new PlainTextEditorFactory;
126     addAutoReleasedObject(m_editorFactory);
127
128     // Goto line functionality for quick open
129     Core::ICore *core = Core::ICore::instance();
130     m_lineNumberFilter = new LineNumberFilter;
131     addAutoReleasedObject(m_lineNumberFilter);
132
133     Core::Context context(TextEditor::Constants::C_TEXTEDITOR);
134     Core::ActionManager *am = core->actionManager();
135
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);
142 #ifndef Q_WS_MAC
143     command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Space")));
144 #else
145     command->setDefaultKeySequence(QKeySequence(tr("Meta+Space")));
146 #endif
147     connect(completionShortcut, SIGNAL(activated()), this, SLOT(invokeCompletion()));
148
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()));
157
158     // Generic highlighter.
159     connect(Core::ICore::instance(), SIGNAL(coreOpened()),
160             Manager::instance(), SLOT(registerMimeTypes()));
161
162     // Add text snippet provider.
163     addAutoReleasedObject(new PlainTextSnippetProvider);
164
165     m_outlineFactory = new OutlineFactory;
166     addAutoReleasedObject(m_outlineFactory);
167
168     return true;
169 }
170
171 void TextEditorPlugin::extensionsInitialized()
172 {
173     m_editorFactory->actionHandler()->initializeActions();
174
175     ExtensionSystem::PluginManager *pluginManager = ExtensionSystem::PluginManager::instance();
176
177     m_searchResultWindow = Find::SearchResultWindow::instance();
178
179     m_outlineFactory->setWidgetFactories(pluginManager->getObjects<TextEditor::IOutlineWidgetFactory>());
180
181     connect(m_settings, SIGNAL(fontSettingsChanged(TextEditor::FontSettings)),
182             this, SLOT(updateSearchResultsFont(TextEditor::FontSettings)));
183
184     updateSearchResultsFont(m_settings->fontSettings());
185
186     addAutoReleasedObject(new FindInFiles(Find::SearchResultWindow::instance()));
187     addAutoReleasedObject(new FindInCurrentFile(Find::SearchResultWindow::instance()));
188
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)));
206 }
207
208 void TextEditorPlugin::initializeEditor(PlainTextEditorWidget *editor)
209 {
210     // common actions
211     m_editorFactory->actionHandler()->setupActions(editor);
212
213     TextEditorSettings::instance()->initializeEditor(editor);
214 }
215
216 void TextEditorPlugin::invokeCompletion()
217 {
218     Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
219     ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
220     if (editor)
221         CompletionSupport::instance()->complete(editor, SemanticCompletion, true);
222 }
223
224 void TextEditorPlugin::invokeQuickFix()
225 {
226     Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
227     ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
228     if (editor)
229         CompletionSupport::instance()->complete(editor, QuickFixCompletion, true);
230 }
231
232 void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
233 {
234     if (m_searchResultWindow)
235         m_searchResultWindow->setTextEditorFont(QFont(settings.family(),
236                                                       settings.fontSize() * settings.fontZoom() / 100));
237 }
238
239 void TextEditorPlugin::updateVariable(const QString &variable)
240 {
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)) {
249         QString value;
250         Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
251         ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
252         if (editor) {
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());
266             }
267         }
268         Core::VariableManager::instance()->insert(variable, value);
269     }
270 }
271
272 void TextEditorPlugin::updateCurrentSelection(const QString &text)
273 {
274     Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
275     ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
276     if (editor) {
277         int pos = editor->position();
278         int anchor = editor->position(ITextEditor::Anchor);
279         if (anchor < 0) // no selection
280             anchor = pos;
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);
287     }
288 }
289
290 Q_EXPORT_PLUGIN(TextEditorPlugin)