work save: parser and code completion done

This commit is contained in:
royqh1979@gmail.com 2021-08-27 08:52:20 +08:00
parent 6c8c100074
commit 61d1430a58
6 changed files with 106 additions and 16 deletions

View File

@ -1848,7 +1848,7 @@ void CppParser::handleKeyword()
break;
case SkipType::skToRightParenthesis:
// skip to )
while (mIndex < mTokenizer.tokenCount() && !mTokenizer[mIndex]->text.startsWith(')'))
while (mIndex < mTokenizer.tokenCount() && !mTokenizer[mIndex]->text.endsWith(')'))
mIndex++;
mIndex++; // step over
break;
@ -2112,7 +2112,29 @@ void CppParser::handleOtherTypedefs()
mIndex++;
return;
}
if ((mIndex+1<mTokenizer.tokenCount())
&& (mTokenizer[mIndex+1]->text == ';')) {
//no old type
QString newType = mTokenizer[mIndex]->text.trimmed();
addStatement(
getCurrentScope(),
mCurrentFile,
"typedef " + newType, // override hint
"",
newType,
"",
"",
startLine,
StatementKind::skTypedef,
getScope(),
mClassScope,
true,
false);
mIndex+=2; //skip ;
return;
}
QString oldType;
// Walk up to first new word (before first comma or ;)
while(true) {
oldType += mTokenizer[mIndex]->text + ' ';
@ -2130,7 +2152,6 @@ void CppParser::handleOtherTypedefs()
}
oldType = oldType.trimmed();
// Add synonyms for old
if ((mIndex+1 < mTokenizer.tokenCount()) && !oldType.isEmpty()) {
QString newType;
@ -2620,6 +2641,23 @@ void CppParser::handleStructs(bool isTypedef)
// Proceed to set first synonym as current class
}
}
if (!firstSynonym) {
//anonymous union/struct/class, add ast a block
firstSynonym=addStatement(
getCurrentScope(),
mCurrentFile,
"", // override hint
"",
"",
"",
"",
startLine,
StatementKind::skBlock,
getScope(),
mClassScope,
true,
false);
}
addSoloScopeLevel(firstSynonym,startLine);
// Step over {
@ -2882,9 +2920,7 @@ void CppParser::internalParse(const QString &fileName)
// mPreprocessor.setProjectIncludePaths(mProjectIncludePaths);
mPreprocessor.setScanOptions(mParseGlobalHeaders, mParseLocalHeaders);
mPreprocessor.preprocess(fileName, buffer);
#ifdef QT_DEBUG
StringsToFile(mPreprocessor.result(),"f:\\preprocess.txt");
#endif
// with TStringList.Create do try
// Text:=fPreprocessor.Result;
// SaveToFile('f:\\Preprocess.txt');
@ -2911,14 +2947,12 @@ void CppParser::internalParse(const QString &fileName)
break;
}
#ifdef QT_DEBUG
mTokenizer.dumpTokens("f:\\tokens.txt");
mPreprocessor.dumpDefinesTo("f:\\defines.txt");
mPreprocessor.dumpIncludesListTo("f:\\includes.txt");
mStatementList.dump("f:\\stats.txt");
//Statements.DumpTo('f:\stats.txt');
//Statements.DumpWithScope('f:\\statements.txt');
//fPreprocessor.DumpDefinesTo('f:\defines.txt');
//fPreprocessor.DumpIncludesListTo('f:\\includes.txt');
// StringsToFile(mPreprocessor.result(),"f:\\preprocess.txt");
// mTokenizer.dumpTokens("f:\\tokens.txt");
// mPreprocessor.dumpDefinesTo("f:\\defines.txt");
// mPreprocessor.dumpIncludesListTo("f:\\includes.txt");
// mStatementList.dump("f:\\stats.txt");
// mStatementList.dumpAll("f:\\all-stats.txt");
#endif
}
}

View File

