Beispiel #1
0
        public string GetFileSelectionUrl(string filePath, int lineStart, int lineEnd, int charStart, int charEnd)
        {
            if (_providers.Count == 0)
            {
                return(null);
            }

            string repositoryPath = GitHelpers.GetRepositoryPath(filePath);
            var    trackingInfo   = GitHelpers.GetTrackingInfoForHead(repositoryPath);

            if (trackingInfo == null)
            {
                Debug.Fail("Should not be executing if there is no valid provider");
                return(null);
            }

            var version   = new VersionInformation(trackingInfo.BranchName, trackingInfo.CommitId);
            var selection = new SelectionInformation(lineStart, lineEnd, charStart, charEnd);

            return(GetFileUrlFromProvider(repositoryPath, trackingInfo.RemoteUrl, filePath, version, selection));
        }
Beispiel #2
0
        public string CreateFileUrl(string repositoryUrl, string relativePath, VersionInformation version, SelectionInformation selection)
        {
            var sb = new UriBuilder(repositoryUrl);

            var query = new Dictionary <string, string>
            {
                ["path"] = $"/{PathHelpers.ToUnixPath(relativePath)}",
            };

            // Version
            if (version != null)
            {
                if (version.BranchName != null)
                {
                    query["version"] = $"GB{version.BranchName}";
                }
                else if (version.CommitId != null)
                {
                    query["version"] = $"GC{version.CommitId}";
                }
            }

            // Selection
            if (selection != null)
            {
                bool isSpanSelection = !(selection.StartLineNumber == selection.EndLineNumber &&
                                         selection.StartCharacterNumber == selection.EndCharacterNumber);

                query["lineStyle"] = "plain";
                query["line"]      = selection.StartLineNumber.ToString();

                // If the user has no selection (just a caret location) then we should only
                // select the entire line and not a span.
                if (isSpanSelection)
                {
                    query["lineEnd"]         = selection.EndLineNumber.ToString();
                    query["lineStartColumn"] = selection.StartCharacterNumber.ToString();
                    query["lineEndColumn"]   = selection.EndCharacterNumber.ToString();
                }
            }

            sb.Query = UriHelper.ToQueryString(query);

            return(sb.ToString());
        }
Beispiel #3
0
        public string CreateFileUrl(string repositoryUrl, string relativePath, VersionInformation version, SelectionInformation selection)
        {
            var sb = new UriBuilder(repositoryUrl);

            string versionString;

            if (version?.BranchName != null)
            {
                versionString = $"blob/{version.BranchName}";
            }
            else if (version?.CommitId != null)
            {
                versionString = $"blob/{version.CommitId}";
            }
            else
            {
                versionString = "blob/master";
            }

            UriHelper.AppendPath(sb, versionString);
            UriHelper.AppendPath(sb, PathHelpers.ToUnixPath(relativePath));

            if (selection != null)
            {
                bool isSpanSelection = selection.StartLineNumber != selection.EndLineNumber;

                // GitHub only supports linking to a single lines or line ranges; it doesn't
                // support column or character selections.
                if (isSpanSelection)
                {
                    sb.Fragment = $"L{selection.StartLineNumber}-L{selection.EndLineNumber}";
                }
                else
                {
                    sb.Fragment = $"L{selection.StartLineNumber}";
                }
            }

            return(sb.ToString());
        }
Beispiel #4
0
        private string GetFileUrlFromProvider(string repositoryPath, string remoteUrl, string filePath, VersionInformation version, SelectionInformation selection)
        {
            string relativePath = PathHelpers.GetRelativePath(repositoryPath, filePath);

            foreach (IWebProvider webProvider in _providers)
            {
                if (webProvider.CanHandle(remoteUrl))
                {
                    string url = webProvider.CreateFileUrl(remoteUrl, relativePath, version, selection);
                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        return(url);
                    }
                }
            }

            return(null);
        }