1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#ifndef CODECOMPLETIONPOPUP_H
18#define CODECOMPLETIONPOPUP_H
19
20#include <QListView>
21#include <QWidget>
22#include "parser/cppparser.h"
23#include "codecompletionlistview.h"
24
25class ColorSchemeItem;
26class CodeCompletionListModel : public QAbstractListModel {
27 Q_OBJECT
28public:
29 explicit CodeCompletionListModel(const StatementList* statements,QObject *parent = nullptr);
30 int rowCount(const QModelIndex &parent) const override;
31 QVariant data(const QModelIndex &index, int role) const override;
32 PStatement statement(const QModelIndex &index) const;
33 QPixmap statementIcon(const QModelIndex &index) const;
34 void notifyUpdated();
35
36private:
37 const StatementList* mStatements;
38};
39
40class CodeCompletionListItemDelegate: public QStyledItemDelegate {
41 Q_OBJECT
42public:
43 CodeCompletionListItemDelegate(CodeCompletionListModel *model=nullptr, QWidget *parent = nullptr);
44
45
46 // QAbstractItemDelegate interface
47public:
48 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
49 CodeCompletionListModel *model() const;
50 void setModel(CodeCompletionListModel *newModel);
51
52 const QColor &normalColor() const;
53 void setNormalColor(const QColor &newNormalColor);
54
55 const QColor &matchedColor() const;
56 void setMatchedColor(const QColor &newMatchedColor);
57
58 const QFont &font() const;
59 void setFont(const QFont &newFont);
60
61private:
62 CodeCompletionListModel *mModel;
63 QColor mNormalColor;
64 QColor mMatchedColor;
65 QFont mFont;
66};
67
68class CodeCompletionPopup : public QWidget
69{
70 Q_OBJECT
71
72public:
73 explicit CodeCompletionPopup(QWidget *parent = nullptr);
74 ~CodeCompletionPopup();
75
76 void setKeypressedCallback(const KeyPressedCallback &newKeypressedCallback);
77 void prepareSearch(const QString& preWord,
78 const QStringList & ownerExpression,
79 const QString& memberOperator,
80 const QStringList& memberExpression,
81 const QString& filename,
82 int line,
83 const QSet<QString>& customKeywords);
84 bool search(const QString& memberPhrase, bool autoHideOnSingleResult);
85
86 PStatement selectedStatement();
87
88 const PCppParser &parser() const;
89 void setParser(const PCppParser &newParser);
90
91 int showCount() const;
92 void setShowCount(int newShowCount);
93
94 bool onlyGlobals() const;
95 void setOnlyGlobals(bool newOnlyGlobals);
96
97 bool recordUsage() const;
98 void setRecordUsage(bool newRecordUsage);
99
100 bool showKeywords() const;
101 void setShowKeywords(bool newShowKeywords);
102
103 bool showCodeSnippets() const;
104 void setShowCodeSnippets(bool newShowCodeIns);
105
106 bool ignoreCase() const;
107 void setIgnoreCase(bool newIgnoreCase);
108
109 bool sortByScope() const;
110 void setSortByScope(bool newSortByScope);
111
112 bool useCppKeyword() const;
113 void setUseCppKeyword(bool newUseCppKeyword);
114
115 bool hideSymbolsStartWithUnderline() const;
116 void setHideSymbolsStartWithUnderline(bool newHideSymbolsStartWithUnderline);
117 bool hideSymbolsStartWithTwoUnderline() const;
118 void setHideSymbolsStartWithTwoUnderline(bool newHideSymbolsStartWithTwoUnderline);
119
120 const PStatement &currentStatement() const;
121 void setCurrentStatement(const PStatement &newCurrentStatement);
122 const std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > >& colors() const;
123 void setColors(const std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > &newColors);
124 const QString &memberPhrase() const;
125 const QList<PCodeSnippet> &codeSnippets() const;
126 void setCodeSnippets(const QList<PCodeSnippet> &newCodeSnippets);
127private:
128 void addChildren(PStatement scopeStatement, const QString& fileName,
129 int line);
130 void addStatement(PStatement statement, const QString& fileName, int line);
131 void filterList(const QString& member);
132 void getCompletionFor(
133 const QStringList& ownerExpression,
134 const QString& memberOperator,
135 const QStringList& memberExpression,
136 const QString& fileName,
137 int line,
138 const QSet<QString>& customKeywords);
139 void getCompletionListForPreWord(const QString& preWord);
140 void addKeyword(const QString& keyword);
141 bool isIncluded(const QString& fileName);
142private:
143 CodeCompletionListView * mListView;
144 CodeCompletionListModel* mModel;
145 QList<PCodeSnippet> mCodeSnippets; //(Code template list)
146 //QList<PStatement> mCodeInsStatements; //temporary (user code template) statements created when show code suggestion
147 StatementList mFullCompletionStatementList;
148 StatementList mCompletionStatementList;
149 QSet<QString> mIncludedFiles;
150 QSet<QString> mUsings;
151 QSet<QString> mAddedStatements;
152 QString mMemberPhrase;
153 QString mMemberOperator;
154 QMutex mMutex;
155 std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > mColors;
156 CodeCompletionListItemDelegate* mDelegate;
157
158 PCppParser mParser;
159 PStatement mCurrentStatement;
160 int mShowCount;
161 bool mOnlyGlobals;
162 bool mRecordUsage;
163 bool mShowKeywords;
164 bool mShowCodeSnippets;
165 bool mIgnoreCase;
166 bool mSortByScope;
167 bool mUseCppKeyword;
168 bool mHideSymbolsStartWithUnderline;
169 bool mHideSymbolsStartWithTwoUnderline;
170
171 // QWidget interface
172protected:
173 void showEvent(QShowEvent *event) override;
174 void hideEvent(QHideEvent *event) override;
175
176 // QObject interface
177public:
178 bool event(QEvent *event) override;
179 const QString &memberOperator() const;
180
181};
182
183#endif // CODECOMPLETIONPOPUP_H
184