RedPanda-CPP/RedPandaIDE/widgets/codecompletionpopup.cpp

935 lines
34 KiB
C++
Raw Normal View History

2021-08-29 00:48:23 +08:00
#include "codecompletionpopup.h"
2021-08-24 15:05:10 +08:00
#include "../utils.h"
#include "../mainwindow.h"
#include "../editor.h"
#include "../editorlist.h"
#include "../symbolusagemanager.h"
2021-08-23 21:50:53 +08:00
2021-08-24 09:59:44 +08:00
#include <QKeyEvent>
2021-08-24 15:05:10 +08:00
#include <QVBoxLayout>
2021-08-27 16:38:55 +08:00
#include <QDebug>
2021-08-28 09:01:40 +08:00
#include <QApplication>
2021-08-24 09:59:44 +08:00
2021-08-29 00:48:23 +08:00
CodeCompletionPopup::CodeCompletionPopup(QWidget *parent) :
2021-08-24 09:59:44 +08:00
QWidget(parent)
2021-08-23 21:50:53 +08:00
{
2021-08-24 09:59:44 +08:00
setWindowFlags(Qt::Popup);
mListView = new CodeCompletionListView(this);
2021-08-25 08:48:33 +08:00
mModel=new CodeCompletionListModel(&mCompletionStatementList);
2021-08-28 09:01:40 +08:00
mModel->setColorCallback([this](PStatement statement)->QColor{
StatementKind kind;
if (mParser) {
kind = mParser->getKindOfStatement(statement);
} else {
kind = statement->kind;
}
2021-09-25 23:12:36 +08:00
return mColors->value(kind,palette().color(QPalette::Text));
2021-08-28 09:01:40 +08:00
});
2021-08-25 08:48:33 +08:00
mListView->setModel(mModel);
2021-08-24 09:59:44 +08:00
setLayout(new QVBoxLayout());
layout()->addWidget(mListView);
layout()->setMargin(0);
2021-08-25 00:20:07 +08:00
mShowKeywords=true;
mUseCppKeyword=true;
2021-08-27 16:38:55 +08:00
mRecordUsage = false;
mSortByScope = true;
2021-08-25 00:20:07 +08:00
mOnlyGlobals = false;
mShowCount = 1000;
2021-09-30 20:10:48 +08:00
mShowCodeSnippets = true;
2021-08-25 00:20:07 +08:00
mIgnoreCase = false;
2021-08-25 08:48:33 +08:00
2021-08-23 21:50:53 +08:00
}
2021-08-29 00:48:23 +08:00
CodeCompletionPopup::~CodeCompletionPopup()
2021-08-23 21:50:53 +08:00
{
2021-08-25 08:48:33 +08:00
delete mListView;
delete mModel;
2021-08-24 09:59:44 +08:00
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setKeypressedCallback(const KeyPressedCallback &newKeypressedCallback)
2021-08-24 09:59:44 +08:00
{
mListView->setKeypressedCallback(newKeypressedCallback);
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::prepareSearch(const QString &phrase, const QString &filename, int line)
2021-08-25 00:20:07 +08:00
{
QMutexLocker locker(&mMutex);
2021-08-25 08:48:33 +08:00
if (!isEnabled())
2021-08-25 00:20:07 +08:00
return;
mPhrase = phrase;
//Screen.Cursor := crHourglass;
QCursor oldCursor = cursor();
setCursor(Qt::CursorShape::WaitCursor);
mIncludedFiles = mParser->getFileIncludes(filename);
getCompletionFor(filename,phrase,line);
//todo: notify model
//CodeComplForm.lbCompletion.Font.Size := FontSize;
//CodeComplForm.lbCompletion.ItemHeight := CodeComplForm.lbCompletion.Canvas.TextHeight('F')+6;
// Round(2 * FontSize);
//CodeComplForm.Update;
setCursor(oldCursor);
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::search(const QString &phrase, bool autoHideOnSingleResult)
2021-08-25 00:20:07 +08:00
{
QMutexLocker locker(&mMutex);
mPhrase = phrase;
if (phrase.isEmpty()) {
hide();
return false;
}
2021-08-25 08:48:33 +08:00
if (!isEnabled()) {
2021-08-25 00:20:07 +08:00
hide();
return false;
}
QCursor oldCursor = cursor();
setCursor(Qt::CursorShape::WaitCursor);
// Sort here by member
int i = mParser->findLastOperator(phrase);
while ((i>=0) && (i<phrase.length()) && (
phrase[i] == '.'
|| phrase[i] == ':'
|| phrase[i] == '-'
|| phrase[i] == '>'))
i++;
QString symbol = phrase.mid(i);
// filter fFullCompletionStatementList to fCompletionStatementList
filterList(symbol);
2021-08-25 08:48:33 +08:00
mModel->notifyUpdated();
setCursor(oldCursor);
2021-08-25 00:20:07 +08:00
if (!mCompletionStatementList.isEmpty()) {
mListView->setCurrentIndex(mModel->index(0,0));
2021-08-25 00:20:07 +08:00
// if only one suggestion, and is exactly the symbol to search, hide the frame (the search is over)
// if only one suggestion and auto hide , don't show the frame
if(mCompletionStatementList.count() == 1)
if (autoHideOnSingleResult
|| (symbol == mCompletionStatementList.front()->command)) {
return true;
}
} else {
hide();
}
2021-08-25 08:48:33 +08:00
return false;
2021-08-25 00:20:07 +08:00
}
2021-08-29 00:48:23 +08:00
PStatement CodeCompletionPopup::selectedStatement()
2021-08-26 17:48:23 +08:00
{
if (isEnabled()) {
int index = mListView->currentIndex().row();
if (mListView->currentIndex().isValid()
&& (index<mCompletionStatementList.count()) ) {
return mCompletionStatementList[index];
} else {
if (!mCompletionStatementList.isEmpty())
return mCompletionStatementList.front();
else
return PStatement();
}
} else
return PStatement();
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::addChildren(PStatement scopeStatement, const QString &fileName, int line)
2021-08-24 15:05:10 +08:00
{
if (scopeStatement && !isIncluded(scopeStatement->fileName)
&& !isIncluded(scopeStatement->definitionFileName))
return;
const StatementMap& children = mParser->statementList().childrenStatements(scopeStatement);
if (children.isEmpty())
return;
if (!scopeStatement) { //Global scope
2021-08-29 00:48:23 +08:00
for (const PStatement& childStatement: children) {
2021-08-24 15:05:10 +08:00
if (childStatement->fileName.isEmpty()) {
// hard defines
addStatement(childStatement,fileName,-1);
} else if (!( childStatement->kind == StatementKind::skConstructor
|| childStatement->kind == StatementKind::skDestructor
|| childStatement->kind == StatementKind::skBlock)
&& (!mAddedStatements.contains(childStatement->command))
&& (
isIncluded(childStatement->fileName)
|| isIncluded(childStatement->definitionFileName)
)
) {
//we must check if the statement is included by the file
addStatement(childStatement,fileName,line);
}
}
} else {
2021-08-29 00:48:23 +08:00
for (const PStatement& childStatement: children) {
2021-08-24 15:05:10 +08:00
if (!( childStatement->kind == StatementKind::skConstructor
|| childStatement->kind == StatementKind::skDestructor
|| childStatement->kind == StatementKind::skBlock)
&& (!mAddedStatements.contains(childStatement->command)))
addStatement(childStatement,fileName,line);
}
}
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::addStatement(PStatement statement, const QString &fileName, int line)
2021-08-24 15:05:10 +08:00
{
2021-08-25 00:20:07 +08:00
if (mAddedStatements.contains(statement->command))
return;
2021-08-24 15:05:10 +08:00
if ((line!=-1)
&& (line < statement->line)
&& (fileName == statement->fileName))
return;
mAddedStatements.insert(statement->command);
mFullCompletionStatementList.append(statement);
}
static bool nameComparator(PStatement statement1,PStatement statement2) {
if (statement1->caseMatch && !statement2->caseMatch) {
return true;
} else if (!statement1->caseMatch && statement2->caseMatch) {
return false;
} else
return statement1->command < statement2->command;
}
2021-08-24 15:05:10 +08:00
static bool defaultComparator(PStatement statement1,PStatement statement2) {
// Show user template first
if (statement1->kind == StatementKind::skUserCodeIn) {
if (statement2->kind != StatementKind::skUserCodeIn)
return true;
else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skUserCodeIn) {
return false;
// show keywords first
} else if ((statement1->kind == StatementKind::skKeyword)
&& (statement2->kind != StatementKind::skKeyword)) {
return true;
} else if ((statement1->kind != StatementKind::skKeyword)
&& (statement2->kind == StatementKind::skKeyword)) {
return false;
} else
return nameComparator(statement1,statement2);
2021-08-24 15:05:10 +08:00
}
static bool sortByScopeComparator(PStatement statement1,PStatement statement2){
// Show user template first
if (statement1->kind == StatementKind::skUserCodeIn) {
if (statement2->kind != StatementKind::skUserCodeIn)
return true;
else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skUserCodeIn) {
return false;
// show keywords first
} else if (statement1->kind == StatementKind::skKeyword) {
if (statement2->kind != StatementKind::skKeyword)
return true;
else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skKeyword) {
return false;
// Show stuff from local headers first
2021-08-28 09:01:40 +08:00
} else if (!(statement1->inSystemHeader) && statement2->inSystemHeader) {
return true;
} else if (statement1->inSystemHeader && !(statement2->inSystemHeader)) {
2021-08-24 15:05:10 +08:00
return false;
// Show local statements first
} else if (statement1->scope != StatementScope::ssGlobal
2021-08-27 16:38:55 +08:00
&& statement2->scope == StatementScope::ssGlobal ) {
2021-08-24 15:05:10 +08:00
return true;
} else if (statement1->scope == StatementScope::ssGlobal
2021-08-27 16:38:55 +08:00
&& statement2->scope != StatementScope::ssGlobal ) {
2021-08-24 15:05:10 +08:00
return false;
} else
return nameComparator(statement1,statement2);
2021-08-24 15:05:10 +08:00
}
static bool sortWithUsageComparator(PStatement statement1,PStatement statement2) {
// Show user template first
if (statement1->kind == StatementKind::skUserCodeIn) {
if (statement2->kind != StatementKind::skUserCodeIn)
return true;
else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skUserCodeIn) {
return false;
//show most freq first
} else if (statement1->freqTop > statement2->freqTop) {
return true;
} else if (statement1->freqTop < statement2->freqTop) {
return false;
// show keywords first
} else if ((statement1->kind != StatementKind::skKeyword)
&& (statement2->kind == StatementKind::skKeyword)) {
return true;
} else if ((statement1->kind == StatementKind::skKeyword)
&& (statement2->kind != StatementKind::skKeyword)) {
2021-08-24 15:05:10 +08:00
return false;
} else
return nameComparator(statement1,statement2);
2021-08-24 15:05:10 +08:00
}
static bool sortByScopeWithUsageComparator(PStatement statement1,PStatement statement2){
// Show user template first
if (statement1->kind == StatementKind::skUserCodeIn) {
if (statement2->kind != StatementKind::skUserCodeIn)
return true;
else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skUserCodeIn) {
return false;
//show most freq first
} else if (statement1->freqTop > statement2->freqTop) {
return true;
} else if (statement1->freqTop < statement2->freqTop) {
return false;
// show keywords first
} else if (statement1->kind == StatementKind::skKeyword) {
if (statement2->kind != StatementKind::skKeyword)
return true;
else
return statement1->command < statement2->command;
} else if (statement2->kind == StatementKind::skKeyword) {
return false;
// Show stuff from local headers first
2021-08-28 09:01:40 +08:00
} else if (statement1->inSystemHeader && ! (statement2->inSystemHeader)) {
2021-08-24 15:05:10 +08:00
return true;
2021-08-28 09:01:40 +08:00
} else if (!(statement1->inSystemHeader) && statement2->inSystemHeader) {
2021-08-24 15:05:10 +08:00
return false;
// Show local statements first
} else if (statement1->scope != StatementScope::ssGlobal
2021-08-27 16:38:55 +08:00
&& statement2->scope == StatementScope::ssGlobal ) {
2021-08-24 15:05:10 +08:00
return true;
} else if (statement1->scope == StatementScope::ssGlobal
2021-08-27 16:38:55 +08:00
&& statement2->scope != StatementScope::ssGlobal ) {
2021-08-24 15:05:10 +08:00
return false;
} else
return nameComparator(statement1,statement2);
2021-08-24 15:05:10 +08:00
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::filterList(const QString &member)
2021-08-24 15:05:10 +08:00
{
QMutexLocker locker(&mMutex);
mCompletionStatementList.clear();
if (!mParser)
return;
if (!mParser->enabled())
return;
//we don't need to freeze here since we use smart pointers
// and data have been retrieved from the parser
// if (!mParser->freeze())
// return;
// {
// auto action = finally([this]{
// mParser->unFreeze();
// });
// if (mParserSerialId!=mParser->serialId()) {
// return;
// }
2021-08-25 00:20:07 +08:00
mCompletionStatementList.clear();
2021-08-24 15:05:10 +08:00
if (!member.isEmpty()) { // filter
2021-08-25 00:20:07 +08:00
mCompletionStatementList.reserve(mFullCompletionStatementList.size());
2021-08-29 00:48:23 +08:00
foreach (const PStatement& statement, mFullCompletionStatementList) {
2021-08-24 15:05:10 +08:00
Qt::CaseSensitivity cs = (mIgnoreCase?
Qt::CaseInsensitive:
Qt::CaseSensitive);
if (statement->command.startsWith(member, cs)) {
if (mIgnoreCase) {
statement->caseMatch =
statement->command.startsWith(
member,Qt::CaseSensitive);
} else {
statement->caseMatch = true;
}
2021-08-25 00:20:07 +08:00
mCompletionStatementList.append(statement);
}
2021-08-24 15:05:10 +08:00
}
} else
2021-08-25 00:20:07 +08:00
mCompletionStatementList.append(mFullCompletionStatementList);
2021-08-24 15:05:10 +08:00
if (mRecordUsage) {
int topCount = 0;
int secondCount = 0;
int thirdCount = 0;
int usageCount;
2021-08-29 00:48:23 +08:00
foreach (const PStatement& statement,mCompletionStatementList) {
if (statement->usageCount == -1) {
PSymbolUsage usage = pMainWindow->symbolUsageManager()->findUsage(statement->fullName);
if (usage) {
usageCount = usage->count;
} else {
usageCount = 0;
}
2021-08-24 15:05:10 +08:00
statement->usageCount = usageCount;
} else
usageCount = statement->usageCount;
if (usageCount>topCount) {
thirdCount = secondCount;
secondCount = topCount;
topCount = usageCount;
} else if (usageCount == topCount) {
continue;
} else if (usageCount > secondCount) {
thirdCount = secondCount;
secondCount = usageCount;
} else if (usageCount == secondCount) {
continue;
} else if (usageCount>thirdCount) {
thirdCount = usageCount;
}
}
2021-08-29 00:48:23 +08:00
foreach (const PStatement& statement, mCompletionStatementList) {
2021-08-24 15:05:10 +08:00
if (statement->usageCount == 0) {
statement->freqTop = 0;
} else if (statement->usageCount == topCount) {
statement->freqTop = 30;
} else if (statement->usageCount == secondCount) {
statement->freqTop = 20;
} else if (statement->usageCount == thirdCount) {
statement->freqTop = 10;
}
}
if (mSortByScope) {
2021-08-25 00:20:07 +08:00
std::sort(mCompletionStatementList.begin(),
mCompletionStatementList.end(),
sortByScopeWithUsageComparator);
2021-08-24 15:05:10 +08:00
} else {
2021-08-25 00:20:07 +08:00
std::sort(mCompletionStatementList.begin(),
mCompletionStatementList.end(),
sortWithUsageComparator);
2021-08-24 15:05:10 +08:00
}
} else if (mSortByScope) {
2021-08-25 00:20:07 +08:00
std::sort(mCompletionStatementList.begin(),
mCompletionStatementList.end(),
sortByScopeComparator);
2021-08-24 15:05:10 +08:00
} else {
2021-08-25 00:20:07 +08:00
std::sort(mCompletionStatementList.begin(),
mCompletionStatementList.end(),
defaultComparator);
}
// }
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::getCompletionFor(const QString &fileName, const QString &phrase, int line)
2021-08-25 00:20:07 +08:00
{
if(!mParser)
return;
if (!mParser->enabled())
return;
if (!mParser->freeze())
return;
{
auto action = finally([this]{
mParser->unFreeze();
});
//C++ preprocessor directives
if (phrase.startsWith('#')) {
if (mShowKeywords) {
2021-08-29 00:48:23 +08:00
foreach (const QString& keyword, CppDirectives) {
2021-08-25 00:20:07 +08:00
PStatement statement = std::make_shared<Statement>();
statement->command = keyword;
2021-08-27 16:38:55 +08:00
statement->kind = StatementKind::skKeyword;
2021-08-25 00:20:07 +08:00
statement->fullName = keyword;
2021-08-27 16:38:55 +08:00
statement->usageCount = 0;
statement->freqTop = 0;
2021-08-25 00:20:07 +08:00
mFullCompletionStatementList.append(statement);
}
}
return;
}
//docstring tags (javadoc style)
if (phrase.startsWith('@')) {
if (mShowKeywords) {
2021-08-29 00:48:23 +08:00
foreach (const QString& keyword,JavadocTags) {
2021-08-25 00:20:07 +08:00
PStatement statement = std::make_shared<Statement>();
statement->command = keyword;
statement->kind = StatementKind::skKeyword;
statement->fullName = keyword;
2021-08-27 16:38:55 +08:00
statement->usageCount = 0;
statement->freqTop = 0;
2021-08-25 00:20:07 +08:00
mFullCompletionStatementList.append(statement);
}
}
return;
}
// Pulling off the same trick as in TCppParser.FindStatementOf, but ignore everything after last operator
int i = mParser->findLastOperator(phrase);
if (i < 0 ) { // don't have any scope prefix
2021-09-30 20:10:48 +08:00
if (mShowCodeSnippets) {
2021-08-25 00:20:07 +08:00
//add custom code templates
2021-09-30 20:10:48 +08:00
foreach (const PCodeSnippet& codeIn,mCodeSnippets) {
2021-08-25 00:20:07 +08:00
PStatement statement = std::make_shared<Statement>();
statement->command = codeIn->prefix;
statement->kind = StatementKind::skUserCodeIn;
statement->fullName = codeIn->prefix;
2021-08-27 16:38:55 +08:00
statement->usageCount = 0;
statement->freqTop = 0;
2021-08-25 00:20:07 +08:00
mFullCompletionStatementList.append(statement);
}
}
if (mShowKeywords) {
//add keywords
if (mUseCppKeyword) {
2021-08-29 00:48:23 +08:00
foreach (const QString& keyword,CppKeywords.keys()) {
2021-08-25 00:20:07 +08:00
PStatement statement = std::make_shared<Statement>();
statement->command = keyword;
statement->kind = StatementKind::skKeyword;
statement->fullName = keyword;
2021-08-27 16:38:55 +08:00
statement->usageCount = 0;
statement->freqTop = 0;
2021-08-25 00:20:07 +08:00
mFullCompletionStatementList.append(statement);
}
} else {
2021-08-29 00:48:23 +08:00
foreach (const QString& keyword,CKeywords) {
2021-08-25 00:20:07 +08:00
PStatement statement = std::make_shared<Statement>();
statement->command = keyword;
statement->kind = StatementKind::skKeyword;
statement->fullName = keyword;
2021-08-27 16:38:55 +08:00
statement->usageCount = 0;
statement->freqTop = 0;
2021-08-25 00:20:07 +08:00
mFullCompletionStatementList.append(statement);
}
}
}
PStatement scopeStatement = mCurrentStatement;
// repeat until reach global
while (scopeStatement) {
//add members of current scope that not added before
if (scopeStatement->kind == StatementKind::skClass) {
addChildren(scopeStatement, fileName, -1);
} else {
addChildren(scopeStatement, fileName, line);
}
// add members of all usings (in current scope ) and not added before
2021-08-29 00:48:23 +08:00
foreach (const QString& namespaceName,scopeStatement->usingList) {
2021-08-25 00:20:07 +08:00
PStatementList namespaceStatementsList =
mParser->findNamespace(namespaceName);
if (!namespaceStatementsList)
continue;
2021-08-29 00:48:23 +08:00
foreach (const PStatement& namespaceStatement,*namespaceStatementsList) {
2021-08-25 00:20:07 +08:00
addChildren(namespaceStatement, fileName, line);
}
}
scopeStatement=scopeStatement->parentScope.lock();
}
// add all global members and not added before
addChildren(nullptr, fileName, line);
// add members of all fusings
mUsings = mParser->getFileUsings(fileName);
2021-08-29 00:48:23 +08:00
foreach (const QString& namespaceName, mUsings) {
2021-08-25 00:20:07 +08:00
PStatementList namespaceStatementsList =
mParser->findNamespace(namespaceName);
if (!namespaceStatementsList)
continue;
2021-08-29 00:48:23 +08:00
foreach (const PStatement& namespaceStatement, *namespaceStatementsList) {
2021-08-25 00:20:07 +08:00
addChildren(namespaceStatement, fileName, line);
}
}
} else { //we are in some statement's scope
MemberOperatorType opType=getOperatorType(phrase,i);
QString scopeName = phrase.mid(0,i);
if (opType == MemberOperatorType::otDColon) {
if (scopeName.isEmpty()) {
// start with '::', we only find in global
// add all global members and not added before
addChildren(nullptr, fileName, line);
return;
} else {
//assume the scope its a namespace
PStatementList namespaceStatementsList =
mParser->findNamespace(scopeName);
if (namespaceStatementsList) {
2021-08-29 00:48:23 +08:00
foreach (const PStatement& namespaceStatement, *namespaceStatementsList) {
2021-08-25 00:20:07 +08:00
addChildren(namespaceStatement, fileName, line);
}
return;
}
//namespace not found let's go on
}
}
PStatement parentTypeStatement;
PStatement statement = mParser->findStatementOf(
fileName, scopeName,mCurrentStatement,parentTypeStatement);
if (!statement)
return;
// find the most inner scope statement that has a name (not a block)
PStatement scopeTypeStatement = mCurrentStatement;
while (scopeTypeStatement && !isScopeTypeKind(scopeTypeStatement->kind)) {
scopeTypeStatement = scopeTypeStatement->parentScope.lock();
}
if (
(opType == MemberOperatorType::otArrow
|| opType == MemberOperatorType::otDot)
&& (
statement->kind == StatementKind::skVariable
|| statement->kind == StatementKind::skParameter
|| statement->kind == StatementKind::skFunction)
) {
// Get type statement of current (scope) statement
PStatement classTypeStatement;
PStatement parentScope = statement->parentScope.lock();
if ((statement->kind == StatementKind::skFunction)
&& parentScope
&& STLContainers.contains(parentScope->fullName)
&& STLElementMethods.contains(statement->command)){
// it's an element method of STL container
// we must find the type in the template parameter
// get the function's owner variable's definition
int lastI = mParser->findLastOperator(scopeName);
QString lastScopeName = scopeName.mid(0,lastI);
PStatement lastScopeStatement =
mParser->findStatementOf(
fileName, lastScopeName,
mCurrentStatement,parentTypeStatement);
if (!lastScopeStatement)
return;
QString typeName =
mParser->findFirstTemplateParamOf(
fileName,lastScopeStatement->type,
lastScopeStatement->parentScope.lock());
classTypeStatement = mParser->findTypeDefinitionOf(
fileName, typeName,
lastScopeStatement->parentScope.lock());
} else
classTypeStatement=mParser->findTypeDefinitionOf(
fileName, statement->type,parentTypeStatement);
if (!classTypeStatement)
return;
//is a smart pointer
if (STLPointers.contains(classTypeStatement->fullName)
&& (opType == MemberOperatorType::otArrow)) {
QString typeName= mParser->findFirstTemplateParamOf(
fileName,
statement->type,
parentScope);
classTypeStatement = mParser->findTypeDefinitionOf(
fileName,
typeName,
parentScope);
if (!classTypeStatement)
return;
}
//is a stl container operator[]
if (STLContainers.contains(classTypeStatement->fullName)
&& scopeName.endsWith(']')) {
QString typeName= mParser->findFirstTemplateParamOf(
fileName,
statement->type,
parentScope);
classTypeStatement = mParser->findTypeDefinitionOf(
fileName,
typeName,
parentScope);
if (!classTypeStatement)
return;
}
if (!isIncluded(classTypeStatement->fileName) &&
!isIncluded(classTypeStatement->definitionFileName))
return;
if ((classTypeStatement == scopeTypeStatement) || (statement->command == "this")) {
//we can use all members
addChildren(classTypeStatement,fileName,-1);
} else { // we can only use public members
const StatementMap& children = mParser->statementList().childrenStatements(classTypeStatement);
if (children.isEmpty())
return;
2021-08-29 00:48:23 +08:00
foreach (const PStatement& childStatement, children) {
2021-08-25 00:20:07 +08:00
if ((childStatement->classScope==StatementClassScope::scsPublic)
&& !(
childStatement->kind == StatementKind::skConstructor
|| childStatement->kind == StatementKind::skDestructor)
&& !mAddedStatements.contains(childStatement->command)) {
addStatement(childStatement,fileName,-1);
}
}
}
//todo friend
} else if ((opType == MemberOperatorType::otDColon)
&& ((statement->kind == StatementKind::skEnumType)
|| (statement->kind == StatementKind::skEnumClassType))) {
2021-08-25 00:20:07 +08:00
//we can add all child enum definess
PStatement classTypeStatement = statement;
if (!isIncluded(classTypeStatement->fileName) &&
!isIncluded(classTypeStatement->definitionFileName))
return;
const StatementMap& children =
mParser->statementList().childrenStatements(classTypeStatement);
2021-08-29 00:48:23 +08:00
foreach (const PStatement& child,children) {
2021-08-25 00:20:07 +08:00
addStatement(child,fileName,line);
}
} else if ((opType == MemberOperatorType::otDColon)
&& (statement->kind == StatementKind::skClass)) {
PStatement classTypeStatement = statement;
if (!isIncluded(classTypeStatement->fileName) &&
!isIncluded(classTypeStatement->definitionFileName))
return;
if (classTypeStatement == scopeTypeStatement) {
//we can use all static members
const StatementMap& children =
mParser->statementList().childrenStatements(classTypeStatement);
2021-08-29 00:48:23 +08:00
foreach (const PStatement& childStatement, children) {
2021-08-25 00:20:07 +08:00
if (
(childStatement->isStatic)
|| (childStatement->kind == StatementKind::skTypedef
|| childStatement->kind == StatementKind::skClass
|| childStatement->kind == StatementKind::skEnum
|| childStatement->kind == StatementKind::skEnumClassType
|| childStatement->kind == StatementKind::skEnumType
)) {
addStatement(childStatement,fileName,-1);
}
}
} else {
// we can only use public static members
const StatementMap& children =
mParser->statementList().childrenStatements(classTypeStatement);
2021-08-29 00:48:23 +08:00
foreach (const PStatement& childStatement,children) {
2021-08-25 00:20:07 +08:00
if (
(childStatement->isStatic)
|| (childStatement->kind == StatementKind::skTypedef
|| childStatement->kind == StatementKind::skClass
|| childStatement->kind == StatementKind::skEnum
|| childStatement->kind == StatementKind::skEnumClassType
|| childStatement->kind == StatementKind::skEnumType
)) {
if (childStatement->classScope == StatementClassScope::scsPublic)
addStatement(childStatement,fileName,-1);
}
}
}
//todo friend
}
}
2021-08-24 15:05:10 +08:00
}
2021-08-25 00:20:07 +08:00
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::isIncluded(const QString &fileName)
2021-08-25 00:20:07 +08:00
{
return mIncludedFiles.contains(fileName);
}
2021-09-30 20:10:48 +08:00
const QList<PCodeSnippet> &CodeCompletionPopup::codeSnippets() const
{
return mCodeSnippets;
}
void CodeCompletionPopup::setCodeSnippets(const QList<PCodeSnippet> &newCodeSnippets)
{
mCodeSnippets = newCodeSnippets;
}
2021-09-25 23:12:36 +08:00
void CodeCompletionPopup::setColors(const std::shared_ptr<QHash<StatementKind, QColor> > &newColors)
{
mColors = newColors;
}
const QString &CodeCompletionPopup::phrase() const
{
return mPhrase;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::showEvent(QShowEvent *)
2021-08-26 17:48:23 +08:00
{
mListView->setFocus();
}
2021-08-29 00:48:23 +08:00
const PStatement &CodeCompletionPopup::currentStatement() const
2021-08-25 23:53:35 +08:00
{
return mCurrentStatement;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setCurrentStatement(const PStatement &newCurrentStatement)
2021-08-25 23:53:35 +08:00
{
mCurrentStatement = newCurrentStatement;
}
2021-09-25 23:12:36 +08:00
const std::shared_ptr<QHash<StatementKind, QColor> >& CodeCompletionPopup::colors() const
2021-08-28 09:01:40 +08:00
{
return mColors;
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::useCppKeyword() const
2021-08-25 23:53:35 +08:00
{
return mUseCppKeyword;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setUseCppKeyword(bool newUseCppKeyword)
2021-08-25 23:53:35 +08:00
{
mUseCppKeyword = newUseCppKeyword;
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::sortByScope() const
2021-08-25 23:53:35 +08:00
{
return mSortByScope;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setSortByScope(bool newSortByScope)
2021-08-25 23:53:35 +08:00
{
mSortByScope = newSortByScope;
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::ignoreCase() const
2021-08-25 23:53:35 +08:00
{
return mIgnoreCase;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setIgnoreCase(bool newIgnoreCase)
2021-08-25 23:53:35 +08:00
{
mIgnoreCase = newIgnoreCase;
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::showCodeIns() const
2021-08-25 23:53:35 +08:00
{
2021-09-30 20:10:48 +08:00
return mShowCodeSnippets;
2021-08-25 23:53:35 +08:00
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setShowCodeIns(bool newShowCodeIns)
2021-08-25 23:53:35 +08:00
{
2021-09-30 20:10:48 +08:00
mShowCodeSnippets = newShowCodeIns;
2021-08-25 23:53:35 +08:00
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::showKeywords() const
2021-08-25 23:53:35 +08:00
{
return mShowKeywords;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setShowKeywords(bool newShowKeywords)
2021-08-25 23:53:35 +08:00
{
mShowKeywords = newShowKeywords;
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::recordUsage() const
2021-08-25 23:53:35 +08:00
{
return mRecordUsage;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setRecordUsage(bool newRecordUsage)
2021-08-25 23:53:35 +08:00
{
mRecordUsage = newRecordUsage;
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::onlyGlobals() const
2021-08-25 23:53:35 +08:00
{
return mOnlyGlobals;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setOnlyGlobals(bool newOnlyGlobals)
2021-08-25 23:53:35 +08:00
{
mOnlyGlobals = newOnlyGlobals;
}
2021-08-29 00:48:23 +08:00
int CodeCompletionPopup::showCount() const
2021-08-25 23:53:35 +08:00
{
return mShowCount;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setShowCount(int newShowCount)
2021-08-25 23:53:35 +08:00
{
mShowCount = newShowCount;
}
2021-08-29 00:48:23 +08:00
const PCppParser &CodeCompletionPopup::parser() const
2021-08-25 23:53:35 +08:00
{
return mParser;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::setParser(const PCppParser &newParser)
2021-08-25 23:53:35 +08:00
{
mParser = newParser;
}
2021-08-29 00:48:23 +08:00
void CodeCompletionPopup::hideEvent(QHideEvent *event)
2021-08-25 00:20:07 +08:00
{
QMutexLocker locker(&mMutex);
2021-08-25 08:48:33 +08:00
mListView->setKeypressedCallback(nullptr);
2021-08-25 00:20:07 +08:00
mCompletionStatementList.clear();
mFullCompletionStatementList.clear();
mIncludedFiles.clear();
mUsings.clear();
mAddedStatements.clear();
QWidget::hideEvent(event);
2021-08-24 15:05:10 +08:00
}
2021-08-29 00:48:23 +08:00
bool CodeCompletionPopup::event(QEvent *event)
{
2021-08-27 16:38:55 +08:00
bool result = QWidget::event(event);
if (event->type() == QEvent::FontChange) {
2021-08-27 16:38:55 +08:00
mListView->setFont(font());
}
2021-08-27 16:38:55 +08:00
return result;
}
2021-08-29 00:48:23 +08:00
CodeCompletionListModel::CodeCompletionListModel(const StatementList *statements, QObject *parent):
2021-08-25 08:48:33 +08:00
QAbstractListModel(parent),
mStatements(statements)
{
}
2021-08-25 23:53:35 +08:00
int CodeCompletionListModel::rowCount(const QModelIndex &) const
2021-08-25 08:48:33 +08:00
{
return mStatements->count();
}
QVariant CodeCompletionListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
2021-08-26 17:48:23 +08:00
if (index.row()>=mStatements->count())
return QVariant();
2021-08-28 09:01:40 +08:00
switch(role) {
case Qt::DisplayRole: {
2021-08-25 08:48:33 +08:00
PStatement statement = mStatements->at(index.row());
return statement->command;
2021-08-28 09:01:40 +08:00
}
case Qt::ForegroundRole: {
PStatement statement = mStatements->at(index.row());
if (mColorCallback)
return mColorCallback(statement);
QApplication *app = dynamic_cast<QApplication *>(QApplication::instance());
return app->palette().color(QPalette::Text);
}
2021-08-25 08:48:33 +08:00
}
return QVariant();
}
void CodeCompletionListModel::notifyUpdated()
{
beginResetModel();
endResetModel();
}
2021-08-28 09:01:40 +08:00
const ColorCallback &CodeCompletionListModel::colorCallback() const
{
return mColorCallback;
}
void CodeCompletionListModel::setColorCallback(const ColorCallback &newColorCallback)
{
mColorCallback = newColorCallback;
}