| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : cl_calltip.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#include "cl_calltip.h" |
| 26 |
|
| 27 |
#include "ctags_manager.h" |
| 28 |
#include "precompiled_header.h" |
| 29 |
|
| 30 |
#include <map> |
| 31 |
|
| 32 |
#ifdef __VISUALC__ |
| 33 |
#ifdef _DEBUG |
| 34 |
#define new DEBUG_NEW |
| 35 |
#endif |
| 36 |
#endif |
| 37 |
|
| 38 |
struct tagCallTipInfo { |
| 39 |
wxString sig; |
| 40 |
wxString retValue; |
| 41 |
std::vector<std::pair<int, int>> paramLen; |
| 42 |
}; |
| 43 |
|
| 44 |
clCallTip::clCallTip() |
| 45 |
: m_curr(0) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
clCallTip::clCallTip(const std::vector<TagEntryPtr>& tips) |
| 50 |
: m_curr(0) |
| 51 |
{ |
| 52 |
Initialize(tips); |
| 53 |
} |
| 54 |
|
| 55 |
clCallTip::clCallTip(const clCallTip& rhs) { *this = rhs; } |
| 56 |
|
| 57 |
clCallTip& clCallTip::operator=(const clCallTip& rhs) |
| 58 |
{ |
| 59 |
if(this == &rhs) |
| 60 |
return *this; |
| 61 |
m_tips = rhs.m_tips; |
| 62 |
m_curr = rhs.m_curr; |
| 63 |
return *this; |
| 64 |
} |
| 65 |
|
| 66 |
thread_local wxString empty_tip; |
| 67 |
wxString clCallTip::First() |
| 68 |
{ |
| 69 |
m_curr = 0; |
| 70 |
if(m_tips.empty()) |
| 71 |
return wxEmptyString; |
| 72 |
return TipAt(0); |
| 73 |
} |
| 74 |
|
| 75 |
wxString clCallTip::TipAt(int at) |
| 76 |
{ |
| 77 |
wxString tip; |
| 78 |
if(m_tips.size() > 1) { |
| 79 |
tip << m_tips.at(at).str; |
| 80 |
} else { |
| 81 |
tip << m_tips.at(0).str; |
| 82 |
} |
| 83 |
return tip; |
| 84 |
} |
| 85 |
|
| 86 |
wxString clCallTip::Next() |
| 87 |
{ |
| 88 |
// format a tip string and return it |
| 89 |
if(m_tips.empty()) |
| 90 |
return wxEmptyString; |
| 91 |
|
| 92 |
m_curr++; |
| 93 |
if(m_curr >= (int)m_tips.size()) { |
| 94 |
m_curr = 0; |
| 95 |
} // if( m_curr >= m_tips.size() ) |
| 96 |
|
| 97 |
return TipAt(m_curr); |
| 98 |
} |
| 99 |
|
| 100 |
wxString clCallTip::Prev() |
| 101 |
{ |
| 102 |
// format a tip string and return it |
| 103 |
if(m_tips.empty()) |
| 104 |
return wxEmptyString; |
| 105 |
|
| 106 |
m_curr--; |
| 107 |
if(m_curr < 0) { |
| 108 |
m_curr = (int)m_tips.size() - 1; |
| 109 |
} |
| 110 |
return TipAt(m_curr); |
| 111 |
} |
| 112 |
|
| 113 |
wxString clCallTip::All() |
| 114 |
{ |
| 115 |
wxString tip; |
| 116 |
for(size_t i = 0; i < m_tips.size(); i++) { |
| 117 |
tip << m_tips.at(i).str << wxT("\n"); |
| 118 |
} |
| 119 |
tip.RemoveLast(); |
| 120 |
return tip; |
| 121 |
} |
| 122 |
|
| 123 |
int clCallTip::Count() const { return (int)m_tips.size(); } |
| 124 |
|
| 125 |
void clCallTip::Initialize(const std::vector<TagEntryPtr>& tags) { FormatTagsToTips(tags, m_tips); } |
| 126 |
|
| 127 |
void clCallTip::GetHighlightPos(int index, int& start, int& len) |
| 128 |
{ |
| 129 |
start = wxNOT_FOUND; |
| 130 |
len = wxNOT_FOUND; |
| 131 |
if(m_curr >= 0 && m_curr < (int)m_tips.size()) { |
| 132 |
clTipInfo ti = m_tips.at(m_curr); |
| 133 |
int base = ti.str.Find(wxT("(")); |
| 134 |
|
| 135 |
// sanity |
| 136 |
if(base != wxNOT_FOUND && index < (int)ti.paramLen.size() && index >= 0) { |
| 137 |
start = ti.paramLen.at(index).first + base; |
| 138 |
len = ti.paramLen.at(index).second; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
wxString clCallTip::Current() |
| 144 |
{ |
| 145 |
// format a tip string and return it |
| 146 |
if(m_tips.empty()) |
| 147 |
return wxEmptyString; |
| 148 |
|
| 149 |
if(m_curr >= (int)m_tips.size() || m_curr < 0) { |
| 150 |
m_curr = 0; |
| 151 |
} |
| 152 |
return TipAt(m_curr); |
| 153 |
} |
| 154 |
|
| 155 |
void clCallTip::SelectSiganture(const wxString& signature) |
| 156 |
{ |
| 157 |
// search for a match |
| 158 |
for(size_t i = 0; i < m_tips.size(); ++i) { |
| 159 |
if(m_tips.at(i).str == signature) { |
| 160 |
m_curr = i; |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void clCallTip::FormatTagsToTips(const TagEntryPtrVector_t& tags, std::vector<clTipInfo>& tips) |
| 167 |
{ |
| 168 |
std::map<wxString, tagCallTipInfo> mymap; |
| 169 |
for(size_t i = 0; i < tags.size(); i++) { |
| 170 |
tagCallTipInfo cti; |
| 171 |
TagEntryPtr t = tags.at(i); |
| 172 |
|
| 173 |
// Use basic signature |
| 174 |
if(t->GetFlags() & TagEntry::Tag_No_Signature_Format) { |
| 175 |
|
| 176 |
wxString raw_sig = t->GetSignature(); |
| 177 |
int startOffset(0); |
| 178 |
if(raw_sig.Find(wxT("(")) != wxNOT_FOUND) { |
| 179 |
startOffset = raw_sig.Find(wxT("(")) + 1; |
| 180 |
} |
| 181 |
|
| 182 |
// Remove the open / close brace |
| 183 |
wxString tmpsig = raw_sig; |
| 184 |
tmpsig.Trim().Trim(false); // remove any whitespaces from right |
| 185 |
|
| 186 |
size_t j = 0; |
| 187 |
size_t depth = -1; // we start collecting after we find the open paren |
| 188 |
for(; j < tmpsig.length(); ++j) { |
| 189 |
if(tmpsig.at(j) == '(') { |
| 190 |
++depth; |
| 191 |
} else if(tmpsig.at(j) == ')') { |
| 192 |
--depth; |
| 193 |
} else if(tmpsig.GetChar(j) == wxT(',') && (depth == 0)) { |
| 194 |
std::pair<int, int> p; |
| 195 |
p.first = startOffset; |
| 196 |
p.second = (j - startOffset); |
| 197 |
cti.paramLen.push_back(p); |
| 198 |
startOffset = j + 1; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if(startOffset != (int)j) { |
| 203 |
std::pair<int, int> p; |
| 204 |
p.first = startOffset; |
| 205 |
p.second = (j - startOffset - |
| 206 |
1); // -1 here since its likely that the signature ends with a ")" so don't include it |
| 207 |
cti.paramLen.push_back(p); |
| 208 |
} |
| 209 |
cti.sig = raw_sig; |
| 210 |
mymap[raw_sig] = cti; |
| 211 |
|
| 212 |
} else { |
| 213 |
if(t->IsMethod()) { |
| 214 |
|
| 215 |
wxString raw_sig(t->GetSignature().Trim().Trim(false)); |
| 216 |
|
| 217 |
// evaluate the return value of the tag |
| 218 |
cti.retValue = TagsManagerST::Get()->GetFunctionReturnValueFromPattern(t); |
| 219 |
|
| 220 |
bool hasDefaultValues = (raw_sig.Find(wxT("=")) != wxNOT_FOUND); |
| 221 |
|
| 222 |
// the key for unique entries is the function prototype without the variables names and |
| 223 |
// any default values |
| 224 |
wxString key = TagsManagerST::Get()->NormalizeFunctionSig(raw_sig, 0); |
| 225 |
|
| 226 |
// the signature that we want to keep is one with name & default values, so try and get the maximum out |
| 227 |
// of the |
| 228 |
// function signature |
| 229 |
wxString full_signature = TagsManagerST::Get()->NormalizeFunctionSig( |
| 230 |
raw_sig, Normalize_Func_Name | Normalize_Func_Default_value, &cti.paramLen); |
| 231 |
cti.sig = full_signature; |
| 232 |
|
| 233 |
if(hasDefaultValues) { |
| 234 |
// incase default values exist in this prototype, |
| 235 |
// update/insert this signature |
| 236 |
mymap[key] = cti; |
| 237 |
} |
| 238 |
|
| 239 |
// make sure we dont add duplicates |
| 240 |
if(mymap.find(key) == mymap.end()) { |
| 241 |
// add it |
| 242 |
mymap[key] = cti; |
| 243 |
} |
| 244 |
|
| 245 |
} else { |
| 246 |
// macro |
| 247 |
wxString macroName = t->GetName(); |
| 248 |
wxString pattern = t->GetPattern(); |
| 249 |
|
| 250 |
int where = pattern.Find(macroName); |
| 251 |
if(where != wxNOT_FOUND) { |
| 252 |
// remove the #define <name> from the pattern |
| 253 |
pattern = pattern.Mid(where + macroName.Length()); |
| 254 |
pattern = pattern.Trim().Trim(false); |
| 255 |
if(pattern.StartsWith(wxT("("))) { |
| 256 |
// this macro has the form of a function |
| 257 |
pattern = pattern.BeforeFirst(wxT(')')); |
| 258 |
pattern.Append(wxT(')')); |
| 259 |
cti.sig = pattern.Trim().Trim(false); |
| 260 |
mymap[cti.sig] = cti; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
std::map<wxString, tagCallTipInfo>::iterator iter = mymap.begin(); |
| 268 |
tips.clear(); |
| 269 |
for(; iter != mymap.end(); iter++) { |
| 270 |
wxString tip; |
| 271 |
tip << iter->second.sig; |
| 272 |
if(iter->second.retValue.empty() == false) { |
| 273 |
tip << " -> " << iter->second.retValue.Trim(false).Trim(); |
| 274 |
} |
| 275 |
|
| 276 |
clTipInfo ti; |
| 277 |
ti.paramLen = iter->second.paramLen; |
| 278 |
ti.str = tip; |
| 279 |
tips.push_back(ti); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
bool clCallTip::SelectTipToMatchArgCount(size_t argcount) |
| 284 |
{ |
| 285 |
for(size_t i = 0; i < m_tips.size(); ++i) { |
| 286 |
if(m_tips.at(i).paramLen.size() > argcount) { |
| 287 |
m_curr = i; |
| 288 |
return true; |
| 289 |
} |
| 290 |
} |
| 291 |
return false; |
| 292 |
} |