Example #1
0
            public IndentationResult?GetDesiredIndentation(FormattingOptions2.IndentStyle indentStyle)
            {
                // If the caller wants no indent, then we'll return an effective '0' indent.
                if (indentStyle == FormattingOptions2.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 == FormattingOptions2.IndentStyle.Block ||
                    _syntaxFacts.IsInInactiveRegion(this.Tree, LineToBeIndented.Start, this.CancellationToken))
                {
                    return(GetDesiredBlockIndentation());
                }

                Debug.Assert(indentStyle == FormattingOptions2.IndentStyle.Smart);
                return(GetDesiredSmartIndentation());
            }
        private Indenter GetIndenter(Document document, int lineNumber, FormattingOptions2.IndentStyle indentStyle, CancellationToken cancellationToken)
        {
            var syntaxFormatting = this.SyntaxFormatting;

#if CODE_STYLE
            var tree = document.GetSyntaxTreeAsync(cancellationToken).WaitAndGetResult_CanCallOnBackground(cancellationToken);
            Contract.ThrowIfNull(tree);

            var options            = document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(tree);
            var indentationOptions = new IndentationOptions(
                syntaxFormatting.GetFormattingOptions(options),
                new AutoFormattingOptions(FormatOnReturn: true, FormatOnTyping: true, FormatOnSemicolon: true, FormatOnCloseBrace: true),
                indentStyle);
#else
            var indentationOptions = IndentationOptions.FromDocumentAsync(document, cancellationToken).WaitAndGetResult_CanCallOnBackground(cancellationToken);
            var tree = document.GetRequiredSyntaxTreeSynchronously(cancellationToken);
#endif

            var sourceText       = tree.GetText(cancellationToken);
            var lineToBeIndented = sourceText.Lines[lineNumber];

#if CODE_STYLE
            var baseIndentationRule = NoOpFormattingRule.Instance;
#else
            var workspace             = document.Project.Solution.Workspace;
            var formattingRuleFactory = workspace.Services.GetRequiredService <IHostDependentFormattingRuleFactoryService>();
            var baseIndentationRule   = formattingRuleFactory.CreateRule(document, lineToBeIndented.Start);
#endif

            var formattingRules = ImmutableArray.Create(
                baseIndentationRule,
                this.GetSpecializedIndentationFormattingRule(indentStyle)).AddRange(
                syntaxFormatting.GetDefaultFormattingRules());

            var smartTokenFormatter = CreateSmartTokenFormatter(
                (TSyntaxRoot)tree.GetRoot(cancellationToken), lineToBeIndented, indentationOptions, baseIndentationRule);
            return(new Indenter(this, tree, formattingRules, indentationOptions, lineToBeIndented, smartTokenFormatter, cancellationToken));
        }
Example #3
0
 protected abstract AbstractFormattingRule GetSpecializedIndentationFormattingRule(FormattingOptions2.IndentStyle indentStyle);
Example #4
0
        public bool SupportsFormattingOnTypedCharacter(Document document, AutoFormattingOptions options, FormattingOptions2.IndentStyle indentStyle, char ch)
        {
            var smartIndentOn = indentStyle == FormattingOptions2.IndentStyle.Smart;

            // We consider the proper placement of a close curly or open curly when it is typed at
            // the start of the line to be a smart-indentation operation.  As such, even if "format
            // on typing" is off, if "smart indent" is on, we'll still format this.  (However, we
            // won't touch anything else in the block this close curly belongs to.).
            //
            // See extended comment in GetFormattingChangesAsync for more details on this.
            if (smartIndentOn)
            {
                if (ch is '{' or '}')
                {
                    return(true);
                }
            }

            // If format-on-typing is not on, then we don't support formatting on any other characters.
            var autoFormattingOnTyping = options.FormatOnTyping;

            if (!autoFormattingOnTyping)
            {
                return(false);
            }

            if (ch == '}' && !options.FormatOnCloseBrace)
            {
                return(false);
            }

            if (ch == ';' && !options.FormatOnSemicolon)
            {
                return(false);
            }

            // don't auto format after these keys if smart indenting is not on.
            if ((ch == '#' || ch == 'n') && !smartIndentOn)
            {
                return(false);
            }

            return(_supportedChars.Contains(ch));
        }
 public static IndentingStyle ToEditorIndentStyle(this FormattingOptions2.IndentStyle value)
 => value switch
 {
 public static AbstractFormattingRule ForIndentStyle(FormattingOptions2.IndentStyle indentStyle)
 {
     Debug.Assert(s_instances[(int)indentStyle]._indentStyle == indentStyle);
     return(s_instances[(int)indentStyle]);
 }
 private BraceCompletionFormattingRule(FormattingOptions2.IndentStyle indentStyle, CSharpSyntaxFormattingOptions options)
 {
     _indentStyle = indentStyle;
     _options     = options;
 }
 public BraceCompletionFormattingRule(FormattingOptions2.IndentStyle indentStyle)
     : this(indentStyle, CSharpSyntaxFormattingOptions.Default)
 {
 }
 public bool SupportsFormattingOnTypedCharacter(Document document, AutoFormattingOptions options, FormattingOptions2.IndentStyle indentStyle, char ch)
 {
     return(_service is IFSharpEditorFormattingServiceWithOptions serviceWithOptions?
            serviceWithOptions.SupportsFormattingOnTypedCharacter(document, new AutoFormattingOptionsWrapper(options, indentStyle), ch) :
                _service.SupportsFormattingOnTypedCharacter(document, ch));
 }
 public AutoFormattingOptionsWrapper(AutoFormattingOptions underlyingObject, FormattingOptions2.IndentStyle indentStyle)
 {
     UnderlyingObject = underlyingObject;
     _indentStyle     = indentStyle;
 }