Code::Blocks  SVN r11506
insertclassmethoddlg.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 10912 $
6  * $Id: insertclassmethoddlg.cpp 10912 2016-09-25 16:10:13Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/plugins/codecompletion/insertclassmethoddlg.cpp $
8  */
9 
10 #include "sdk.h"
11 
12 #ifndef CB_PRECOMP
13  #include <wx/button.h>
14  #include <wx/checkbox.h>
15  #include <wx/checklst.h>
16  #include <wx/intl.h>
17  #include <wx/listbox.h>
18  #include <wx/radiobox.h>
19  #include <wx/xrc/xmlres.h>
20  #include "globals.h"
21  #include "manager.h"
22  #include "logmanager.h"
23 #endif
24 
25 #include "insertclassmethoddlg.h"
26 
27 #include "parser/parser.h"
28 
30 {
31  inline void DoFillMethodsFor(wxCheckListBox* clb, Token* parentToken, const wxString& ns, bool includePrivate,
32  bool includeProtected, bool includePublic)
33  {
34  if (!parentToken)
35  return;
36  TokenTree* tree = parentToken->GetTree();
37  if (!tree)
38  return;
39 
40  // loop ascending the inheritance tree
41  tree->RecalcInheritanceChain(parentToken);
42 
43  for (TokenIdxSet::const_iterator it = parentToken->m_Children.begin(); it != parentToken->m_Children.end(); ++it)
44  {
45  int idx = *it;
46  const Token* token = tree->at(idx);
47  if (!token)
48  continue;
49 
50  const bool valid = token->m_TokenKind & (tkFunction | tkConstructor | tkDestructor)
51  && ( (includePrivate && token->m_Scope == tsPrivate)
52  || (includeProtected && token->m_Scope == tsProtected)
53  || (includePublic && token->m_Scope == tsPublic) );
54  if (valid)
55  {
56  wxString str;
57  str << token->m_FullType << _T(" ") << ns << token->m_Name << token->GetFormattedArgs();
58  str.Replace(_T("&"), _T("&&"));
59  if (clb->FindString(str) == wxNOT_FOUND)
60  clb->Append(str);
61  }
62  }
63 
64  // inheritance
65  for (TokenIdxSet::const_iterator it = parentToken->m_DirectAncestors.begin();
66  it != parentToken->m_DirectAncestors.end();
67  ++it)
68  {
69  int idx = *it;
70  Token* token = tree->at(idx);
71  if (!token)
72  continue;
74  clb,
75  token,
76  ns,
77  includePrivate,
78  includeProtected,
79  includePublic);
80  }
81  }
82 }// namespace InsertClassMethodDlgHelper
83 
84 BEGIN_EVENT_TABLE(InsertClassMethodDlg, wxScrollingDialog)
85  EVT_LISTBOX(XRCID("lstClasses"), InsertClassMethodDlg::OnClassesChange)
86  EVT_RADIOBOX(XRCID("rbCode"), InsertClassMethodDlg::OnCodeChange)
87  EVT_CHECKBOX(XRCID("chkPrivate"), InsertClassMethodDlg::OnFilterChange)
88  EVT_CHECKBOX(XRCID("chkProtected"), InsertClassMethodDlg::OnFilterChange)
89  EVT_CHECKBOX(XRCID("chkPublic"), InsertClassMethodDlg::OnFilterChange)
90 END_EVENT_TABLE()
91 
93  m_Parser(parser),
94  m_Decl(true),
95  m_Filename(filename)
96 {
97  //ctor
98  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgInsertClassMethod"),_T("wxScrollingDialog"));
99  XRCCTRL(*this, "rbCode", wxRadioBox)->SetSelection(0);
100  XRCCTRL(*this, "wxID_OK", wxButton)->SetDefault();
101  FillClasses();
102 }
103 
105 {
106  //dtor
107 }
108 
110 {
111  wxArrayString array;
112  const wxCheckListBox* clb = XRCCTRL(*this, "chklstMethods", wxCheckListBox);
113 
114  for (size_t i = 0; i < clb->GetCount(); ++i)
115  {
116  if (clb->IsChecked(i))
117  {
118  wxString str;
119  if (XRCCTRL(*this, "chkAddDoc", wxCheckBox)->IsChecked())
120  {
121  // add doc block
122  str << _T("/** @brief (one liner)\n *\n * (documentation goes here)\n */\n");
123  }
124  str << clb->GetString(i);
125  str.Replace(_T("&&"), _T("&"));
126  array.Add(str + (m_Decl ? _T(";\n") : _T("\n{\n\t\n}\n\n")));
127  }
128  }
129 
130  return array;
131 } // end of GetCode
132 
134 {
135  wxListBox* lb = XRCCTRL(*this, "lstClasses", wxListBox);
136  lb->Freeze();
137  lb->Clear();
138 
139  TokenTree* tree = m_Parser->GetTokenTree();
140  for (size_t i = 0; i < tree->size(); ++i)
141  {
142  Token* token = tree->at(i);
143  //CCLogger::Get()->DebugLog(wxT("m_Filename=%s, token=%s"), m_Filename.wx_str(), token->m_Filename.wx_str());
144  if (token && (token->m_TokenKind & (tkClass | tkTypedef)))
145  {
146  // TODO: check against file's pair too
147  lb->Append(token->m_Name, token);
148  }
149  }
150 
151  lb->Thaw();
152  FillMethods();
153 }
154 
156 {
157  wxListBox* lb = XRCCTRL(*this, "lstClasses", wxListBox);
158  wxCheckListBox* clb = XRCCTRL(*this, "chklstMethods", wxCheckListBox);
159  clb->Clear();
160 
161  if (lb->GetSelection() == -1)
162  return;
163 
164  bool includePrivate = XRCCTRL(*this, "chkPrivate", wxCheckBox)->IsChecked();
165  bool includeProtected = XRCCTRL(*this, "chkProtected", wxCheckBox)->IsChecked();
166  bool includePublic = XRCCTRL(*this, "chkPublic", wxCheckBox)->IsChecked();
167 
168  Token* parentToken = reinterpret_cast<Token*>(lb->GetClientData(lb->GetSelection()));
169 
170  clb->Freeze();
172  clb,
173  parentToken,
174  parentToken ? parentToken->m_Name + _T("::") : _T(""),
175  includePrivate,
176  includeProtected,
177  includePublic);
178  clb->Thaw();
179 }
180 
181 // events
183 {
184  FillMethods();
185 }
186 
188 {
189  m_Decl = XRCCTRL(*this, "rbCode", wxRadioBox)->GetSelection() == 0;
190 }
191 
193 {
194  FillMethods();
195 }
TokenTree * GetTree() const
get the TokenTree associated with the current Token
Definition: token.h:173
destructor class member function
Definition: token.h:51
void OnCodeChange(wxCommandEvent &event)
constructor class member function
Definition: token.h:48
class or struct
Definition: token.h:37
wxString GetFormattedArgs() const
remove all &#39; &#39; in the original function argument string
Definition: token.cpp:199
Token * at(int idx)
Definition: tokentree.h:51
wxString m_Name
Token&#39;s name, it can be searched in the TokenTree.
Definition: token.h:188
typedef, note typedefs are stored as classes inheriting from the typedef&#39;d type, this takes advantage...
Definition: token.h:45
virtual wxString GetString(unsigned int n) const
a container class to hold all the Tokens getting from parsing stage
Definition: tokentree.h:37
TokenIdxSet m_Children
if it is a class kind token, then it contains all the member tokens
Definition: token.h:268
#define _T(string)
size_t size()
total size of std::vector<Token*>
Definition: tokentree.cpp:98
bool IsChecked(unsigned int item) const
TokenScope m_Scope
public? private? protected?
Definition: token.h:231
#define wxNOT_FOUND
void RecalcInheritanceChain(Token *token)
convert the Token&#39;s ancestor string to it&#39;s IDs this contains recursive calls, for example in such co...
Definition: tokentree.cpp:643
virtual int FindString(const wxString &s, bool bCase=false) const
a symbol found in the parsed files, it can be many kinds, such as a variable, a class and so on...
Definition: token.h:82
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
wxArrayString GetCode() const
void OnClassesChange(wxCommandEvent &event)
void OnFilterChange(wxCommandEvent &event)
TokenKind m_TokenKind
See TokenKind class.
Definition: token.h:234
general function, not constructor nor destructor
Definition: token.h:54
size_t Add(const wxString &str, size_t copies=1)
static wxXmlResource * Get()
Definition: token.h:26
wxString m_FullType
this is the full return value (if any): e.g.
Definition: token.h:182
void DoFillMethodsFor(wxCheckListBox *clb, Token *parentToken, const wxString &ns, bool includePrivate, bool includeProtected, bool includePublic)
TokenIdxSet m_DirectAncestors
the nearest ancestors
Definition: token.h:274
wxObject * LoadObject(wxWindow *parent, const wxString &name, const wxString &classname)
virtual unsigned int GetCount() const