Exemple #1
0
 public static WaitIndicatorResult Wait(
     this IWaitIndicator waitIndicator, string title, string message, bool allowCancel, Action <IWaitContext> action)
 {
     return(waitIndicator.Wait(title, message, allowCancel, showProgress: false, action: action));
 }
 public static WaitIndicatorResult Wait(
     this IWaitIndicator waitIndicator,
     string titleAndMessage,
     bool allowCancel,
     Action <IWaitContext> action
     ) => waitIndicator.Wait(titleAndMessage, titleAndMessage, allowCancel, action);
        public void ExecuteCommand(AutomaticLineEnderCommandArgs args, Action nextHandler)
        {
            // get editor operation
            var operations = _editorOperationsFactoryService.GetEditorOperations(args.TextView);

            if (operations == null)
            {
                nextHandler();
                return;
            }

            var document = args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document == null)
            {
                NextAction(operations, nextHandler);
                return;
            }

            // feature off
            if (!document.Project.Solution.Workspace.Options.GetOption(InternalFeatureOnOffOptions.AutomaticLineEnder))
            {
                NextAction(operations, nextHandler);
                return;
            }

            _waitIndicator.Wait(
                title: EditorFeaturesResources.AutomaticLineEnder,
                message: EditorFeaturesResources.AutomaticallyCompleting,
                allowCancel: false, action: w =>
            {
                // caret is not on the subject buffer. nothing we can do
                var position = args.TextView.GetCaretPoint(args.SubjectBuffer);
                if (!position.HasValue)
                {
                    NextAction(operations, nextHandler);
                    return;
                }

                var subjectLineWhereCaretIsOn = position.Value.GetContainingLine();
                var insertionPoint            = GetInsertionPoint(document, subjectLineWhereCaretIsOn, w.CancellationToken);
                if (!insertionPoint.HasValue)
                {
                    NextAction(operations, nextHandler);
                    return;
                }

                // special cases where we treat this command simply as Return.
                if (TreatAsReturn(document, position.Value.Position, w.CancellationToken))
                {
                    // leave it to the VS editor to handle this command.
                    // VS editor's default implementation of SmartBreakLine is simply BreakLine, which inserts
                    // a new line and positions the caret with smart indent.
                    nextHandler();
                    return;
                }

                using (var transaction = args.TextView.CreateEditTransaction(EditorFeaturesResources.AutomaticLineEnder, _undoRegistry, _editorOperationsFactoryService))
                {
                    // try to move the caret to the end of the line on which the caret is
                    args.TextView.TryMoveCaretToAndEnsureVisible(subjectLineWhereCaretIsOn.End);

                    // okay, now insert ending if we need to
                    var newDocument = InsertEndingIfRequired(document, insertionPoint.Value, position.Value, w.CancellationToken);

                    // format the document and apply the changes to the workspace
                    FormatAndApply(newDocument, insertionPoint.Value, w.CancellationToken);

                    // now, insert new line
                    NextAction(operations, nextHandler);

                    transaction.Complete();
                }
            });
        }
Exemple #4
0
        public void ExecuteCommand(SyncClassViewCommandArgs args, Action nextHandler)
        {
            this.AssertIsForeground();

            var caretPosition = args.TextView.GetCaretPoint(args.SubjectBuffer) ?? -1;

            if (caretPosition < 0)
            {
                nextHandler();
                return;
            }

            var snapshot = args.SubjectBuffer.CurrentSnapshot;

            _waitIndicator.Wait(
                title: string.Format(ServicesVSResources.Synchronize_0, ClassView),
                message: string.Format(ServicesVSResources.Synchronizing_with_0, ClassView),
                allowCancel: true,
                action: context =>
            {
                var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
                if (document == null)
                {
                    return;
                }

                var syntaxFactsService = document.Project.LanguageServices.GetService <ISyntaxFactsService>();
                if (syntaxFactsService == null)
                {
                    return;
                }

                var libraryService = document.Project.LanguageServices.GetService <ILibraryService>();
                if (libraryService == null)
                {
                    return;
                }

                var semanticModel = document
                                    .GetSemanticModelAsync(context.CancellationToken)
                                    .WaitAndGetResult(context.CancellationToken);

                var root = semanticModel.SyntaxTree
                           .GetRootAsync(context.CancellationToken)
                           .WaitAndGetResult(context.CancellationToken);

                var memberDeclaration = syntaxFactsService.GetContainingMemberDeclaration(root, caretPosition);

                var symbol = memberDeclaration != null
                        ? semanticModel.GetDeclaredSymbol(memberDeclaration, context.CancellationToken)
                        : null;

                while (symbol != null && !IsValidSymbolToSynchronize(symbol))
                {
                    symbol = symbol.ContainingSymbol;
                }

                IVsNavInfo navInfo = null;
                if (symbol != null)
                {
                    navInfo = libraryService.NavInfoFactory.CreateForSymbol(symbol, document.Project, semanticModel.Compilation, useExpandedHierarchy: true);
                }

                if (navInfo == null)
                {
                    navInfo = libraryService.NavInfoFactory.CreateForProject(document.Project);
                }

                if (navInfo == null)
                {
                    return;
                }

                var navigationTool = _serviceProvider.GetService <SVsClassView, IVsNavigationTool>();
                navigationTool.NavigateToNavInfo(navInfo);
            });
        }
