AbstractBlockCommentEditingCommandHandler.cs 2.8 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
2 3 4 5

using System;
using Microsoft.CodeAnalysis.Editor.Commands;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
P
Paul Chen 已提交
6
using Microsoft.CodeAnalysis.Editor.Shared.Options;
7 8 9 10
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Operations;
using Roslyn.Utilities;

11
namespace Microsoft.CodeAnalysis.Editor.Implementation.BlockCommentEditing
12
{
13
    internal abstract class AbstractBlockCommentEditingCommandHandler : ICommandHandler<ReturnKeyCommandArgs>
14 15 16 17
    {
        private readonly ITextUndoHistoryRegistry _undoHistoryRegistry;
        private readonly IEditorOperationsFactoryService _editorOperationsFactoryService;

18
        protected AbstractBlockCommentEditingCommandHandler(
19 20 21 22 23 24 25 26 27 28 29 30 31
            ITextUndoHistoryRegistry undoHistoryRegistry,
            IEditorOperationsFactoryService editorOperationsFactoryService)
        {
            Contract.ThrowIfNull(undoHistoryRegistry);
            Contract.ThrowIfNull(editorOperationsFactoryService);

            _undoHistoryRegistry = undoHistoryRegistry;
            _editorOperationsFactoryService = editorOperationsFactoryService;
        }

        public CommandState GetCommandState(ReturnKeyCommandArgs args, Func<CommandState> nextHandler) => nextHandler();

        public void ExecuteCommand(ReturnKeyCommandArgs args, Action nextHandler)
P
Paul Chen 已提交
32 33 34 35 36 37 38 39 40 41
        {
            if (TryHandleReturnKey(args))
            {
                return;
            }

            nextHandler();
        }

        private bool TryHandleReturnKey(ReturnKeyCommandArgs args)
42 43 44 45
        {
            var subjectBuffer = args.SubjectBuffer;
            var textView = args.TextView;

J
Jason Malinowski 已提交
46
            if (!subjectBuffer.GetFeatureOnOffOption(FeatureOnOffOptions.AutoInsertBlockCommentStartString))
P
Paul Chen 已提交
47 48 49 50
            {
                return false;
            }

51 52 53
            var caretPosition = textView.GetCaretPoint(subjectBuffer);
            if (caretPosition == null)
            {
P
Paul Chen 已提交
54
                return false;
55 56 57 58 59
            }

            var exteriorText = GetExteriorTextForNextLine(caretPosition.Value);
            if (exteriorText == null)
            {
P
Paul Chen 已提交
60
                return false;
61 62
            }

63
            using (var transaction = _undoHistoryRegistry.GetHistory(args.TextView.TextBuffer).CreateTransaction(EditorFeaturesResources.Insert_new_line))
64 65 66 67 68 69 70
            {
                var editorOperations = _editorOperationsFactoryService.GetEditorOperations(args.TextView);

                editorOperations.InsertNewLine();
                editorOperations.InsertText(exteriorText);

                transaction.Complete();
P
Paul Chen 已提交
71
                return true;
72 73 74 75 76 77
            }
        }

        protected abstract string GetExteriorTextForNextLine(SnapshotPoint caretPosition);
    }
}