@ -93,7 +93,7 @@ void CppPreprocessor::addDefineByLine(const QString &line, bool hardCoded)
{
// Remove define
constexpr int DEFINE_LEN=6;
QString s = line.mid(DEFINE_LEN,0).trimmed();
QString s = line.mid(DEFINE_LEN).trimmed();
QString name, args, value;
// Get parts from generalized function
@ -826,6 +826,7 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
case ContentType::RawString:
if (line.mid(0,pos).endsWith(')'+delimiter))
currentType = ContentType::Other;
break;
case ContentType::Other:
currentType=ContentType::String;
break;
@ -839,8 +840,10 @@ QStringList CppPreprocessor::removeComments(const QStringList &text)
switch (currentType) {
case ContentType::Character:
currentType=ContentType::Other;
break;
case ContentType::Other:
currentType=ContentType::Character;
break;
case ContentType::RawStringPrefix:
delimiter+=ch;
break;

View File

@ -20,6 +20,10 @@ void StatementModel::add(PStatement statement)
addMember(mGlobalStatements,statement);
}
mCount++;
#ifdef QT_DEBUG
mAllStatements.append(statement);
#endif
}
void StatementModel::deleteStatement(PStatement statement)
@ -35,6 +39,10 @@ void StatementModel::deleteStatement(PStatement statement)
count = deleteMember(mGlobalStatements,statement);
}
mCount -= count;
#ifdef QT_DEBUG
mAllStatements.removeOne(statement);
#endif
}
const StatementMap &StatementModel::childrenStatements(PStatement statement) const
@ -66,6 +74,29 @@ void StatementModel::dump(const QString &logFile)
}
}
#ifdef QT_DEBUG
void StatementModel::dumpAll(const QString &logFile)
{
QFile file(logFile);
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream out(&file);
for (PStatement statement:mAllStatements) {
out<<QString("%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12")
.arg(statement->command).arg(int(statement->kind))
.arg(statement->type).arg(statement->fullName)
.arg((size_t)(statement->parentScope.lock().get()))
.arg((int)statement->classScope)
.arg(statement->fileName)
.arg(statement->line)
.arg(statement->endLine)
.arg(statement->definitionFileName)
.arg(statement->definitionLine)
.arg(statement->definitionEndLine)<<Qt::endl;
}
}
}
#endif
void StatementModel::addMember(StatementMap &map, PStatement statement)
{
if (!statement)
@ -88,7 +119,7 @@ int StatementModel::deleteMember(StatementMap &map, PStatement statement)
void StatementModel::dumpStatementMap(StatementMap &map, QTextStream &out, int level)
{
QString indent(level,' ');
QString indent(level,'\t');
for (PStatement statement:map.values()) {
out<<indent<<QString("%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12")
.arg(statement->command).arg(int(statement->kind))

View File

@ -19,7 +19,9 @@ public:
const StatementMap& childrenStatements(std::weak_ptr<Statement> statement) const;
void clear();
void dump(const QString& logFile);
#ifdef QT_DEBUG
void dumpAll(const QString& logFile);
#endif
signals:
private:
@ -29,6 +31,9 @@ private:
private:
int mCount;
StatementMap mGlobalStatements; //may have overloaded functions, so use PStatementList to store
#ifdef QT_DEBUG
StatementList mAllStatements;
#endif
};
#endif // STATEMENTMODEL_H

View File

@ -803,6 +803,14 @@ void CodeCompletionView::hideEvent(QHideEvent *event)
QWidget::hideEvent(event);
}
bool CodeCompletionView::event(QEvent *event)
{
QWidget::event(event);
if (event->type() == QEvent::FontChange) {
mListView->setFont(font());
}
}
CodeCompletionListView::CodeCompletionListView(QWidget *parent) : QListView(parent)
{
@ -810,6 +818,11 @@ CodeCompletionListView::CodeCompletionListView(QWidget *parent) : QListView(pare
void CodeCompletionListView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Up
|| event->key() == Qt::Key_Down) {
QListView::keyPressEvent(event);
return;
}
if (!mKeypressedCallback || !mKeypressedCallback(event)) {
QListView::keyPressEvent(event);
}

View File

@ -114,6 +114,10 @@ private:
protected:
void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override;
// QObject interface
public:
bool event(QEvent *event) override;
};
#endif // CODECOMPLETIONVIEW_H