コード例 #1
0
        /// <summary>
        /// True if this service would like to format the document based on the user typing the
        /// provided character.
        /// </summary>
        public static bool SupportsFormattingOnTypedCharacter(Document document, char ch)
        {
            Contract.ThrowIfFalse(document.Project.Language is LanguageNames.CSharp);
            var formattingService = document.GetRequiredLanguageService <IFormattingInteractionService>();
            var options           = AutoFormattingOptions.From(document.Project);

            return(formattingService.SupportsFormattingOnTypedCharacter(document, options, ch));
        }
コード例 #2
0
        public bool SupportsFormattingOnTypedCharacter(Document document, AutoFormattingOptions options, char ch)
        {
            var smartIndentOn = options.IndentStyle == FormattingOptions.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));
        }
コード例 #3
0
 public RazorAutoFormattingOptions(AutoFormattingOptions underlyingObject)
 => UnderlyingObject = underlyingObject;
コード例 #4
0
 public bool SupportsFormattingOnTypedCharacter(Document document, AutoFormattingOptions options, char ch)
 {
     return(_service.SupportsFormattingOnTypedCharacter(document, ch));
 }
コード例 #5
0
 public AutoFormattingOptionsWrapper(AutoFormattingOptions underlyingObject)
 => UnderlyingObject = underlyingObject;
コード例 #6
0
        private void ExecuteReturnOrTypeCommandWorker(EditorCommandArgs args, CancellationToken cancellationToken)
        {
            var textView      = args.TextView;
            var subjectBuffer = args.SubjectBuffer;

            if (!CanExecuteCommand(subjectBuffer))
            {
                return;
            }

            var caretPosition = textView.GetCaretPoint(args.SubjectBuffer);

            if (!caretPosition.HasValue)
            {
                return;
            }

            var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document == null)
            {
                return;
            }

            var service = document.GetLanguageService <IFormattingInteractionService>();

            if (service == null)
            {
                return;
            }

            IList <TextChange>?textChanges;

            // save current caret position
            if (args is ReturnKeyCommandArgs)
            {
                if (!service.SupportsFormatOnReturn)
                {
                    return;
                }

                textChanges = service.GetFormattingChangesOnReturnAsync(
                    document, caretPosition.Value, documentOptions: null, cancellationToken).WaitAndGetResult(cancellationToken);
            }
            else if (args is TypeCharCommandArgs typeCharArgs)
            {
                var options = AutoFormattingOptions.From(document.Project);
                if (!service.SupportsFormattingOnTypedCharacter(document, options, typeCharArgs.TypedChar))
                {
                    return;
                }

                textChanges = service.GetFormattingChangesAsync(
                    document, typeCharArgs.TypedChar, caretPosition.Value, documentOptions: null, cancellationToken).WaitAndGetResult(cancellationToken);
            }
            else
            {
                throw ExceptionUtilities.UnexpectedValue(args);
            }

            if (textChanges == null || textChanges.Count == 0)
            {
                return;
            }

            using (var transaction = CreateEditTransaction(textView, EditorFeaturesResources.Automatic_Formatting))
            {
                transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance;
                document.Project.Solution.Workspace.ApplyTextChanges(document.Id, textChanges, cancellationToken);
                transaction.Complete();
            }

            // get new caret position after formatting
            var newCaretPositionMarker = args.TextView.GetCaretPoint(args.SubjectBuffer);

            if (!newCaretPositionMarker.HasValue)
            {
                return;
            }

            var snapshotAfterFormatting = args.SubjectBuffer.CurrentSnapshot;

            var oldCaretPosition = caretPosition.Value.TranslateTo(snapshotAfterFormatting, PointTrackingMode.Negative);
            var newCaretPosition = newCaretPositionMarker.Value.TranslateTo(snapshotAfterFormatting, PointTrackingMode.Negative);

            if (oldCaretPosition.Position == newCaretPosition.Position)
            {
                return;
            }

            // caret has moved to wrong position, move it back to correct position
            args.TextView.TryMoveCaretToAndEnsureVisible(oldCaretPosition);
        }
コード例 #7
0
 public bool SupportsFormattingOnTypedCharacter(Document document, AutoFormattingOptions options, char ch)
 {
     return(_service is IFSharpEditorFormattingServiceWithOptions serviceWithOptions?
            serviceWithOptions.SupportsFormattingOnTypedCharacter(document, new AutoFormattingOptionsWrapper(options), ch) :
                _service.SupportsFormattingOnTypedCharacter(document, ch));
 }
コード例 #8
0
 public AutoFormattingOptionsWrapper(AutoFormattingOptions underlyingObject, FormattingOptions2.IndentStyle indentStyle)
 {
     UnderlyingObject = underlyingObject;
     _indentStyle     = indentStyle;
 }