99using Roslyn . Compilers . Common ;
1010using Ninject . Extensions . Logging ;
1111using System . Threading . Tasks ;
12+ using System . Linq . Expressions ;
1213
1314namespace SourceCodeReader . Web . LanguageServices . DotNet
1415{
@@ -18,7 +19,7 @@ public class DotNetCodeEditorService : IEditorService
1819 private ILogger logger ;
1920
2021 public DotNetCodeEditorService (
21- IApplicationConfigurationProvider applicationConfigurationProvider ,
22+ IApplicationConfigurationProvider applicationConfigurationProvider ,
2223 ILogger logger )
2324 {
2425 this . applicationConfigurationProvider = applicationConfigurationProvider ;
@@ -46,23 +47,62 @@ public string BuildNavigatableSourceCodeFromFile(string filename)
4647 }
4748 }
4849
50+
51+ public FindReferenceResult GoToDefinition ( FindReferenceParameter parameter , IFindReferenceProgress findReferenceProgressListener )
52+ {
53+ FindReferenceResult result = null ;
54+ var goToDefinitionSyntaxWalker = new GoToDefinitionSyntaxWalker ( ) ;
55+ TokenKind searchedTokenKind = ( TokenKind ) Enum . Parse ( typeof ( TokenKind ) , parameter . Kind ) ;
56+ TraverseThroughAllTheProjectFiles ( parameter , findReferenceProgressListener , ( documentName , documentRelativePath , syntaxRoot ) =>
57+ {
58+ goToDefinitionSyntaxWalker . DoVisit ( syntaxRoot , parameter . Text , searchedTokenKind , ( foundlocation ) =>
59+ {
60+ result = new FindReferenceResult
61+ {
62+ FileName = documentName ,
63+ Path = documentRelativePath ,
64+ Position = foundlocation
65+ } ;
66+ } ) ;
67+ } ) ;
68+
69+ findReferenceProgressListener . OnFindReferenceCompleted ( ) ;
70+
71+ return result ;
72+ }
73+
4974 public List < FindReferenceResult > FindRefernces ( FindReferenceParameter parameter , IFindReferenceProgress findReferenceProgressListener )
75+ {
76+ var result = new List < FindReferenceResult > ( ) ;
77+ var findReferenceSyntaxWalker = new FindReferenceSyntaxWalker ( ) ;
78+ TraverseThroughAllTheProjectFiles ( parameter , findReferenceProgressListener , ( documentName , documentRelativePath , syntaxRoot ) =>
79+ {
80+ findReferenceSyntaxWalker . DoVisit ( syntaxRoot , parameter . Text , ( foundlocation ) =>
81+ {
82+ result . Add ( new FindReferenceResult { FileName = documentName , Path = documentRelativePath , Position = foundlocation } ) ;
83+ } ) ;
84+ } ) ;
85+
86+ findReferenceProgressListener . OnFindReferenceCompleted ( result . Count ) ;
87+ return result ;
88+ }
89+
90+ private void TraverseThroughAllTheProjectFiles ( FindReferenceParameter parameter , IFindReferenceProgress findReferenceProgressListener , Action < string , string , CommonSyntaxNode > visitorAction )
5091 {
5192 findReferenceProgressListener . OnFindReferenceStarted ( ) ;
5293
5394 var projectSourceCodeDirectory = this . applicationConfigurationProvider . GetProjectSourceCodePath ( parameter . Username , parameter . Project ) ;
5495 var projectCodeDirectory = new DirectoryInfo ( projectSourceCodeDirectory ) . GetDirectories ( ) [ 0 ] ;
5596 var solutionPath = FindSolutionPath ( projectCodeDirectory , parameter . Project ) ;
56- var result = new List < FindReferenceResult > ( ) ;
5797 if ( solutionPath == null )
5898 {
5999 findReferenceProgressListener . OnFindReferenceCompleted ( 0 ) ;
60- return result ;
100+ return ;
61101 }
62102
63103 findReferenceProgressListener . OnFindReferenceInProgress ( ) ;
64104
65- var workspace = Roslyn . Services . Workspace . LoadSolution ( solutionPath ) ;
105+ var workspace = Roslyn . Services . Workspace . LoadSolution ( solutionPath ) ;
66106 var currentFilePath = Path . Combine ( projectCodeDirectory . FullName , parameter . Path . Replace ( @"/" , @"\" ) ) ;
67107 var solution = workspace . CurrentSolution ;
68108
@@ -83,11 +123,8 @@ public List<FindReferenceResult> FindRefernces(FindReferenceParameter parameter,
83123 CommonSyntaxNode syntaxRoot = null ;
84124 if ( documentSemanticModel . SyntaxTree . TryGetRoot ( out syntaxRoot ) )
85125 {
86- findReferenceSyntaxtWalker . DoVisit ( syntaxRoot , parameter . Text , ( foundLocation ) =>
87- {
88- var documentRelativePath = new Uri ( projectCodeDirectory . FullName + Path . DirectorySeparatorChar ) . MakeRelativeUri ( new Uri ( document . FilePath ) ) . ToString ( ) ;
89- result . Add ( new FindReferenceResult { FileName = document . Name , Path = documentRelativePath , Position = foundLocation } ) ;
90- } ) ;
126+ var documentRelativePath = new Uri ( projectCodeDirectory . FullName + Path . DirectorySeparatorChar ) . MakeRelativeUri ( new Uri ( document . FilePath ) ) . ToString ( ) ;
127+ visitorAction ( document . Name , documentRelativePath , syntaxRoot ) ;
91128 }
92129 }
93130 }
@@ -96,9 +133,6 @@ public List<FindReferenceResult> FindRefernces(FindReferenceParameter parameter,
96133 this . logger . Error ( ex , "An error has occured while loading the project {0}" , project . Name ) ;
97134 }
98135 }
99-
100- findReferenceProgressListener . OnFindReferenceCompleted ( result . Count ) ;
101- return result ;
102136 }
103137
104138 private string FindSolutionPath ( DirectoryInfo projectDirectory , string project )
@@ -118,5 +152,6 @@ private string FindSolutionPath(DirectoryInfo projectDirectory, string project)
118152
119153 return null ;
120154 }
155+
121156 }
122157}
0 commit comments