work save

This commit is contained in:
Roy Qu 2021-12-08 21:44:40 +08:00
parent 63d0321fcf
commit 095a0897f2
3 changed files with 133 additions and 44 deletions

View File

@ -1660,15 +1660,19 @@ QStringList Editor::getOwnerExpressionAndMemberAtPositionForCompletion(
} }
} }
QStringList ownerExpression;
if (lastMemberOperatorPos<0) { if (lastMemberOperatorPos<0) {
memberOperator = ""; memberOperator = "";
memberExpression = expression; memberExpression = expression;
return QStringList();
} else { } else {
memberOperator = expression[lastMemberOperatorPos]; memberOperator = expression[lastMemberOperatorPos];
memberExpression = expression.mid(lastMemberOperatorPos+1); memberExpression = expression.mid(lastMemberOperatorPos+1);
return expression.mid(0,lastMemberOperatorPos); ownerExpression = expression.mid(0,lastMemberOperatorPos);
} }
if (memberExpression.length()>1) {
memberExpression = memberExpression.mid(memberExpression.length()-1,1);
}
return ownerExpression;
} }
QStringList Editor::getExpressionAtPositionForCompletion( QStringList Editor::getExpressionAtPositionForCompletion(
@ -2674,6 +2678,7 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete)
caretXY(), caretXY(),
memberOperator, memberOperator,
memberExpression); memberExpression);
qDebug()<<ownerExpression<<memberExpression;
word = memberExpression.join(""); word = memberExpression.join("");
mCompletionPopup->prepareSearch( mCompletionPopup->prepareSearch(
preWord, preWord,
@ -2681,14 +2686,14 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete)
memberOperator, memberOperator,
memberExpression, memberExpression,
mFilename, mFilename,
pBeginPos.Line); caretY());
} else { } else {
QStringList memberExpression; QStringList memberExpression;
memberExpression.append(word); memberExpression.append(word);
mCompletionPopup->prepareSearch(preWord, mCompletionPopup->prepareSearch(preWord,
QStringList(), QStringList(),
"", "",
memberExpression, mFilename, pBeginPos.Line); memberExpression, mFilename, caretY());
} }
// Filter the whole statement list // Filter the whole statement list

View File

