Skip to content

Commit 972a02f

Browse files
committed
initial support for finding references
1 parent af1e2f0 commit 972a02f

File tree

12 files changed

+572
-31
lines changed

12 files changed

+572
-31
lines changed

TODO

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ Any/dmd:
127127
- arguments to local functions treated as locals
128128
- local functions/delegates should have debug info for closure vars ("this" exists)
129129
- extern(D) function arguments shown in the wrong order
130-
- x64: no type information for dAssocArray
131130
- support GDC+mago
131+
- bugzilla 14286: x64: no type information for dAssocArray
132+
- bugzilla 4014: no debug info from libraries
132133

133134
Mago:
134135
- show derived class
@@ -144,7 +145,7 @@ Mago:
144145
- allow symbolic address input
145146
+ no symbols for publics without debug info
146147
- improve stepping through C
147-
- load symbols from Microsoft Symbol Server, respect symbol settings
148+
+ load symbols from Microsoft Symbol Server, respect symbol settings
148149
+ stack: arguments show locals aswell
149150
- identically named locals show the same value
150151
+ dstring shows as uint[] without string representation

stdext/com.d

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module stdext.com;
1111
//version(none):
1212
import std.utf;
1313
import std.string;
14+
import std.exception;
1415
import core.stdc.string;
1516
import core.stdc.wchar_ : wcslen;
1617

@@ -363,6 +364,14 @@ wstring to_wstring(in char* pText)
363364
return toUTF16(pText[0 .. len]);
364365
}
365366

