- fix: Code completion info for stl::map/std::unordered_map is not correct.

This commit is contained in:
Roy Qu 2023-03-11 19:56:40 +08:00
parent b220df600b
commit 0b0e941155
3 changed files with 122 additions and 42 deletions

View File

@ -4,6 +4,7 @@ Red Panda C++ Version 2.18
- enhancement: Code completion for embedded stl containers. - enhancement: Code completion for embedded stl containers.
- enhancement: Slightly speed up code parsing. - enhancement: Slightly speed up code parsing.
- enhancement: Sort header completion infos by suffix-trimmed filename. - enhancement: Sort header completion infos by suffix-trimmed filename.
- fix: Code completion info for stl::map/std::unordered_map is not correct.
Red Panda C++ Version 2.17 Red Panda C++ Version 2.17

View File

@ -399,6 +399,16 @@ PStatement CppParser::doFindStatementOf(const QString &fileName,
lastScopeParent ); lastScopeParent );
typeStatement=doFindTypeDefinitionOf(fileName, typeName, typeStatement=doFindTypeDefinitionOf(fileName, typeName,
lastScopeParent ); lastScopeParent );
} else if (parentScope
&& STLMaps.contains(parentScope->fullName)
&& STLElementMethods.contains(statement->command)
&& lastScopeStatement) {
isSTLContainerFunctions = true;
PStatement lastScopeParent = lastScopeStatement->parentScope.lock();
typeName=doFindTemplateParamOf(fileName,lastScopeStatement->type,1,
lastScopeParent );
typeStatement=doFindTypeDefinitionOf(fileName, typeName,
lastScopeParent );
} }
} }
if (!isSTLContainerFunctions) if (!isSTLContainerFunctions)
@ -420,6 +430,15 @@ PStatement CppParser::doFindStatementOf(const QString &fileName,
parentScope); parentScope);
typeStatement = doFindTypeDefinitionOf(fileName, typeName, typeStatement = doFindTypeDefinitionOf(fileName, typeName,
parentScope); parentScope);
} else if ((typeStatement)
&& STLMaps.contains(typeStatement->fullName)
&& nextScopeWord.endsWith(']')) {
//it's a std container
PStatement parentScope = statement->parentScope.lock();
typeName = doFindFirstTemplateParamOf(fileName,statement->type,
parentScope);
typeStatement = doFindTypeDefinitionOf(fileName, typeName,
parentScope);
} }
lastScopeStatement = statement; lastScopeStatement = statement;
if (typeStatement) if (typeStatement)
@ -3645,11 +3664,10 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
getCurrentScope(), getCurrentScope(),
PEvalStatement(), PEvalStatement(),
true); true);
if(aliasStatement && aliasStatement->effectiveTypeStatement if(aliasStatement && aliasStatement->effectiveTypeStatement) {
&& STLContainers.contains(aliasStatement->effectiveTypeStatement->fullName)) {
if (STLMaps.contains(aliasStatement->effectiveTypeStatement->fullName)) { if (STLMaps.contains(aliasStatement->effectiveTypeStatement->fullName)) {
addedVar->type = "std::pair"+aliasStatement->templateParams; addedVar->type = "std::pair"+aliasStatement->templateParams;
} else { } else if (STLContainers.contains(aliasStatement->effectiveTypeStatement->fullName)){
QString type=doFindFirstTemplateParamOf(mCurrentFile,aliasStatement->templateParams, QString type=doFindFirstTemplateParamOf(mCurrentFile,aliasStatement->templateParams,
getCurrentScope()); getCurrentScope());
if (!type.isEmpty()) if (!type.isEmpty())
@ -3726,6 +3744,9 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
if (parentStatement if (parentStatement
&& STLContainers.contains(parentStatement->fullName)) { && STLContainers.contains(parentStatement->fullName)) {
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command; addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
} else if (parentStatement
&& STLMaps.contains(parentStatement->fullName)) {
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
} }
} }
} else } else
@ -3822,6 +3843,9 @@ void CppParser::handleVar(const QString& typePrefix,bool isExtern,bool isStatic)
if (parentStatement if (parentStatement
&& STLContainers.contains(parentStatement->fullName)) { && STLContainers.contains(parentStatement->fullName)) {
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command; addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
} else if (parentStatement
&& STLMaps.contains(parentStatement->fullName)) {
addedVar->type = parentStatement->fullName+aliasStatement->templateParams+"::"+aliasStatement->typeStatement->command;
} }
} }
} else } else
@ -4250,6 +4274,17 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
PStatement parentScope = result->typeStatement->parentScope.lock(); PStatement parentScope = result->typeStatement->parentScope.lock();
if (STLContainers.contains(parentScope->fullName)) { if (STLContainers.contains(parentScope->fullName)) {
QString typeName=doFindFirstTemplateParamOf(fileName,result->templateParams, parentScope); QString typeName=doFindFirstTemplateParamOf(fileName,result->templateParams, parentScope);
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->definitionString=typeName;
result->kind = EvalStatementKind::Variable;
} else {
result = PEvalStatement();
}
} else if (STLMaps.contains(parentScope->fullName)) {
QString typeName=doFindTemplateParamOf(fileName,result->templateParams,1,parentScope);
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command; // qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope); PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) { if (typeStatement) {
@ -4260,6 +4295,7 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
result = PEvalStatement(); result = PEvalStatement();
} }
} }
} else { } else {
PStatement typeStatement = result->effectiveTypeStatement; PStatement typeStatement = result->effectiveTypeStatement;
if ((typeStatement) if ((typeStatement)
@ -4409,28 +4445,49 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
if (result->baseStatement && lastResult) { if (result->baseStatement && lastResult) {
PStatement parentScope = result->baseStatement->parentScope.lock(); PStatement parentScope = result->baseStatement->parentScope.lock();
if (parentScope if (parentScope
&& STLContainers.contains(parentScope->fullName)
&& STLElementMethods.contains(result->baseStatement->command) && STLElementMethods.contains(result->baseStatement->command)
) { ) {
//stl container methods if (STLContainers.contains(parentScope->fullName)) {
PStatement typeStatement = result->effectiveTypeStatement; //stl container methods
QString typeName; PStatement typeStatement = result->effectiveTypeStatement;
if (!lastResult->definitionString.isEmpty()) QString typeName;
typeName = doFindFirstTemplateParamOf(fileName,lastResult->definitionString,parentScope); if (!lastResult->definitionString.isEmpty())
else if (lastResult->baseStatement) typeName = doFindFirstTemplateParamOf(fileName,lastResult->definitionString,parentScope);
typeName = doFindFirstTemplateParamOf(fileName,lastResult->baseStatement->type, parentScope); else if (lastResult->baseStatement)
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command; typeName = doFindFirstTemplateParamOf(fileName,lastResult->baseStatement->type, parentScope);
if (!typeName.isEmpty()) // qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope); if (!typeName.isEmpty())
if (typeStatement) { typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
result = doCreateEvalType(fileName,typeStatement); if (typeStatement) {
result->definitionString = typeName; result = doCreateEvalType(fileName,typeStatement);
result->kind = EvalStatementKind::Variable; result->definitionString = typeName;
lastResult = result; result->kind = EvalStatementKind::Variable;
} else { lastResult = result;
return PEvalStatement(); } else {
return PEvalStatement();
}
} else if (STLMaps.contains(parentScope->fullName)) {
//stl map methods
PStatement typeStatement = result->effectiveTypeStatement;
QString typeName;
if (!lastResult->definitionString.isEmpty())
typeName = doFindTemplateParamOf(fileName,lastResult->definitionString,1,parentScope);
else if (lastResult->baseStatement)
typeName = doFindTemplateParamOf(fileName,lastResult->baseStatement->type,1, parentScope);
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
if (!typeName.isEmpty())
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->definitionString = typeName;
result->kind = EvalStatementKind::Variable;
lastResult = result;
} else {
return PEvalStatement();
}
} }
} }
} }
result->kind = EvalStatementKind::Variable; result->kind = EvalStatementKind::Variable;
} else } else
@ -4443,25 +4500,44 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
else { else {
PStatement typeStatement = result->effectiveTypeStatement; PStatement typeStatement = result->effectiveTypeStatement;
if (typeStatement if (typeStatement
&& STLContainers.contains(typeStatement->fullName)
&& result->kind == EvalStatementKind::Variable && result->kind == EvalStatementKind::Variable
&& result->baseStatement) { && result->baseStatement) {
PStatement parentScope = result->baseStatement->parentScope.lock(); if (STLContainers.contains(typeStatement->fullName)) {
QString typeName; PStatement parentScope = result->baseStatement->parentScope.lock();
if (!lastResult || lastResult->definitionString.isEmpty()) QString typeName;
typeName = doFindFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope); if (!lastResult || lastResult->definitionString.isEmpty())
else typeName = doFindFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
typeName = doFindFirstTemplateParamOf(fileName,lastResult->definitionString,parentScope); else
typeStatement = doFindTypeDefinitionOf(fileName, typeName, typeName = doFindFirstTemplateParamOf(fileName,lastResult->definitionString,parentScope);
parentScope); typeStatement = doFindTypeDefinitionOf(fileName, typeName,
if (typeStatement) { parentScope);
result = doCreateEvalType(fileName,typeStatement); if (typeStatement) {
result->definitionString=typeName; result = doCreateEvalType(fileName,typeStatement);
result->kind = EvalStatementKind::Variable; result->definitionString=typeName;
lastResult = result; result->kind = EvalStatementKind::Variable;
} else { lastResult = result;
return PEvalStatement(); } else {
return PEvalStatement();
}
} else if (STLMaps.contains(typeStatement->fullName)) {
PStatement parentScope = result->baseStatement->parentScope.lock();
QString typeName;
if (!lastResult || lastResult->definitionString.isEmpty())
typeName = doFindTemplateParamOf(fileName,result->baseStatement->type, 1,parentScope);
else
typeName = doFindTemplateParamOf(fileName,lastResult->definitionString,1,parentScope);
typeStatement = doFindTypeDefinitionOf(fileName, typeName,
parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->definitionString=typeName;
result->kind = EvalStatementKind::Variable;
lastResult = result;
} else {
return PEvalStatement();
}
} }
} else { } else {
return PEvalStatement(); return PEvalStatement();
} }
@ -4690,6 +4766,9 @@ PEvalStatement CppParser::doEvalTerm(const QString &fileName,
if (parentStatement if (parentStatement
&& STLContainers.contains(parentStatement->fullName)) { && STLContainers.contains(parentStatement->fullName)) {
result->templateParams = previousResult->templateParams; result->templateParams = previousResult->templateParams;
} else if (parentStatement
&& STLMaps.contains(parentStatement->fullName)) {
result->templateParams = previousResult->templateParams;
} }
} }
} }

View File

@ -257,14 +257,14 @@ void initParser()
STLContainers.insert("std::list"); STLContainers.insert("std::list");
STLContainers.insert("std::set"); STLContainers.insert("std::set");
STLContainers.insert("std::map"); // STLContainers.insert("std::map");
STLContainers.insert("std::multilist"); // STLContainers.insert("std::multilist");
STLContainers.insert("std::multimap"); // STLContainers.insert("std::multimap");
STLContainers.insert("std::unordered_set"); STLContainers.insert("std::unordered_set");
STLContainers.insert("std::unordered_map"); // STLContainers.insert("std::unordered_map");
STLContainers.insert("std::unordered_multiset"); STLContainers.insert("std::unordered_multiset");
STLContainers.insert("std::unordered_multimap"); // STLContainers.insert("std::unordered_multimap");
STLContainers.insert("std::stack"); STLContainers.insert("std::stack");
STLContainers.insert("std::queue"); STLContainers.insert("std::queue");