- 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
@ -4259,7 +4283,19 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
} else { } else {
result = PEvalStatement(); 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;
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->definitionString=typeName;
result->kind = EvalStatementKind::Variable;
} else {
result = PEvalStatement();
} }
}
} else { } else {
PStatement typeStatement = result->effectiveTypeStatement; PStatement typeStatement = result->effectiveTypeStatement;
if ((typeStatement) if ((typeStatement)
@ -4409,9 +4445,9 @@ 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)
) { ) {
if (STLContainers.contains(parentScope->fullName)) {
//stl container methods //stl container methods
PStatement typeStatement = result->effectiveTypeStatement; PStatement typeStatement = result->effectiveTypeStatement;
QString typeName; QString typeName;
@ -4430,8 +4466,29 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
} else { } else {
return PEvalStatement(); 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
result = PEvalStatement(); result = PEvalStatement();
@ -4443,9 +4500,9 @@ 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) {
if (STLContainers.contains(typeStatement->fullName)) {
PStatement parentScope = result->baseStatement->parentScope.lock(); PStatement parentScope = result->baseStatement->parentScope.lock();
QString typeName; QString typeName;
if (!lastResult || lastResult->definitionString.isEmpty()) if (!lastResult || lastResult->definitionString.isEmpty())
@ -4462,6 +4519,25 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
} else { } else {
return PEvalStatement(); 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");