367+
wstring to_tmpwstring(in wchar* pText)
368+
{
369+
if(!pText)
370+
return ""w;
371+
size_t len = wcslen(pText);
372+
return assumeUnique(pText[0 .. len]);
373+
}
374+
366375
///////////////////////////////////////////////////////////////////////
367376
struct ScopedBSTR
368377
{

vdc/abothe/comserver/VDServer.cs

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
using D_Parser.Completion;
2121
using D_Parser.Resolver;
2222
using D_Parser.Resolver.TypeResolution;
23-
using D_Parser.Completion.ToolTips;
23+
using D_Parser.Completion.ToolTips;
24+
using D_Parser.Refactoring;
2425

2526
namespace DParserCOMServer
2627
{
@@ -46,7 +47,9 @@ public interface IVDServer
4647
void GetBinaryIsInLocations(string filename, out uint[] locs); // array of pairs of DWORD
4748
void GetLastMessage(out string message);
4849
void GetDefinition(string filename, int startLine, int startIndex, int endLine, int endIndex);
49-
void GetDefinitionResult(out int startLine, out int startIndex, out int endLine, out int endIndex, out string filename);
50+
void GetDefinitionResult(out int startLine, out int startIndex, out int endLine, out int endIndex, out string filename);
51+
void GetReferences(string filename, string tok, uint line, uint idx, string expr);
52+
void GetReferencesResult(out string stringList);
5053
}
5154

5255
class NodeToolTipContentGen : NodeTooltipRepresentationGen
@@ -71,22 +74,26 @@ protected override void AppendFormat (string content, StringBuilder sb, FormatFl
7174
public class VDServer : IVDServer
7275
{
7376
private CodeLocation _tipStart, _tipEnd;
74-
private readonly StringBuilder _tipText = new StringBuilder();
75-
private StringBuilder _expansions;
77+
private readonly StringBuilder _tipText = new StringBuilder();
78+
private StringBuilder _expansions;
79+
private StringBuilder _references;
7680
private string _imports;
7781
private string _stringImports;
7882
private string _versionIds;
7983
private string _debugIds;
8084
private uint _flags;
8185
EditorData _editorData = new EditorData();
8286

87+
// remember modules, the global cache might not yet be ready or might have forgotten a module
8388
public Dictionary<string, DModule> _modules = new Dictionary<string, DModule>();
89+
8490
public DModule GetModule(string fileName)
8591
{
8692
DModule mod;
87-
if (!_modules.TryGetValue (fileName, out mod))
88-
GlobalParseCache.GetModule (fileName);
89-
return mod;
93+
mod = GlobalParseCache.GetModule (fileName);
94+
if (mod == null)
95+
_modules.TryGetValue(fileName, out mod);
96+
return mod;
9097
}
9198
public Dictionary<string, string> _sources = new Dictionary<string, string>();
9299

@@ -211,7 +218,8 @@ public void GetTipResult(out int startLine, out int startIndex, out int endLine,
211218
//MessageBox.Show("GetTipResult()");
212219
//throw new NotImplementedException();
213220
}
214-
public void GetSemanticExpansions(string filename, string tok, uint line, uint idx, string expr)
221+
222+
public void GetSemanticExpansions(string filename, string tok, uint line, uint idx, string expr)
215223
{
216224
var ast = GetModule(filename);
217225

@@ -244,17 +252,8 @@ public void GetSemanticExpansionsResult(out string stringList)
244252
//MessageBox.Show("GetSemanticExpansionsResult()");
245253
//throw new NotImplementedException();
246254
}
247-
public void IsBinaryOperator(string filename, uint startLine, uint startIndex, uint endLine, uint endIndex, out bool pIsOp)
248-
{
249-
var ast = GetModule(filename);
250255

251-
if (ast == null)
252-
throw new COMException("module not found", 1);
253-
254-
//MessageBox.Show("IsBinaryOperator()");
255-
throw new NotImplementedException();
256-
}
257-
public void GetParseErrors(string filename, out string errors)
256+
public void GetParseErrors(string filename, out string errors)
258257
{
259258
var ast = GetModule(filename);
260259

@@ -273,19 +272,32 @@ public void GetParseErrors(string filename, out string errors)
273272
errors = errs;
274273
//MessageBox.Show("GetParseErrors()");
275274
//throw new COMException("No Message", 1);
276-
}
277-
public void GetBinaryIsInLocations(string filename, out uint[] locs) // array of pairs of DWORD
275+
}
276+
277+
public void IsBinaryOperator(string filename, uint startLine, uint startIndex, uint endLine, uint endIndex, out bool pIsOp)
278+
{
279+
var ast = GetModule(filename);
280+
281+
if (ast == null)
282+
throw new COMException("module not found", 1);
283+
284+
//MessageBox.Show("IsBinaryOperator()");
285+
throw new NotImplementedException();
286+
}
287+
public void GetBinaryIsInLocations(string filename, out uint[] locs) // array of pairs of DWORD
278288
{
279289
//MessageBox.Show("GetBinaryIsInLocations()");
280290
locs = null;
281291
//throw new COMException("No Message", 1);
282292
}
283-
public void GetLastMessage(out string message)
293+
294+
public void GetLastMessage(out string message)
284295
{
285296
//MessageBox.Show("GetLastMessage()");
286297
message = "__no_message__"; // avoid throwing exception
287298
//throw new COMException("No Message", 1);
288299
}
300+
289301
public void GetDefinition(string filename, int startLine, int startIndex, int endLine, int endIndex)
290302
{
291303
var ast = GetModule(filename);
@@ -332,17 +344,80 @@ public void GetDefinition(string filename, int startLine, int startIndex, int en
332344
_tipText.Append((node as DModule).FileName);
333345
}
334346
}
335-
336347
public void GetDefinitionResult(out int startLine, out int startIndex, out int endLine, out int endIndex, out string filename)
337348
{
338349
startLine = _tipStart.Line;
339350
startIndex = _tipStart.Column - 1;
340351
endLine = _tipEnd.Line;
341352
endIndex = _tipEnd.Column - 1;
342353
filename = _tipText.ToString();
343-
}
354+
}
355+
356+
public void GetReferences(string filename, string tok, uint line, uint idx, string expr)
357+
{
358+
var ast = GetModule(filename);
359+
360+
if (ast == null)
361+
throw new COMException("module not found", 1);
362+
363+
_setupEditorData();
364+
CodeLocation loc = new CodeLocation((int)idx + 1, (int)line);
365+
_editorData.CaretLocation = loc;
366+
_editorData.SyntaxTree = ast as DModule;
367+
_editorData.ModuleCode = _sources[filename];
368+
_editorData.CaretOffset = getCodeOffset(_editorData.ModuleCode, loc);
369+
370+
_references = null;
371+
372+
ISyntaxRegion sr;
373+
DResolver.NodeResolutionAttempt attempt;
374+
var rr = DResolver.ResolveTypeLoosely(_editorData, out attempt, out sr);
375+
376+
StringBuilder refs = new StringBuilder();
377+
if (rr != null)
378+
{
379+
var n = DResolver.GetResultMember(rr, true);
344380

345-
///////////////////////////////////
381+
if (n != null)
382+
{
383+
var ctxt = ResolutionContext.Create(_editorData, true);
384+
if (n.ContainsAttribute(DTokens.Private) || ((n is DVariable) && (n as DVariable).IsLocal))
385+
{
386+
GetReferencesInModule(ast, refs, n, ctxt);
387+
}
388+
else
389+
{
390+
foreach (var basePath in _imports.Split('\n'))
391+
foreach (var mod in GlobalParseCache.EnumModulesRecursively(basePath))
392+
GetReferencesInModule(mod, refs, n, ctxt);
393+
}
394+
}
395+
//var res = TypeReferenceFinder.Scan(_editorData, System.Threading.CancellationToken.None, null);
396+
}
397+
_references = refs;
398+
}
399+
400+
private static void GetReferencesInModule(DModule ast, StringBuilder refs, DNode n, ResolutionContext ctxt)
401+
{
402+
var res = ReferencesFinder.Scan(ast, n, ctxt);
403+
404+
int cnt = res.Count();
405+
foreach (var r in res)
406+
{
407+
var rfilename = ast.FileName;
408+
var rloc = r.Location;
409+
var ln = String.Format("{0},{1},{2},{3}:{4}\n", rloc.Line, rloc.Column - 1, rloc.Line, rloc.Column, rfilename);
410+
refs.Append(ln);
411+
}
412+
}
413+
public void GetReferencesResult(out string stringList)
414+
{
415+
stringList = _references != null ? _references.ToString() : null;
416+
//MessageBox.Show("GetSemanticExpansionsResult()");
417+
//throw new NotImplementedException();
418+
}
419+
420+
///////////////////////////////////
346421
void _setupEditorData()
347422
{
348423
string versions = _versionIds;

vdc/ivdserver.d

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ public:
164164
//
165165
// return S_FALSE as long as the semantic analysis is still running
166166
HRESULT GetDefinitionResult(ref int startLine, ref int startIndex, ref int endLine, ref int endIndex, BSTR* filename);
167+
168+
// request a list of references for a given text location
169+
//
170+
// filename: file name
171+
// tok: the identifier to reference
172+
// line, idx: the location of the caret in the text editor
173+
// expr: the expression to evaluate at the insertion point in case of parser issues
174+
// with the current text
175+
// it is assumed that the reference finding is forwarded to some other thread
176+
// and that the status can be polled by GetReferencesResult
177+
HRESULT GetReferences(in BSTR filename, in BSTR tok, uint line, uint idx, in BSTR expr);
178+
HRESULT GetReferencesResult(BSTR* stringList);
167179
}
168180

169181
///////////////////////////////////////////////////////////////////////

vdc/vdserver.d

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,16 @@ class VDServer : ComObject, IVDServer
603603
return S_OK;
604604
}
605605

