Commit 4adfca0b authored by David Nolden's avatar David Nolden

- When the user types "bla.|" and starts completion, while bla is a pointer,...

- When the user types "bla.|" and starts completion, while bla is a pointer, automatically change it to "bla->".
- When the user types "bla->" and bla is not a pointer, does not have an operator-> member, and is a class, then automatically change it to "bla."
parent 062a1bac
......@@ -43,9 +43,13 @@
#include "cppduchain/cppduchain.h"
#include "cppdebughelper.h"
#include "missingincludecompletionitem.h"
#include <interfaces/idocumentcontroller.h>
#define LOCKDUCHAIN DUChainReadLocker lock(DUChain::lock())
///If this is enabled, KDevelop corrects wrong member access operators like "." on a pointer automatically
const bool assistAccessType = true;
#ifdef TEST_COMPLETION
//Stub implementation that does nothing
QList<KDevelop::CompletionTreeItemPointer> missingIncludeCompletionItems(QString expression, QString displayTextPrefix, Cpp::ExpressionEvaluationResult expressionResult, KDevelop::DUContext* context, int argumentHintDepth, bool namespaceAllowed) {
......@@ -317,11 +321,6 @@ CodeCompletionContext::CodeCompletionContext(DUContextPointer context, const QSt
}
}
break;
case MemberChoose:
case StaticMemberChoose:
{
///@todo Check whether it is a MemberChoose
}
case ArrowMemberAccess:
{
LOCKDUCHAIN;
......@@ -349,7 +348,9 @@ CodeCompletionContext::CodeCompletionContext(DUContextPointer context, const QSt
log( QString("arrow-operator of class is not a function: %1").arg(containerType ? containerType->toString() : QString("null") ) );
}
} else {
log( QString("arrow-operator on type without operator* member: %1").arg(containerType ? containerType->toString() : QString("null") ) );
log( QString("arrow-operator on type without operator-> member: %1").arg(containerType ? containerType->toString() : QString("null") ) );
if(idDecl->internalContext()->type() == DUContext::Class)
replaceCurrentAccess("->", ".");
}
} else {
log( QString("arrow-operator on type without declaration and context: %1").arg(containerType ? containerType->toString() : QString("null") ) );
......@@ -366,6 +367,8 @@ CodeCompletionContext::CodeCompletionContext(DUContextPointer context, const QSt
m_expressionResult.isInstance = true;
}
}
case MemberChoose:
case StaticMemberChoose:
case MemberAccess:
{
if( expr.trimmed().isEmpty() ) {
......@@ -384,6 +387,8 @@ CodeCompletionContext::CodeCompletionContext(DUContextPointer context, const QSt
if(delayed && delayed->kind() == DelayedType::Unresolved)
m_storedItems += missingIncludeCompletionItems(m_expression, m_followingText.trimmed() + ": ", m_expressionResult, m_duContext.data());
#endif
if(type.cast<PointerType>())
replaceCurrentAccess(".", "->");
}else{
log( "No type for expression" );
}
......@@ -824,5 +829,23 @@ void CodeCompletionContext::standardAccessCompletionItems(const KDevelop::Simple
}
}
void CodeCompletionContext::replaceCurrentAccess(QString old, QString _new)
{
IDocument* document = ICore::self()->documentController()->documentForUrl(m_duContext->url().toUrl());
if(document) {
KTextEditor::Document* textDocument = document->textDocument();
if(textDocument) {
KTextEditor::View* activeView = textDocument->activeView();
if(activeView) {
KTextEditor::Cursor cursor = activeView->cursorPosition();
KTextEditor::Range oldRange = KTextEditor::Range(cursor-KTextEditor::Cursor(0,old.length()), cursor);
if(oldRange.start().column() >= 0 && textDocument->text(oldRange) == old) {
textDocument->replaceText(oldRange, _new);
}
}
}
}
}
}
......@@ -160,6 +160,8 @@ namespace Cpp {
* */
QList<KDevelop::AbstractType::Ptr> additionalMatchTypes() const;
private:
///Replaces the member-access type at the current cursor position from "from" to "new", for example from "->" to "."
void replaceCurrentAccess(QString old, QString _new);
///@return the extracted expression, without any access-operation
QString expression() const;
......
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