Exemple #5
0
            public int GetNameOfLocation(
                IVsTextBuffer pBuffer,
                int iLine,
                int iCol,
                out string?pbstrName,
                out int piLineOffset
                )
            {
                using (
                    Logger.LogBlock(
                        FunctionId.Debugging_VsLanguageDebugInfo_GetNameOfLocation,
                        CancellationToken.None
                        )
                    )
                {
                    string?name       = null;
                    var    lineOffset = 0;

                    if (_languageDebugInfo != null)
                    {
                        _waitIndicator.Wait(
                            title: ServicesVSResources.Debugger,
                            message: ServicesVSResources.Determining_breakpoint_location,
                            allowCancel: true,
                            action: waitContext =>
                        {
                            var cancellationToken = waitContext.CancellationToken;
                            var textBuffer        =
                                _languageService.EditorAdaptersFactoryService.GetDataBuffer(
                                    pBuffer
                                    );
                            if (textBuffer != null)
                            {
                                var nullablePoint = textBuffer.CurrentSnapshot.TryGetPoint(
                                    iLine,
                                    iCol
                                    );
                                if (nullablePoint.HasValue)
                                {
                                    var point    = nullablePoint.Value;
                                    var document =
                                        point.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

                                    if (document != null)
                                    {
                                        // NOTE(cyrusn): We have to wait here because the debuggers'
                                        // GetNameOfLocation is a blocking call.  In the future, it
                                        // would be nice if they could make it async.
                                        var debugLocationInfo = _languageDebugInfo
                                                                .GetLocationInfoAsync(
                                            document,
                                            point,
                                            cancellationToken
                                            )
                                                                .WaitAndGetResult(cancellationToken);

                                        if (!debugLocationInfo.IsDefault)
                                        {
                                            name       = debugLocationInfo.Name;
                                            lineOffset = debugLocationInfo.LineOffset;
                                        }
                                    }
                                }
                            }
                        }
                            );

                        if (name != null)
                        {
                            pbstrName    = name;
                            piLineOffset = lineOffset;
                            return(VSConstants.S_OK);
                        }
                    }

                    // Note(DustinCa): Docs say that GetNameOfLocation should return S_FALSE if a name could not be found.
                    // Also, that's what the old native code does, so we should do it here.
                    pbstrName    = null;
                    piLineOffset = 0;
                    return(VSConstants.S_FALSE);
                }
            }
Exemple #6
0
        public void AugmentPeekSession(IPeekSession session, IList <IPeekableItem> peekableItems)
        {
            if (!string.Equals(session.RelationshipName, PredefinedPeekRelationships.Definitions.Name, StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            var triggerPoint = session.GetTriggerPoint(_textBuffer.CurrentSnapshot);

            if (!triggerPoint.HasValue)
            {
                return;
            }

            var document = triggerPoint.Value.Snapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document == null)
            {
                return;
            }

            _waitIndicator.Wait(EditorFeaturesResources.Peek, EditorFeaturesResources.Loading_Peek_information, allowCancel: true, action: context =>
            {
                var cancellationToken = context.CancellationToken;

                IEnumerable <IPeekableItem> results;

                if (!document.SupportsSemanticModel)
                {
                    // For documents without semantic models, just try to use the goto-def service
                    // as a reasonable place to peek at.
                    var goToDefinitionService = document.GetLanguageService <IGoToDefinitionService>();
                    if (goToDefinitionService == null)
                    {
                        return;
                    }

                    var navigableItems = goToDefinitionService.FindDefinitionsAsync(document, triggerPoint.Value.Position, cancellationToken)
                                         .WaitAndGetResult(cancellationToken);

                    results = GetPeekableItemsForNavigableItems(navigableItems, document.Project, _peekResultFactory, cancellationToken);
                }
                else
                {
                    var semanticModel = document.GetSemanticModelAsync(cancellationToken).WaitAndGetResult(cancellationToken);
                    var symbol        = SymbolFinder.GetSemanticInfoAtPositionAsync(
                        semanticModel,
                        triggerPoint.Value.Position,
                        document.Project.Solution.Workspace,
                        cancellationToken).WaitAndGetResult(cancellationToken)
                                        .GetAnySymbol(includeType: true);

                    if (symbol == null)
                    {
                        return;
                    }

                    symbol = symbol.GetOriginalUnreducedDefinition();

                    // Get the symbol back from the originating workspace
                    var symbolMappingService = document.Project.Solution.Workspace.Services.GetRequiredService <ISymbolMappingService>();

                    var mappingResult = symbolMappingService.MapSymbolAsync(document, symbol, cancellationToken)
                                        .WaitAndGetResult(cancellationToken);

                    mappingResult ??= new SymbolMappingResult(document.Project, symbol);

                    results = _peekableItemFactory.GetPeekableItemsAsync(mappingResult.Symbol, mappingResult.Project, _peekResultFactory, cancellationToken)
                              .WaitAndGetResult(cancellationToken);
                }

                peekableItems.AddRange(results);
            });
        }