done: find completion word

This commit is contained in:
Roy Qu 2021-12-02 18:29:37 +08:00
parent 9cdac8c3ef
commit 54443c20f8
2 changed files with 54 additions and 28 deletions

View File

@ -1622,16 +1622,6 @@ void Editor::deleteToBOL()
ExecuteCommand(SynEditorCommand::ecDeleteBOL,QChar(),nullptr); ExecuteCommand(SynEditorCommand::ecDeleteBOL,QChar(),nullptr);
} }
enum class LastSymbolType {
Identifier,
MemberOperator,
MatchingBracket,
BracketMatched,
MatchingParenthesis,
ParenthesisMatched,
None
};
QStringList Editor::getExpressionAtPositionForCompletion(const BufferCoord &pos) QStringList Editor::getExpressionAtPositionForCompletion(const BufferCoord &pos)
{ {
QStringList result; QStringList result;
@ -1639,7 +1629,6 @@ QStringList Editor::getExpressionAtPositionForCompletion(const BufferCoord &pos)
return result; return result;
int line = pos.Line-1; int line = pos.Line-1;
int ch = pos.Char-1; int ch = pos.Char-1;
QString symbolToMatch;
int symbolMatchingLevel = 0; int symbolMatchingLevel = 0;
LastSymbolType lastSymbolType=LastSymbolType::None; LastSymbolType lastSymbolType=LastSymbolType::None;
while (true) { while (true) {
@ -1677,18 +1666,19 @@ QStringList Editor::getExpressionAtPositionForCompletion(const BufferCoord &pos)
switch(lastSymbolType) { switch(lastSymbolType) {
case LastSymbolType::None: case LastSymbolType::None:
case LastSymbolType::MemberOperator: case LastSymbolType::MemberOperator:
case LastSymbolType::SymbolMatched: case LastSymbolType::ParenthesisMatched:
case LastSymbolType::BracketMatched:
if ((token =="::" || token == "." || token == "->") if ((token =="::" || token == "." || token == "->")
&& lastSymbolType!=LastSymbolType::None && lastSymbolType!=LastSymbolType::MemberOperator
){ ){
lastSymbolType=LastSymbolType::MemberOperator; lastSymbolType=LastSymbolType::MemberOperator;
} else if (token == ")" ) { } else if (token == ")" ) {
lastSymbolType=LastSymbolType::MatchingSymbol; lastSymbolType=LastSymbolType::MatchingParenthesis;
symbolToMatch = "(";
symbolMatchingLevel = 0; symbolMatchingLevel = 0;
} else if (token == "]") { } else if (token == "]") {
lastSymbolType=LastSymbolType::MatchingSymbol; if (lastSymbolType == LastSymbolType::BracketMatched)
symbolToMatch = "["; return result;
lastSymbolType=LastSymbolType::MatchingBracket;
symbolMatchingLevel = 0; symbolMatchingLevel = 0;
} else if (isIdentChar(token.front())) { } else if (isIdentChar(token.front())) {
lastSymbolType=LastSymbolType::Identifier; lastSymbolType=LastSymbolType::Identifier;
@ -1702,11 +1692,31 @@ QStringList Editor::getExpressionAtPositionForCompletion(const BufferCoord &pos)
result.push_front(token); result.push_front(token);
} else } else
return result; // stop matching; return result; // stop matching;
case LastSymbolType::MatchingSymbol: break;
if (token==symbolToMatch && symbolMatchingLevel==0) { case LastSymbolType::MatchingParenthesis:
lastSymbolType=LastSymbolType::MemberOperator; if (token=="(") {
if (symbolMatchingLevel==0) {
lastSymbolType=LastSymbolType::ParenthesisMatched;
} else {
symbolMatchingLevel--;
} }
} else if (token==")") {
symbolMatchingLevel++;
}
result.push_front(token);
break;
case LastSymbolType::MatchingBracket:
if (token=="[") {
if (symbolMatchingLevel==0) {
lastSymbolType=LastSymbolType::BracketMatched;
} else {
symbolMatchingLevel--;
}
} else if (token=="]") {
symbolMatchingLevel++;
}
result.push_front(token);
break;
} }
} }
@ -2473,8 +2483,10 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete)
mParser->findAndScanBlockAt(mFilename, caretY()) mParser->findAndScanBlockAt(mFilename, caretY())
); );
if (word.isEmpty()) if (word.isEmpty()) {
word=getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpCompletion); //word=getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpCompletion);
word = getExpressionAtPositionForCompletion(caretXY()).join("");
}
mCompletionPopup->prepareSearch(preWord, word, mFilename, pBeginPos.Line); mCompletionPopup->prepareSearch(preWord, word, mFilename, pBeginPos.Line);
// Filter the whole statement list // Filter the whole statement list
@ -2725,6 +2737,9 @@ bool Editor::onCompletionKeyPressed(QKeyEvent *event)
QChar ch = event->text().front(); QChar ch = event->text().front();
if (isIdentChar(ch)) { if (isIdentChar(ch)) {
setSelText(ch); setSelText(ch);
if (purpose == WordPurpose::wpCompletion) {
phrase = getExpressionAtPositionForCompletion(caretXY()).join("");
} else
phrase = getWordAtPosition(this,caretXY(), phrase = getWordAtPosition(this,caretXY(),
pBeginPos,pEndPos, pBeginPos,pEndPos,
purpose); purpose);
@ -2804,10 +2819,11 @@ bool Editor::onCompletionInputMethod(QInputMethodEvent *event)
return processed; return processed;
QString s=event->commitString(); QString s=event->commitString();
if (!s.isEmpty()) { if (!s.isEmpty()) {
BufferCoord pBeginPos,pEndPos; QString phrase = getExpressionAtPositionForCompletion(caretXY()).join("");
QString phrase = getWordAtPosition(this,caretXY(), // BufferCoord pBeginPos,pEndPos;
pBeginPos,pEndPos, // QString phrase = getWordAtPosition(this,caretXY(),
WordPurpose::wpCompletion); // pBeginPos,pEndPos,
// WordPurpose::wpCompletion);
mLastIdCharPressed = phrase.length(); mLastIdCharPressed = phrase.length();
mCompletionPopup->search(phrase, false); mCompletionPopup->search(phrase, false);
return true; return true;

View File

@ -41,6 +41,16 @@ class Editor : public SynEdit
{ {
Q_OBJECT Q_OBJECT
public: public:
enum class LastSymbolType {
Identifier,
MemberOperator,
MatchingBracket,
BracketMatched,
MatchingParenthesis,
ParenthesisMatched,
None
};
enum MarginNumber { enum MarginNumber {
LineNumberMargin = 0, LineNumberMargin = 0,
MarkerMargin = 1, MarkerMargin = 1,