Commit ae5d92d6 authored by Marco Bubke's avatar Marco Bubke Committed by Nikolai Kosjar

Clang: Refactor ClangCompletionContextAnalyzer

Change-Id: Ib42ddc672da8b068591129e2e0b9652d3e07ad58
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent 5a791e88
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "activationsequencecontextprocessor.h"
#include "activationsequenceprocessor.h"
#include <cplusplus/BackwardsScanner.h>
#include <cplusplus/ExpressionUnderCursor.h>
#include <cplusplus/SimpleLexer.h>
#include <QRegExp>
#include <QTextDocument>
namespace ClangCodeModel {
namespace Internal {
ActivationSequenceContextProcessor::ActivationSequenceContextProcessor(const ClangCompletionAssistInterface *assistInterface)
: m_textCursor(assistInterface->textDocument()),
m_assistInterface(assistInterface),
m_positionInDocument(assistInterface->position()),
m_positionAfterOperator(m_positionInDocument),
m_positionBeforeOperator(m_positionAfterOperator)
{
m_textCursor.setPosition(m_positionInDocument);
process();
}
CPlusPlus::Kind ActivationSequenceContextProcessor::completionKind() const
{
return m_completionKind;
}
const QTextCursor &ActivationSequenceContextProcessor::textCursor_forTestOnly() const
{
return m_textCursor;
}
int ActivationSequenceContextProcessor::positionAfterOperator() const
{
return m_positionAfterOperator;
}
int ActivationSequenceContextProcessor::positionBeforeOperator() const
{
return m_positionBeforeOperator;
}
void ActivationSequenceContextProcessor::process()
{
skipeWhiteSpacesAndIdentifierBeforeCursor();
processActivationSequence();
if (m_completionKind != CPlusPlus::T_EOF_SYMBOL) {
processStringLiteral();
processComma();
generateTokens();
processDoxygenComment();
processComment();
processInclude();
processSlashOutsideOfAString();
processLeftParen();
processPreprocessorInclude();
resetPositionForEOFCompletionKind();
}
}
void ActivationSequenceContextProcessor::processActivationSequence()
{
const auto activationSequence = m_assistInterface->textAt(m_positionInDocument - 3, 3);
ActivationSequenceProcessor activationSequenceProcessor(activationSequence,
m_positionInDocument,
true);
m_completionKind = activationSequenceProcessor.completionKind();
m_positionBeforeOperator = activationSequenceProcessor.position();
}
void ActivationSequenceContextProcessor::processStringLiteral()
{
if (m_completionKind == CPlusPlus::T_STRING_LITERAL) {
QTextCursor selectionTextCursor = m_textCursor;
selectionTextCursor.movePosition(QTextCursor::StartOfLine, QTextCursor::KeepAnchor);
QString selection = selectionTextCursor.selectedText();
if (selection.indexOf(QLatin1Char('"')) < selection.length() - 1)
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
}
void ActivationSequenceContextProcessor::processComma()
{
if (m_completionKind == CPlusPlus::T_COMMA) {
CPlusPlus::ExpressionUnderCursor expressionUnderCursor(m_assistInterface->languageFeatures());
if (expressionUnderCursor.startOfFunctionCall(m_textCursor) == -1)
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
}
void ActivationSequenceContextProcessor::generateTokens()
{
CPlusPlus::SimpleLexer tokenize;
tokenize.setLanguageFeatures(m_assistInterface->languageFeatures());
tokenize.setSkipComments(false);
auto state = CPlusPlus::BackwardsScanner::previousBlockState(m_textCursor.block());
m_tokens = tokenize(m_textCursor.block().text(), state);
int leftOfCursorTokenIndex = std::max(0, m_textCursor.positionInBlock() - 1);
m_tokenIndex= CPlusPlus::SimpleLexer::tokenBefore(m_tokens, leftOfCursorTokenIndex); // get the token at the left of the cursor
if (m_tokenIndex > -1)
m_token = m_tokens.at(m_tokenIndex);
}
void ActivationSequenceContextProcessor::processDoxygenComment()
{
if (m_completionKind == CPlusPlus::T_DOXY_COMMENT
&& !(m_token.is(CPlusPlus::T_DOXY_COMMENT)
|| m_token.is(CPlusPlus::T_CPP_DOXY_COMMENT)))
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
void ActivationSequenceContextProcessor::processComment()
{
if (m_token.is(CPlusPlus::T_COMMENT) || m_token.is(CPlusPlus::T_CPP_COMMENT))
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
void ActivationSequenceContextProcessor::processInclude()
{
if (m_token.isLiteral() && !isCompletionKindStringLiteralOrSlash())
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
void ActivationSequenceContextProcessor::processSlashOutsideOfAString()
{
if (m_completionKind ==CPlusPlus::T_SLASH
&& (m_token.isNot(CPlusPlus::T_STRING_LITERAL)
&& m_token.isNot(CPlusPlus::T_ANGLE_STRING_LITERAL)))
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
void ActivationSequenceContextProcessor::processLeftParen()
{
if (m_completionKind == CPlusPlus::T_LPAREN) {
if (m_tokenIndex > 0) {
// look at the token at the left of T_LPAREN
const CPlusPlus::Token &previousToken = m_tokens.at(m_tokenIndex - 1);
switch (previousToken.kind()) {
case CPlusPlus::T_IDENTIFIER:
case CPlusPlus::T_GREATER:
case CPlusPlus::T_SIGNAL:
case CPlusPlus::T_SLOT:
break; // good
default:
// that's a bad token :)
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
}
}
}
bool ActivationSequenceContextProcessor::isCompletionKindStringLiteralOrSlash() const
{
return m_completionKind == CPlusPlus::T_STRING_LITERAL
|| m_completionKind == CPlusPlus::T_ANGLE_STRING_LITERAL
|| m_completionKind == CPlusPlus::T_SLASH;
}
bool ActivationSequenceContextProcessor::isProbablyPreprocessorIncludeDirective() const
{
return m_tokens.size() >= 3
&& m_tokens.at(0).is(CPlusPlus::T_POUND)
&& m_tokens.at(1).is(CPlusPlus::T_IDENTIFIER)
&& (m_tokens.at(2).is(CPlusPlus::T_STRING_LITERAL)
|| m_tokens.at(2).is(CPlusPlus::T_ANGLE_STRING_LITERAL));
}
void ActivationSequenceContextProcessor::processPreprocessorInclude()
{
if (isCompletionKindStringLiteralOrSlash()) {
if (isProbablyPreprocessorIncludeDirective()) {
const CPlusPlus::Token &directiveToken = m_tokens.at(1);
QString directive = m_textCursor.block().text().mid(directiveToken.bytesBegin(),
directiveToken.bytes());
if (directive != QStringLiteral("include")
&& directive != QStringLiteral("include_next")
&& directive != QStringLiteral("import"))
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
} else {
m_completionKind = CPlusPlus::T_EOF_SYMBOL;
}
}
}
void ActivationSequenceContextProcessor::resetPositionForEOFCompletionKind()
{
if (m_completionKind == CPlusPlus::T_EOF_SYMBOL)
m_positionBeforeOperator = m_positionInDocument;
}
void ActivationSequenceContextProcessor::skipeWhiteSpacesAndIdentifierBeforeCursor()
{
QTextDocument *document = m_assistInterface->textDocument();
const QRegExp findNonWhiteSpaceRegularExpression(QStringLiteral("[^\\s\\w]"));
auto nonWhiteSpaceTextCursor = document->find(findNonWhiteSpaceRegularExpression,
m_positionInDocument,
QTextDocument::FindBackward);
if (!nonWhiteSpaceTextCursor.isNull()) {
m_positionInDocument = nonWhiteSpaceTextCursor.position();
m_positionAfterOperator = m_positionInDocument;
m_textCursor.setPosition(m_positionInDocument);
}
}
} // namespace Internal
} // namespace ClangCodeModel
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CLANGCODEMODEL_INTERNAL_ACTIVATIONSEQUENCECONTEXTPROCESSOR_H
#define CLANGCODEMODEL_INTERNAL_ACTIVATIONSEQUENCECONTEXTPROCESSOR_H
#include <clangcodemodel/clangcompletionassistinterface.h>
#include <cplusplus/Token.h>
#include <QTextCursor>
QT_BEGIN_NAMESPACE
class QTextDocument;
QT_END_NAMESPACE
namespace ClangCodeModel {
namespace Internal {
class ActivationSequenceContextProcessor
{
public:
ActivationSequenceContextProcessor(const ClangCompletionAssistInterface *assistInterface);
CPlusPlus::Kind completionKind() const;
const QTextCursor &textCursor_forTestOnly() const;
int positionAfterOperator() const;
int positionBeforeOperator() const;
protected:
void process();
void skipeWhiteSpacesAndIdentifierBeforeCursor();
void processActivationSequence();
void processStringLiteral();
void processComma();
void generateTokens();
void processDoxygenComment();
void processComment();
void processInclude();
void processSlashOutsideOfAString();
void processLeftParen();
void processPreprocessorInclude();
void resetPositionForEOFCompletionKind();
bool isCompletionKindStringLiteralOrSlash() const;
bool isProbablyPreprocessorIncludeDirective() const;
private:
QVector<CPlusPlus::Token> m_tokens;
QTextCursor m_textCursor;
CPlusPlus::Token m_token;
const ClangCompletionAssistInterface *m_assistInterface;
int m_tokenIndex;
int m_positionInDocument;
int m_positionAfterOperator;
int m_positionBeforeOperator;
CPlusPlus::Kind m_completionKind;
};
} // namespace Internal
} // namespace ClangCodeModel
#endif // CLANGCODEMODEL_INTERNAL_ACTIVATIONSEQUENCECONTEXTPROCESSOR_H
......@@ -164,8 +164,8 @@ void ActivationSequenceProcessor::processArrowStar()
void ActivationSequenceProcessor::processDoxyGenComment()
{
if ((m_char2 == QLatin1Char('\\') || m_char2 == QLatin1Char('@'))
&& (m_char3.isNull() || m_char3.isSpace())) {
if ((m_char2.isNull() || m_char2.isSpace())
&& (m_char3 == QLatin1Char('\\') || m_char3 == QLatin1Char('@'))) {
m_completionKind = CPlusPlus::T_DOXY_COMMENT;
m_offset = 1;
}
......
......@@ -12,6 +12,7 @@ DEFINES += "\"CLANG_RESOURCE_DIR=\\\"$${LLVM_LIBDIR}/clang/$${LLVM_VERSION}/incl
unix:QMAKE_LFLAGS += -Wl,-rpath,\'$$LLVM_LIBDIR\'
SOURCES += \
activationsequencecontextprocessor.cpp \
activationsequenceprocessor.cpp \
clangassistproposal.cpp \
clangassistproposalitem.cpp \
......@@ -48,6 +49,7 @@ SOURCES += \
HEADERS += \
activationsequencecontextprocessor.h \
activationsequenceprocessor.h \
clangassistproposal.h \
clangassistproposalitem.h \
......@@ -107,13 +109,10 @@ equals(TEST, 1) {
test/clang_tests_database.qrc
HEADERS += \
test/clangcodecompletion_test.h \
test/clangcompletioncontextanalyzertest.h
test/clangcodecompletion_test.h
SOURCES += \
test/clangcodecompletion_test.cpp \
test/clangcompletioncontextanalyzertest.cpp
test/clangcodecompletion_test.cpp
DISTFILES += \
test/mysource.cpp \
......
......@@ -53,6 +53,8 @@ QtcPlugin {
name: "Completion support"
condition: product.clangCompletion
files: [
"activationsequencecontextprocessor.cpp",
"activationsequencecontextprocessor.h",
"activationsequenceprocessor.cpp",
"activationsequenceprocessor.h",
"clangassistproposal.cpp",
......@@ -104,8 +106,6 @@ QtcPlugin {
"clang_tests_database.qrc",
"clangcodecompletion_test.cpp",
"clangcodecompletion_test.h",
"clangcompletioncontextanalyzertest.cpp",
"clangcompletioncontextanalyzertest.h",
]
}
......
......@@ -36,7 +36,6 @@
#ifdef WITH_TESTS
# include "test/clangcodecompletion_test.h"
# include "test/clangcompletioncontextanalyzertest.h"
#endif
#include <cpptools/cppmodelmanager.h>
......@@ -93,7 +92,6 @@ QList<QObject *> ClangCodeModelPlugin::createTestObjects() const
{
return {
new Tests::ClangCodeCompletionTest,
new Tests::ClangCompletionContextAnalyzerTest
};
}
#endif
......
INCLUDEPATH += $$PWD
SOURCES += $$PWD/completionchunkstotextconverter.cpp \
$$PWD/activationsequenceprocessor.cpp
$$PWD/activationsequenceprocessor.cpp \
$$PWD/activationsequencecontextprocessor.cpp \
$$PWD/clangcompletioncontextanalyzer.cpp
HEADERS += $$PWD/completionchunkstotextconverter.h \
$$PWD/activationsequenceprocessor.h
$$PWD/activationsequenceprocessor.h \
$$PWD/activationsequencecontextprocessor.h \
$$PWD/clangcompletioncontextanalyzer.h
......@@ -40,10 +40,12 @@ namespace TextEditor { class AssistInterface; }
namespace ClangCodeModel {
namespace Internal {
class ClangCompletionAssistInterface;
class ClangCompletionContextAnalyzer
{
public:
ClangCompletionContextAnalyzer(const TextEditor::AssistInterface *assistInterface,
ClangCompletionContextAnalyzer(const ClangCompletionAssistInterface *assistInterface,
CPlusPlus::LanguageFeatures languageFeatures);
void analyze();
......@@ -72,16 +74,21 @@ private:
int findStartOfName(int position = -1) const;
int skipPrecedingWhitespace(int position) const;
int startOfOperator(int position, unsigned *kind, bool wantFunctionCall) const;
void setActionAndClangPosition(CompletionAction action, int position);
void setAction(CompletionAction action);
bool handleNonFunctionCall(int position);
void handleCommaInFunctionCall();
void handleFunctionCall(int endOfOperator);
const TextEditor::AssistInterface * const m_interface; // Not owned
private:
const ClangCompletionAssistInterface *m_interface; // Not owned
const CPlusPlus::LanguageFeatures m_languageFeatures; // TODO: Get from assistInterface?!
// Results
CompletionAction m_completionAction = PassThroughToLibClang;
unsigned m_completionOperator = CPlusPlus::T_EOF_SYMBOL;
CPlusPlus::Kind m_completionOperator = CPlusPlus::T_EOF_SYMBOL;
int m_positionForProposal = -1;
int m_positionForClang = -1;
int m_positionEndOfExpression = -1;
......
......@@ -28,32 +28,32 @@
**
****************************************************************************/
#ifndef CLANGCOMPLETIONCONTEXTANALYZERTEST_H
#define CLANGCOMPLETIONCONTEXTANALYZERTEST_H
#ifndef CLANGCODEMODEL_INTERNAL_CLANGCOMPLETIONASSISTINTERFACE_H
#define CLANGCODEMODEL_INTERNAL_CLANGCOMPLETIONASSISTINTERFACE_H
#include <QObject>
#include <texteditor/codeassist/assistinterface.h>
#include <cplusplus/Token.h>
namespace ClangCodeModel {
namespace Internal {
namespace Tests {
class ClangCompletionContextAnalyzerTest : public QObject
class ClangCompletionAssistInterface: public TextEditor::AssistInterface
{
Q_OBJECT
private slots:
void testPassThroughToClangAndSignalSlotRecognition();
void testPassThroughToClangAndSignalSlotRecognition_data();
public:
ClangCompletionAssistInterface(const QByteArray &text,
int position)
: TextEditor::AssistInterface(text, position),
languageFeatures_(CPlusPlus::LanguageFeatures::defaultFeatures())
{}
void testSpecialCompletionRecognition();
void testSpecialCompletionRecognition_data();
CPlusPlus::LanguageFeatures languageFeatures() const { return languageFeatures_; }
void testAvoidSpecialCompletionRecognition();
void testAvoidSpecialCompletionRecognition_data();
private:
CPlusPlus::LanguageFeatures languageFeatures_;
};
} // namespace Tests
} // namespace Internal
} // namespace ClangCodeModel
#endif // CLANGCOMPLETIONCONTEXTANALYZERTEST_H
#endif // CLANGCODEMODEL_INTERNAL_CLANGCOMPLETIONASSISTINTERFACE_H
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef ASSISTENUMS_H
#define ASSISTENUMS_H
namespace TextEditor {
enum AssistKind
{
Completion,
QuickFix,
FollowSymbol
};
enum AssistReason
{
IdleEditor,
ActivationCharacter,
ExplicitlyInvoked
};
} // TextEditor
#endif // ASSISTENUMS_H
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef ASSISTINTERFACE_H
#define ASSISTINTERFACE_H
#include <QTextCursor>
#include <QTextDocument>
#include "../assistenums.h"
namespace TextEditor {
class AssistInterface
{
public:
AssistInterface(const QByteArray &text,
int position)
: textDocument_(QString::fromUtf8(text)),
position_(position)
{}
AssistInterface(QTextDocument *textDocument,
int position,
const QString &fileName,
AssistReason reason)
: textDocument_(textDocument),
fileName_(fileName),
position_(position),
reason_(reason)
{}
QTextDocument *textDocument() const;
virtual int position() const;
virtual QChar characterAt(int position) const;
virtual QString textAt(int position, int length) const;
virtual QString fileName() const;
virtual AssistReason reason() const;
private:
mutable QTextDocument textDocument_;
QString fileName_;
int position_;
AssistReason reason_ = IdleEditor;
};
inline QTextDocument *AssistInterface::textDocument() const
{
return &textDocument_;