1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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.
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 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
28 **************************************************************************/
30 #include "qtscripteditor.h"
31 #include "qtscripteditorconstants.h"
32 #include "qtscripthighlighter.h"
33 #include "qtscripteditorplugin.h"
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"
43 #include <utils/uncommentselection.h>
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>
53 #include <QtCore/QTimer>
54 #include <QtCore/QtDebug>
56 #include <QtGui/QComboBox>
57 #include <QtGui/QHBoxLayout>
58 #include <QtGui/QMenu>
59 #include <QtGui/QToolBar>
62 UPDATE_DOCUMENT_DEFAULT_INTERVAL = 100
65 using namespace JavaScript::AST;
68 namespace QtScriptEditor {
71 class FindDeclarations: protected Visitor
73 QList<Declaration> declarations;
76 QList<Declaration> accept(JavaScript::AST::Node *node)
78 JavaScript::AST::Node::acceptChild(node, this);
85 virtual bool visit(FunctionExpression *)
90 virtual bool visit(FunctionDeclaration *ast)
95 QString text = ast->name->asString();
97 text += QLatin1Char('(');
98 for (FormalParameterList *it = ast->formals; it; it = it->next) {
100 text += it->name->asString();
103 text += QLatin1String(", ");
106 text += QLatin1Char(')');
110 d.startLine = ast->startLine;
111 d.startColumn = ast->startColumn;
112 d.endLine = ast->endLine;
113 d.endColumn = ast->endColumn;
115 declarations.append(d);
120 virtual bool visit(VariableDeclaration *ast)
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;
132 declarations.append(d);
137 ScriptEditorEditable::ScriptEditorEditable(ScriptEditor *editor, const QList<int>& context)
138 : BaseTextEditorEditable(editor), m_context(context)
142 ScriptEditor::ScriptEditor(const Context &context,
144 TextEditor::BaseTextEditor(parent),
148 setParenthesesMatchingEnabled(true);
149 setMarksVisible(true);
150 setCodeFoldingSupported(true);
151 setCodeFoldingVisible(true);
152 setMimeType(QtScriptEditor::Constants::C_QTSCRIPTEDITOR_MIMETYPE);
154 m_updateDocumentTimer = new QTimer(this);
155 m_updateDocumentTimer->setInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
156 m_updateDocumentTimer->setSingleShot(true);
158 connect(m_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
160 connect(this, SIGNAL(textChanged()), this, SLOT(updateDocument()));
162 baseTextDocument()->setSyntaxHighlighter(new QtScriptHighlighter);
165 ScriptEditor::~ScriptEditor()
169 QList<Declaration> ScriptEditor::declarations() const
170 { return m_declarations; }
172 QStringList ScriptEditor::words() const
175 Core::IEditor *ScriptEditorEditable::duplicate(QWidget *parent)
177 ScriptEditor *newEditor = new ScriptEditor(m_context, parent);
178 newEditor->duplicateFrom(editor());
179 QtScriptEditorPlugin::instance()->initializeEditor(newEditor);
180 return newEditor->editableInterface();
183 const char *ScriptEditorEditable::kind() const
185 return QtScriptEditor::Constants::C_QTSCRIPTEDITOR;
188 ScriptEditor::Context ScriptEditorEditable::context() const
193 void ScriptEditor::updateDocument()
195 m_updateDocumentTimer->start(UPDATE_DOCUMENT_DEFAULT_INTERVAL);
198 void ScriptEditor::updateDocumentNow()
200 // ### move in the parser thread.
202 m_updateDocumentTimer->stop();
204 const QString fileName = file()->fileName();
205 const QString code = toPlainText();
207 JavaScriptParser parser;
208 JavaScriptEnginePrivate driver;
210 JavaScript::NodePool nodePool(fileName, &driver);
211 driver.setNodePool(&nodePool);
213 JavaScript::Lexer lexer(&driver);
214 lexer.setCode(code, /*line = */ 1);
215 driver.setLexer(&lexer);
217 if (parser.parse(&driver)) {
218 JavaScript::AST::Visitor v;
219 driver.ast()->accept(&v);
221 FindDeclarations decls;
222 m_declarations = decls.accept(driver.ast());
225 foreach (const JavaScriptNameIdImpl &id, driver.literals())
226 m_words.append(id.asString());
229 items.append(tr("<Select Symbol>"));
231 foreach (Declaration decl, m_declarations)
232 items.append(decl.text);
234 m_methodCombo->clear();
235 m_methodCombo->addItems(items);
236 updateMethodBoxIndex();
239 QList<QTextEdit::ExtraSelection> selections;
241 QTextCharFormat errorFormat;
242 errorFormat.setUnderlineColor(Qt::red);
243 errorFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
245 QTextCharFormat warningFormat;
246 warningFormat.setUnderlineColor(Qt::darkYellow);
247 warningFormat.setUnderlineStyle(QTextCharFormat::WaveUnderline);
249 QTextEdit::ExtraSelection sel;
251 foreach (const JavaScriptParser::DiagnosticMessage &d, parser.diagnosticMessages()) {
253 int column = d.column;
259 sel.format = warningFormat;
261 sel.format = errorFormat;
263 QTextCursor c(document()->findBlockByNumber(line - 1));
265 sel.cursor.setPosition(c.position() + column - 1);
266 sel.cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
268 selections.append(sel);
271 setExtraSelections(CodeWarningsSelection, selections);
274 void ScriptEditor::jumpToMethod(int index)
277 Declaration d = m_declarations.at(index - 1);
278 gotoLine(d.startLine, d.startColumn - 1);
283 void ScriptEditor::updateMethodBoxIndex()
285 int line = 0, column = 0;
286 convertPosition(position(), &line, &column);
288 int currentSymbolIndex = 0;
291 while (index < m_declarations.size()) {
292 const Declaration &d = m_declarations.at(index++);
294 if (line < d.startLine)
297 currentSymbolIndex = index;
300 m_methodCombo->setCurrentIndex(currentSymbolIndex);
303 void ScriptEditor::updateMethodBoxToolTip()
307 void ScriptEditor::updateFileName()
311 void ScriptEditor::setFontSettings(const TextEditor::FontSettings &fs)
313 TextEditor::BaseTextEditor::setFontSettings(fs);
314 QtScriptHighlighter *highlighter = qobject_cast<QtScriptHighlighter*>(baseTextDocument()->syntaxHighlighter());
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);
330 highlighter->setFormats(fs.toTextCharFormats(categories));
331 highlighter->rehighlight();
334 bool ScriptEditor::isElectricCharacter(const QChar &ch) const
336 if (ch == QLatin1Char('{') || ch == QLatin1Char('}'))
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,
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);
359 void ScriptEditor::indentBlock(QTextDocument *doc, QTextBlock block, QChar typedChar)
361 const TextEditor::TextBlockIterator begin(doc->begin());
362 const TextEditor::TextBlockIterator end(block.next());
363 indentScriptBlock(tabSettings(), block, begin, end, typedChar);
366 TextEditor::BaseTextEditorEditable *ScriptEditor::createEditableInterface()
368 ScriptEditorEditable *editable = new ScriptEditorEditable(this, m_context);
369 createToolBar(editable);
373 void ScriptEditor::createToolBar(ScriptEditorEditable *editable)
375 m_methodCombo = new QComboBox;
376 m_methodCombo->setMinimumContentsLength(22);
377 //m_methodCombo->setSizeAdjustPolicy(QComboBox::AdjustToContents);
379 // Make the combo box prefer to expand
380 QSizePolicy policy = m_methodCombo->sizePolicy();
381 policy.setHorizontalPolicy(QSizePolicy::Expanding);
382 m_methodCombo->setSizePolicy(policy);
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()));
388 connect(file(), SIGNAL(changed()), this, SLOT(updateFileName()));
390 QToolBar *toolBar = static_cast<QToolBar*>(editable->toolBar());
392 QList<QAction*> actions = toolBar->actions();
393 QWidget *w = toolBar->widgetForAction(actions.first());
394 static_cast<QHBoxLayout*>(w->layout())->insertWidget(0, m_methodCombo, 1);
397 void ScriptEditor::contextMenuEvent(QContextMenuEvent *e)
399 QMenu *menu = new QMenu();
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);
407 appendStandardContextMenuActions(menu);
408 menu->exec(e->globalPos());
412 void ScriptEditor::unCommentSelection()
414 Utils::unCommentSelection(this);
417 } // namespace Internal
418 } // namespace QtScriptEditor