forked from qt-creator/qt-creator
Automagically pop up the completion box (experimental) for QML/JS code (experimental).
This commit is contained in:
@@ -472,6 +472,9 @@ TextEditor::ITextEditable *QmlCodeCompletion::editor() const
|
||||
int QmlCodeCompletion::startPosition() const
|
||||
{ return m_startPosition; }
|
||||
|
||||
bool QmlCodeCompletion::shouldRestartCompletion()
|
||||
{ return false; }
|
||||
|
||||
bool QmlCodeCompletion::supportsEditor(TextEditor::ITextEditable *editor)
|
||||
{
|
||||
if (qobject_cast<QmlJSTextEditor *>(editor->widget()))
|
||||
@@ -480,12 +483,53 @@ bool QmlCodeCompletion::supportsEditor(TextEditor::ITextEditable *editor)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool checkStartOfIdentifier(const QString &word)
|
||||
{
|
||||
if (word.isEmpty())
|
||||
return false;
|
||||
|
||||
const QChar ch = word.at(0);
|
||||
|
||||
switch (ch.unicode()) {
|
||||
case '_': case '$':
|
||||
return true;
|
||||
|
||||
default:
|
||||
return ch.isLetter();
|
||||
}
|
||||
}
|
||||
|
||||
static bool isIdentifierChar(QChar ch)
|
||||
{
|
||||
switch (ch.unicode()) {
|
||||
case '_': case '$':
|
||||
return true;
|
||||
|
||||
default:
|
||||
return ch.isLetterOrNumber();
|
||||
}
|
||||
}
|
||||
|
||||
bool QmlCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
|
||||
{
|
||||
const QChar ch = editor->characterAt(editor->position() - 1);
|
||||
|
||||
if (ch == QLatin1Char('(') || ch == QLatin1Char('.'))
|
||||
return true;
|
||||
else if (isIdentifierChar(ch)) {
|
||||
if (QmlJSTextEditor *ed = qobject_cast<QmlJSTextEditor *>(editor->widget())) {
|
||||
QTextCursor tc = ed->textCursor();
|
||||
tc.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor);
|
||||
const QString word = tc.selectedText();
|
||||
if (word.length() > 2 && checkStartOfIdentifier(word)) {
|
||||
for (int i = 0; i < word.length(); ++i) {
|
||||
if (! isIdentifierChar(word.at(i)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ public:
|
||||
|
||||
virtual TextEditor::ITextEditable *editor() const;
|
||||
virtual int startPosition() const;
|
||||
virtual bool shouldRestartCompletion();
|
||||
virtual bool supportsEditor(TextEditor::ITextEditable *editor);
|
||||
virtual bool triggersCompletion(TextEditor::ITextEditable *editor);
|
||||
virtual int startCompletion(TextEditor::ITextEditable *editor);
|
||||
|
||||
@@ -77,6 +77,9 @@ void CompletionSupport::cleanupCompletions()
|
||||
disconnect(m_completionList, SIGNAL(destroyed(QObject*)),
|
||||
this, SLOT(cleanupCompletions()));
|
||||
|
||||
if (m_checkCompletionTrigger)
|
||||
m_checkCompletionTrigger = m_completionCollector->shouldRestartCompletion();
|
||||
|
||||
m_completionList = 0;
|
||||
m_completionCollector->cleanup();
|
||||
|
||||
|
||||
@@ -167,3 +167,8 @@ void ICompletionCollector::filter(const QList<TextEditor::CompletionItem> &items
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ICompletionCollector::shouldRestartCompletion()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -77,6 +77,7 @@ public:
|
||||
virtual ~ICompletionCollector() {}
|
||||
|
||||
virtual QList<CompletionItem> getCompletions();
|
||||
virtual bool shouldRestartCompletion();
|
||||
|
||||
/* Returns the current active ITextEditable */
|
||||
virtual ITextEditable *editor() const = 0;
|
||||
|
||||
Reference in New Issue
Block a user