Commit b8b37cb0 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

Fixed the completion to take into account auto parentheses insertion

Also skip semicolons in when auto parentheses insertion is enabled.

Done with mae.
parent 9ff527e2
...@@ -1468,6 +1468,11 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item) ...@@ -1468,6 +1468,11 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
QString toInsert; QString toInsert;
QString extraChars; QString extraChars;
int extraLength = 0; int extraLength = 0;
int cursorOffset = 0;
bool autoParenthesesEnabled = false;
if (TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(m_editor->widget()))
autoParenthesesEnabled = edit->tabSettings().m_autoParentheses;
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) { if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
toInsert = item.m_text; toInsert = item.m_text;
...@@ -1497,14 +1502,21 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item) ...@@ -1497,14 +1502,21 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
} else if (! function->isAmbiguous()) { } else if (! function->isAmbiguous()) {
extraChars += QLatin1Char('('); extraChars += QLatin1Char('(');
// If the function doesn't return anything, automatically place the semicolon,
// unless we're doing a scope completion (then it might be function definition).
bool endWithSemicolon = function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON;
// If the function takes no arguments, automatically place the closing parenthesis // If the function takes no arguments, automatically place the closing parenthesis
if (item.m_duplicateCount == 0 && ! function->hasArguments()) { if (item.m_duplicateCount == 0 && ! function->hasArguments()) {
extraChars += QLatin1Char(')'); extraChars += QLatin1Char(')');
if (endWithSemicolon)
// If the function doesn't return anything, automatically place the semicolon, extraChars += QLatin1Char(';');
// unless we're doing a scope completion (then it might be function definition). } else if (autoParenthesesEnabled) {
if (function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON) { extraChars += QLatin1Char(')');
--cursorOffset;
if (endWithSemicolon) {
extraChars += QLatin1Char(';'); extraChars += QLatin1Char(';');
--cursorOffset;
} }
} }
} }
...@@ -1528,6 +1540,8 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item) ...@@ -1528,6 +1540,8 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
int length = m_editor->position() - m_startPosition + extraLength; int length = m_editor->position() - m_startPosition + extraLength;
m_editor->setCurPos(m_startPosition); m_editor->setCurPos(m_startPosition);
m_editor->replace(length, toInsert); m_editor->replace(length, toInsert);
if (cursorOffset)
m_editor->setCurPos(m_editor->position() + cursorOffset);
} }
bool CppCodeCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems) bool CppCodeCompletion::partiallyComplete(const QList<TextEditor::CompletionItem> &completionItems)
......
...@@ -1091,9 +1091,10 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e) ...@@ -1091,9 +1091,10 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
bool skip = false; bool skip = false;
QChar first = text.at(0); QChar first = text.at(0);
if (first == QLatin1Char(')')) { if (first == QLatin1Char(')')
skip = (first == lookAhead); || first == QLatin1Char(']')
} else if (first == QLatin1Char(']')) { || first == QLatin1Char(';')
) {
skip = (first == lookAhead); skip = (first == lookAhead);
} else if (first == QLatin1Char('\"') || first == QLatin1Char('\'')) { } else if (first == QLatin1Char('\"') || first == QLatin1Char('\'')) {
if (first == lookAhead) { if (first == lookAhead) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment