-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCodeToolTips.h
391 lines (377 loc) · 18.2 KB
/
CodeToolTips.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#pragma once
#include "ToolTipResult.h"
#include "Compile\ScriptOMAll.h"
#include "SCIPicEditor.h"
#include "Compile\PMachine.h"
const key_value_pair<PCSTR, PCSTR> c_szVarToClass[] =
{
{ TEXT("gEgo"), TEXT("Ego") },
{ TEXT("gRoom"), TEXT("Rm") },
{ TEXT("gGame"), TEXT("Game") },
{ TEXT("gWindow"), TEXT("Window") },
};
using std::string;
using std::vector;
using std::auto_ptr;
using sci::ClassVector;
using sci::SendCall;
using sci::MethodVector;
using sci::DefineVector;
using sci::ClassPropertyVector;
using sci::ProcedureVector;
using sci::NodeType;
using sci::FunctionPtr;
using sci::ProcedureDefinition;
using sci::VariableDecl;
using sci::VariableDeclVector;
using sci::ClassDefinition;
using sci::ClassPtr;
using sci::NodeTypeSendCall;
using sci::FunctionBase;
void _GetMethodInfoHelper(PTSTR szBuf, size_t cchBuf, const sci::FunctionBase *pMethod);
void _GetClassInfoHelper(PTSTR szBuf, size_t cchBuf, const sci::ClassDefinition *pClass);
template<typename _TContext>
std::string _ClassFromObjectName(SCIClassBrowser &browser, _TContext *pContext, const std::string &strObject)
{
std::string className;
if (!strObject.empty())
{
// Get the last known good version of this script (for resolving things that reside in this script)
const sci::Script *pGood = browser.GetLKGScript(browser.GetScriptNumberHelper(&pContext->Script()));
if (pContext->ClassPtr)
{
if (strObject == "self")
{
className = pContext->ClassPtr->GetName();
}
else if (strObject == "super")
{
className = pContext->ClassPtr->GetSuperClass();
}
}
if (className.empty())
{
// No luck yet... find the last known good version of this script, and try to match it to an instance.
if (pGood)
{
const ClassVector &classes = pGood->GetClasses();
for (size_t i = 0; i < classes.size(); i++)
{
const ClassDefinition *pClass = classes[i];
if (pClass->IsInstance() && (strObject == pClass->GetName()))
{
// We have a match! Get the instance's super class.
// REVIEW: if we had an instant way to get a instance's methods/props, by specifying
// a script number and the instance name, then we could avoid all this.
className = pClass->GetName();
break;
}
}
}
}
if (className.empty())
{
// Map some well know global variables to classes.
// Ideally, they should be mapped to instances, but we really only know
// how to deal with instances in the current script.
className = LookupStringValue(c_szVarToClass, ARRAYSIZE(c_szVarToClass), strObject, "");
}
if (className.empty())
{
className = strObject; // A class name itself. View:new(), etc...
}
}
return className;
}
template<typename _TContext>
ToolTipResult GetToolTipResult(_TContext *pContext)
{
TCHAR szTip[MAX_PATH * 4]; // Scratch buffer
szTip[0] = 0;
bool fFound = false;
ToolTipResult result;
SCIClassBrowser &browser = theApp.GetResourceMap().GetClassBrowser();
ClassBrowserLock lock(browser);
if (lock.TryLock())
{
// Try to figure out where we are.
// 1. A classname in a class definition? (Detect this by seeing if name is empty, but superclass is not)
if (pContext->ClassPtr &&
!pContext->ClassPtr->GetName().empty() &&
pContext->ClassPtr->GetSuperClass().empty())
{
std::string strText = pContext->ScratchString();
const ClassVector &classes = browser.GetAllClasses();
const ClassVector::const_iterator classIt = match_name(classes.begin(), classes.end(), strText);
if (classIt != classes.end())
{
_GetClassInfoHelper(szTip, ARRAYSIZE(szTip), (*classIt));
result.strTip = szTip;
// Give location information for it.
result.strBaseText = (*classIt)->GetName();
result.iLineNumber = (*classIt)->GetLineNumber();
result.scriptId = ScriptId((*classIt)->GetOwnerScript()->GetPath().c_str());
return result;
}
}
else
{
// Get the last known good version of this script (for resolving things that reside in this script)
const sci::Script *pGood = browser.GetLKGScript(browser.GetScriptNumberHelper(&pContext->Script()));
// 2) A selector in a send statement (e.g. x in (send gEgo:x))
std::string strText = pContext->ScratchString();
if (!strText.empty())
{
bool fFound = false;
NodeType type = pContext->GetTopKnownNode();
if (type == NodeTypeSendCall)
{
// Then strText should be a selector.
const SendCall *pSend = static_cast<const SendCall*>(pContext->GetSyntaxNode(type));
ASSERT(pSend);
// Now we have the object to which we're sending...
// Figure out which species/class it is.
std::string object = _ClassFromObjectName(browser, pContext, pSend->GetObject());
if (!object.empty())
{
auto_ptr<MethodVector> pMethods(browser.CreateMethodArray(object, &pContext->Script()));
const MethodVector::const_iterator methodIt = match_name(pMethods->begin(), pMethods->end(), strText);
fFound = (methodIt != pMethods->end());
if (fFound)
{
// Make a std::string like
// species:method(param1 param2 param3)
fFound = true;
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("%s::"), (*methodIt)->GetOwnerClass()->GetName().c_str());
TCHAR szTemp[200];
_GetMethodInfoHelper(szTemp, ARRAYSIZE(szTemp), (*methodIt));
StringCchCat(szTip, ARRAYSIZE(szTip), szTemp);
result.strTip = szTip;
// "goto definition" info
result.iLineNumber = (*methodIt)->GetLineNumber();
result.scriptId = ScriptId((*methodIt)->GetOwnerScript()->GetPath().c_str());
StringCchPrintf(szTemp, ARRAYSIZE(szTemp), TEXT("%s::%s"), (*methodIt)->GetOwnerClass()->GetName().c_str(), (*methodIt)->GetName().c_str());
result.strBaseText = szTemp;
}
else
{
// Maybe it's a property
auto_ptr<ClassPropertyVector> pProperties(browser.CreatePropertyArray(object, &pContext->Script()));
fFound = matches_name(pProperties->begin(), pProperties->end(), strText);
if (fFound)
{
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("%s::%s"), object.c_str(), strText.c_str());
result.strTip = szTip;
}
}
}
}
if (!fFound)
{
// 3. Is it a define?
// Check for defines.
DefineVector::const_iterator defineIt;
// Local ones.
if (pGood)
{
const DefineVector &defines = pGood->GetDefines();
defineIt = match_name(defines.begin(), defines.end(), strText);
fFound = (defineIt != defines.end());
}
if (!fFound)
{
// Global ones (even those not included by this script)
const vector<sci::Script*> &headers = browser.GetHeaders();
vector<sci::Script*>::const_iterator headerIt = headers.begin();
for (; !fFound && (headerIt != headers.end()); ++headerIt)
{
const DefineVector &defines = (*headerIt)->GetDefines();
defineIt = match_name(defines.begin(), defines.end(), strText);
fFound = (defineIt != defines.end());
}
}
if (fFound)
{
// It was a define.
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("(define %s %04x)"), strText.c_str(), (*defineIt)->GetValue());
result.strTip = szTip;
// Give location information for it.
result.strBaseText = (*defineIt)->GetLabel().c_str();
result.iLineNumber = (*defineIt)->GetLineNumber();
result.scriptId = ScriptId((*defineIt)->GetOwnerScript()->GetPath().c_str());
}
}
if (!fFound)
{
// 4. Maybe it's a procedure
const ProcedureDefinition *pProc = NULL;
// Check public procedures
const ProcedureVector &publicProcs = browser.GetPublicProcedures();
ProcedureVector::const_iterator procIt = match_name(publicProcs.begin(), publicProcs.end(), strText);
fFound = (procIt != publicProcs.end());
if (!fFound && pGood)
{
const ProcedureVector &localProcs = pGood->GetProcedures();
procIt = match_name(localProcs.begin(), localProcs.end(), strText);
fFound = (procIt != localProcs.end());
}
if (fFound)
{
_GetMethodInfoHelper(szTip, ARRAYSIZE(szTip), *procIt);
result.strTip = szTip;
// "goto definition" info
result.iLineNumber = (*procIt)->GetLineNumber();
result.scriptId = ScriptId((*procIt)->GetOwnerScript()->GetPath().c_str());
result.strBaseText = (*procIt)->GetName();
}
}
if (!fFound)
{
// 5. Kernel function?
KernelInfo *pkernel = match_name_dot(KrnlInfo, KrnlInfo + ARRAYSIZE(KrnlInfo), strText);
fFound = (pkernel != KrnlInfo + ARRAYSIZE(KrnlInfo));
if (fFound)
{
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("%s %s(%s)"), (PCTSTR)pkernel->Ret, (PCTSTR)pkernel->Name, (PCTSTR)pkernel->Params);
result.strTip = szTip;
}
}
if (!fFound)
{
// 6. Local method variables
const FunctionPtr pFunction = pContext->FunctionPtr;
if (pFunction)
{
const VariableDeclVector &tempVars = pFunction->GetVariables();
fFound = matches_name(tempVars.begin(), tempVars.end(), strText);
if (fFound)
{
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("Temporary variable: %s"), strText.c_str());
result.strTip = szTip;
}
if (!fFound)
{
// TODO: needs updating due to GetParams
/*
const vector<std::string> &functionParams = pFunction->GetParams();
fFound = find(functionParams.begin(), functionParams.end(), strText) != functionParams.end();
if (fFound)
{
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("Parameter: %s"), strText.c_str());
result.strTip = szTip;
}*/
}
}
}
if (!fFound)
{
// 7. Script variables
VariableDecl *pVar = NULL; // Used for goto information
if (pGood)
{
// Locals
const VariableDeclVector &localVars = pGood->GetScriptVariables();
VariableDeclVector::const_iterator varIt = match_name(localVars.begin(), localVars.end(), strText);
fFound = (varIt != localVars.end());
if (fFound)
{
pVar = *varIt;
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("Script variable: %s"), strText.c_str());
result.strTip = szTip;
}
}
if (!fFound)
{
// Globals
const VariableDeclVector *pVars = browser.GetMainGlobals();
if (pVars)
{
VariableDeclVector::const_iterator varIt = match_name(pVars->begin(), pVars->end(), strText);
fFound = (varIt != pVars->end());
if (fFound)
{
pVar = *varIt;
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("Global variable: %s"), strText.c_str());
result.strTip = szTip;
}
}
}
if (fFound && pVar)
{
// Add some goto info
result.iLineNumber = pVar->GetLineNumber();
result.scriptId = ScriptId(pVar->GetOwnerScript()->GetPath().c_str());
result.strBaseText = pVar->GetName().c_str();
}
}
if (!fFound)
{
// 8. Class property
const ClassPtr pClass = pContext->ClassPtr;
if (pClass && !pClass->GetSuperClass().empty())
{
// Since we already know the superclass, don't pass the script in (since that relies on a
// successfully compiled previous script)
auto_ptr<ClassPropertyVector> pProperties(browser.CreatePropertyArray(pClass->GetName(), NULL, pClass->GetSuperClass().c_str()));
fFound = matches_name(pProperties->begin(), pProperties->end(), strText);
if (fFound)
{
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("%s::%s"), pClass->GetSuperClass().c_str(), strText.c_str());
result.strTip = szTip;
}
}
}
if (!fFound && pGood)
{
// 9. Local instance (or class, but we would have already handled that in 8)
// Find a class that it matches
const ClassVector &classes = pGood->GetClasses();
ClassVector::const_iterator classIt = match_name(classes.begin(), classes.end(), strText);
fFound = (classIt != classes.end());
if (fFound)
{
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("%s of %s"), strText.c_str(), (*classIt)->GetSuperClass().c_str());
result.strTip = szTip;
// "goto definition" info
result.iLineNumber = (*classIt)->GetLineNumber();
result.scriptId = ScriptId((*classIt)->GetOwnerScript()->GetPath().c_str());
result.strBaseText = (*classIt)->GetName();
}
}
if (!fFound)
{
// TODO - look up saids - could probably do this by having the
// various parses set a bit indicating what kind of parsing they're doing (singleq, doubleq, etc...)
// And checking if there is a pContext->PropertyValue, or a ComplexPropertyValue statement.
/*
PCTSTR pszWord = FindValidVocabStringFromRight(_strValueTurd.c_str());
if (pszWord)
{
// Part of a Said std::string?
Vocab000 *pVocab000 = theApp.GetResourceMap().GetVocab000();
if (pVocab000)
{
Vocab000::WordGroup dwGroup;
if (pVocab000->LookupWord(pszWord, dwGroup))
{
WordClass dwClass;
if (SUCCEEDED(pVocab000->GetGroupClass(dwGroup, &dwClass)))
{
std::string strWordClass;
GetWordClassString(dwClass, strWordClass);
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("%s: %s"), pszWord, strWordClass.c_str());
}
}
else
{
StringCchPrintf(szTip, ARRAYSIZE(szTip), TEXT("%s - not found"), pszWord);
}
}
}
*/
}
}
}
}
return result;
}