Commit d63624af authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh
Browse files

C++: Accept language features in BackwardsScanner et al



Change-Id: Id97ca27fa909979573efca12dc0cd14b28eacd17
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent 1a37605f
......@@ -38,6 +38,7 @@
using namespace CPlusPlus;
BackwardsScanner::BackwardsScanner(const QTextCursor &cursor,
const LanguageFeatures &languageFeatures,
int maxBlockCount,
const QString &suffix,
bool skipComments)
......@@ -46,13 +47,7 @@ BackwardsScanner::BackwardsScanner(const QTextCursor &cursor,
, _block(cursor.block())
, _maxBlockCount(maxBlockCount)
{
// FIXME: Why these defaults?
LanguageFeatures features;
features.qtMocRunEnabled = true;
features.qtEnabled = true;
features.qtKeywordsEnabled = true;
features.objCEnabled = true;
_tokenize.setLanguageFeatures(features);
_tokenize.setLanguageFeatures(languageFeatures);
_tokenize.setSkipComments(skipComments);
_text = _block.text().left(cursor.position() - cursor.block().position());
......
......@@ -43,10 +43,11 @@ class CPLUSPLUS_EXPORT BackwardsScanner
enum { MAX_BLOCK_COUNT = 10 };
public:
explicit BackwardsScanner(const QTextCursor &cursor,
int maxBlockCount = MAX_BLOCK_COUNT,
const QString &suffix = QString(),
bool skipComments = true);
BackwardsScanner(const QTextCursor &cursor,
const LanguageFeatures &languageFeatures,
int maxBlockCount = MAX_BLOCK_COUNT,
const QString &suffix = QString(),
bool skipComments = true);
int startToken() const;
......
......@@ -40,8 +40,9 @@
using namespace CPlusPlus;
ExpressionUnderCursor::ExpressionUnderCursor()
ExpressionUnderCursor::ExpressionUnderCursor(const LanguageFeatures &languageFeatures)
: _jumpedComma(false)
, _languageFeatures(languageFeatures)
{ }
int ExpressionUnderCursor::startOfExpression(BackwardsScanner &tk, int index)
......@@ -243,7 +244,7 @@ bool ExpressionUnderCursor::isAccessToken(const Token &tk)
QString ExpressionUnderCursor::operator()(const QTextCursor &cursor)
{
BackwardsScanner scanner(cursor);
BackwardsScanner scanner(cursor, _languageFeatures);
_jumpedComma = false;
......@@ -257,7 +258,7 @@ QString ExpressionUnderCursor::operator()(const QTextCursor &cursor)
int ExpressionUnderCursor::startOfFunctionCall(const QTextCursor &cursor) const
{
BackwardsScanner scanner(cursor);
BackwardsScanner scanner(cursor, _languageFeatures);
int index = scanner.startToken();
......
......@@ -32,6 +32,7 @@
#define CPLUSPLUS_EXPRESSIONUNDERCURSOR_H
#include <cplusplus/CPlusPlusForwardDeclarations.h>
#include <cplusplus/Token.h>
#include <QList>
......@@ -47,7 +48,7 @@ class BackwardsScanner;
class CPLUSPLUS_EXPORT ExpressionUnderCursor
{
public:
ExpressionUnderCursor();
ExpressionUnderCursor(const LanguageFeatures &languageFeatures);
QString operator()(const QTextCursor &cursor);
int startOfFunctionCall(const QTextCursor &cursor) const;
......@@ -59,6 +60,7 @@ private:
private:
bool _jumpedComma;
LanguageFeatures _languageFeatures;
};
} // namespace CPlusPlus
......
......@@ -154,7 +154,8 @@ QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QStri
if (text.isEmpty() || !shouldInsertMatchingText(la))
return QString();
BackwardsScanner tk(tc, MAX_NUM_LINES, textToProcess.left(*skippedChars));
BackwardsScanner tk(tc, LanguageFeatures::defaultFeatures(), MAX_NUM_LINES,
textToProcess.left(*skippedChars));
const int startToken = tk.startToken();
int index = startToken;
......@@ -215,7 +216,7 @@ static bool shouldInsertNewline(const QTextCursor &tc)
QString MatchingText::insertParagraphSeparator(const QTextCursor &tc)
{
BackwardsScanner tk(tc, MAX_NUM_LINES);
BackwardsScanner tk(tc, LanguageFeatures::defaultFeatures(), MAX_NUM_LINES);
int index = tk.startToken();
if (tk[index - 1].isNot(T_LBRACE))
......
......@@ -79,7 +79,7 @@ CPlusPlus::Symbol *AnalyzerUtils::findSymbolUnderCursor()
QTC_ASSERT(doc, return 0);
// fetch the expression's code
CPlusPlus::ExpressionUnderCursor expressionUnderCursor;
CPlusPlus::ExpressionUnderCursor expressionUnderCursor(doc->languageFeatures());
moveCursorToEndOfName(&tc);
const QString &expression = expressionUnderCursor(tc);
CPlusPlus::Scope *scope = doc->scopeAt(line, column);
......
......@@ -639,7 +639,7 @@ int ClangCompletionAssistProcessor::startCompletionHelper()
return m_startPosition;
}
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures());
QTextCursor tc(m_interface->textDocument());
if (m_model->m_completionOperator == T_COMMA) {
......@@ -705,7 +705,7 @@ int ClangCompletionAssistProcessor::startOfOperator(int pos,
}
if (*kind == T_COMMA) {
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures());
if (expressionUnderCursor.startOfFunctionCall(tc) == -1) {
*kind = T_EOF_SYMBOL;
start = pos;
......@@ -931,7 +931,7 @@ int ClangCompletionAssistProcessor::startCompletionInternal(const QString fileNa
QTextCursor tc(m_interface->textDocument());
tc.setPosition(index);
ExpressionUnderCursor euc;
ExpressionUnderCursor euc(m_interface->languageFeatures());
index = euc.startOfFunctionCall(tc);
int nameStart = findStartOfName(index);
QTextCursor tc2(m_interface->textDocument());
......
......@@ -77,7 +77,7 @@ Scope *CanonicalSymbol::getScopeAndExpression(const QTextCursor &cursor, QString
++pos;
tc.setPosition(pos);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(m_document->languageFeatures());
*code = expressionUnderCursor(tc);
return m_document->scopeAt(line, column);
}
......
......@@ -102,7 +102,7 @@ void CppElementEvaluator::execute()
CppTools::moveCursorToEndOfIdentifier(&m_tc);
// Fetch the expression's code
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(doc->languageFeatures());
const QString &expression = expressionUnderCursor(m_tc);
Scope *scope = doc->scopeAt(line, column);
......
......@@ -615,7 +615,7 @@ TextEditorWidget::Link FollowSymbolUnderCursor::findLink(const QTextCursor &curs
return link;
// Evaluate the type of the expression under the cursor
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(features);
QString expression = expressionUnderCursor(tc);
for (int pos = tc.position();; ++pos) {
......
......@@ -168,7 +168,7 @@ static bool isDereferenced(TextEditorWidget *editorWidget, int basePosition)
QTextCursor cursor = editorWidget->textCursor();
cursor.setPosition(basePosition);
BackwardsScanner scanner(cursor);
BackwardsScanner scanner(cursor, LanguageFeatures());
for (int pos = scanner.startToken()-1; pos >= 0; pos--) {
switch (scanner[pos].kind()) {
case T_COLON_COLON:
......@@ -670,7 +670,7 @@ private:
QTextCursor cursor(m_assistInterface->textDocument());
cursor.setPosition(m_position + 1);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(m_assistInterface->languageFeatures());
const QString expression = expressionUnderCursor(cursor);
if (expression.isEmpty())
return false;
......@@ -951,7 +951,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos,
}
if (*kind == T_COMMA) {
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures());
if (expressionUnderCursor.startOfFunctionCall(tc) == -1) {
*kind = T_EOF_SYMBOL;
start = pos;
......@@ -1099,7 +1099,7 @@ int InternalCppCompletionAssistProcessor::startCompletionHelper()
return m_startPosition;
}
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures());
QTextCursor tc(m_interface->textDocument());
if (m_model->m_completionOperator == T_COMMA) {
......@@ -1173,7 +1173,7 @@ bool InternalCppCompletionAssistProcessor::tryObjCCompletion()
QTextCursor tc(m_interface->textDocument());
tc.setPosition(end);
BackwardsScanner tokens(tc);
BackwardsScanner tokens(tc, m_interface->languageFeatures());
if (tokens[tokens.startToken() - 1].isNot(T_RBRACKET))
return false;
......@@ -1439,7 +1439,7 @@ int InternalCppCompletionAssistProcessor::startCompletionInternal(const QString
QTextCursor tc(m_interface->textDocument());
tc.setPosition(index);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures());
const QString baseExpression = expressionUnderCursor(tc);
// Resolve the type of this expression
......@@ -2086,7 +2086,7 @@ bool InternalCppCompletionAssistProcessor::completeConstructorOrFunction(const Q
QTextCursor tc(m_interface->textDocument());
tc.setPosition(endOfExpression);
BackwardsScanner bs(tc);
BackwardsScanner bs(tc, m_interface->languageFeatures());
const int startToken = bs.startToken();
int lineStartToken = bs.startOfLine(startToken);
// make sure the required tokens are actually available
......
......@@ -286,6 +286,9 @@ QString cppExpressionAt(TextEditorWidget *editorWidget, int pos,
if (function)
function->clear();
const QString fileName = editorWidget->textDocument()->filePath().toString();
const Snapshot snapshot = CppModelManager::instance()->snapshot();
const Document::Ptr document = snapshot.document(fileName);
QTextCursor tc = editorWidget->textCursor();
QString expr = tc.selectedText();
if (expr.isEmpty()) {
......@@ -295,21 +298,18 @@ QString cppExpressionAt(TextEditorWidget *editorWidget, int pos,
tc.movePosition(QTextCursor::EndOfWord);
// Fetch the expression's code.
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(document ? document->languageFeatures()
: LanguageFeatures::defaultFeatures());
expr = expressionUnderCursor(tc);
}
*column = tc.positionInBlock();
*line = tc.blockNumber() + 1;
if (!expr.isEmpty()) {
QString fileName = editorWidget->textDocument()->filePath().toString();
const Snapshot snapshot = CppModelManager::instance()->snapshot();
if (const Document::Ptr document = snapshot.document(fileName)) {
QString func = document->functionAt(*line, *column, scopeFromLine, scopeToLine);
if (function)
*function = func;
}
if (!expr.isEmpty() && document) {
QString func = document->functionAt(*line, *column, scopeFromLine, scopeToLine);
if (function)
*function = func;
}
return expr;
......
......@@ -263,7 +263,8 @@ IAssistProposal *GlslCompletionAssistProcessor::perform(const AssistInterface *i
while (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
ch = m_interface->characterAt(--pos);
CPlusPlus::ExpressionUnderCursor expressionUnderCursor;
CPlusPlus::ExpressionUnderCursor expressionUnderCursor(
CPlusPlus::LanguageFeatures::defaultFeatures());
//GLSLTextEditorWidget *edit = qobject_cast<GLSLTextEditorWidget *>(editor->widget());
QList<GLSL::Symbol *> members;
......
......@@ -987,7 +987,7 @@ static QString createConstructor(ClassSpecifierAST *classAST)
bool checkGenerated(const QTextCursor &cursor, int *doxyStart)
{
BackwardsScanner tokens(cursor, 10, QString(), false);
BackwardsScanner tokens(cursor, LanguageFeatures::defaultFeatures(), 10, QString(), false);
Token prevToken = tokens.LA(1);
if (prevToken.kind() != T_DOXY_COMMENT && prevToken.kind() != T_CPP_DOXY_COMMENT)
return false;
......
......@@ -532,7 +532,7 @@ void tst_Semantic::expression_under_cursor_1()
QTextCursor tc(&textDocument);
tc.movePosition(QTextCursor::End);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(LanguageFeatures::defaultFeatures());
const QString expression = expressionUnderCursor(tc);
QCOMPARE(expression, QString("bar"));
......@@ -548,7 +548,7 @@ void tst_Semantic::bracketed_expression_under_cursor_1()
QTextCursor tc(&textDocument);
tc.movePosition(QTextCursor::End);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(LanguageFeatures::defaultFeatures());
const QString expression = expressionUnderCursor(tc);
QCOMPARE(expression, QString("i"));
......@@ -564,7 +564,7 @@ void tst_Semantic::bracketed_expression_under_cursor_2()
QTextCursor tc(&textDocument);
tc.movePosition(QTextCursor::End);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(LanguageFeatures::defaultFeatures());
const QString expression = expressionUnderCursor(tc);
QCOMPARE(expression, plainText);
......@@ -580,7 +580,7 @@ void tst_Semantic::bracketed_expression_under_cursor_3()
QTextCursor tc(&textDocument);
tc.movePosition(QTextCursor::End);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(LanguageFeatures::defaultFeatures());
const QString expression = expressionUnderCursor(tc);
QCOMPARE(expression, QString("[receiver message"));
......@@ -596,7 +596,7 @@ void tst_Semantic::bracketed_expression_under_cursor_4()
QTextCursor tc(&textDocument);
tc.movePosition(QTextCursor::End);
ExpressionUnderCursor expressionUnderCursor;
ExpressionUnderCursor expressionUnderCursor(LanguageFeatures::defaultFeatures());
const QString expression = expressionUnderCursor(tc);
QCOMPARE(expression, QString("i"));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment