1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 395562 - Follow-up to fix a regression where completing the method name in an out-of-line method definition would no longer insert parentheses

Change-Id: I8bbf083e874f6d01aa85c2ba4173685228160963
This commit is contained in:
Nathan Ridge 2017-01-24 17:20:52 -05:00 committed by Gerrit Code Review @ Eclipse.org
parent add2a14628
commit a90caec05e
2 changed files with 34 additions and 5 deletions

View file

@ -720,6 +720,16 @@ public class CompletionTests extends CompletionTestBase {
assertMinimumCompletionResults(fCursorOffset, expected, REPLACEMENT);
}
// struct Waldo {
// void find();
// };
// void Waldo::f/*cursor*/
public void testRegression_395562() throws Exception {
// Should include parentheses in replacement string when completing
// out-of-line method definition.
assertCompletionResults(new String[] { "find()" });
}
// typedef struct {
// int sx;
// } my_struct;
@ -1236,7 +1246,7 @@ public class CompletionTests extends CompletionTestBase {
public void testDestructorDefinition_456293() throws Exception {
final String[] expectedDisplay = { "~Waldo(void)" };
assertCompletionResults(fCursorOffset, expectedDisplay, DISPLAY);
final String[] expectedReplacement = { "Waldo" };
final String[] expectedReplacement = { "Waldo()" };
assertCompletionResults(fCursorOffset, expectedReplacement, REPLACEMENT);
}

View file

@ -529,7 +529,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
return relevance;
}
// Returns whether a function name being completed could be a call that function.
// Returns whether a function name being completed could be a call to that function.
private boolean canBeCall(IFunction function, IASTCompletionContext astContext,
CContentAssistInvocationContext cContext) {
// Can't have a call in a using-directive.
@ -548,6 +548,23 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
return true;
}
// Returns whether a function name being completed could be a definition of that function.
private boolean canBeDefinition(IFunction function, IASTCompletionContext astContext) {
if (!(astContext instanceof IASTName)) {
return true;
}
// If content assist is invoked while completing the destructor name in an
// out-of-line destructor definition, the parser doesn't have enough information
// to recognize that this is a function definition, and so getRoleOfName() will
// incorrectly return r_reference. Since destructors are rarely referred to
// explicitly, just assume destructor name is a definition.
if (function instanceof ICPPMethod && ((ICPPMethod) function).isDestructor()) {
return true;
}
IASTName name = (IASTName) astContext;
return name.getRoleOfName(false) == IASTNameOwner.r_definition;
}
private String getFunctionNameForReplacement(IFunction function, IASTCompletionContext astContext) {
// If we are completiong a destructor name ...
if (function instanceof ICPPMethod && ((ICPPMethod) function).isDestructor()) {
@ -574,6 +591,8 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
repStringBuff.append(getFunctionNameForReplacement(function, astContext));
boolean canBeCall = canBeCall(function, astContext, cContext);
boolean canBeDefinition = canBeDefinition(function, astContext);
boolean wantParens = canBeCall || canBeDefinition;
StringBuilder dispArgs = new StringBuilder(); // For the dispArgString
StringBuilder idArgs = new StringBuilder(); // For the idArgString
@ -652,8 +671,8 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
boolean inUsingDeclaration = cContext.isInUsingDirective();
if (canBeCall && !cContext.isFollowedByOpeningParen()) {
// If we might be calling the function in this context, assume we are
if (wantParens && !cContext.isFollowedByOpeningParen()) {
// If we might be calling or defining the function in this context, assume we are
// (since that's the most common case) and emit parentheses.
repStringBuff.append('(');
repStringBuff.append(')');