/// <summary>
        /// Returns the text changes necessary to format the document after the user enters a
        /// character.  The position provided is the position of the caret in the document after
        /// the character been inserted into the document.
        /// </summary>
        public static async Task <ImmutableArray <TextChange> > GetFormattingChangesAsync(
            Document document,
            char typedChar,
            int position,
            RazorIndentationOptions indentationOptions,
            RazorAutoFormattingOptions autoFormattingOptions,
            FormattingOptions.IndentStyle indentStyle,
            CancellationToken cancellationToken)
        {
            Contract.ThrowIfFalse(document.Project.Language is LanguageNames.CSharp);
            var formattingService = document.GetRequiredLanguageService <ISyntaxFormattingService>();
            var documentSyntax    = await ParsedDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);

            if (!formattingService.ShouldFormatOnTypedCharacter(documentSyntax, typedChar, position, cancellationToken))
            {
                return(ImmutableArray <TextChange> .Empty);
            }

            var formattingOptions        = GetFormattingOptions(indentationOptions);
            var roslynIndentationOptions = new IndentationOptions(formattingOptions)
            {
                AutoFormattingOptions = autoFormattingOptions.UnderlyingObject,
                IndentStyle           = (FormattingOptions2.IndentStyle)indentStyle
            };

            return(formattingService.GetFormattingChangesOnTypedCharacter(documentSyntax, position, roslynIndentationOptions, cancellationToken));
        }
Esempio n. 2
0
        private Indenter GetIndenter(
            Document document,
            int lineNumber,
            FormattingOptions.IndentStyle indentStyle,
            CancellationToken cancellationToken
            )
        {
            var documentOptions = document
                                  .GetOptionsAsync(cancellationToken)
                                  .WaitAndGetResult_CanCallOnBackground(cancellationToken);
            var syntacticDoc = SyntacticDocument
                               .CreateAsync(document, cancellationToken)
                               .WaitAndGetResult_CanCallOnBackground(cancellationToken);

            var sourceText       = syntacticDoc.Root.SyntaxTree.GetText(cancellationToken);
            var lineToBeIndented = sourceText.Lines[lineNumber];

            var formattingRules = GetFormattingRules(document, lineToBeIndented.Start, indentStyle);

            return(new Indenter(
                       this,
                       syntacticDoc,
                       formattingRules,
                       documentOptions,
                       lineToBeIndented,
                       cancellationToken
                       ));
        }
Esempio n. 3
0
        public IndentationResult GetIndentation(
            Document document,
            int lineNumber,
            FormattingOptions.IndentStyle indentStyle,
            CancellationToken cancellationToken
            )
        {
            var indenter = GetIndenter(document, lineNumber, indentStyle, cancellationToken);

            if (indentStyle == FormattingOptions.IndentStyle.None)
            {
                // If there is no indent style, then do nothing.
                return(new IndentationResult(basePosition: 0, offset: 0));
            }

            if (
                indentStyle == FormattingOptions.IndentStyle.Smart &&
                indenter.TryGetSmartTokenIndentation(out var indentationResult)
                )
            {
                return(indentationResult);
            }

            // If the indenter can't produce a valid result, just default to 0 as our indentation.
            return(indenter.GetDesiredIndentation(indentStyle) ?? default);
        }
            public IndentationResult?GetDesiredIndentation(
                FormattingOptions.IndentStyle indentStyle
                )
            {
                // If the caller wants no indent, then we'll return an effective '0' indent.
                if (indentStyle == FormattingOptions.IndentStyle.None)
                {
                    return(null);
                }

                // If the user has explicitly set 'block' indentation, or they're in an inactive preprocessor region,
                // then just do simple block indentation.
                if (
                    indentStyle == FormattingOptions.IndentStyle.Block ||
                    _syntaxFacts.IsInInactiveRegion(
                        Document.SyntaxTree,
                        LineToBeIndented.Start,
                        this.CancellationToken
                        )
                    )
                {
                    return(GetDesiredBlockIndentation());
                }

                Debug.Assert(indentStyle == FormattingOptions.IndentStyle.Smart);
                return(GetDesiredSmartIndentation());
            }
Esempio n. 5
0
 public SimpleStringSplitter(
     Document document, int position,
     SyntaxNode root, SourceText sourceText, SyntaxToken token,
     bool useTabs, int tabSize, FormattingOptions.IndentStyle indentStyle, CancellationToken cancellationToken)
     : base(document, position, root, sourceText, useTabs, tabSize, indentStyle, cancellationToken)
 {
     _token = token;
 }
Esempio n. 6
0
        private IEnumerable <AbstractFormattingRule> GetFormattingRules(
            Document document,
            int position,
            FormattingOptions.IndentStyle indentStyle
            )
        {
            var workspace             = document.Project.Solution.Workspace;
            var formattingRuleFactory =
                workspace.Services.GetRequiredService <IHostDependentFormattingRuleFactoryService>();
            var baseIndentationRule = formattingRuleFactory.CreateRule(document, position);

            var formattingRules = new[]
            {
                baseIndentationRule,
                this.GetSpecializedIndentationFormattingRule(indentStyle)
            }.Concat(Formatter.GetDefaultFormattingRules(document));

            return(formattingRules);
        }
Esempio n. 7
0
 protected override AbstractFormattingRule GetSpecializedIndentationFormattingRule(FormattingOptions.IndentStyle indentStyle)
 {
     return(s_instance);
 }
Esempio n. 8
0
 private BraceCompletionFormattingRule(FormattingOptions.IndentStyle indentStyle)
 {
     _indentStyle = indentStyle;
 }
 protected abstract AbstractFormattingRule GetSpecializedIndentationFormattingRule(FormattingOptions.IndentStyle indentStyle);
Esempio n. 10
0
 private BraceCompletionFormattingRule(FormattingOptions.IndentStyle indentStyle, CSharpSyntaxFormattingOptions options)
 {
     _indentStyle = indentStyle;
     _options     = options;
 }
Esempio n. 11
0
 public BraceCompletionFormattingRule(FormattingOptions.IndentStyle indentStyle)
     : this(indentStyle, CSharpSyntaxFormattingOptions.Default)
 {
 }
Esempio n. 12
0
 public IndentationResult GetIndentation(Document document, int lineNumber, FormattingOptions.IndentStyle indentStyle, CancellationToken cancellationToken)
 {
     // all F# documents should have a file path
     if (document.FilePath == null)
     {
         return(default);
 IndentationResult IIndentationService.GetBlankLineIndentation(
     Document document, int lineNumber, FormattingOptions.IndentStyle indentStyle, CancellationToken cancellationToken)
 {
     return(default);