fix #300 - enhancement: Show type completion hint after "constexpr"/"extern"/"static"/"consteval"/"constinit"/"const"/"volatile" etc.

This commit is contained in:
Roy Qu 2024-03-22 19:53:09 +08:00
parent 0782994f95
commit e291a2e2fb
3 changed files with 25 additions and 15 deletions

View File

@ -60,7 +60,6 @@ Red Panda C++ Version 2.27
- fix: Custom compile options not correctly parsed. - fix: Custom compile options not correctly parsed.
- enhancement: "Mouse scroll direction" option in Options / Editor / General - enhancement: "Mouse scroll direction" option in Options / Editor / General
- change: Invert scroll direction in horizontal, like in vertical. - change: Invert scroll direction in horizontal, like in vertical.
- enhancement: Show type completion info after 'const' and 'volatile'
- fix: Caret unseen when move to a long line end by press END. - fix: Caret unseen when move to a long line end by press END.
- fix: No icons for inherited class private members. - fix: No icons for inherited class private members.
- fix: Ctrl+Return insert linebreak shouldn't scroll unnecessarilly. - fix: Ctrl+Return insert linebreak shouldn't scroll unnecessarilly.
@ -78,7 +77,7 @@ Red Panda C++ Version 2.27
- enhancement: Improvement in italic font support. - enhancement: Improvement in italic font support.
- fix: History not correctly loaded with up/down arrow key in the debug console. - fix: History not correctly loaded with up/down arrow key in the debug console.
- enhancement: Improve lambda support. - enhancement: Improve lambda support.
- enhancement: Show type completion hint after "constexpr"/"extern"/"static"/"consteval"/"constinit"/"const"/"volatile" etc.
Red Panda C++ Version 2.26 Red Panda C++ Version 2.26
- enhancement: Code suggestion for embedded std::vectors. - enhancement: Code suggestion for embedded std::vectors.

View File

@ -58,6 +58,17 @@
QHash<ParserLanguage,std::weak_ptr<CppParser>> Editor::mSharedParsers; QHash<ParserLanguage,std::weak_ptr<CppParser>> Editor::mSharedParsers;
static QSet<QString> CppTypeQualifiers {
"const",
"consteval",
"constexpr",
"constinit",
"extern",
"static",
"mutable",
"volatile",
};
Editor::Editor(QWidget *parent): Editor::Editor(QWidget *parent):
Editor(parent,"untitled",ENCODING_AUTO_DETECT,nullptr,true,nullptr) Editor(parent,"untitled",ENCODING_AUTO_DETECT,nullptr,true,nullptr)
{ {
@ -875,11 +886,11 @@ void Editor::keyPressEvent(QKeyEvent *event)
handled=true; handled=true;
return; return;
} else { } else {
bool hasConst; bool hasTypeQualifier;
QString lastWord = getPreviousWordAtPositionForSuggestion(ws, hasConst); QString lastWord = getPreviousWordAtPositionForSuggestion(ws, hasTypeQualifier);
if (mParser && (!lastWord.isEmpty() || hasConst)) { if (mParser && (!lastWord.isEmpty() || hasTypeQualifier)) {
if (lastWord.isEmpty()) { if (lastWord.isEmpty()) {
Q_ASSERT(hasConst); Q_ASSERT(hasTypeQualifier);
processCommand(QSynedit::EditCommand::Char,ch,nullptr); processCommand(QSynedit::EditCommand::Char,ch,nullptr);
showCompletion(lastWord,false, CodeCompletionType::Types); showCompletion(lastWord,false, CodeCompletionType::Types);
handled=true; handled=true;
@ -1442,8 +1453,8 @@ void Editor::inputMethodEvent(QInputMethodEvent *event)
int idCharPressed=caretX()-ws.ch; int idCharPressed=caretX()-ws.ch;
idCharPressed += s.length(); idCharPressed += s.length();
if (idCharPressed>=pSettings->codeCompletion().minCharRequired()) { if (idCharPressed>=pSettings->codeCompletion().minCharRequired()) {
bool hasConst; bool hasTypeQualifier;
QString lastWord = getPreviousWordAtPositionForSuggestion(caretXY(), hasConst); QString lastWord = getPreviousWordAtPositionForSuggestion(caretXY(), hasTypeQualifier);
if (mParser && !lastWord.isEmpty()) { if (mParser && !lastWord.isEmpty()) {
if (CppTypeKeywords.contains(lastWord)) { if (CppTypeKeywords.contains(lastWord)) {
return; return;
@ -4326,9 +4337,9 @@ void Editor::updateFunctionTip(bool showTip)
//handle class initializer //handle class initializer
if (x >= 0 && hasPreviousWord) { if (x >= 0 && hasPreviousWord) {
QSynedit::BufferCoord pos = pWordBegin; QSynedit::BufferCoord pos = pWordBegin;
bool hasConst; bool hasTypeQualifier;
pos.ch = pWordBegin.ch; pos.ch = pWordBegin.ch;
QString previousWord = getPreviousWordAtPositionForSuggestion(pos, hasConst); QString previousWord = getPreviousWordAtPositionForSuggestion(pos, hasTypeQualifier);
PStatement statement = mParser->findStatementOf( PStatement statement = mParser->findStatementOf(
mFilename, mFilename,
@ -4920,9 +4931,9 @@ QString getWordAtPosition(QSynedit::QSynEdit *editor, const QSynedit::BufferCoor
return result; return result;
} }
QString Editor::getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoord &p, bool &hasConst) QString Editor::getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoord &p, bool &hasTypeQualifier)
{ {
hasConst = false; hasTypeQualifier = false;
QString result; QString result;
if ((p.line<1) || (p.line>document()->count())) { if ((p.line<1) || (p.line>document()->count())) {
return ""; return "";
@ -4978,8 +4989,8 @@ QString Editor::getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoo
return ""; return "";
result = s.mid(wordBegin, wordEnd - wordBegin+1); result = s.mid(wordBegin, wordEnd - wordBegin+1);
if (result == "const" || result == "volatile") if (CppTypeQualifiers.contains(result))
hasConst = true; hasTypeQualifier = true;
else if (!skipNextWord) else if (!skipNextWord)
break; break;
// if ((result != "const") && !skipNextWord) // if ((result != "const") && !skipNextWord)

View File

@ -184,7 +184,7 @@ public:
void removeBreakpointFocus(); void removeBreakpointFocus();
void modifyBreakpointProperty(int line); void modifyBreakpointProperty(int line);
void setActiveBreakpointFocus(int Line, bool setFocus=true); void setActiveBreakpointFocus(int Line, bool setFocus=true);
QString getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoord& p, bool &hasConst); QString getPreviousWordAtPositionForSuggestion(const QSynedit::BufferCoord& p, bool &hasTypeQualifier);
QString getPreviousWordAtPositionForCompleteFunctionDefinition(const QSynedit::BufferCoord& p); QString getPreviousWordAtPositionForCompleteFunctionDefinition(const QSynedit::BufferCoord& p);
void reformat(bool doReparse=true); void reformat(bool doReparse=true);
void checkSyntaxInBack(); void checkSyntaxInBack();