Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/ProjectFileTools/GoToDefinition/GoToDefinitionController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using EnvDTE;
using FarTestProvider;
using Microsoft.VisualStudio;
Expand All @@ -10,7 +11,9 @@
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using ProjectFileTools.Completion;
using ProjectFileTools.MSBuild;
using ProjectFileTools.NuGetSearch.Contracts;

namespace ProjectFileTools
{
Expand Down Expand Up @@ -41,11 +44,55 @@ public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, Int
return Next?.QueryStatus(pguidCmdGroup, cCmds, prgCmds, pCmdText) ?? (int)Microsoft.VisualStudio.OLE.Interop.Constants.OLECMDERR_E_NOTSUPPORTED;
}

private static bool PackageExistsOnNuGet(string packageName, string version, out string url)
{
string packageAndVersionUrl = $"https://www.nuget.org/packages/{packageName}/{version}/";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if the logic of finding the Nuget package would reside inside the .Nuget project.

On a side note, the Definition class is intended to be an abstraction level for the set of matches. However, the implementation currently is very limited (we would need to implement what we were calling Resolvers; that would allow us to write this code in a more generic way).

For now, I think that is fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed #29 to track moving the logic

string packageUrl = $"https://www.nuget.org/packages/{packageName}/";

try
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = client.GetAsync(packageAndVersionUrl).Result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to issue a HEAD request here? You're not interested in the body after all...


if (response.StatusCode == System.Net.HttpStatusCode.OK)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of a NuGet gallery bug (NuGet/NuGetGallery#4624), this will always be true, as long as the package ID is correct.

{
url = packageAndVersionUrl;
return true;
}
else
{
response = client.GetAsync(packageAndVersionUrl).Result;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you fetching the same URL again? Isn't this supposed to be packageUrl?


if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
url = packageUrl;
return true;
}
}
}
}
catch { }

url = null;
return false;
}

public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
if (pguidCmdGroup == _vSStd97CmdIDGuid && nCmdID == (uint)VSConstants.VSStd97CmdID.GotoDefn)
{
TextView.TextBuffer.Properties.TryGetProperty<ITextDocument>(typeof(ITextDocument), out ITextDocument textDoc);

if (PackageCompletionSource.IsInRangeForPackageCompletion(TextView.TextSnapshot, TextView.Caret.Position.BufferPosition.Position, out Span targetSpan, out string packageName, out string packageVersion, out string completionType))
{
if(PackageExistsOnNuGet(packageName, packageVersion, out string url))
{
System.Diagnostics.Process.Start(url);
return VSConstants.S_OK;
}
}

IWorkspace workspace = _workspaceManager.GetWorkspace(textDoc.FilePath);
List<Definition> definitions = workspace.ResolveDefinition(textDoc.FilePath, TextView.TextSnapshot.GetText(), TextView.Caret.Position.BufferPosition.Position);

Expand Down
3 changes: 2 additions & 1 deletion src/ProjectFileTools/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"Newtonsoft.Json": "8.0.3",
"NuGet.VisualStudio": "4.3.0",
"RoslynTools.Microsoft.SignTool": "0.3.3-beta",
"System.Diagnostics.DiagnosticSource": "4.4.1"
"System.Diagnostics.DiagnosticSource": "4.4.1",
"System.Net.Http": "4.0.0"
},
"runtimes": {
"win": {}
Expand Down