Developer forums (C::B DEVELOPMENT STRICTLY!) > CodeCompletion redesign
Find Declaration of constructor doesn't work
ollydbg:
--- Quote from: proxym on January 20, 2014, 04:29:41 pm ---Find Declaration of constructor still doesn't work. 2014 year now.
Instead of showing declaration of constructor it shows declaration of class.
Code::blocks svn 9455.
--- End quote ---
Indeed, it is not fixed. Now, I need some suggestions.
Let's still look at the sample code in this Re: Find Declaration of constructor doesn't work
What is the expect behavior?
--- Code: ---#include <iostream>
using namespace std;
class AAA //-----------------------(a)
{
public:
AAA(){}; //-----------------------(b)
int a;
};
int main()
{
AAA * p = new AAA();
^^^----------------------------------------------(1)
^^^-------------------------------(2)
cout << "Hello world!" << endl;
return 0;
}
--- End code ---
So, here is my rules:
case 1: find declaration in (1) should go to (a), not (b)
case 2: find declaration in (2) should go to (b), not (a)
But
case 3: do you think that find declaration in (1) should go to show a list box showing both (a) and (b), and let the user select where to go?
case 4: do you think that find declaration in (1) should go to show a list box showing both (a) and (b), and let the user select where to go?
I think Alpha has did some work which is quite similar to the solution of this issue, see the SVN commit WebSVN - codeblocks - Rev 9560 - /, but that's only for call-tip.
Question again here, does call-tip need to show a class definition Token? I basically think that a call-tip should show only a function prototype tip, unless this class does not have a constructor.
ollydbg:
--- Code: ---#include <iostream>
using namespace std;
class AAA
{
public:
AAA(int b){};
int a;
};
class BBB
{
public:
int a;
};
int main()
{
AAA * p = new AAA(6);
BBB * p = new BBB(6);
cout << "Hello world!" << endl;
return 0;
}
--- End code ---
The above code is to demonstrate the fix of this issue, you can use the mouse to hover "AAA" or "BBB", and find declaration goes correctly according to the rules:
--- Quote ---case 1: find declaration in (1) should go to (a), not (b)
case 2: find declaration in (2) should go to (b), not (a)
--- End quote ---
Note that in "BBB", there is no constructor defined, so it go to the class definition.
Here is the patch:
--- Code: ---From a204e285017afe0aa41e4660b0eaa2a59504aaed Mon Sep 17 00:00:00 2001
From: asmwarrior
Date: Sun, 26 Jan 2014 14:19:04 +0800
Subject: [PATCH] * CC: find declaration of a class constructor goes to the
correct constructor functions, not the class definition. This fix a bug
reported here:
http://forums.codeblocks.org/index.php/topic,13753.msg92654.html#msg92654
---
src/plugins/codecompletion/codecompletion.cpp | 41 ++++++++++--------------
src/plugins/codecompletion/nativeparser_base.cpp | 36 ++++++++++++++++++++-
src/plugins/codecompletion/nativeparser_base.h | 3 ++
3 files changed, 55 insertions(+), 25 deletions(-)
diff --git a/src/plugins/codecompletion/codecompletion.cpp b/src/plugins/codecompletion/codecompletion.cpp
index ba48475..1fc3af5 100644
--- a/src/plugins/codecompletion/codecompletion.cpp
+++ b/src/plugins/codecompletion/codecompletion.cpp
@@ -2219,34 +2219,27 @@ void CodeCompletion::OnGotoDeclaration(wxCommandEvent& event)
}
}
}
- // special handle constructor function
- else
+ else // special handle constructor and functions
{
- bool isClassOrConstructor = false;
- for (TokenIdxSet::const_iterator it = result.begin(); it != result.end(); ++it)
+ // AAA * p = new AAA();
+ // ^^^---------------------------------------------this should go to class definition
+ // ^^^-------------------------------this should go to functions such as constructors
+ const bool isFunction = CodeCompletionHelper::GetNextNonWhitespaceChar(editor->GetControl(), endPos) == _T('(');
+ TokenIdxSet savedResult = result;
+ for (TokenIdxSet::const_iterator it = result.begin(); it != result.end();)
{
const Token* token = tree->at(*it);
- if (token && (token->m_TokenKind == tkClass || token->m_TokenKind == tkConstructor))
- {
- isClassOrConstructor = true;
- break;
- }
- }
- if (isClassOrConstructor)
- {
- const bool isConstructor = CodeCompletionHelper::GetNextNonWhitespaceChar(editor->GetControl(), endPos) == _T('(')
- && CodeCompletionHelper::GetLastNonWhitespaceChar(editor->GetControl(), startPos) == _T(':');
- for (TokenIdxSet::const_iterator it = result.begin(); it != result.end();)
- {
- const Token* token = tree->at(*it);
- if (isConstructor && token && token->m_TokenKind == tkClass)
- result.erase(it++);
- else if (!isConstructor && token && token->m_TokenKind == tkConstructor)
- result.erase(it++);
- else
- ++it;
- }
+ if (isFunction && token && token->m_TokenKind == tkClass)
+ result.erase(it++); // a class token is removed
+ else if (!isFunction && token && token->m_TokenKind == tkConstructor)
+ result.erase(it++); // a constructor token is removed
+ else
+ ++it;
}
+ // in a special case that a class definition don't have a constructor defined (implicitly defined by compiler)
+ // add shouldn't remove those class type tokens, so we need to restore the saved result
+ if (!result.size())
+ result = savedResult;
}
// special handle for function overloading
diff --git a/src/plugins/codecompletion/nativeparser_base.cpp b/src/plugins/codecompletion/nativeparser_base.cpp
index ee06770..3b8a02f 100644
--- a/src/plugins/codecompletion/nativeparser_base.cpp
+++ b/src/plugins/codecompletion/nativeparser_base.cpp
@@ -964,11 +964,45 @@ size_t NativeParserBase::ResolveExpression(TokenTree* tree,
}// while
if (!initialScope.empty())
- result = initialScope;
+ {
+ // if a Token in initialScope is a class, and we need to add its public constructors, this
+ // is useful when we try to find declaration of a constructor, see
+ // http://forums.codeblocks.org/index.php/topic,13753.msg92654.html#msg92654
+ AddConstructors(tree, initialScope, result);
+ }
return result.size();
}
+void NativeParserBase::AddConstructors(TokenTree *tree, const TokenIdxSet& source, TokenIdxSet& dest)
+{
+ for (TokenIdxSet::iterator It = source.begin(); It != source.end(); ++It)
+ {
+ const Token* token = tree->at(*It);
+ if (!token)
+ continue;
+ dest.insert(*It);
+
+ // add constructors of the class type token
+ if (token->m_TokenKind == tkClass)
+ {
+ // loop on its children, add its public constructors
+ for (TokenIdxSet::iterator chIt = token->m_Children.begin();
+ chIt != token->m_Children.end();
+ ++chIt)
+ {
+ const Token* tk = tree->at(*chIt);
+ if ( tk && ( tk->m_TokenKind == tkConstructor
+ || (tk->m_IsOperator && tk->m_Name.EndsWith(wxT("()"))) )
+ && (tk->m_Scope == tsPublic || tk->m_Scope == tsUndefined) )
+ {
+ dest.insert(*chIt);
+ }
+ }
+ }
+ }
+}
+
void NativeParserBase::ResolveOperator(TokenTree* tree,
const OperatorType& tokenOperatorType,
const TokenIdxSet& tokens,
diff --git a/src/plugins/codecompletion/nativeparser_base.h b/src/plugins/codecompletion/nativeparser_base.h
index fc25c04..b2cff33 100644
--- a/src/plugins/codecompletion/nativeparser_base.h
+++ b/src/plugins/codecompletion/nativeparser_base.h
@@ -449,6 +449,9 @@ private:
return false;
}
+ /** loop on the source Token set, and add all its public constructors to dest */
+ void AddConstructors(TokenTree *tree, const TokenIdxSet& source, TokenIdxSet& dest);
+
// for GenerateResultSet()
bool MatchText(const wxString& text, const wxString& target, bool caseSens, bool isPrefix)
{
--
1.8.5.2.msysgit.0
--- End code ---
I add a function named AddConstructors, which add all the constructor tokens of a class type to the result. This function will be called in the final stage of the NativeParserBase::ResolveExpression().
Comments? Maybe, Alpha's code in rev9560 is redundant after my patch?
MortenMacFly:
--- Quote from: ollydbg on January 26, 2014, 07:28:03 am ---Comments?
--- End quote ---
You know mine: Cannot try as the patch does not apply. :)
oBFusCATed:
Probably it is time to tryout git-svn:)
oBFusCATed:
There is a translation that should apply with svn: http://cmpt.benbmp.org/codeblocks/patches/cc.ollydbg.svn.patch
ollydbg: your git patch is missing the email address and fails to apply cleanly with git am, please fix it.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version