Exemplo n.º 1
0
        protected async override Task CorrectIndentingImplementationAsync(Ide.Editor.TextEditor editor, DocumentContext context, int startLine, int endLine, CancellationToken cancellationToken)
        {
            if (editor.IndentationTracker == null)
            {
                return;
            }
            var startSegment = editor.GetLine(startLine);

            if (startSegment == null)
            {
                return;
            }
            var endSegment = startLine != endLine?editor.GetLine(endLine) : startSegment;

            if (endSegment == null)
            {
                return;
            }

            try {
                var document = context.AnalysisDocument;

                var formattingService = document.GetLanguageService <IEditorFormattingService> ();
                if (formattingService == null || !formattingService.SupportsFormatSelection)
                {
                    return;
                }

                var formattingRules = new List <AbstractFormattingRule> ();
                formattingRules.Add(ContainedDocumentPreserveFormattingRule.Instance);
                formattingRules.AddRange(Formatter.GetDefaultFormattingRules(document));

                var workspace = document.Project.Solution.Workspace;
                var root      = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);

                var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);

                var changes = Formatter.GetFormattedTextChanges(
                    root, new TextSpan [] { new TextSpan(startSegment.Offset, endSegment.EndOffset - startSegment.Offset) },
                    workspace, options, formattingRules, cancellationToken);

                if (changes == null)
                {
                    return;
                }
                await Runtime.RunInMainThread(delegate {
                    editor.ApplyTextChanges(changes);
                    editor.FixVirtualIndentation();
                });
            } catch (Exception e) {
                LoggingService.LogError("Error while indenting", e);
            }
        }
Exemplo n.º 2
0
        protected override async void OnTheFlyFormatImplementation(Ide.Editor.TextEditor editor, DocumentContext context, int startOffset, int length)
        {
            var doc = context.AnalysisDocument;

            var formattingService = doc.GetLanguageService <IEditorFormattingService> ();

            if (formattingService == null || !formattingService.SupportsFormatSelection)
            {
                return;
            }

            var changes = await formattingService.GetFormattingChangesAsync(doc, new TextSpan (startOffset, length), default(System.Threading.CancellationToken));

            if (changes == null)
            {
                return;
            }
            editor.ApplyTextChanges(changes);
            editor.FixVirtualIndentation();
        }