C++: fixed completion in braceless statements following a control statement.

- Changed generator to generate:
- Changed AST::lastToken to return 1 if there is none
- Changed Bind.cpp to set the scope of the block following a control
  statement to extend upto (and not including) the first token after
  the block. So basically, including any whitespace after the control
  statement.

Reviewed-by: Roberto Raggi
This commit is contained in:
Erik Verbruggen
2010-09-09 15:18:17 +02:00
parent 6afb8d4077
commit cacb0db0eb
5 changed files with 183 additions and 181 deletions

View File

@@ -67,7 +67,9 @@ bool ASTPath::preVisit(AST *ast)
unsigned firstToken = ast->firstToken(); unsigned firstToken = ast->firstToken();
unsigned lastToken = ast->lastToken(); unsigned lastToken = ast->lastToken();
if (firstToken > 0 && lastToken > firstToken) { if (firstToken > 0) {
Q_ASSERT(lastToken > firstToken);
unsigned startLine, startColumn; unsigned startLine, startColumn;
getTokenStartPosition(firstToken, &startLine, &startColumn); getTokenStartPosition(firstToken, &startLine, &startColumn);

View File

@@ -184,10 +184,9 @@ int CppRefactoringFile::endOf(unsigned index) const
int CppRefactoringFile::endOf(const AST *ast) const int CppRefactoringFile::endOf(const AST *ast) const
{ {
if (unsigned end = ast->lastToken()) unsigned end = ast->lastToken();
return endOf(end - 1); Q_ASSERT(end > 0);
else return endOf(end - 1);
return 0;
} }
void CppRefactoringFile::startAndEndOf(unsigned index, int *start, int *end) const void CppRefactoringFile::startAndEndOf(unsigned index, int *start, int *end) const

File diff suppressed because it is too large Load Diff

View File

@@ -1224,7 +1224,7 @@ bool Bind::visit(ForeachStatementAST *ast)
Block *block = control()->newBlock(ast->firstToken()); Block *block = control()->newBlock(ast->firstToken());
const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken(); const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
block->setStartOffset(tokenAt(startScopeToken).end()); block->setStartOffset(tokenAt(startScopeToken).end());
block->setEndOffset(tokenAt(ast->lastToken() - 1).end()); block->setEndOffset(tokenAt(ast->lastToken()).begin());
_scope->addMember(block); _scope->addMember(block);
ast->symbol = block; ast->symbol = block;
@@ -1244,8 +1244,8 @@ bool Bind::visit(ForeachStatementAST *ast)
block->addMember(decl); block->addMember(decl);
} }
ExpressionTy initializer = this->expression(ast->initializer); /*ExpressionTy initializer =*/ this->expression(ast->initializer);
ExpressionTy expression = this->expression(ast->expression); /*ExpressionTy expression =*/ this->expression(ast->expression);
this->statement(ast->statement); this->statement(ast->statement);
(void) switchScope(previousScope); (void) switchScope(previousScope);
return false; return false;
@@ -1256,15 +1256,15 @@ bool Bind::visit(ForStatementAST *ast)
Block *block = control()->newBlock(ast->firstToken()); Block *block = control()->newBlock(ast->firstToken());
const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken(); const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
block->setStartOffset(tokenAt(startScopeToken).end()); block->setStartOffset(tokenAt(startScopeToken).end());
block->setEndOffset(tokenAt(ast->lastToken() - 1).end()); block->setEndOffset(tokenAt(ast->lastToken()).begin());
_scope->addMember(block); _scope->addMember(block);
ast->symbol = block; ast->symbol = block;
Scope *previousScope = switchScope(block); Scope *previousScope = switchScope(block);
this->statement(ast->initializer); this->statement(ast->initializer);
ExpressionTy condition = this->expression(ast->condition); /*ExpressionTy condition =*/ this->expression(ast->condition);
// unsigned semicolon_token = ast->semicolon_token; // unsigned semicolon_token = ast->semicolon_token;
ExpressionTy expression = this->expression(ast->expression); /*ExpressionTy expression =*/ this->expression(ast->expression);
// unsigned rparen_token = ast->rparen_token; // unsigned rparen_token = ast->rparen_token;
this->statement(ast->statement); this->statement(ast->statement);
(void) switchScope(previousScope); (void) switchScope(previousScope);
@@ -1276,12 +1276,12 @@ bool Bind::visit(IfStatementAST *ast)
Block *block = control()->newBlock(ast->firstToken()); Block *block = control()->newBlock(ast->firstToken());
const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken(); const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
block->setStartOffset(tokenAt(startScopeToken).end()); block->setStartOffset(tokenAt(startScopeToken).end());
block->setEndOffset(tokenAt(ast->lastToken() - 1).end()); block->setEndOffset(tokenAt(ast->lastToken()).begin());
_scope->addMember(block); _scope->addMember(block);
ast->symbol = block; ast->symbol = block;
Scope *previousScope = switchScope(block); Scope *previousScope = switchScope(block);
ExpressionTy condition = this->expression(ast->condition); /*ExpressionTy condition =*/ this->expression(ast->condition);
this->statement(ast->statement); this->statement(ast->statement);
this->statement(ast->else_statement); this->statement(ast->else_statement);
(void) switchScope(previousScope); (void) switchScope(previousScope);
@@ -1332,12 +1332,12 @@ bool Bind::visit(SwitchStatementAST *ast)
Block *block = control()->newBlock(ast->firstToken()); Block *block = control()->newBlock(ast->firstToken());
const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken(); const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
block->setStartOffset(tokenAt(startScopeToken).end()); block->setStartOffset(tokenAt(startScopeToken).end());
block->setEndOffset(tokenAt(ast->lastToken() - 1).end()); block->setEndOffset(tokenAt(ast->lastToken()).begin());
_scope->addMember(block); _scope->addMember(block);
ast->symbol = block; ast->symbol = block;
Scope *previousScope = switchScope(block); Scope *previousScope = switchScope(block);
ExpressionTy condition = this->expression(ast->condition); /*ExpressionTy condition =*/ this->expression(ast->condition);
this->statement(ast->statement); this->statement(ast->statement);
(void) switchScope(previousScope); (void) switchScope(previousScope);
return false; return false;
@@ -1358,7 +1358,7 @@ bool Bind::visit(CatchClauseAST *ast)
Block *block = control()->newBlock(ast->firstToken()); Block *block = control()->newBlock(ast->firstToken());
const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken(); const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
block->setStartOffset(tokenAt(startScopeToken).end()); block->setStartOffset(tokenAt(startScopeToken).end());
block->setEndOffset(tokenAt(ast->lastToken() - 1).end()); block->setEndOffset(tokenAt(ast->lastToken()).begin());
_scope->addMember(block); _scope->addMember(block);
ast->symbol = block; ast->symbol = block;
@@ -1375,12 +1375,12 @@ bool Bind::visit(WhileStatementAST *ast)
Block *block = control()->newBlock(ast->firstToken()); Block *block = control()->newBlock(ast->firstToken());
const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken(); const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
block->setStartOffset(tokenAt(startScopeToken).end()); block->setStartOffset(tokenAt(startScopeToken).end());
block->setEndOffset(tokenAt(ast->lastToken() - 1).end()); block->setEndOffset(tokenAt(ast->lastToken()).begin());
_scope->addMember(block); _scope->addMember(block);
ast->symbol = block; ast->symbol = block;
Scope *previousScope = switchScope(block); Scope *previousScope = switchScope(block);
ExpressionTy condition = this->expression(ast->condition); /*ExpressionTy condition =*/ this->expression(ast->condition);
this->statement(ast->statement); this->statement(ast->statement);
(void) switchScope(previousScope); (void) switchScope(previousScope);
return false; return false;
@@ -1391,7 +1391,7 @@ bool Bind::visit(ObjCFastEnumerationAST *ast)
Block *block = control()->newBlock(ast->firstToken()); Block *block = control()->newBlock(ast->firstToken());
const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken(); const unsigned startScopeToken = ast->lparen_token ? ast->lparen_token : ast->firstToken();
block->setStartOffset(tokenAt(startScopeToken).end()); block->setStartOffset(tokenAt(startScopeToken).end());
block->setEndOffset(tokenAt(ast->lastToken() - 1).end()); block->setEndOffset(tokenAt(ast->lastToken()).begin());
_scope->addMember(block); _scope->addMember(block);
ast->symbol = block; ast->symbol = block;
@@ -1410,8 +1410,8 @@ bool Bind::visit(ObjCFastEnumerationAST *ast)
block->addMember(decl); block->addMember(decl);
} }
ExpressionTy initializer = this->expression(ast->initializer); /*ExpressionTy initializer =*/ this->expression(ast->initializer);
ExpressionTy fast_enumeratable_expression = this->expression(ast->fast_enumeratable_expression); /*ExpressionTy fast_enumeratable_expression =*/ this->expression(ast->fast_enumeratable_expression);
this->statement(ast->statement); this->statement(ast->statement);
(void) switchScope(previousScope); (void) switchScope(previousScope);
return false; return false;
@@ -2825,10 +2825,10 @@ bool Bind::visit(ReferenceAST *ast)
// PostfixAST // PostfixAST
bool Bind::visit(CallAST *ast) bool Bind::visit(CallAST *ast)
{ {
ExpressionTy base_expression = this->expression(ast->base_expression); /*ExpressionTy base_expression =*/ this->expression(ast->base_expression);
// unsigned lparen_token = ast->lparen_token; // unsigned lparen_token = ast->lparen_token;
for (ExpressionListAST *it = ast->expression_list; it; it = it->next) { for (ExpressionListAST *it = ast->expression_list; it; it = it->next) {
ExpressionTy value = this->expression(it->value); /*ExpressionTy value =*/ this->expression(it->value);
} }
// unsigned rparen_token = ast->rparen_token; // unsigned rparen_token = ast->rparen_token;
return false; return false;
@@ -2836,9 +2836,9 @@ bool Bind::visit(CallAST *ast)
bool Bind::visit(ArrayAccessAST *ast) bool Bind::visit(ArrayAccessAST *ast)
{ {
ExpressionTy base_expression = this->expression(ast->base_expression); /*ExpressionTy base_expression =*/ this->expression(ast->base_expression);
// unsigned lbracket_token = ast->lbracket_token; // unsigned lbracket_token = ast->lbracket_token;
ExpressionTy expression = this->expression(ast->expression); /*ExpressionTy expression =*/ this->expression(ast->expression);
// unsigned rbracket_token = ast->rbracket_token; // unsigned rbracket_token = ast->rbracket_token;
return false; return false;
} }
@@ -2899,7 +2899,7 @@ bool Bind::visit(FunctionDeclaratorAST *ast)
this->trailingReturnType(ast->trailing_return_type, type); this->trailingReturnType(ast->trailing_return_type, type);
if (ast->as_cpp_initializer != 0) { if (ast->as_cpp_initializer != 0) {
fun->setAmbiguous(true); fun->setAmbiguous(true);
ExpressionTy as_cpp_initializer = this->expression(ast->as_cpp_initializer); /*ExpressionTy as_cpp_initializer =*/ this->expression(ast->as_cpp_initializer);
} }
ast->symbol = fun; ast->symbol = fun;
_type = type; _type = type;

View File

@@ -982,7 +982,7 @@ static QString createConstructor(ClassSpecifierAST *classAST)
bool checkGenerated(const QTextCursor &cursor, int *doxyStart) bool checkGenerated(const QTextCursor &cursor, int *doxyStart)
{ {
BackwardsScanner tokens(cursor); BackwardsScanner tokens(cursor, 10, QString(), false);
Token prevToken = tokens.LA(1); Token prevToken = tokens.LA(1);
if (prevToken.kind() != T_DOXY_COMMENT && prevToken.kind() != T_CPP_DOXY_COMMENT) if (prevToken.kind() != T_DOXY_COMMENT && prevToken.kind() != T_CPP_DOXY_COMMENT)
return false; return false;
@@ -1048,7 +1048,7 @@ void generateLastToken(QTextStream &os, const QString &className, const QStringL
} }
} }
os << " return 0;" << endl; os << " return 1;" << endl;
os << "}" << endl << endl; os << "}" << endl << endl;
} }
@@ -1122,6 +1122,7 @@ void generateAST_cpp(const Snapshot &snapshot, const QDir &cplusplusDir)
const int start = cpp_document.findBlockByNumber(line - 1).position() + column - 1; const int start = cpp_document.findBlockByNumber(line - 1).position() + column - 1;
cursor.setPosition(start); cursor.setPosition(start);
int doxyStart = start; int doxyStart = start;
const bool isGenerated = checkGenerated(cursor, &doxyStart); const bool isGenerated = checkGenerated(cursor, &doxyStart);
AST_cpp_document->translationUnit()->getTokenEndPosition(funDef->lastToken() - 1, &line, &column); AST_cpp_document->translationUnit()->getTokenEndPosition(funDef->lastToken() - 1, &line, &column);