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 <david.schulz@theqtcompany.com>
This commit is contained in:
Nikolai Kosjar
2016-03-29 13:53:55 +02:00
parent da5309cbc6
commit 4b3a987c39
3 changed files with 30 additions and 24 deletions

View File

@@ -335,16 +335,15 @@ int ClangCompletionAssistProcessor::startOfOperator(int positionInDocument,
&& !isDoxygenTagCompletionCharacter(characterBeforePositionInDocument)) && !isDoxygenTagCompletionCharacter(characterBeforePositionInDocument))
|| (tk.isLiteral() && (*kind != T_STRING_LITERAL || (tk.isLiteral() && (*kind != T_STRING_LITERAL
&& *kind != T_ANGLE_STRING_LITERAL && *kind != T_ANGLE_STRING_LITERAL
&& *kind != T_SLASH))) { && *kind != T_SLASH
&& *kind != T_DOT))) {
*kind = T_EOF_SYMBOL; *kind = T_EOF_SYMBOL;
start = positionInDocument; start = positionInDocument;
}
// Include completion: can be triggered by slash, but only in a string // 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; *kind = T_EOF_SYMBOL;
start = positionInDocument; start = positionInDocument;
} } else if (*kind == T_LPAREN) {
else if (*kind == T_LPAREN) {
if (tokenIdx > 0) { if (tokenIdx > 0) {
const Token &previousToken = tokens.at(tokenIdx - 1); // look at the token at the left of T_LPAREN const Token &previousToken = tokens.at(tokenIdx - 1); // look at the token at the left of T_LPAREN
switch (previousToken.kind()) { switch (previousToken.kind()) {
@@ -362,14 +361,15 @@ int ClangCompletionAssistProcessor::startOfOperator(int positionInDocument,
} }
} }
// Check for include preprocessor directive // 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; bool include = false;
if (tokens.size() >= 3) { if (tokens.size() >= 3) {
if (tokens.at(0).is(T_POUND) && tokens.at(1).is(T_IDENTIFIER) && (tokens.at(2).is(T_STRING_LITERAL) || 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))) { tokens.at(2).is(T_ANGLE_STRING_LITERAL))) {
const Token &directiveToken = tokens.at(1); const Token &directiveToken = tokens.at(1);
QString directive = tc.block().text().mid(directiveToken.bytesBegin(), QString directive = tc.block().text().mid(directiveToken.utf16charsBegin(),
directiveToken.bytes()); directiveToken.utf16chars());
if (directive == QLatin1String("include") || if (directive == QLatin1String("include") ||
directive == QLatin1String("include_next") || directive == QLatin1String("include_next") ||
directive == QLatin1String("import")) { directive == QLatin1String("import")) {

View File

@@ -941,19 +941,25 @@ IAssistProposal *InternalCppCompletionAssistProcessor::createHintProposal(
return proposal; return proposal;
} }
int InternalCppCompletionAssistProcessor::startOfOperator(int pos, int InternalCppCompletionAssistProcessor::startOfOperator(int positionInDocument,
unsigned *kind, unsigned *kind,
bool wantFunctionCall) const bool wantFunctionCall) const
{ {
const QChar ch = pos > -1 ? m_interface->characterAt(pos - 1) : QChar(); const QChar ch = positionInDocument > -1
const QChar ch2 = pos > 0 ? m_interface->characterAt(pos - 2) : QChar(); ? m_interface->characterAt(positionInDocument - 1)
const QChar ch3 = pos > 1 ? m_interface->characterAt(pos - 3) : QChar(); : 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); wantFunctionCall, /*wantQt5SignalSlots*/ true);
if (start != pos) { if (start != positionInDocument) {
QTextCursor tc(m_interface->textDocument()); 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 // Include completion: make sure the quote character is the first one on the line
if (*kind == T_STRING_LITERAL) { if (*kind == T_STRING_LITERAL) {
@@ -962,7 +968,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos,
QString sel = s.selectedText(); QString sel = s.selectedText();
if (sel.indexOf(QLatin1Char('"')) < sel.length() - 1) { if (sel.indexOf(QLatin1Char('"')) < sel.length() - 1) {
*kind = T_EOF_SYMBOL; *kind = T_EOF_SYMBOL;
start = pos; start = positionInDocument;
} }
} }
@@ -970,7 +976,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos,
ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures()); ExpressionUnderCursor expressionUnderCursor(m_interface->languageFeatures());
if (expressionUnderCursor.startOfFunctionCall(tc) == -1) { if (expressionUnderCursor.startOfFunctionCall(tc) == -1) {
*kind = T_EOF_SYMBOL; *kind = T_EOF_SYMBOL;
start = pos; start = positionInDocument;
} }
} }
@@ -984,10 +990,10 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos,
if (*kind == T_AMPER && tokenIdx > 0) { if (*kind == T_AMPER && tokenIdx > 0) {
const Token &previousToken = tokens.at(tokenIdx - 1); const Token &previousToken = tokens.at(tokenIdx - 1);
if (previousToken.kind() == T_COMMA) 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))) { } else if (*kind == T_DOXY_COMMENT && !(tk.is(T_DOXY_COMMENT) || tk.is(T_CPP_DOXY_COMMENT))) {
*kind = T_EOF_SYMBOL; *kind = T_EOF_SYMBOL;
start = pos; start = positionInDocument;
} }
// Don't complete in comments or strings, but still check for include completion // Don't complete in comments or strings, but still check for include completion
else if (tk.is(T_COMMENT) || tk.is(T_CPP_COMMENT) 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_SLASH
&& *kind != T_DOT))) { && *kind != T_DOT))) {
*kind = T_EOF_SYMBOL; *kind = T_EOF_SYMBOL;
start = pos; start = positionInDocument;
// Include completion: can be triggered by slash, but only in a string // 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; *kind = T_EOF_SYMBOL;
start = pos; start = positionInDocument;
} else if (*kind == T_LPAREN) { } else if (*kind == T_LPAREN) {
if (tokenIdx > 0) { if (tokenIdx > 0) {
const Token &previousToken = tokens.at(tokenIdx - 1); // look at the token at the left of T_LPAREN 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: default:
// that's a bad token :) // that's a bad token :)
*kind = T_EOF_SYMBOL; *kind = T_EOF_SYMBOL;
start = pos; start = positionInDocument;
} }
} }
} }
@@ -1040,7 +1046,7 @@ int InternalCppCompletionAssistProcessor::startOfOperator(int pos,
if (!include) { if (!include) {
*kind = T_EOF_SYMBOL; *kind = T_EOF_SYMBOL;
start = pos; start = positionInDocument;
} else { } else {
if (*kind == T_DOT) { if (*kind == T_DOT) {
start = findStartOfName(start); start = findStartOfName(start);

View File

@@ -106,7 +106,7 @@ private:
TextEditor::IAssistProposal *createHintProposal(QList<CPlusPlus::Function *> symbols) const; TextEditor::IAssistProposal *createHintProposal(QList<CPlusPlus::Function *> symbols) const;
bool accepts() 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 findStartOfName(int pos = -1) const;
int startCompletionHelper(); int startCompletionHelper();
bool tryObjCCompletion(); bool tryObjCCompletion();