606+
HRESULT GetReferences(in BSTR filename, in BSTR tok, uint line, uint idx, in BSTR expr)
607+
{
608+
return E_NOTIMPL;
609+
}
610+
611+
HRESULT GetReferencesResult(BSTR* stringList)
612+
{
613+
return E_NOTIMPL;
614+
}
615+
606616
///////////////////////////////////////////////////////////////
607617
// create our own task pool to be able to destroy it (it keeps a the
608618
// arguments to the last task, so they are never collected)

vdc/vdserver.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ interface IVDServer : IUnknown
3232
HRESULT GetLastMessage([out,retval] BSTR* message);
3333
HRESULT GetDefinition([in] BSTR filename, [in] DWORD startLine, [in] DWORD startIndex, [in] DWORD endLine, [in] DWORD endIndex);
3434
HRESULT GetDefinitionResult([out] DWORD *startLine, [out] DWORD *startIndex, [out] DWORD *endLine, [out] DWORD *endIndex, [out,retval] BSTR* filename);
35+
HRESULT GetReferences([in] BSTR filename, [in] BSTR tok, [in] DWORD line, [in] DWORD idx, [in] BSTR expr);
36+
HRESULT GetReferencesResult([out,retval] BSTR* stringList);
3537
};
3638

3739
// library statement

visuald/dlangsvc.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,12 @@ class LanguageService : DisposingComObject,
763763
wstring expr = src.FindExpressionBefore(line, idx);
764764
return vdServerClient.GetSemanticExpansions(src.GetFileName(), tok, line, idx, expr, cb);
765765
}
766+
uint GetReferences(Source src, string tok, int line, int idx, GetReferencesCallBack cb)
767+
{
768+
ConfigureSemanticProject(src);
769+
wstring expr;
770+
return vdServerClient.GetReferences(src.GetFileName(), tok, line, idx, expr, cb);
771+
}
766772
void UpdateSemanticModule(Source src)
767773
{
768774
}

visuald/dpackage.d

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,13 @@ version(none)
10441044
assert(s_instance);
10451045
return s_instance.mLibInfos;
10461046
}
1047-
1047+
1048+
static Library GetLibrary()
1049+
{
1050+
assert(s_instance);
1051+
return s_instance.mLibrary;
1052+
}
1053+
10481054
static void scheduleUpdateLibrary()
10491055
{
10501056
assert(s_instance);

0 commit comments

Comments
 (0)