internal bool TryExecuteCommand(Document document, int caretPosition, IGoToDefinitionService goToDefinitionService)
        {
            string errorMessage = null;

            var result = _waitIndicator.Wait(
                title: EditorFeaturesResources.Go_to_Definition,
                message: EditorFeaturesResources.Navigating_to_definition,
                allowCancel: true,
                action: waitContext =>
            {
                if (goToDefinitionService != null &&
                    goToDefinitionService.TryGoToDefinition(document, caretPosition, waitContext.CancellationToken))
                {
                    return;
                }

                errorMessage = EditorFeaturesResources.Cannot_navigate_to_the_symbol_under_the_caret;
            });

            if (result == WaitIndicatorResult.Completed && errorMessage != null)
            {
                var workspace           = document.Project.Solution.Workspace;
                var notificationService = workspace.Services.GetService <INotificationService>();
                notificationService.SendNotification(errorMessage, title: EditorFeaturesResources.Go_to_Definition, severity: NotificationSeverity.Information);
            }

            return(true);
        }
        internal bool TryExecuteCommand(Document document, int caretPosition, IGoToDefinitionService goToDefinitionService, CommandExecutionContext context)
        {
            string errorMessage = null;

            using (context.OperationContext.AddScope(allowCancellation: true, EditorFeaturesResources.Navigating_to_definition))
            {
                if (goToDefinitionService != null &&
                    goToDefinitionService.TryGoToDefinition(document, caretPosition, context.OperationContext.UserCancellationToken))
                {
                    return(true);
                }

                errorMessage = EditorFeaturesResources.Cannot_navigate_to_the_symbol_under_the_caret;
            }

            if (errorMessage != null)
            {
                // We are about to show a modal UI dialog so we should take over the command execution
                // wait context. That means the command system won't attempt to show its own wait dialog
                // and also will take it into consideration when measuring command handling duration.
                context.OperationContext.TakeOwnership();
                var workspace           = document.Project.Solution.Workspace;
                var notificationService = workspace.Services.GetService <INotificationService>();
                notificationService.SendNotification(errorMessage, title: EditorFeaturesResources.Go_to_Definition, severity: NotificationSeverity.Information);
            }

            return(true);
        }
        /// <summary>
        /// Try to go to the definition of the symbol under the caret. This function is designed here for solutions
        /// containing only of a single code document.
        /// </summary>
        /// <param name="caretOffset">The caret offset.</param>
        /// <returns>The position of the symbol in the document where it is defined. If the symbol under the caret is not defined in the document,
        /// the return value is null.</returns>
        public int?GoToDefinition(int caretOffset)
        {
            if (null == _goToDefinitionService)
            {
                _goToDefinitionService = new CSharpGoToDefinitionService();
            }

            var document    = Workspace.CurrentSolution.GetDocument(DocumentId);
            var definitions = _goToDefinitionService.FindDefinitionsAsync(document, caretOffset, CancellationToken.None).Result;
            var location    = definitions.FirstOrDefault();

            return(location?.SourceSpan.Start);
        }
Esempio n. 4
0
 public static void ExecuteCommand(Document document, int caretPosition, IGoToDefinitionService goToDefinitionService, CommandExecutionContext context)
 => GoToDefinitionCommandHandler.ExecuteCommand(document, caretPosition, goToDefinitionService, context);