Beispiel #1
0
        async void CompletionWindowManager_WindowClosed(object sender, EventArgs e)
        {
            var document = DocumentContext.AnalysisDocument;

            if (document == null)
            {
                return;
            }
            var caretPosition = Editor.CaretOffset;
            var token         = await CSharpEditorFormattingService.GetTokenBeforeTheCaretAsync(document, caretPosition, default(CancellationToken)).ConfigureAwait(false);

            if (token.IsMissing || !token.Parent.IsKind(SyntaxKind.ElseDirectiveTrivia))
            {
                return;
            }
            var tokenRange = Microsoft.CodeAnalysis.CSharp.Utilities.FormattingRangeHelper.FindAppropriateRange(token);

            if (tokenRange == null || !tokenRange.HasValue || tokenRange.Value.Item1.Equals(tokenRange.Value.Item2))
            {
                return;
            }

            var value = tokenRange.Value;

            using (var undo = Editor.OpenUndoGroup()) {
                OnTheFlyFormatter.Format(Editor, DocumentContext, value.Item1.SpanStart, value.Item2.Span.End, optionSet: optionSet);
            }
        }
Beispiel #2
0
        void HandleOnTheFlyFormatting(KeyDescriptor descriptor)
        {
            if (descriptor.KeyChar == '{')
            {
                return;
            }
            SafeUpdateIndentEngine(Editor.CaretOffset);
            bool skipFormatting = stateTracker.IsInsideOrdinaryCommentOrString;

            if (!skipFormatting && !(stateTracker.IsInsideComment || stateTracker.IsInsideString))
            {
                if (DocumentContext.ParsedDocument == null || DocumentContext.ParsedDocument.GetAst <SemanticModel> () == null)
                {
                    return;
                }
                var document = DocumentContext.AnalysisDocument;
                if (document == null)
                {
                    return;
                }
                if (!skipFormatting && service.SupportsFormattingOnTypedCharacter(document, descriptor.KeyChar))
                {
                    var caretPosition = Editor.CaretOffset;
                    var token         = CSharpEditorFormattingService.GetTokenBeforeTheCaretAsync(document, caretPosition, default(CancellationToken)).Result;
                    if (token.IsMissing || !service.ValidSingleOrMultiCharactersTokenKind(descriptor.KeyChar, token.Kind()) || token.IsKind(SyntaxKind.EndOfFileToken) || token.IsKind(SyntaxKind.None))
                    {
                        return;
                    }
                    if (CSharpEditorFormattingService.TokenShouldNotFormatOnTypeChar(token))
                    {
                        return;
                    }
                    using (var undo = Editor.OpenUndoGroup()) {
                        if (OnTheFlyFormatting && Editor != null && Editor.EditMode == EditMode.Edit)
                        {
                            var oldVersion = Editor.Version;
                            OnTheFlyFormatter.FormatStatmentAt(Editor, DocumentContext, Editor.CaretLocation, optionSet: optionSet);
                            if (oldVersion.CompareAge(Editor.Version) != 0)
                            {
                                CompletionWindowManager.HideWindow();
                            }
                        }
                    }
                }
            }
            if (OnTheFlyFormatting && descriptor.SpecialKey == SpecialKey.Return)
            {
                try {
                    FormatOnReturn();
                } catch (Exception e) {
                    LoggingService.LogError("Exception while formatting", e);
                }
            }
        }
Beispiel #3
0
        async void FormatOnReturn(CancellationToken cancellationToken = default(CancellationToken))
        {
            var document = DocumentContext.AnalysisDocument;

            if (document == null)
            {
                return;
            }
            var caretPosition = Editor.CaretOffset;
            var token         = await CSharpEditorFormattingService.GetTokenBeforeTheCaretAsync(document, caretPosition, cancellationToken).ConfigureAwait(false);

            if (token.IsMissing)
            {
                return;
            }

            string text = null;

            if (service.IsInvalidToken(token, ref text))
            {
                return;
            }
            // Check to see if the token is ')' and also the parent is a using statement. If not, bail
            if (CSharpEditorFormattingService.TokenShouldNotFormatOnReturn(token))
            {
                return;
            }
            var tokenRange = Microsoft.CodeAnalysis.CSharp.Utilities.FormattingRangeHelper.FindAppropriateRange(token);

            if (tokenRange == null || !tokenRange.HasValue || tokenRange.Value.Item1.Equals(tokenRange.Value.Item2))
            {
                return;
            }
            var value = tokenRange.Value;

            using (var undo = Editor.OpenUndoGroup()) {
                OnTheFlyFormatter.Format(Editor, DocumentContext, value.Item1.SpanStart, value.Item2.Span.End, optionSet: optionSet);
            }
        }