OSDN Git Service

implement the standard context menu also for the script editors
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qtscripteditor / qtscripteditor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
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 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include "qtscripteditor.h"
31 #include "qtscripteditorconstants.h"
32 #include "qtscripthighlighter.h"
33 #include "qtscripteditorplugin.h"
34
35 #include "parser/javascriptengine_p.h"
36 #include "parser/javascriptparser_p.h"
37 #include "parser/javascriptlexer_p.h"
38 #include "parser/javascriptnodepool_p.h"
39 #include "parser/javascriptastvisitor_p.h"
40 #include "parser/javascriptast_p.h"
41
42 #include <indenter.h>
43 #include <utils/uncommentselection.h>
44
45 #include <coreplugin/icore.h>
46 #include <coreplugin/actionmanager/actionmanager.h>
47 #include <texteditor/basetextdocument.h>
48 #include <texteditor/fontsettings.h>
49 #include <texteditor/textblockiterator.h>
50 #include <texteditor/texteditorconstants.h>
51 #include <texteditor/texteditorsettings.h>
52
53 #include <QtCore/QTimer>
54 #include <QtCore/QtDebug>
55
56 #include <QtGui/QComboBox>
57 #include <QtGui/QHBoxLayout>
58 #include <QtGui/QMenu>
59 #include <QtGui/QToolBar>
60
61 enum {
62     UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100
63 };
64
65 using namespace JavaScript::AST;
66
67
68 namespace QtScriptEditor {
69 namespace Internal {
70
71 class FindDeclarations: protected Visitor
72 {
73     QList<Declaration> declarations;
74
75 public:
76     QList<Declaration> accept(JavaScript::AST::Node *node)
77     {
78         JavaScript::AST::Node::acceptChild(node, this);
79         return declarations;
80     }
81
82 protected:
83     using Visitor::visit;
84
85     virtual bool visit(FunctionExpression *)
86     {
87         return false;
88     }
89
90     virtual bool visit(FunctionDeclaration *ast)
91     {
92         if (! ast->name)
93             return false;
94
95         QString text = ast->name->asString();
96
97         text += QLatin1Char('(');
98         for (FormalParameterList *it = ast->formals; it; it = it->next) {
99             if (it->name)
100                 text += it->name->asString();
101
102             if (it->next)
103                 text += QLatin1String(", ");
104         }
105
106         text += QLatin1Char(')');
107
108         Declaration d;
109         d.text = text;
110         d.startLine = ast->startLine;
111         d.startColumn = ast->startColumn;
112         d.endLine = ast->endLine;
113         d.endColumn = ast->endColumn;
114
115         declarations.append(d);
116
117         return false;
118     }
119
120     virtual bool visit(VariableDeclaration *ast)
121     {
122         if (! ast->name)
123             return false;
124
125         Declaration d;
126         d.text = ast->name->asString();
127         d.startLine= ast->startLine;
128         d.startColumn = ast->startColumn;
129         d.endLine = ast->endLine;
130         d.endColumn = ast->endColumn;
131
132         declarations.append(d);
133         return false;
134     }
135 };
136
137 ScriptEditorEditable::ScriptEditorEditable(ScriptEditor *editor, const QList<int>& context)
138     : BaseTextEditorEditable(editor), m_context(context)
139 {
140 }
141
142 ScriptEditor::ScriptEditor(const Context &context,
143                            QWidget *parent) :
144     TextEditor::BaseTextEditor(parent),
145     m_context(context),
146     m_methodCombo(0)
147 {
148     setParenthesesMatchingEnabled(true);
149     setMarksVisible(true);
150     setCodeFoldingSupported(true);
151     setCodeFoldingVisible(true);
152     setMimeType(QtScriptEditor::Constants::C_QTSCRIPTEDITOR_MIMETYPE);
153
154     m_updateDocumentTimer = new QTimer(this);
155     m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
156     m_updateDocumentTimer->setSingleShot(true);
157
158     connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
159
160     connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
161
162     baseTextDocument()->setSyntaxHighlighter(new QtScriptHighlighter);
163 }
164
165 ScriptEditor::~ScriptEditor()
166 {
167 }
168
169 QList<Declaration> ScriptEditor::declarations() const
170 { return m_declarations; }
171
172 QStringList ScriptEditor::words() const
173 { return m_words; }
174
175 Core::IEditor *ScriptEditorEditable::duplicate(QWidget *parent)
176 {
177     ScriptEditor *newEditor = new ScriptEditor(m_context, parent);
178     newEditor->duplicateFrom(editor());
179     QtScriptEditorPlugin::instance()->initializeEditor(newEditor);
180     return newEditor->editableInterface();
181 }
182
183 const char *ScriptEditorEditable::kind() const
184 {
185     return QtScriptEditor::Constants::C_QTSCRIPTEDITOR;
186 }
187
188 ScriptEditor::Context ScriptEditorEditable::context() const
189 {
190     return m_context;
191 }
192
193 void ScriptEditor::updateDocument()
194 {
195     m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
196 }
197
198 void ScriptEditor::updateDocumentNow()
199 {
200     // ### move in the parser thread.
201
202     m_updateDocumentTimer->stop();
203
204     const QString fileName = file()->fileName();
205     const QString code = toPlainText();
206
207     JavaScriptParser parser;
208     JavaScriptEnginePrivate driver;
209
210     JavaScript::NodePool nodePool(fileName, &driver);
211     driver.setNodePool(&nodePool);
212
213     JavaScript::Lexer lexer(&driver);
214     lexer.setCode(code, /*line = */ 1);
215     driver.setLexer(&lexer);
216
217     if (parser.parse(&driver)) {
218         JavaScript::AST::Visitor v;
219         driver.ast()->accept(&v);
220
221         FindDeclarations decls;
222         m_declarations = decls.accept(driver.ast());
223
224         m_words.clear();
225         foreach (const JavaScriptNameIdImpl &id, driver.literals())
226             m_words.append(id.asString());
227
228         QStringList items;
229         items.append(tr("<Select Symbol>"));
230
231         foreach (Declaration decl, m_declarations)
232             items.append(decl.text);
233
234         m_methodCombo->clear();
235         m_methodCombo->addItems(items);
236         updateMethodBoxIndex();
237     }
238
239     QList<QTextEdit::ExtraSelection> selections;
240
241     QTextCharFormat errorFormat;
242     errorFormat.setUnderlineColor(Qt::red);
243     errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
244
245     QTextCharFormat warningFormat;
246     warningFormat.setUnderlineColor(Qt::darkYellow);
247     warningFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
248
249     QTextEdit::ExtraSelection sel;
250
251     foreach (const JavaScriptParser::DiagnosticMessage &d, parser.diagnosticMessages()) {
252         int line = d.line;
253         int column = d.column;
254
255         if (column == 0)
256             column = 1;
257
258         if (d.isWarning())
259             sel.format = warningFormat;
260         else
261             sel.format = errorFormat;
262
263         QTextCursor c(document()->findBlockByNumber(line - 1));
264         sel.cursor = c;
265         sel.cursor.setPosition(c.position() + column - 1);
266         sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
267
268         selections.append(sel);
269     }
270
271     setExtraSelections(CodeWarningsSelection, selections);
272 }
273
274 void ScriptEditor::jumpToMethod(int index)
275 {
276     if (index) {
277         Declaration d = m_declarations.at(index - 1);
278         gotoLine(d.startLine, d.startColumn - 1);
279         setFocus();
280     }
281 }
282
283 void ScriptEditor::updateMethodBoxIndex()
284 {
285     int line = 0, column = 0;
286     convertPosition(position(), &line, &column);
287
288     int currentSymbolIndex = 0;
289
290     int index = 0;
291     while (index < m_declarations.size()) {
292         const Declaration &d = m_declarations.at(index++);
293
294         if (line < d.startLine)
295             break;
296         else
297             currentSymbolIndex = index;
298     }
299
300     m_methodCombo->setCurrentIndex(currentSymbolIndex);
301 }
302
303 void ScriptEditor::updateMethodBoxToolTip()
304 {
305 }
306
307 void ScriptEditor::updateFileName()
308 {
309 }
310
311 void ScriptEditor::setFontSettings(const TextEditor::FontSettings &fs)
312 {
313     TextEditor::BaseTextEditor::setFontSettings(fs);
314     QtScriptHighlighter *highlighter = qobject_cast<QtScriptHighlighter*>(baseTextDocument()->syntaxHighlighter());
315     if (!highlighter)
316         return;
317
318     static QVector<QString> categories;
319     if (categories.isEmpty()) {
320         categories << QLatin1String(TextEditor::Constants::C_NUMBER)
321                    << QLatin1String(TextEditor::Constants::C_STRING)
322                    << QLatin1String(TextEditor::Constants::C_TYPE)
323                    << QLatin1String(TextEditor::Constants::C_KEYWORD)
324                    << QLatin1String(TextEditor::Constants::C_PREPROCESSOR)
325                    << QLatin1String(TextEditor::Constants::C_LABEL)
326                    << QLatin1String(TextEditor::Constants::C_COMMENT)
327                    << QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
328     }
329
330     highlighter->setFormats(fs.toTextCharFormats(categories));
331     highlighter->rehighlight();
332 }
333
334 bool ScriptEditor::isElectricCharacter(const QChar &ch) const
335 {
336     if (ch == QLatin1Char('{') || ch == QLatin1Char('}'))
337         return true;
338     return false;
339 }
340
341     // Indent a code line based on previous
342 template <class Iterator>
343 static void indentScriptBlock(const TextEditor::TabSettings &ts,
344                               const QTextBlock &block,
345                               const Iterator &programBegin,
346                               const Iterator &programEnd,
347                               QChar typedChar)
348 {
349     typedef typename SharedTools::Indenter<Iterator> Indenter ;
350     Indenter &indenter = Indenter::instance();
351     indenter.setTabSize(ts.m_tabSize);
352     indenter.setIndentSize(ts.m_indentSize);
353     const TextEditor::TextBlockIterator current(block);
354     const int indent = indenter.indentForBottomLine(current, programBegin,
355                                                     programEnd, typedChar);
356     ts.indentLine(block, indent);
357 }
358
359 void ScriptEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
360 {
361     const TextEditor::TextBlockIterator begin(doc->begin());
362     const TextEditor::TextBlockIterator end(block.next());
363     indentScriptBlock(tabSettings(), block, begin, end, typedChar);
364 }
365
366 TextEditor::BaseTextEditorEditable *ScriptEditor::createEditableInterface()
367 {
368     ScriptEditorEditable *editable = new ScriptEditorEditable(this, m_context);
369     createToolBar(editable);
370     return editable;
371 }
372
373 void ScriptEditor::createToolBar(ScriptEditorEditable *editable)
374 {
375     m_methodCombo = new QComboBox;
376     m_methodCombo->setMinimumContentsLength(22);
377     //m_methodCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
378
379     // Make the combo box prefer to expand
380     QSizePolicy policy = m_methodCombo->sizePolicy();
381     policy.setHorizontalPolicy(QSizePolicy::Expanding);
382     m_methodCombo->setSizePolicy(policy);
383
384     connect(m_methodCombo, SIGNAL(activated(int)), this, SLOT(jumpToMethod(int)));
385     connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(updateMethodBoxIndex()));
386     connect(m_methodCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMethodBoxToolTip()));
387
388     connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
389
390     QToolBar *toolBar = static_cast<QToolBar*>(editable->toolBar());
391
392     QList<QAction*> actions = toolBar->actions();
393     QWidget *w = toolBar->widgetForAction(actions.first());
394     static_cast<QHBoxLayout*>(w->layout())->insertWidget(0, m_methodCombo, 1);
395 }
396
397 void ScriptEditor::contextMenuEvent(QContextMenuEvent *e)
398 {
399     QMenu *menu = new QMenu();
400
401     if (Core::ActionContainer *mcontext = Core::ICore::instance()->actionManager()->actionContainer(QtScriptEditor::Constants::M_CONTEXT)) {
402         QMenu *contextMenu = mcontext->menu();
403         foreach (QAction *action, contextMenu->actions())
404             menu->addAction(action);
405     }
406
407     appendStandardContextMenuActions(menu);
408     menu->exec(e->globalPos());
409     delete menu;
410 }
411
412 void ScriptEditor::unCommentSelection()
413 {
414     Utils::unCommentSelection(this);
415 }
416
417 } // namespace Internal
418 } // namespace QtScriptEditor