@ -403,7 +403,7 @@ PEvalStatement CppParser::evalExpression(
QMutexLocker locker(&mMutex); QMutexLocker locker(&mMutex);
if (mParsing) if (mParsing)
return PEvalStatement(); return PEvalStatement();
qDebug()<<phraseExpression; // qDebug()<<phraseExpression;
int pos = 0; int pos = 0;
return doEvalExpression(fileName, return doEvalExpression(fileName,
phraseExpression, phraseExpression,
@ -3412,7 +3412,7 @@ PEvalStatement CppParser::doEvalPointerArithmetic(const QString &fileName, const
} else } else
break; break;
} }
qDebug()<<pos<<"pointer add member end"; // qDebug()<<pos<<"pointer add member end";
return currentResult; return currentResult;
} }
@ -3482,7 +3482,23 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
freeScoped); freeScoped);
if (result) { if (result) {
//todo: STL container; //todo: STL container;
result->pointerLevel--; if (result->pointerLevel==0) {
PStatement typeStatement = result->effectiveTypeStatement;
if ((typeStatement)
&& STLPointers.contains(typeStatement->fullName)
&& result->kind == EvalStatementKind::Variable
&& result->baseStatement) {
PStatement parentScope = result->baseStatement->parentScope.lock();
QString typeName=findFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
// qDebug()<<"typeName"<<typeName;
typeStatement=findTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->kind = EvalStatementKind::Variable;
}
}
} else
result->pointerLevel--;
} }
} else if (phraseExpression[pos]=="&") { } else if (phraseExpression[pos]=="&") {
pos++; //skip "&" pos++; //skip "&"
@ -3510,7 +3526,7 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
//parse //parse
int startPos = pos; int startPos = pos;
pos++; pos++;
qDebug()<<"parse type cast ()"; // qDebug()<<"parse type cast ()";
PEvalStatement evalType = doEvalExpression( PEvalStatement evalType = doEvalExpression(
fileName, fileName,
phraseExpression, phraseExpression,
@ -3518,13 +3534,13 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
scope, scope,
PEvalStatement(), PEvalStatement(),
true); true);
qDebug()<<pos; // qDebug()<<pos;
if (pos >= phraseExpression.length() || phraseExpression[pos]!=")") { if (pos >= phraseExpression.length() || phraseExpression[pos]!=")") {
return PEvalStatement(); return PEvalStatement();
} else if (evalType && } else if (evalType &&
(evalType->kind == EvalStatementKind::Type)) { (evalType->kind == EvalStatementKind::Type)) {
pos++; // skip ")" pos++; // skip ")"
qDebug()<<"parse type cast exp"; // qDebug()<<"parse type cast exp";
//it's a type cast //it's a type cast
result = doEvalCCast(fileName, result = doEvalCCast(fileName,
phraseExpression, phraseExpression,
@ -3533,7 +3549,7 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
previousResult, previousResult,
freeScoped); freeScoped);
if (result) { if (result) {
qDebug()<<"type cast"; // qDebug()<<"type cast";
result->assignType(evalType); result->assignType(evalType);
} }
} else //it's not a type cast } else //it's not a type cast
@ -3552,11 +3568,11 @@ PEvalStatement CppParser::doEvalCCast(const QString &fileName,
scope, scope,
previousResult, previousResult,
freeScoped); freeScoped);
if (result) { // if (result) {
qDebug()<<pos<<(int)result->kind<<result->baseType; // qDebug()<<pos<<(int)result->kind<<result->baseType;
} else { // } else {
qDebug()<<"!!!!!!!!!!!not found"; // qDebug()<<"!!!!!!!!!!!not found";
} // }
return result; return result;
} }
@ -3572,6 +3588,7 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
PEvalStatement result; PEvalStatement result;
if (pos>=phraseExpression.length()) if (pos>=phraseExpression.length())
return result; return result;
PEvalStatement lastResult = previousResult;
result = doEvalScopeResolution( result = doEvalScopeResolution(
fileName, fileName,
phraseExpression, phraseExpression,
@ -3602,17 +3619,52 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
result = newResult; result = newResult;
} else if (result->kind == EvalStatementKind::Function) { } else if (result->kind == EvalStatementKind::Function) {
doSkipInExpression(phraseExpression,pos,"(",")"); doSkipInExpression(phraseExpression,pos,"(",")");
//function call // qDebug()<<"????"<<(result->baseStatement!=nullptr)<<(lastResult!=nullptr);
if (result->baseStatement && lastResult && lastResult->baseStatement) {
PStatement parentScope = result->baseStatement->parentScope.lock();
if (parentScope
&& STLContainers.contains(parentScope->fullName)
&& STLElementMethods.contains(result->baseStatement->command)
) {
//stl container methods
PStatement typeStatement = result->effectiveTypeStatement;
QString typeName=findFirstTemplateParamOf(fileName,lastResult->baseStatement->type, parentScope);
// qDebug()<<"typeName"<<typeName<<lastResult->baseStatement->type<<lastResult->baseStatement->command;
typeStatement=findTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->kind = EvalStatementKind::Variable;
}
}
}
result->kind = EvalStatementKind::Variable; result->kind = EvalStatementKind::Variable;
} else } else
result = PEvalStatement(); result = PEvalStatement();
} else if (phraseExpression[pos] == "[") { } else if (phraseExpression[pos] == "[") {
//skip to "]" //skip to "]"
doSkipInExpression(phraseExpression,pos,"[","]"); doSkipInExpression(phraseExpression,pos,"[","]");
//todo: STL container; if (result->pointerLevel>0)
result->pointerLevel--; result->pointerLevel--;
else {
PStatement typeStatement = result->effectiveTypeStatement;
if (typeStatement
&& STLContainers.contains(typeStatement->fullName)
&& result->kind == EvalStatementKind::Variable
&& result->baseStatement) {
PStatement parentScope = result->baseStatement->parentScope.lock();
QString typeName = findFirstTemplateParamOf(fileName,result->baseStatement->type,
parentScope);
typeStatement = findTypeDefinitionOf(fileName, typeName,
parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->kind = EvalStatementKind::Variable;
}
}
}
} else if (phraseExpression[pos] == ".") { } else if (phraseExpression[pos] == ".") {
pos++; pos++;
lastResult = result;
result = doEvalScopeResolution( result = doEvalScopeResolution(
fileName, fileName,
phraseExpression, phraseExpression,
@ -3620,11 +3672,29 @@ PEvalStatement CppParser::doEvalMemberAccess(const QString &fileName,
scope, scope,
result, result,
false); false);
qDebug()<<(result!=nullptr)<<pos<<"after ."; // qDebug()<<(result!=nullptr)<<pos<<"after .";
} else if (phraseExpression[pos] == "->") { } else if (phraseExpression[pos] == "->") {
pos++; pos++;
//todo: STL container // qDebug()<<"pointer level"<<result->pointerLevel;
result->pointerLevel--; if (result->pointerLevel==0) {
PStatement typeStatement = result->effectiveTypeStatement;
if ((typeStatement)
&& STLPointers.contains(typeStatement->fullName)
&& result->kind == EvalStatementKind::Variable
&& result->baseStatement) {
PStatement parentScope = result->baseStatement->parentScope.lock();
QString typeName=findFirstTemplateParamOf(fileName,result->baseStatement->type, parentScope);
// qDebug()<<"typeName"<<typeName;
typeStatement=findTypeDefinitionOf(fileName, typeName,parentScope);
if (typeStatement) {
result = doCreateEvalType(fileName,typeStatement);
result->kind = EvalStatementKind::Variable;
}
}
} else {
result->pointerLevel--;
}
lastResult = result;
result = doEvalScopeResolution( result = doEvalScopeResolution(
fileName, fileName,
phraseExpression, phraseExpression,
@ -3700,11 +3770,11 @@ PEvalStatement CppParser::doEvalTerm(const QString &fileName,
const PEvalStatement& previousResult, const PEvalStatement& previousResult,
bool freeScoped) bool freeScoped)
{ {
if (previousResult) { // if (previousResult) {
qDebug()<<"eval term "<<pos<<phraseExpression<<previousResult->baseType<<freeScoped; // qDebug()<<"eval term "<<pos<<phraseExpression<<previousResult->baseType<<freeScoped;
} else { // } else {
qDebug()<<"eval term "<<pos<<phraseExpression<<"no type"<<freeScoped; // qDebug()<<"eval term "<<pos<<phraseExpression<<"no type"<<freeScoped;
} // }
PEvalStatement result; PEvalStatement result;
if (pos>=phraseExpression.length()) if (pos>=phraseExpression.length())
return result; return result;
@ -3753,11 +3823,11 @@ PEvalStatement CppParser::doEvalTerm(const QString &fileName,
if (!previousResult) { if (!previousResult) {
statement = findStatementInScope(phraseExpression[pos],PStatement()); statement = findStatementInScope(phraseExpression[pos],PStatement());
} else { } else {
if (previousResult->effectiveTypeStatement) { // if (previousResult->effectiveTypeStatement) {
qDebug()<<phraseExpression[pos]<<previousResult->effectiveTypeStatement->fullName; // qDebug()<<phraseExpression[pos]<<previousResult->effectiveTypeStatement->fullName;
} else { // } else {
qDebug()<<phraseExpression[pos]<<"no type"; // qDebug()<<phraseExpression[pos]<<"no type";
} // }
statement = findStatementInScope(phraseExpression[pos],previousResult->effectiveTypeStatement); statement = findStatementInScope(phraseExpression[pos],previousResult->effectiveTypeStatement);
// if (!statement) { // if (!statement) {
// qDebug()<<"not found!"; // qDebug()<<"not found!";
@ -3773,6 +3843,14 @@ PEvalStatement CppParser::doEvalTerm(const QString &fileName,
case StatementKind::skNamespace: case StatementKind::skNamespace:
result = doCreateEvalNamespace(statement); result = doCreateEvalNamespace(statement);
break; break;
case StatementKind::skAlias: {
PStatement parentScopeType;
statement = findStatementOf(fileName, statement->type,
scope, parentScopeType, true);
if (statement)
result = doCreateEvalNamespace(statement);
}
break;
case StatementKind::skVariable: case StatementKind::skVariable:
case StatementKind::skParameter: case StatementKind::skParameter:
result = doCreateEvalVariable(fileName,statement); result = doCreateEvalVariable(fileName,statement);
@ -3805,9 +3883,9 @@ PEvalStatement CppParser::doEvalTerm(const QString &fileName,
pos++; pos++;
} else } else
return result; return result;
if (result) { // if (result) {
qDebug()<<"term kind:"<<(int)result->kind; // qDebug()<<"term kind:"<<(int)result->kind;
} // }
if (result && result->kind == EvalStatementKind::Type) { if (result && result->kind == EvalStatementKind::Type) {
//skip "struct", "const", "static", etc //skip "struct", "const", "static", etc
while(pos < phraseExpression.length()) { while(pos < phraseExpression.length()) {
@ -3850,7 +3928,7 @@ PEvalStatement CppParser::doCreateEvalType(const QString& fileName,const PStatem
PStatement statement = doParseEvalTypeInfo( PStatement statement = doParseEvalTypeInfo(
fileName, fileName,
typeStatement->parentScope.lock(), typeStatement->parentScope.lock(),
typeStatement->type, typeStatement->type + typeStatement->args,
baseType, baseType,
pointerLevel); pointerLevel);
return std::make_shared<EvalStatement>( return std::make_shared<EvalStatement>(
@ -3887,9 +3965,10 @@ PEvalStatement CppParser::doCreateEvalVariable(const QString &fileName, PStateme
PStatement typeStatement = doParseEvalTypeInfo( PStatement typeStatement = doParseEvalTypeInfo(
fileName, fileName,
varStatement->parentScope.lock(), varStatement->parentScope.lock(),
varStatement->type, varStatement->type+ varStatement->args,
baseType, baseType,
pointerLevel); pointerLevel);
// qDebug()<<"parse ..."<<baseType<<pointerLevel;
return std::make_shared<EvalStatement>( return std::make_shared<EvalStatement>(
baseType, baseType,
EvalStatementKind::Variable, EvalStatementKind::Variable,
@ -3938,8 +4017,10 @@ void CppParser::doSkipInExpression(const QStringList &expression, int &pos, cons
level++; level++;
} else if (token == endSymbol) { } else if (token == endSymbol) {
level--; level--;
if (level==0) if (level==0) {
pos++;
return; return;
}
} }
pos++; pos++;
} }
@ -3989,7 +4070,8 @@ PStatement CppParser::doParseEvalTypeInfo(
int &pointerLevel) int &pointerLevel)
{ {
// Remove pointer stuff from type // Remove pointer stuff from type
QString s = type; // 'Type' is a keyword QString s = type;
// qDebug()<<"eval type info"<<type;
int position = s.length()-1; int position = s.length()-1;
SynEditCppHighlighter highlighter; SynEditCppHighlighter highlighter;
highlighter.resetState(); highlighter.resetState();
@ -4007,6 +4089,7 @@ PStatement CppParser::doParseEvalTypeInfo(
if (token!= "const") if (token!= "const")
baseType += token; baseType += token;
} else if (token == "[") { } else if (token == "[") {
pointerLevel++;
bracketLevel++; bracketLevel++;
} else if (token == "<") { } else if (token == "<") {
templateLevel++; templateLevel++;
@ -4017,13 +4100,13 @@ PStatement CppParser::doParseEvalTypeInfo(
if (token == "[") { if (token == "[") {
bracketLevel++; bracketLevel++;
} else if (token == "]") { } else if (token == "]") {
bracketLevel++; bracketLevel--;
} }
} else if (templateLevel > 0) { } else if (templateLevel > 0) {
if (token == "<") { if (token == "<") {
templateLevel++; templateLevel++;
} else if (token == ">") { } else if (token == ">") {
templateLevel++; templateLevel--;
} }
} }
highlighter.next(); highlighter.next();

View File

@ -559,7 +559,7 @@ void CodeCompletionPopup::getCompletionFor(
// qDebug()<<memberOperator; // qDebug()<<memberOperator;
// qDebug()<<memberExpression; // qDebug()<<memberExpression;
if(!ownerStatement || !ownerStatement->effectiveTypeStatement) { if(!ownerStatement || !ownerStatement->effectiveTypeStatement) {
qDebug()<<"statement not found!"; // qDebug()<<"statement not found!";
return; return;
} }
// qDebug()<<"found: "<<ownerStatement->fullName; // qDebug()<<"found: "<<ownerStatement->fullName;
@ -595,10 +595,11 @@ void CodeCompletionPopup::getCompletionFor(
//is a smart pointer //is a smart pointer
if (STLPointers.contains(classTypeStatement->fullName) if (STLPointers.contains(classTypeStatement->fullName)
&& (memberOperator == "->" && (memberOperator == "->"
|| memberOperator == "->*")) { || memberOperator == "->*")
&& ownerStatement->baseStatement) {
QString typeName= mParser->findFirstTemplateParamOf( QString typeName= mParser->findFirstTemplateParamOf(
fileName, fileName,
ownerStatement->baseType, ownerStatement->baseStatement->type,
scope); scope);
classTypeStatement = mParser->findTypeDefinitionOf( classTypeStatement = mParser->findTypeDefinitionOf(
fileName, fileName,