2009-12-02 17:59:27 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
#include "qmlcodecompletion.h"
|
2010-01-15 17:20:03 +01:00
|
|
|
#include "qmljseditor.h"
|
2009-10-02 10:49:33 +02:00
|
|
|
#include "qmlmodelmanagerinterface.h"
|
|
|
|
|
#include "qmlexpressionundercursor.h"
|
|
|
|
|
#include "qmllookupcontext.h"
|
|
|
|
|
#include "qmlresolveexpression.h"
|
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
#include <qmljs/qmljssymbol.h>
|
2009-04-22 15:21:04 +02:00
|
|
|
#include <texteditor/basetexteditor.h>
|
2009-12-02 17:59:27 +01:00
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
using namespace QmlJSEditor;
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
QmlCodeCompletion::QmlCodeCompletion(QmlModelManagerInterface *modelManager, QmlJS::TypeSystem *typeSystem, QObject *parent)
|
2009-04-22 15:21:04 +02:00
|
|
|
: TextEditor::ICompletionCollector(parent),
|
2009-10-02 10:49:33 +02:00
|
|
|
m_modelManager(modelManager),
|
2009-04-22 15:21:04 +02:00
|
|
|
m_editor(0),
|
|
|
|
|
m_startPosition(0),
|
2009-12-07 16:57:31 +01:00
|
|
|
m_caseSensitivity(Qt::CaseSensitive),
|
|
|
|
|
m_typeSystem(typeSystem)
|
2009-10-02 10:49:33 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(modelManager);
|
2009-12-07 16:57:31 +01:00
|
|
|
Q_ASSERT(typeSystem);
|
2009-10-02 10:49:33 +02:00
|
|
|
}
|
2009-04-22 15:21:04 +02:00
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
QmlCodeCompletion::~QmlCodeCompletion()
|
2009-04-22 15:21:04 +02:00
|
|
|
{ }
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
Qt::CaseSensitivity QmlCodeCompletion::caseSensitivity() const
|
2009-04-22 15:21:04 +02:00
|
|
|
{ return m_caseSensitivity; }
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
|
2009-04-22 15:21:04 +02:00
|
|
|
{ m_caseSensitivity = caseSensitivity; }
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
bool QmlCodeCompletion::supportsEditor(TextEditor::ITextEditable *editor)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2010-01-15 17:20:03 +01:00
|
|
|
if (qobject_cast<QmlJSTextEditor *>(editor->widget()))
|
2009-04-22 15:21:04 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
bool QmlCodeCompletion::triggersCompletion(TextEditor::ITextEditable *)
|
2009-04-22 15:21:04 +02:00
|
|
|
{ return false; }
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
int QmlCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
m_editor = editor;
|
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(m_editor->widget());
|
2009-04-22 15:21:04 +02:00
|
|
|
if (! edit)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
int pos = editor->position();
|
|
|
|
|
|
|
|
|
|
while (editor->characterAt(pos - 1).isLetterOrNumber() || editor->characterAt(pos - 1) == QLatin1Char('_'))
|
|
|
|
|
--pos;
|
|
|
|
|
|
|
|
|
|
m_startPosition = pos;
|
|
|
|
|
m_completions.clear();
|
|
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
QmlJS::Document::Ptr qmlDocument = edit->qmlDocument();
|
2009-10-02 11:29:54 +02:00
|
|
|
// qDebug() << "*** document:" << qmlDocument;
|
2009-10-02 10:49:33 +02:00
|
|
|
if (qmlDocument.isNull())
|
|
|
|
|
return pos;
|
|
|
|
|
|
2010-01-18 12:38:36 +01:00
|
|
|
if (!qmlDocument->qmlProgram())
|
2009-10-02 10:49:33 +02:00
|
|
|
qmlDocument = m_modelManager->snapshot().value(qmlDocument->fileName());
|
|
|
|
|
|
2009-10-09 11:24:24 +02:00
|
|
|
// FIXME: this completion strategy is not going to work when the document was never parsed correctly.
|
2010-01-18 12:38:36 +01:00
|
|
|
if (qmlDocument && qmlDocument->qmlProgram()) {
|
|
|
|
|
QmlJS::AST::UiProgram *program = qmlDocument->qmlProgram();
|
2009-10-09 11:24:24 +02:00
|
|
|
// qDebug() << "*** program:" << program;
|
2009-10-02 10:49:33 +02:00
|
|
|
|
|
|
|
|
if (program) {
|
|
|
|
|
QmlExpressionUnderCursor expressionUnderCursor;
|
|
|
|
|
QTextCursor cursor(edit->document());
|
|
|
|
|
cursor.setPosition(pos);
|
|
|
|
|
expressionUnderCursor(cursor, qmlDocument);
|
|
|
|
|
|
2009-12-07 16:57:31 +01:00
|
|
|
QmlLookupContext context(expressionUnderCursor.expressionScopes(), qmlDocument, m_modelManager->snapshot(), m_typeSystem);
|
2009-10-02 10:49:33 +02:00
|
|
|
QmlResolveExpression resolver(context);
|
2009-10-09 11:24:24 +02:00
|
|
|
// qDebug()<<"*** expression under cursor:"<<expressionUnderCursor.expressionNode();
|
2010-01-18 16:15:23 +01:00
|
|
|
QList<QmlJS::Symbol*> symbols = resolver.visibleSymbols(expressionUnderCursor.expressionNode());
|
2009-10-09 11:24:24 +02:00
|
|
|
// qDebug()<<"***"<<symbols.size()<<"visible symbols";
|
2009-10-02 10:49:33 +02:00
|
|
|
|
2010-01-18 16:15:23 +01:00
|
|
|
foreach (QmlJS::Symbol *symbol, symbols) {
|
2009-10-02 10:49:33 +02:00
|
|
|
QString word;
|
|
|
|
|
|
|
|
|
|
if (symbol->isIdSymbol()) {
|
|
|
|
|
word = symbol->asIdSymbol()->id();
|
|
|
|
|
} else {
|
|
|
|
|
word = symbol->name();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (word.isEmpty())
|
|
|
|
|
continue;
|
|
|
|
|
|
2009-07-06 16:29:44 +02:00
|
|
|
TextEditor::CompletionItem item(this);
|
2009-10-05 18:32:08 +02:00
|
|
|
item.text = word;
|
2009-07-06 16:29:44 +02:00
|
|
|
m_completions.append(item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-22 15:21:04 +02:00
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::completions(QList<TextEditor::CompletionItem> *completions)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
// ### FIXME: this code needs to be generalized.
|
|
|
|
|
|
|
|
|
|
const int length = m_editor->position() - m_startPosition;
|
|
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
|
*completions = m_completions;
|
|
|
|
|
else if (length > 0) {
|
|
|
|
|
const QString key = m_editor->textAt(m_startPosition, length);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This code builds a regular expression in order to more intelligently match
|
|
|
|
|
* camel-case style. This means upper-case characters will be rewritten as follows:
|
|
|
|
|
*
|
|
|
|
|
* A => [a-z0-9_]*A (for any but the first capital letter)
|
|
|
|
|
*
|
|
|
|
|
* Meaning it allows any sequence of lower-case characters to preceed an
|
|
|
|
|
* upper-case character. So for example gAC matches getActionController.
|
|
|
|
|
*/
|
|
|
|
|
QString keyRegExp;
|
|
|
|
|
keyRegExp += QLatin1Char('^');
|
|
|
|
|
bool first = true;
|
|
|
|
|
foreach (const QChar &c, key) {
|
|
|
|
|
if (c.isUpper() && !first) {
|
|
|
|
|
keyRegExp += QLatin1String("[a-z0-9_]*");
|
|
|
|
|
keyRegExp += c;
|
|
|
|
|
} else if (m_caseSensitivity == Qt::CaseInsensitive && c.isLower()) {
|
|
|
|
|
keyRegExp += QLatin1Char('[');
|
|
|
|
|
keyRegExp += c;
|
|
|
|
|
keyRegExp += c.toUpper();
|
|
|
|
|
keyRegExp += QLatin1Char(']');
|
|
|
|
|
} else {
|
|
|
|
|
keyRegExp += QRegExp::escape(c);
|
|
|
|
|
}
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
const QRegExp regExp(keyRegExp, Qt::CaseSensitive);
|
|
|
|
|
|
|
|
|
|
foreach (TextEditor::CompletionItem item, m_completions) {
|
2009-10-05 18:32:08 +02:00
|
|
|
if (regExp.indexIn(item.text) == 0) {
|
|
|
|
|
item.relevance = (key.length() > 0 &&
|
|
|
|
|
item.text.startsWith(key, Qt::CaseInsensitive)) ? 1 : 0;
|
2009-04-22 15:21:04 +02:00
|
|
|
(*completions) << item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::complete(const TextEditor::CompletionItem &item)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
2009-10-05 18:32:08 +02:00
|
|
|
const QString toInsert = item.text;
|
2009-04-22 15:21:04 +02:00
|
|
|
const int length = m_editor->position() - m_startPosition;
|
|
|
|
|
m_editor->setCurPos(m_startPosition);
|
|
|
|
|
m_editor->replace(length, toInsert);
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
bool QmlCodeCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems)
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
if (completionItems.count() == 1) {
|
|
|
|
|
complete(completionItems.first());
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
// Compute common prefix
|
2009-10-05 18:32:08 +02:00
|
|
|
QString firstKey = completionItems.first().text;
|
|
|
|
|
QString lastKey = completionItems.last().text;
|
2009-04-22 15:21:04 +02:00
|
|
|
const int length = qMin(firstKey.length(), lastKey.length());
|
|
|
|
|
firstKey.truncate(length);
|
|
|
|
|
lastKey.truncate(length);
|
|
|
|
|
|
|
|
|
|
while (firstKey != lastKey) {
|
|
|
|
|
firstKey.chop(1);
|
|
|
|
|
lastKey.chop(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int typedLength = m_editor->position() - m_startPosition;
|
|
|
|
|
if (!firstKey.isEmpty() && firstKey.length() > typedLength) {
|
|
|
|
|
m_editor->setCurPos(m_startPosition);
|
|
|
|
|
m_editor->replace(typedLength, firstKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlCodeCompletion::cleanup()
|
2009-04-22 15:21:04 +02:00
|
|
|
{
|
|
|
|
|
m_editor = 0;
|
|
|
|
|
m_startPosition = 0;
|
|
|
|
|
m_completions.clear();
|
|
|
|
|
}
|
|
|
|
|
|