- fix: Code completion doesn't work if "min id length to show completion" is not 1.

This commit is contained in:
Roy Qu 2023-07-15 08:20:26 +08:00
parent 2943226e46
commit b579139191
2 changed files with 20 additions and 43 deletions

View File

@ -77,7 +77,6 @@ Editor::Editor(QWidget *parent, const QString& filename,
mSyntaxWarningColor{"orange"}, mSyntaxWarningColor{"orange"},
mLineCount{0}, mLineCount{0},
mActiveBreakpointLine{-1}, mActiveBreakpointLine{-1},
mLastIdCharPressed{0},
mCurrentTipType{TipType::None}, mCurrentTipType{TipType::None},
mSaving{false}, mSaving{false},
mHoverModifiedLine{-1}, mHoverModifiedLine{-1},
@ -285,7 +284,6 @@ void Editor::loadFile(QString filename) {
reparseTodo(); reparseTodo();
} }
mLastIdCharPressed = 0;
saveAutoBackup(); saveAutoBackup();
} }
@ -702,7 +700,6 @@ void Editor::keyPressEvent(QKeyEvent *event)
switch (event->key()) { switch (event->key()) {
case Qt::Key_Return: case Qt::Key_Return:
case Qt::Key_Enter: case Qt::Key_Enter:
mLastIdCharPressed = 0;
if (mTabStopBegin>=0) { // editing user code template if (mTabStopBegin>=0) { // editing user code template
handled = true; handled = true;
mTabStopBegin = -1; mTabStopBegin = -1;
@ -783,7 +780,6 @@ void Editor::keyPressEvent(QKeyEvent *event)
} }
return; return;
case Qt::Key_Escape: // Update function tip case Qt::Key_Escape: // Update function tip
mLastIdCharPressed = 0;
if (mTabStopBegin>=0) { if (mTabStopBegin>=0) {
mTabStopBegin = -1; mTabStopBegin = -1;
setBlockEnd(caretXY()); setBlockEnd(caretXY());
@ -805,7 +801,6 @@ void Editor::keyPressEvent(QKeyEvent *event)
handled = true; handled = true;
pMainWindow->functionTip()->previousTip(); pMainWindow->functionTip()->previousTip();
} else { } else {
mLastIdCharPressed = 0;
clearUserCodeInTabStops(); clearUserCodeInTabStops();
} }
return; return;
@ -814,20 +809,17 @@ void Editor::keyPressEvent(QKeyEvent *event)
handled = true; handled = true;
pMainWindow->functionTip()->nextTip(); pMainWindow->functionTip()->nextTip();
} else { } else {
mLastIdCharPressed = 0;
clearUserCodeInTabStops(); clearUserCodeInTabStops();
} }
return; return;
case Qt::Key_Delete: case Qt::Key_Delete:
// remove completed character // remove completed character
mLastIdCharPressed = 0;
if (!selAvail()) { if (!selAvail()) {
undoSymbolCompletion(caretX()); undoSymbolCompletion(caretX());
} }
return; return;
case Qt::Key_Backspace: case Qt::Key_Backspace:
// remove completed character // remove completed character
mLastIdCharPressed = 0;
if (!selAvail()) { if (!selAvail()) {
undoSymbolCompletion(caretX()-1); undoSymbolCompletion(caretX()-1);
} }
@ -842,12 +834,14 @@ void Editor::keyPressEvent(QKeyEvent *event)
return; return;
QChar ch = t[0]; QChar ch = t[0];
QSynedit::BufferCoord ws=wordStart();
int idCharPressed=caretX()-ws.ch;
qDebug()<<idCharPressed;
if (isIdentChar(ch)) { if (isIdentChar(ch)) {
mLastIdCharPressed++; idCharPressed++;
qDebug()<<mLastIdCharPressed<<pSettings->codeCompletion().minCharRequired();
if (pSettings->codeCompletion().enabled() if (pSettings->codeCompletion().enabled()
&& pSettings->codeCompletion().showCompletionWhileInput() && pSettings->codeCompletion().showCompletionWhileInput()
&& mLastIdCharPressed==pSettings->codeCompletion().minCharRequired()) { && idCharPressed>=pSettings->codeCompletion().minCharRequired()) {
if (mParser) { if (mParser) {
if (mParser->isIncludeLine(lineText())) { if (mParser->isIncludeLine(lineText())) {
// is a #include line // is a #include line
@ -856,10 +850,7 @@ void Editor::keyPressEvent(QKeyEvent *event)
handled=true; handled=true;
return; return;
} else { } else {
QSynedit::BufferCoord cursor=caretXY(); QString lastWord = getPreviousWordAtPositionForSuggestion(ws);
cursor.ch = std::max(1, cursor.ch-mLastIdCharPressed+1);
QString lastWord = getPreviousWordAtPositionForSuggestion(cursor);
if (mParser && !lastWord.isEmpty()) { if (mParser && !lastWord.isEmpty()) {
if (lastWord == "typedef" || lastWord == "const") { if (lastWord == "typedef" || lastWord == "const") {
processCommand(QSynedit::EditCommand::Char,ch,nullptr); processCommand(QSynedit::EditCommand::Char,ch,nullptr);
@ -930,7 +921,7 @@ void Editor::keyPressEvent(QKeyEvent *event)
return; return;
} }
} }
lastWord = getPreviousWordAtPositionForCompleteFunctionDefinition(cursor); lastWord = getPreviousWordAtPositionForCompleteFunctionDefinition(ws);
if (mParser && !lastWord.isEmpty()) { if (mParser && !lastWord.isEmpty()) {
PStatement currentScope = mParser->findScopeStatement(mFilename,caretY()); PStatement currentScope = mParser->findScopeStatement(mFilename,caretY());
while(currentScope && currentScope->kind==StatementKind::skBlock) { while(currentScope && currentScope->kind==StatementKind::skBlock) {
@ -962,17 +953,15 @@ void Editor::keyPressEvent(QKeyEvent *event)
&& pSettings->codeCompletion().showCompletionWhileInput() ) { && pSettings->codeCompletion().showCompletionWhileInput() ) {
if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::CPP) { if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::CPP) {
//preprocessor ? //preprocessor ?
if ((mLastIdCharPressed==0) && (ch=='#') && lineText().isEmpty()) { if ((idCharPressed==0) && (ch=='#') && lineText().isEmpty()) {
mLastIdCharPressed++;
processCommand(QSynedit::EditCommand::Char,ch,nullptr); processCommand(QSynedit::EditCommand::Char,ch,nullptr);
showCompletion("",false,CodeCompletionType::Normal); showCompletion("",false,CodeCompletionType::Normal);
handled=true; handled=true;
return; return;
} }
//javadoc directive? //javadoc directive?
if ((mLastIdCharPressed==0) && (ch=='@') && if ((idCharPressed==0) && (ch=='@') &&
lineText().trimmed().startsWith('*')) { lineText().trimmed().startsWith('*')) {
mLastIdCharPressed++;
processCommand(QSynedit::EditCommand::Char,ch,nullptr); processCommand(QSynedit::EditCommand::Char,ch,nullptr);
showCompletion("",false,CodeCompletionType::Normal); showCompletion("",false,CodeCompletionType::Normal);
handled=true; handled=true;
@ -980,22 +969,19 @@ void Editor::keyPressEvent(QKeyEvent *event)
} }
} else if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::LUA) { } else if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::LUA) {
if (ch=='.') { if (ch=='.') {
mLastIdCharPressed++;
processCommand(QSynedit::EditCommand::Char,ch,nullptr); processCommand(QSynedit::EditCommand::Char,ch,nullptr);
showCompletion("",false,CodeCompletionType::KeywordsOnly); showCompletion("",false,CodeCompletionType::KeywordsOnly);
handled=true; handled=true;
return; return;
} }
} else if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly) { } else if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly) {
if ((mLastIdCharPressed==0) && (ch=='.')) { if ((idCharPressed==0) && (ch=='.')) {
mLastIdCharPressed++;
processCommand(QSynedit::EditCommand::Char,ch,nullptr); processCommand(QSynedit::EditCommand::Char,ch,nullptr);
showCompletion("",false,CodeCompletionType::KeywordsOnly); showCompletion("",false,CodeCompletionType::KeywordsOnly);
handled=true; handled=true;
return; return;
} }
if ((mLastIdCharPressed==0) && (ch=='%')) { if ((idCharPressed==0) && (ch=='%')) {
mLastIdCharPressed++;
processCommand(QSynedit::EditCommand::Char,ch,nullptr); processCommand(QSynedit::EditCommand::Char,ch,nullptr);
showCompletion("",false,CodeCompletionType::KeywordsOnly); showCompletion("",false,CodeCompletionType::KeywordsOnly);
handled=true; handled=true;
@ -1003,7 +989,6 @@ void Editor::keyPressEvent(QKeyEvent *event)
} }
} }
} }
mLastIdCharPressed = 0;
switch (ch.unicode()) { switch (ch.unicode()) {
case '"': case '"':
case '\'': case '\'':
@ -1298,10 +1283,6 @@ bool Editor::event(QEvent *event)
void Editor::mouseReleaseEvent(QMouseEvent *event) void Editor::mouseReleaseEvent(QMouseEvent *event)
{ {
if (event->button() | Qt::LeftButton) {
mLastIdCharPressed = 0;
}
// if ctrl+clicked // if ctrl+clicked
if ((event->modifiers() == Qt::ControlModifier) if ((event->modifiers() == Qt::ControlModifier)
&& (event->button() == Qt::LeftButton)) { && (event->button() == Qt::LeftButton)) {
@ -1337,10 +1318,12 @@ void Editor::inputMethodEvent(QInputMethodEvent *event)
onCompletionInputMethod(event); onCompletionInputMethod(event);
return; return;
} else { } else {
mLastIdCharPressed+=s.length();
if (pSettings->codeCompletion().enabled() if (pSettings->codeCompletion().enabled()
&& pSettings->codeCompletion().showCompletionWhileInput() ) { && pSettings->codeCompletion().showCompletionWhileInput() ) {
if (mLastIdCharPressed>=pSettings->codeCompletion().minCharRequired()) { QSynedit::BufferCoord ws=wordStart();
int idCharPressed=caretX()-ws.ch;
idCharPressed += s.length();
if (idCharPressed>=pSettings->codeCompletion().minCharRequired()) {
QString lastWord = getPreviousWordAtPositionForSuggestion(caretXY()); QString lastWord = getPreviousWordAtPositionForSuggestion(caretXY());
if (mParser && !lastWord.isEmpty()) { if (mParser && !lastWord.isEmpty()) {
if (CppTypeKeywords.contains(lastWord)) { if (CppTypeKeywords.contains(lastWord)) {
@ -3239,9 +3222,9 @@ void Editor::insertCodeSnippet(const QString &code)
mTabStopEnd = caretX(); mTabStopEnd = caretX();
popUserCodeInTabStops(); popUserCodeInTabStops();
} }
if (!code.isEmpty()) { // if (!code.isEmpty()) {
mLastIdCharPressed = 0; // mLastIdCharPressed = 0;
} // }
} }
void Editor::print() void Editor::print()
@ -3758,8 +3741,8 @@ void Editor::completionInsert(bool appendFunc)
} else } else
setSelText(statement->command + funcAddOn); setSelText(statement->command + funcAddOn);
if (!funcAddOn.isEmpty()) // if (!funcAddOn.isEmpty())
mLastIdCharPressed = 0; // mLastIdCharPressed = 0;
// Move caret inside the ()'s, only when the user has something to do there... // Move caret inside the ()'s, only when the user has something to do there...
if (!funcAddOn.isEmpty() if (!funcAddOn.isEmpty()
@ -3843,7 +3826,6 @@ bool Editor::onCompletionKeyPressed(QKeyEvent *event)
phrase = getWordAtPosition(this,caretXY(), phrase = getWordAtPosition(this,caretXY(),
pBeginPos,pEndPos, pBeginPos,pEndPos,
purpose); purpose);
mLastIdCharPressed = phrase.length();
if (phrase.isEmpty()) { if (phrase.isEmpty()) {
mCompletionPopup->hide(); mCompletionPopup->hide();
} else { } else {
@ -3875,7 +3857,6 @@ bool Editor::onCompletionKeyPressed(QKeyEvent *event)
phrase = getWordAtPosition(this,caretXY(), phrase = getWordAtPosition(this,caretXY(),
pBeginPos,pEndPos, pBeginPos,pEndPos,
purpose); purpose);
mLastIdCharPressed = phrase.length();
mCompletionPopup->search(phrase, false); mCompletionPopup->search(phrase, false);
return true; return true;
} else { } else {
@ -3902,7 +3883,6 @@ bool Editor::onHeaderCompletionKeyPressed(QKeyEvent *event)
phrase = getWordAtPosition(this,caretXY(), phrase = getWordAtPosition(this,caretXY(),
pBeginPos,pEndPos, pBeginPos,pEndPos,
WordPurpose::wpHeaderCompletion); WordPurpose::wpHeaderCompletion);
mLastIdCharPressed = phrase.length();
mHeaderCompletionPopup->search(phrase, false); mHeaderCompletionPopup->search(phrase, false);
return true; return true;
case Qt::Key_Escape: case Qt::Key_Escape:
@ -3932,7 +3912,6 @@ bool Editor::onHeaderCompletionKeyPressed(QKeyEvent *event)
phrase = getWordAtPosition(this,caretXY(), phrase = getWordAtPosition(this,caretXY(),
pBeginPos,pEndPos, pBeginPos,pEndPos,
WordPurpose::wpHeaderCompletion); WordPurpose::wpHeaderCompletion);
mLastIdCharPressed = phrase.length();
mHeaderCompletionPopup->search(phrase, false); mHeaderCompletionPopup->search(phrase, false);
return true; return true;
} else { } else {
@ -3952,7 +3931,6 @@ bool Editor::onCompletionInputMethod(QInputMethodEvent *event)
QString s=event->commitString(); QString s=event->commitString();
if (mParser && !s.isEmpty()) { if (mParser && !s.isEmpty()) {
QString phrase = getWordForCompletionSearch(caretXY(),mCompletionPopup->memberOperator()=="::"); QString phrase = getWordForCompletionSearch(caretXY(),mCompletionPopup->memberOperator()=="::");
mLastIdCharPressed = phrase.length();
mCompletionPopup->search(phrase, false); mCompletionPopup->search(phrase, false);
return true; return true;
} }

View File

@ -325,7 +325,6 @@ private:
PCppParser mParser; PCppParser mParser;
std::shared_ptr<CodeCompletionPopup> mCompletionPopup; std::shared_ptr<CodeCompletionPopup> mCompletionPopup;
std::shared_ptr<HeaderCompletionPopup> mHeaderCompletionPopup; std::shared_ptr<HeaderCompletionPopup> mHeaderCompletionPopup;
int mLastIdCharPressed;
bool mUseCppSyntax; bool mUseCppSyntax;
QString mCurrentWord; QString mCurrentWord;
QString mCurrentDebugTipWord; QString mCurrentDebugTipWord;