- enhancement: symbol completion when editor has selection

This commit is contained in:
Roy Qu 2022-03-07 21:53:52 +08:00
parent 81f404d63c
commit fa9916e28e
3 changed files with 109 additions and 37 deletions

View File

@ -20,6 +20,7 @@ Red Panda C++ Version 0.14.5
- fix: calculation of caret position is not in consistence. - fix: calculation of caret position is not in consistence.
- fix: undo one symbol completion as a whole operation - fix: undo one symbol completion as a whole operation
- fix: crash when open a project that contains custom folder - fix: crash when open a project that contains custom folder
- enhancement: symbol completion when editor has selection
Red Panda C++ Version 0.14.4 Red Panda C++ Version 0.14.4
- enhancement: git - log - enhancement: git - log

View File

@ -1947,7 +1947,7 @@ QChar Editor::getCurrentChar()
bool Editor::handleSymbolCompletion(QChar key) bool Editor::handleSymbolCompletion(QChar key)
{ {
if (!pSettings->editor().completeSymbols() || selAvail()) if (!pSettings->editor().completeSymbols())
return false; return false;
if (!insertMode()) if (!insertMode())
return false; return false;
@ -1994,6 +1994,8 @@ bool Editor::handleSymbolCompletion(QChar key)
} }
return false; return false;
case ')': case ')':
if (selAvail())
return false;
if (pSettings->editor().completeParenthese() && pSettings->editor().overwriteSymbols()) { if (pSettings->editor().completeParenthese() && pSettings->editor().overwriteSymbols()) {
return handleParentheseSkip(); return handleParentheseSkip();
} }
@ -2004,6 +2006,8 @@ bool Editor::handleSymbolCompletion(QChar key)
} }
return false; return false;
case ']': case ']':
if (selAvail())
return false;
if (pSettings->editor().completeBracket() && pSettings->editor().overwriteSymbols()) { if (pSettings->editor().completeBracket() && pSettings->editor().overwriteSymbols()) {
return handleBracketSkip(); return handleBracketSkip();
} }
@ -2020,6 +2024,8 @@ bool Editor::handleSymbolCompletion(QChar key)
} }
return false; return false;
case '}': case '}':
if (selAvail())
return false;
if (pSettings->editor().completeBrace() && pSettings->editor().overwriteSymbols()) { if (pSettings->editor().completeBrace() && pSettings->editor().overwriteSymbols()) {
return handleBraceSkip(); return handleBraceSkip();
} }
@ -2035,11 +2041,15 @@ bool Editor::handleSymbolCompletion(QChar key)
} }
return false; return false;
case '<': case '<':
if (selAvail())
return false;
if (pSettings->editor().completeGlobalInclude()) { // #include <> if (pSettings->editor().completeGlobalInclude()) { // #include <>
return handleGlobalIncludeCompletion(); return handleGlobalIncludeCompletion();
} }
return false; return false;
case '>': case '>':
if (selAvail())
return false;
if (pSettings->editor().completeGlobalInclude() && pSettings->editor().overwriteSymbols()) { // #include <> if (pSettings->editor().completeGlobalInclude() && pSettings->editor().overwriteSymbols()) { // #include <>
return handleGlobalIncludeSkip(); return handleGlobalIncludeSkip();
} }
@ -2052,6 +2062,16 @@ bool Editor::handleParentheseCompletion()
{ {
QuoteStatus status = getQuoteStatus(); QuoteStatus status = getQuoteStatus();
if (status == QuoteStatus::RawString || status == QuoteStatus::NotQuote) { if (status == QuoteStatus::RawString || status == QuoteStatus::NotQuote) {
if (selAvail() && status == QuoteStatus::NotQuote) {
QString text=selText();
beginUpdate();
beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'(');
setSelText(text);
commandProcessor(SynEditorCommand::ecChar,')');
endUndoBlock();
endUpdate();
} else {
beginUpdate(); beginUpdate();
beginUndoBlock(); beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'('); commandProcessor(SynEditorCommand::ecChar,'(');
@ -2060,6 +2080,7 @@ bool Editor::handleParentheseCompletion()
setCaretXY(oldCaret); setCaretXY(oldCaret);
endUndoBlock(); endUndoBlock();
endUpdate(); endUpdate();
}
return true; return true;
} }
// if (status == QuoteStatus::NotQuote) && FunctionTipAllowed then // if (status == QuoteStatus::NotQuote) && FunctionTipAllowed then
@ -2103,6 +2124,17 @@ bool Editor::handleBracketCompletion()
{ {
// QuoteStatus status = getQuoteStatus(); // QuoteStatus status = getQuoteStatus();
// if (status == QuoteStatus::RawString || status == QuoteStatus::NotQuote) { // if (status == QuoteStatus::RawString || status == QuoteStatus::NotQuote) {
QuoteStatus status = getQuoteStatus();
if (selAvail() && status == QuoteStatus::NotQuote) {
QString text=selText();
beginUpdate();
beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'[');
setSelText(text);
commandProcessor(SynEditorCommand::ecChar,']');
endUndoBlock();
endUpdate();
} else {
beginUpdate(); beginUpdate();
beginUndoBlock(); beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'['); commandProcessor(SynEditorCommand::ecChar,'[');
@ -2111,6 +2143,7 @@ bool Editor::handleBracketCompletion()
setCaretXY(oldCaret); setCaretXY(oldCaret);
endUndoBlock(); endUndoBlock();
endUpdate(); endUpdate();
}
return true; return true;
// } // }
} }
@ -2139,12 +2172,18 @@ bool Editor::handleBracketSkip()
bool Editor::handleMultilineCommentCompletion() bool Editor::handleMultilineCommentCompletion()
{ {
if ((caretX()-2 < lineText().length()) && (lineText()[caretX() - 2] == '/')) { if ((caretX()-2 < lineText().length()) && (lineText()[caretX() - 2] == '/')) {
QString text=selText();
beginUpdate(); beginUpdate();
beginUndoBlock(); beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'*'); commandProcessor(SynEditorCommand::ecChar,'*');
BufferCoord oldCaret = caretXY(); BufferCoord oldCaret;
if (text.isEmpty())
oldCaret = caretXY();
else
setSelText(text);
commandProcessor(SynEditorCommand::ecChar,'*'); commandProcessor(SynEditorCommand::ecChar,'*');
commandProcessor(SynEditorCommand::ecChar,'/'); commandProcessor(SynEditorCommand::ecChar,'/');
if (text.isEmpty())
setCaretXY(oldCaret); setCaretXY(oldCaret);
endUndoBlock(); endUndoBlock();
endUpdate(); endUpdate();
@ -2161,10 +2200,18 @@ bool Editor::handleBraceCompletion()
s=lines()->getString(i); s=lines()->getString(i);
i--; i--;
} }
QString text=selText();
beginUpdate(); beginUpdate();
beginUndoBlock(); beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'{'); commandProcessor(SynEditorCommand::ecChar,'{');
BufferCoord oldCaret = caretXY(); BufferCoord oldCaret;
if (text.isEmpty()) {
oldCaret = caretXY();
} else {
commandProcessor(SynEditorCommand::ecInsertLine);
setSelText(text);
commandProcessor(SynEditorCommand::ecInsertLine);
}
commandProcessor(SynEditorCommand::ecChar,'}'); commandProcessor(SynEditorCommand::ecChar,'}');
if ( if (
( (s.startsWith("struct") ( (s.startsWith("struct")
@ -2178,6 +2225,7 @@ bool Editor::handleBraceCompletion()
) || s.endsWith('=')) { ) || s.endsWith('=')) {
commandProcessor(SynEditorCommand::ecChar,';'); commandProcessor(SynEditorCommand::ecChar,';');
} }
if (text.isEmpty())
setCaretXY(oldCaret); setCaretXY(oldCaret);
endUndoBlock(); endUndoBlock();
endUpdate(); endUpdate();
@ -2217,12 +2265,23 @@ bool Editor::handleSingleQuoteCompletion()
QuoteStatus status = getQuoteStatus(); QuoteStatus status = getQuoteStatus();
QChar ch = getCurrentChar(); QChar ch = getCurrentChar();
if (ch == '\'') { if (ch == '\'') {
if (status == QuoteStatus::SingleQuote) { if (status == QuoteStatus::SingleQuote && !selAvail()) {
setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over
return true; return true;
} }
} else { } else {
if (status == QuoteStatus::NotQuote) { if (status == QuoteStatus::NotQuote) {
if (selAvail()) {
QString text=selText();
beginUpdate();
beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'\'');
setSelText(text);
commandProcessor(SynEditorCommand::ecChar,'\'');
endUndoBlock();
endUpdate();
return true;
}
if (ch == 0 || highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) { if (ch == 0 || highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) {
// insert '' // insert ''
beginUpdate(); beginUpdate();
@ -2245,12 +2304,24 @@ bool Editor::handleDoubleQuoteCompletion()
QuoteStatus status = getQuoteStatus(); QuoteStatus status = getQuoteStatus();
QChar ch = getCurrentChar(); QChar ch = getCurrentChar();
if (ch == '"') { if (ch == '"') {
if (status == QuoteStatus::DoubleQuote || status == QuoteStatus::RawString) { if ((status == QuoteStatus::DoubleQuote || status == QuoteStatus::RawString)
&& !selAvail()) {
setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over setCaretXY( BufferCoord{caretX() + 1, caretY()}); // skip over
return true; return true;
} }
} else { } else {
if (status == QuoteStatus::NotQuote) { if (status == QuoteStatus::NotQuote) {
if (selAvail()) {
QString text=selText();
beginUpdate();
beginUndoBlock();
commandProcessor(SynEditorCommand::ecChar,'"');
setSelText(text);
commandProcessor(SynEditorCommand::ecChar,'"');
endUndoBlock();
endUpdate();
return true;
}
if ((ch == 0) || highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) { if ((ch == 0) || highlighter()->isWordBreakChar(ch) || highlighter()->isSpaceChar(ch)) {
// insert "" // insert ""
beginUpdate(); beginUpdate();

View File

@ -686,7 +686,7 @@ void SynEdit::invalidateGutterLines(int FirstLine, int LastLine)
DisplayCoord SynEdit::pixelsToRowColumn(int aX, int aY) const DisplayCoord SynEdit::pixelsToRowColumn(int aX, int aY) const
{ {
return { return {
std::max(1, mLeftChar + ((aX - mGutterWidth - 2) / mCharWidth)), std::max(1, (int)(mLeftChar + round((aX - mGutterWidth - 2.0) / mCharWidth))),
std::max(1, mTopLine + (aY / mTextHeight)) std::max(1, mTopLine + (aY / mTextHeight))
}; };
} }