public static bool TryGoToDefinition(
            ISymbol symbol,
            Project project,
            ITypeSymbol containingTypeSymbol,
            bool throwOnHiddenDefinition,
            CancellationToken cancellationToken)
        {
            var alias = symbol as IAliasSymbol;

            if (alias != null)
            {
                var ns = alias.Target as INamespaceSymbol;
                if (ns != null && ns.IsGlobalNamespace)
                {
                    return(false);
                }
            }

            // VB global import aliases have a synthesized SyntaxTree.
            // We can't go to the definition of the alias, so use the target type.

            var solution = project.Solution;

            if (symbol is IAliasSymbol &&
                GeneratedCodeRecognitionService.GetPreferredSourceLocations(solution, symbol).All(l => project.Solution.GetDocument(l.SourceTree) == null))
            {
                symbol = ((IAliasSymbol)symbol).Target;
            }

            var definition = SymbolFinder.FindSourceDefinitionAsync(symbol, solution, cancellationToken).Result;

            cancellationToken.ThrowIfCancellationRequested();

            symbol = definition ?? symbol;

            if (TryThirdPartyNavigation(symbol, solution, containingTypeSymbol))
            {
                return(true);
            }

            // If it is a partial method declaration with no body, choose to go to the implementation
            // that has a method body.
            if (symbol is IMethodSymbol)
            {
                symbol = ((IMethodSymbol)symbol).PartialImplementationPart ?? symbol;
            }

            var preferredSourceLocations = GeneratedCodeRecognitionService.GetPreferredSourceLocations(solution, symbol).ToArray();

            if (GoToDefinitionService.TryNavigateToSymbol(symbol, project, true))
            {
                return(true);
            }
            else if (!preferredSourceLocations.Any())
            {
                return(false);
            }

            // If we have a single location, then just navigate to it.
            if (preferredSourceLocations.Length == 1)
            {
                var firstItem = preferredSourceLocations[0];
                var workspace = project.Solution.Workspace;
                if (GoToDefinitionService.CanNavigateToSpan(workspace, solution.GetDocument(firstItem.SourceTree).Id, firstItem.SourceSpan))
                {
                    return(GoToDefinitionService.TryNavigateToSpan(workspace, solution.GetDocument(firstItem.SourceTree).Id, firstItem.SourceSpan, true));
                }
                else
                {
                    if (throwOnHiddenDefinition)
                    {
                        const int E_FAIL = -2147467259;
                        throw new COMException("The definition of the object is hidden.", E_FAIL);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            else
            {
                // We have multiple viable source locations, so ask the host what to do. Most hosts
                // will simply display the results to the user and allow them to choose where to
                // go.
                GoToDefinitionService.DisplayMultiple(preferredSourceLocations.Select(location => Tuple.Create(solution, symbol, location)).ToList());

                return(false);
            }
        }