2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-08-29 00:48:23 +08:00
|
|
|
#ifndef CODECOMPLETIONPOPUP_H
|
|
|
|
#define CODECOMPLETIONPOPUP_H
|
2021-08-23 21:50:53 +08:00
|
|
|
|
2021-08-24 09:59:44 +08:00
|
|
|
#include <QListView>
|
2021-08-23 21:50:53 +08:00
|
|
|
#include <QWidget>
|
2021-08-24 15:05:10 +08:00
|
|
|
#include "parser/cppparser.h"
|
2021-08-29 00:48:23 +08:00
|
|
|
#include "codecompletionlistview.h"
|
2021-08-28 09:01:40 +08:00
|
|
|
|
2021-10-10 21:23:25 +08:00
|
|
|
class ColorSchemeItem;
|
2021-08-25 08:48:33 +08:00
|
|
|
class CodeCompletionListModel : public QAbstractListModel {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2021-08-29 00:48:23 +08:00
|
|
|
explicit CodeCompletionListModel(const StatementList* statements,QObject *parent = nullptr);
|
2021-08-25 08:48:33 +08:00
|
|
|
int rowCount(const QModelIndex &parent) const override;
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override;
|
2022-01-27 20:31:44 +08:00
|
|
|
PStatement statement(const QModelIndex &index) const;
|
|
|
|
QPixmap statementIcon(const QModelIndex &index) const;
|
2021-08-25 08:48:33 +08:00
|
|
|
void notifyUpdated();
|
2021-08-28 09:01:40 +08:00
|
|
|
|
2021-08-25 08:48:33 +08:00
|
|
|
private:
|
|
|
|
const StatementList* mStatements;
|
2022-01-27 20:31:44 +08:00
|
|
|
};
|
|
|
|
|
2022-11-10 09:05:34 +08:00
|
|
|
enum class CodeCompletionType {
|
|
|
|
Normal,
|
2022-11-25 10:01:10 +08:00
|
|
|
ComplexKeyword,
|
2022-11-27 13:32:14 +08:00
|
|
|
FunctionWithoutDefinition,
|
2023-02-09 21:01:01 +08:00
|
|
|
Namespaces,
|
2023-07-03 09:36:27 +08:00
|
|
|
Types,
|
2023-02-09 21:01:01 +08:00
|
|
|
KeywordsOnly
|
2022-11-10 09:05:34 +08:00
|
|
|
};
|
|
|
|
|
2022-01-27 20:31:44 +08:00
|
|
|
class CodeCompletionListItemDelegate: public QStyledItemDelegate {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
CodeCompletionListItemDelegate(CodeCompletionListModel *model=nullptr, QWidget *parent = nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
// QAbstractItemDelegate interface
|
|
|
|
public:
|
|
|
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
|
|
|
CodeCompletionListModel *model() const;
|
|
|
|
void setModel(CodeCompletionListModel *newModel);
|
|
|
|
|
|
|
|
const QColor &normalColor() const;
|
|
|
|
void setNormalColor(const QColor &newNormalColor);
|
|
|
|
|
|
|
|
const QColor &matchedColor() const;
|
|
|
|
void setMatchedColor(const QColor &newMatchedColor);
|
|
|
|
|
2022-02-11 20:19:48 +08:00
|
|
|
const QFont &font() const;
|
|
|
|
void setFont(const QFont &newFont);
|
|
|
|
|
2022-01-27 20:31:44 +08:00
|
|
|
private:
|
|
|
|
CodeCompletionListModel *mModel;
|
|
|
|
QColor mNormalColor;
|
|
|
|
QColor mMatchedColor;
|
2022-02-11 20:19:48 +08:00
|
|
|
QFont mFont;
|
2021-08-25 08:48:33 +08:00
|
|
|
};
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
class CodeCompletionPopup : public QWidget
|
2021-08-23 21:50:53 +08:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2021-08-29 00:48:23 +08:00
|
|
|
explicit CodeCompletionPopup(QWidget *parent = nullptr);
|
|
|
|
~CodeCompletionPopup();
|
2021-08-23 21:50:53 +08:00
|
|
|
|
2021-08-24 09:59:44 +08:00
|
|
|
void setKeypressedCallback(const KeyPressedCallback &newKeypressedCallback);
|
2021-12-04 10:02:07 +08:00
|
|
|
void prepareSearch(const QString& preWord,
|
|
|
|
const QStringList & ownerExpression,
|
|
|
|
const QString& memberOperator,
|
|
|
|
const QStringList& memberExpression,
|
|
|
|
const QString& filename,
|
2022-01-23 23:27:48 +08:00
|
|
|
int line,
|
2022-11-10 13:35:13 +08:00
|
|
|
CodeCompletionType completionType,
|
2022-01-23 23:27:48 +08:00
|
|
|
const QSet<QString>& customKeywords);
|
2021-12-04 18:38:54 +08:00
|
|
|
bool search(const QString& memberPhrase, bool autoHideOnSingleResult);
|
2021-08-25 00:20:07 +08:00
|
|
|
|
2021-08-26 17:48:23 +08:00
|
|
|
PStatement selectedStatement();
|
2021-08-24 09:59:44 +08:00
|
|
|
|
2021-08-25 23:53:35 +08:00
|
|
|
const PCppParser &parser() const;
|
|
|
|
void setParser(const PCppParser &newParser);
|
|
|
|
|
|
|
|
int showCount() const;
|
|
|
|
void setShowCount(int newShowCount);
|
|
|
|
|
|
|
|
bool recordUsage() const;
|
|
|
|
void setRecordUsage(bool newRecordUsage);
|
|
|
|
|
|
|
|
bool showKeywords() const;
|
|
|
|
void setShowKeywords(bool newShowKeywords);
|
|
|
|
|
2021-09-30 21:25:48 +08:00
|
|
|
bool showCodeSnippets() const;
|
|
|
|
void setShowCodeSnippets(bool newShowCodeIns);
|
2021-08-25 23:53:35 +08:00
|
|
|
|
|
|
|
bool ignoreCase() const;
|
|
|
|
void setIgnoreCase(bool newIgnoreCase);
|
|
|
|
|
|
|
|
bool sortByScope() const;
|
|
|
|
void setSortByScope(bool newSortByScope);
|
|
|
|
|
2022-03-01 22:03:54 +08:00
|
|
|
bool hideSymbolsStartWithUnderline() const;
|
|
|
|
void setHideSymbolsStartWithUnderline(bool newHideSymbolsStartWithUnderline);
|
|
|
|
bool hideSymbolsStartWithTwoUnderline() const;
|
|
|
|
void setHideSymbolsStartWithTwoUnderline(bool newHideSymbolsStartWithTwoUnderline);
|
|
|
|
|
2022-11-10 08:05:04 +08:00
|
|
|
const PStatement ¤tScope() const;
|
|
|
|
void setCurrentScope(const PStatement &newCurrentStatement);
|
2021-10-10 21:23:25 +08:00
|
|
|
const std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > >& colors() const;
|
|
|
|
void setColors(const std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > &newColors);
|
2021-12-04 10:02:07 +08:00
|
|
|
const QString &memberPhrase() const;
|
|
|
|
const QList<PCodeSnippet> &codeSnippets() const;
|
|
|
|
void setCodeSnippets(const QList<PCodeSnippet> &newCodeSnippets);
|
2021-08-24 15:05:10 +08:00
|
|
|
private:
|
2022-11-28 11:28:02 +08:00
|
|
|
void addChildren(const PStatement& scopeStatement, const QString& fileName,
|
2023-05-26 08:55:21 +08:00
|
|
|
int line, bool onlyTypes=false);
|
2022-11-28 11:28:02 +08:00
|
|
|
void addFunctionWithoutDefinitionChildren(const PStatement& scopeStatement, const QString& fileName,
|
2022-11-10 13:35:13 +08:00
|
|
|
int line);
|
2022-11-28 11:28:02 +08:00
|
|
|
void addStatement(const PStatement& statement, const QString& fileName, int line);
|
2021-08-24 15:05:10 +08:00
|
|
|
void filterList(const QString& member);
|
2023-02-09 21:01:01 +08:00
|
|
|
void getKeywordCompletionFor(const QSet<QString>& customKeywords);
|
2021-12-04 10:02:07 +08:00
|
|
|
void getCompletionFor(
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList ownerExpression,
|
2021-12-04 10:02:07 +08:00
|
|
|
const QString& memberOperator,
|
|
|
|
const QStringList& memberExpression,
|
|
|
|
const QString& fileName,
|
2022-01-23 23:27:48 +08:00
|
|
|
int line,
|
|
|
|
const QSet<QString>& customKeywords);
|
2022-11-10 13:35:13 +08:00
|
|
|
|
|
|
|
void getCompletionForFunctionWithoutDefinition(
|
|
|
|
const QString& preWord,
|
2022-12-11 19:47:43 +08:00
|
|
|
QStringList ownerExpression,
|
2022-11-10 13:35:13 +08:00
|
|
|
const QString& memberOperator,
|
|
|
|
const QStringList& memberExpression,
|
|
|
|
const QString& fileName,
|
|
|
|
int line);
|
|
|
|
|
2023-07-03 09:36:27 +08:00
|
|
|
void getCompletionListForComplexKeyword(const QString& preWord);
|
2022-11-27 13:32:14 +08:00
|
|
|
void getCompletionListForNamespaces(const QString &preWord,
|
|
|
|
const QString& fileName,
|
|
|
|
int line);
|
2023-07-03 09:36:27 +08:00
|
|
|
void getCompletionListForTypes(const QString &preWord,
|
|
|
|
const QString& fileName,
|
|
|
|
int line);
|
2021-11-18 12:51:05 +08:00
|
|
|
void addKeyword(const QString& keyword);
|
2021-08-24 15:05:10 +08:00
|
|
|
bool isIncluded(const QString& fileName);
|
2021-08-23 21:50:53 +08:00
|
|
|
private:
|
2021-08-24 09:59:44 +08:00
|
|
|
CodeCompletionListView * mListView;
|
2021-08-25 08:48:33 +08:00
|
|
|
CodeCompletionListModel* mModel;
|
2021-09-30 21:25:48 +08:00
|
|
|
QList<PCodeSnippet> mCodeSnippets; //(Code template list)
|
2021-08-24 15:05:10 +08:00
|
|
|
//QList<PStatement> mCodeInsStatements; //temporary (user code template) statements created when show code suggestion
|
2021-08-25 08:48:33 +08:00
|
|
|
StatementList mFullCompletionStatementList;
|
|
|
|
StatementList mCompletionStatementList;
|
2021-08-24 15:05:10 +08:00
|
|
|
QSet<QString> mIncludedFiles;
|
|
|
|
QSet<QString> mUsings;
|
|
|
|
QSet<QString> mAddedStatements;
|
2021-12-04 10:02:07 +08:00
|
|
|
QString mMemberPhrase;
|
|
|
|
QString mMemberOperator;
|
2022-11-10 09:05:34 +08:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
|
|
|
|
QRecursiveMutex mMutex;
|
|
|
|
#else
|
2022-01-04 16:50:54 +08:00
|
|
|
QMutex mMutex;
|
2022-11-10 09:05:34 +08:00
|
|
|
#endif
|
2021-10-10 21:23:25 +08:00
|
|
|
std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > mColors;
|
2022-01-27 20:31:44 +08:00
|
|
|
CodeCompletionListItemDelegate* mDelegate;
|
2021-08-25 23:53:35 +08:00
|
|
|
|
|
|
|
PCppParser mParser;
|
2022-11-10 08:05:04 +08:00
|
|
|
PStatement mCurrentScope;
|
2021-08-25 23:53:35 +08:00
|
|
|
int mShowCount;
|
2021-08-24 15:05:10 +08:00
|
|
|
bool mRecordUsage;
|
|
|
|
bool mShowKeywords;
|
2021-09-30 21:25:48 +08:00
|
|
|
bool mShowCodeSnippets;
|
2021-08-24 15:05:10 +08:00
|
|
|
bool mIgnoreCase;
|
|
|
|
bool mSortByScope;
|
2022-03-01 22:03:54 +08:00
|
|
|
bool mHideSymbolsStartWithUnderline;
|
|
|
|
bool mHideSymbolsStartWithTwoUnderline;
|
2021-08-25 00:20:07 +08:00
|
|
|
|
|
|
|
// QWidget interface
|
|
|
|
protected:
|
|
|
|
void hideEvent(QHideEvent *event) override;
|
2021-08-27 08:52:20 +08:00
|
|
|
|
|
|
|
// QObject interface
|
|
|
|
public:
|
|
|
|
bool event(QEvent *event) override;
|
2021-12-04 10:02:07 +08:00
|
|
|
const QString &memberOperator() const;
|
2022-03-01 22:03:54 +08:00
|
|
|
|
2021-08-23 21:50:53 +08:00
|
|
|
};
|
|
|
|
|
2021-08-29 00:48:23 +08:00
|
|
|
#endif // CODECOMPLETIONPOPUP_H
|