Skip to content

Commit 3438d16

Browse files
committed
Go to definition support
- Support for Go to definition instead of find all references on click - Bundling all the javascripts into one - Support for application loader
1 parent f3a20c2 commit 3438d16

File tree

14 files changed

+206
-87
lines changed

14 files changed

+206
-87
lines changed

SourceCodeReader.Web/App_Start/BundleConfig.cs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,30 @@ public static void RegisterBundles(BundleCollection bundles)
1313

1414
private static void RegisterScriptBundles(BundleCollection bundles)
1515
{
16-
bundles.Add(new ScriptBundle("~/bundles/jquery")
17-
.Include("~/Scripts/jquery-1.*"));
18-
19-
bundles.Add(new ScriptBundle("~/bundles/jqueryval")
20-
.Include("~/Scripts/jquery.unobtrusive*",
21-
"~/Scripts/jquery.validate*"));
22-
23-
bundles.Add(new ScriptBundle("~/bundles/jquerycookie")
24-
.Include("~/Scripts/jquery.cookie.js"));
25-
26-
bundles.Add(new ScriptBundle("~/bundles/bootstrap")
27-
.Include("~/Scripts/bootstrap*"));
16+
bundles.Add(new ScriptBundle("~/bundles/all")
17+
.Include("~/Scripts/jquery-1.7.2.js",
18+
"~/Scripts/jquery.cookie.js",
19+
"~/Scripts/bootstrap.js",
20+
"~/Scripts/knockout-2.1.0.js",
21+
"~/Scripts/sammy-latest.min.js",
22+
"~/Scripts/jsuri-1.1.1.min.js",
23+
"~/Scripts/jquery.signalR-0.5.1.js"));
2824

2925
bundles.Add(new ScriptBundle("~/bundles/modernizr")
3026
.Include("~/Scripts/modernizr-*"));
3127

32-
bundles.Add(new ScriptBundle("~/bundles/spa")
33-
.Include("~/Scripts/knockout-2.*",
34-
"~/Scripts/sammy-latest.min.js"));
35-
3628
bundles.Add(new ScriptBundle("~/bundles/applicationjs")
37-
.Include("~/Scripts/jsuri-1.1.1.min.js",
38-
"~/Scripts/App/Application.js"));
29+
.Include("~/Scripts/App/Application.js"));
3930

40-
bundles.Add(new ScriptBundle("~/bundles/signalr")
41-
.Include("~/Scripts/jquery.signalR-0.5*"));
31+
4232
}
4333

4434
private static void RegisterStyleBundles(BundleCollection bundles)
4535
{
46-
bundles.Add(new StyleBundle("~/Content/bootrap")
47-
.Include("~/Content/bootstrap.css",
48-
"~/Content/bootstrap-responsive.css"));
49-
50-
bundles.Add(new StyleBundle("~/Content/css")
51-
.Include("~/Content/site.css"));
36+
bundles.Add(new StyleBundle("~/content/all")
37+
.Include("~/Content/bootstrap.css",
38+
"~/Content/bootstrap-responsive.css",
39+
"~/Content/site.css"));
5240
}
5341
}
5442
}

