From 4b3a987c39cb92f01033255b0da51d534eb9d2cd Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Tue, 29 Mar 2016 13:53:55 +0200 Subject: [PATCH] C++: Equalize startOfOperator() There are two versions of startOfOperator: * InternalCppCompletionAssistProcessor::startOfOperator * ClangCompletionAssistProcessor::startOfOperator The latter started as a copy of the former, but the former got some bug fixes in the meantime. Adjust both versions to each other, so it's easy to diff them and to extract the duplication in a follow-up change. Change-Id: Icf48386bf1ad0fa473bec476c5412be9b1890139 Reviewed-by: David Schulz --- .../clangcompletionassistprocessor.cpp | 16 ++++----- src/plugins/cpptools/cppcompletionassist.cpp | 36 +++++++++++-------- src/plugins/cpptools/cppcompletionassist.h | 2 +- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp b/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp index 9fa20e4fab9..3887489d3db 100644 --- a/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp +++ b/src/plugins/clangcodemodel/clangcompletionassistprocessor.cpp @@ -335,16 +335,15 @@ int ClangCompletionAssistProcessor::startOfOperator(int positionInDocument, && !isDoxygenTagCompletionCharacter(characterBeforePositionInDocument)) || (tk.isLiteral() && (*kind != T_STRING_LITERAL && *kind != T_ANGLE_STRING_LITERAL - && *kind != T_SLASH))) { + && *kind != T_SLASH + && *kind != T_DOT))) { *kind = T_EOF_SYMBOL; start = positionInDocument; - } // Include completion: can be triggered by slash, but only in a string - else if (*kind == T_SLASH && (tk.isNot(T_STRING_LITERAL) && tk.isNot(T_ANGLE_STRING_LITERAL))) { + } else if (*kind == T_SLASH && (tk.isNot(T_STRING_LITERAL) && tk.isNot(T_ANGLE_STRING_LITERAL))) { *kind = T_EOF_SYMBOL; start = positionInDocument; - } - else if (*kind == T_LPAREN) { + } else if (*kind == T_LPAREN) { if (tokenIdx > 0) { const Token &previousToken = tokens.at(tokenIdx - 1); // look at the token at the left of T_LPAREN switch (previousToken.kind()) { @@ -362,14 +361,15 @@ int ClangCompletionAssistProcessor::startOfOperator(int positionInDocument, } } // Check for include preprocessor directive - else if (*kind == T_STRING_LITERAL || *kind == T_ANGLE_STRING_LITERAL || *kind == T_SLASH) { + else if (*kind == T_STRING_LITERAL || *kind == T_ANGLE_STRING_LITERAL|| *kind == T_SLASH + || (*kind == T_DOT && (tk.is(T_STRING_LITERAL) || tk.is(T_ANGLE_STRING_LITERAL)))) { bool include = false; if (tokens.size() >= 3) { if (tokens.at(0).is(T_POUND) && tokens.at(1).is(T_IDENTIFIER) && (tokens.at(2).is(T_STRING_LITERAL) || tokens.at(2).is(T_ANGLE_STRING_LITERAL))) { const Token &directiveToken = tokens.at(1); - QString directive = tc.block().text().mid(directiveToken.bytesBegin(), - directiveToken.bytes()); + QString directive = tc.block().text().mid(directiveToken.utf16charsBegin(), + directiveToken.utf16chars()); if (directive == QLatin1String("include") || directive == QLatin1String("include_next") || directive == QLatin1String("import")) { diff --git a/src/plugins/cpptools/cppcompletionassist.cpp b/src/plugins/cpptools/cppcompletionassist.cpp index 98a433015b3..a0baaf6aa63 100644 --- a/src/plugins/cpptools/cppcompletionassist.cpp +++ b/src/plugins/cpptools/cppcompletionassist.cpp @@ -941,19 +941,25 @@ IAssistProposal *InternalCppCompletionAssistProcessor::createHintProposal( return proposal; } -int InternalCppCompletionAssistProcessor::startOfOperator(int pos, +int InternalCppCompletionAssistProcessor::startOfOperator(int positionInDocument, unsigned *kind, bool wantFunctionCall) const { - const QChar ch = pos > -1 ? m_interface->characterAt(pos - 1) : QChar(); - const QChar ch2 = pos > 0 ? m_interface->characterAt(pos - 2) : QChar(); - const QChar ch3 = pos > 1 ? m_interface->characterAt(pos - 3) : QChar(); + const QChar ch = positionInDocument > -1 + ? m_interface->characterAt(positionInDocument - 1) + : QChar(); + const QChar ch2 = positionInDocument > 0 + ? m_interface->characterAt(positionInDocument - 2) + : QChar(); + const QChar ch3 = positionInDocument > 1 + ? m_interface->characterAt(positionInDocument - 3) + : QChar(); - int start = pos - CppCompletionAssistProvider::activationSequenceChar(ch, ch2, ch3, kind, + int start = positionInDocument - CppCompletionAssistProvider::activationSequenceChar(ch, ch2, ch3, kind, wantFunctionCall, /*wantQt5SignalSlots*/ true); - if (start != pos) { + if (start != positionInDocument) { QTextCursor tc(m_interface->textDocument()); - tc.setPosition(pos); + tc.setPosition(positionInDocument); // Include completion: make sure the quote character is the first one on the line if (*kind == T_STRING_LITERAL) { @@ -962,7 +968,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos, QString sel = s.selectedText(); if (sel.indexOf(QLatin1Char('"')) < sel.length() - 1) { *kind = T_EOF_SYMBOL; - start = pos; + start = positionInDocument; } } @@ -970,7 +976,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos, ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures()); if (expressionUnderCursor.startOfFunctionCall(tc) == -1) { *kind = T_EOF_SYMBOL; - start = pos; + start = positionInDocument; } } @@ -984,10 +990,10 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos, if (*kind == T_AMPER && tokenIdx > 0) { const Token &previousToken = tokens.at(tokenIdx - 1); if (previousToken.kind() == T_COMMA) - start = pos - (tk.utf16charOffset - previousToken.utf16charOffset) - 1; + start = positionInDocument - (tk.utf16charOffset - previousToken.utf16charOffset) - 1; } else if (*kind == T_DOXY_COMMENT && !(tk.is(T_DOXY_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))) { *kind = T_EOF_SYMBOL; - start = pos; + start = positionInDocument; } // Don't complete in comments or strings, but still check for include completion else if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT) @@ -998,11 +1004,11 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos, && *kind != T_SLASH && *kind != T_DOT))) { *kind = T_EOF_SYMBOL; - start = pos; + start = positionInDocument; // Include completion: can be triggered by slash, but only in a string } else if (*kind == T_SLASH && (tk.isNot(T_STRING_LITERAL) && tk.isNot(T_ANGLE_STRING_LITERAL))) { *kind = T_EOF_SYMBOL; - start = pos; + start = positionInDocument; } else if (*kind == T_LPAREN) { if (tokenIdx > 0) { const Token &previousToken = tokens.at(tokenIdx - 1); // look at the token at the left of T_LPAREN @@ -1016,7 +1022,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos, default: // that's a bad token :) *kind = T_EOF_SYMBOL; - start = pos; + start = positionInDocument; } } } @@ -1040,7 +1046,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos, if (!include) { *kind = T_EOF_SYMBOL; - start = pos; + start = positionInDocument; } else { if (*kind == T_DOT) { start = findStartOfName(start); diff --git a/src/plugins/cpptools/cppcompletionassist.h b/src/plugins/cpptools/cppcompletionassist.h index 738840cd15f..78727590814 100644 --- a/src/plugins/cpptools/cppcompletionassist.h +++ b/src/plugins/cpptools/cppcompletionassist.h @@ -106,7 +106,7 @@ private: TextEditor::IAssistProposal *createHintProposal(QList symbols) const; bool accepts() const; - int startOfOperator(int pos, unsigned *kind, bool wantFunctionCall) const; + int startOfOperator(int positionInDocument, unsigned *kind, bool wantFunctionCall) const; int findStartOfName(int pos = -1) const; int startCompletionHelper(); bool tryObjCCompletion();