- enhancement: Code completion for '->' operator on std iterators.

This commit is contained in:
Roy Qu 2023-06-08 17:15:55 +08:00
parent 36cb8b7867
commit 4746d1b63c
5 changed files with 89 additions and 22 deletions

View File

@ -1,6 +1,7 @@
Red Panda C++ Version 2.23
- fix: When selection is availalbe, Ctrl+Click shouldn't jump to declaration/definition.
- enhancement: Code completion for '->' operator on std iterators.
Red Panda C++ Version 2.22

View File

@ -193,6 +193,14 @@ QString CppParser::findFirstTemplateParamOf(const QString &fileName, const QStri
return doFindFirstTemplateParamOf(fileName,phrase,currentScope);
}
QString CppParser::findTemplateParamOf(const QString &fileName, const QString &phrase, int index, const PStatement &currentScope)
{
QMutexLocker locker(&mMutex);
if (mParsing)
return "";
return doFindTemplateParamOf(fileName,phrase,index,currentScope);
}
PStatement CppParser::findFunctionAt(const QString &fileName, int line)
{
QMutexLocker locker(&mMutex);
@ -4632,8 +4640,37 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
false);
} else if (phraseExpression[pos] == "->") {
pos++;
// qDebug()<<"pointer level"<<result->pointerLevel;
if (result->pointerLevel==0) {
// iterator
if (result->typeStatement
&& STLIterators.contains(result->typeStatement->command)
) {
PStatement parentScope = result->typeStatement->parentScope.lock();
if (STLContainers.contains(parentScope->fullName)) {
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;
PStatement typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->definitionString=typeName;
result->kind = EvalStatementKind::Variable;
} else {
result = PEvalStatement();
}
}
} else {
//smart pointer
PStatement typeStatement = result->effectiveTypeStatement;
if ((typeStatement)
&& STLPointers.contains(typeStatement->fullName)
@ -4645,7 +4682,7 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
typeName = doFindFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
else
typeName = doFindFirstTemplateParamOf(fileName,previousResult->definitionString,parentScope);
// qDebug()<<"typeName"<<typeName;
// qDebug()<<"typeName"<<typeName;
typeStatement=doFindTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
@ -4655,6 +4692,7 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
return PEvalStatement();
}
}
}
} else {
result->pointerLevel--;
}

View File

@ -52,6 +52,10 @@ public:
QString findFirstTemplateParamOf(const QString& fileName,
const QString& phrase,
const PStatement& currentScope);
QString findTemplateParamOf(const QString& fileName,
const QString& phrase,
int index,
const PStatement& currentScope);
PStatement findFunctionAt(const QString& fileName,
int line);
int findLastOperator(const QString& phrase) const;

View File

@ -303,8 +303,10 @@ void initParser()
STLPointers.insert("std::shared_ptr");
STLPointers.insert("std::weak_ptr");
//STLPointers.insert("__gnu_cxx::__normal_iterator");
//STLPointers.insert("std::reverse_iterator");
//STLPointers.insert("std::iterator");
// STLPointers.insert("std::reverse_iterator");
// STLPointers.insert("std::iterator");
// STLPointers.insert("std::const_iterator");
// STLPointers.insert("std::const_reverse_iterator");
AutoTypes.insert("auto");
AutoTypes.insert("auto &");

View File

@ -752,6 +752,28 @@ void CodeCompletionPopup::getCompletionFor(
if (!classTypeStatement)
return;
// It's a iterator
if (ownerStatement
&& ownerStatement->typeStatement
&& STLIterators.contains(ownerStatement->typeStatement->command)
&& (memberOperator == "->"
|| memberOperator == "->*")
) {
PStatement parentScope = ownerStatement->typeStatement->parentScope.lock();
if (STLContainers.contains(parentScope->fullName)) {
QString typeName=mParser->findFirstTemplateParamOf(fileName,ownerStatement->templateParams, parentScope);
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
classTypeStatement=mParser->findTypeDefinitionOf(fileName, typeName,parentScope);
if (!classTypeStatement)
return;
} else if (STLMaps.contains(parentScope->fullName)) {
QString typeName=mParser->findTemplateParamOf(fileName,ownerStatement->templateParams,1,parentScope);
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
classTypeStatement=mParser->findTypeDefinitionOf(fileName, typeName,parentScope);
if (!classTypeStatement)
return;
}
}
//is a smart pointer
if (STLPointers.contains(classTypeStatement->fullName)
&& (memberOperator == "->"