SourceCodeReader.Web/Content/Site.css

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
@import url(http://fonts.googleapis.com/css?family=Merienda+One&text=Source%20Code%20Reader);
2-
3-
1+

42
body {
53
font-size: .85em;
64
font-family: "Segoe UI", Verdana, Helvetica, Sans-Serif;
@@ -91,6 +89,7 @@ body {
9189
background: url('/images/loader.gif') no-repeat center center;
9290
}
9391

92+
9493
.stylishscrollbar::-webkit-scrollbar{
9594
width:10px;
9695
height:10px;

SourceCodeReader.Web/LanguageServices/DefaultFindReferenceProgressListener.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public void OnFindReferenceInProgress()
2222
this.Caller.findReferenceStatus("Searching in progress...");
2323
}
2424

25+
public void OnFindReferenceCompleted()
26+
{
27+
this.Caller.findReferenceStatus("Search completed.");
28+
}
29+
2530
public void OnFindReferenceCompleted(int searchResultCount)
2631
{
2732
this.Caller.findReferenceStatus(string.Format("Searching completed with {0} results", searchResultCount));

SourceCodeReader.Web/LanguageServices/DotNet/DotNetCodeEditorService.cs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Roslyn.Compilers.Common;
1010
using Ninject.Extensions.Logging;
1111
using System.Threading.Tasks;
12+
using System.Linq.Expressions;
1213

1314
namespace 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
}

SourceCodeReader.Web/LanguageServices/DotNet/DotNetSyntaxNavigationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public string GetCodeAsNavigatableHtml(string sourceCode, IDotNetSourceCodeNavig
1717
{
1818
case TokenKind.ObjectCreation:
1919
case TokenKind.MethodCall:
20-
htmlBuilder.Append(string.Format(@"<a href=""javascript:$.findReferences('{0}', '{1}', {2})"">{1}</a>", tk, text, start.GetValueOrDefault()));
20+
htmlBuilder.Append(string.Format(@"<a href=""javascript:$.goToDefinition('{0}', '{1}', {2})"">{1}</a>", tk, text, start.GetValueOrDefault()));
2121
break;
2222
default:
2323
htmlBuilder.Append(System.Web.HttpUtility.HtmlEncode(text));
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using Roslyn.Compilers.Common;
6+
7+
namespace SourceCodeReader.Web.LanguageServices.DotNet
8+
{
9+
internal class GoToDefinitionSyntaxWalker : CommonSyntaxWalker
10+
{
11+
private Action<int> symbolFoundDelegate;
12+
private string textToSearch;
13+
private TokenKind searchTokenKind;
14+
15+
internal void DoVisit(CommonSyntaxNode token, string textToSearch, TokenKind searchTokenKind, Action<int> symbolFoundDelegate)
16+
{
17+
this.symbolFoundDelegate = symbolFoundDelegate;
18+
this.textToSearch = textToSearch;
19+
this.searchTokenKind = searchTokenKind;
20+
Visit(token);
21+
}
22+
23+
protected override void VisitToken(CommonSyntaxToken token)
24+
{
25+
if (token.GetText() == textToSearch)
26+
{
27+
if ((searchTokenKind == TokenKind.MethodCall && token.Parent.Kind == (int)Roslyn.Compilers.CSharp.SyntaxKind.MethodDeclaration) ||
28+
(searchTokenKind == TokenKind.ObjectCreation && token.Parent.Kind == (int)Roslyn.Compilers.CSharp.SyntaxKind.ClassDeclaration))
29+
{
30+
symbolFoundDelegate(token.SyntaxTree.GetLineSpan(token.Span, false).StartLinePosition.Line);
31+
}
32+
}
33+
34+
base.VisitToken(token);
35+
}
36+
}
37+
}

SourceCodeReader.Web/LanguageServices/IEditorService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ public interface IEditorService
1111
string BuildNavigatableSourceCodeFromFile(string filename);
1212

1313
List<FindReferenceResult> FindRefernces(FindReferenceParameter parameter, IFindReferenceProgress findReferenceProgressListener);
14+
15+
FindReferenceResult GoToDefinition(FindReferenceParameter parameter, IFindReferenceProgress findReferenceProgressListener);
1416
}
1517
}

SourceCodeReader.Web/LanguageServices/IFindReferenceProgress.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public interface IFindReferenceProgress
1212

1313
void OnFindReferenceInProgress();
1414

15+
void OnFindReferenceCompleted();
16+
1517
void OnFindReferenceCompleted(int searchResultCount);
1618
}
1719
}

SourceCodeReader.Web/Models/FindReferenceParameter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ public class FindReferenceParameter
1616
public int Position { get; set; }
1717

1818
public string Text { get; set; }
19+
20+
public string Kind { get; set; }
1921
}
2022
}

SourceCodeReader.Web/Scripts/App/Application.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ function appViewModel() {
201201
project: project.name(),
202202
path: currentFilePath,
203203
text: text,
204-
position: position
204+
position: position,
205+
kind: kind
205206
},
206207
function (result) {
207208
self.findResult({ items: result });
@@ -210,6 +211,28 @@ function appViewModel() {
210211
);
211212
};
212213

214+
self.goToDefinition = function (kind, text, position) {
215+
var project = self.project();
216+
var currentFilePath = project.file().path;
217+
var goToDefinitionUrl = '/api/solution/gotodefinition';
218+
219+
$.post(goToDefinitionUrl,
220+
{
221+
username: project.username(),
222+
project: project.name(),
223+
path: currentFilePath,
224+
text: text,
225+
position: position,
226+
kind: kind
227+
},
228+
function (result) {
229+
if (result) {
230+
self.openFile(result);
231+
}
232+
}
233+
);
234+
};
235+
213236
self.openFile = function (findResult) {
214237
if (findResult) {
215238
location.hash = '#/open/' + self.project().username() + '/' + self.project().name() + '/' + findResult.Path + '?line=' + findResult.Position;
@@ -251,6 +274,12 @@ function appViewModel() {
251274

252275
$(function () {
253276

277+
// This is for getting rid of the UI flickering
278+
setTimeout(function () {
279+
$('#apploader').toggleClass('hide');
280+
$('#main').toggleClass('hide');
281+
}, 500);
282+
254283
ko.bindingHandlers.scrollTo = {
255284
update: function (element, valueAccessor, allBindingsAccessor) {
256285
var currentElement = $(element);
@@ -259,7 +288,6 @@ $(function () {
259288
}
260289
};
261290

262-
263291
var application = new appViewModel();
264292
ko.applyBindings(application);
265293

@@ -268,4 +296,10 @@ $(function () {
268296
application.findReferences(kind, text, position);
269297
}
270298
};
299+
300+
$.goToDefinition = function (kind, text, position) {
301+
if (application) {
302+
application.goToDefinition(kind, text, position);
303+
}
304+
};
271305
});

0 commit comments

Comments
 (0)