2020using D_Parser . Completion ;
2121using D_Parser . Resolver ;
2222using D_Parser . Resolver . TypeResolution ;
23- using D_Parser . Completion . ToolTips ;
23+ using D_Parser . Completion . ToolTips ;
24+ using D_Parser . Refactoring ;
2425
2526namespace 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 ;
0 commit comments