Beispiel #1
0
        public bool BeforeKeyPress(string key, TextSelection selection, bool inStatementCompletion, ref bool cancelKeyPress)
        {
            if (!selection.IsEmpty || key != _key)
            {
                return(false);
            }

            cancelKeyPress = true;

            var closeUndoContext = !DTE.UndoContext.IsOpen;

            if (closeUndoContext)
            {
                selection.BeginUpdate("insert " + _template);
            }

            selection.Insert(_template);
            if (_caret != 0)
            {
                selection.CharLeft(false, -_caret);
            }

            if (closeUndoContext)
            {
                selection.EndUpdate();
            }

            return(true);
        }
        private void ApplyInline(TextSelection selection)
        {
            var closeUndoContext = false;

            if (!selection.DTE.UndoContext.IsOpen)
            {
                selection.BeginUpdate("Surround With " + Name);
                closeUndoContext = true;
            }

            var sb = new StringBuilder();

            sb.Append(_preText[0]);
            sb.Append(selection.Text);
            sb.Append(_postText[0]);

            var line = selection.TopLine;
            var col  = selection.TopPoint.DisplayColumn;

            selection.Delete();
            selection.Insert(sb.ToString());

            if (_caretLine >= 0)
            {
                selection.MoveToDisplayColumn(line, col + _caretCol - 1);
            }
            else
            {
                selection.MoveToDisplayColumn(line, col, true);
            }

            if (closeUndoContext)
            {
                selection.EndUpdate();
            }
        }
        public void Apply(TextSelection selection)
        {
            if (Inline)
            {
                ApplyInline(selection);
                return;
            }

            var closeUndoContext = false;

            if (!selection.DTE.UndoContext.IsOpen)
            {
                selection.BeginUpdate("Surround With " + Name);
                closeUndoContext = true;
            }

            if (_extendSelection)
            {
                selection.ExtendToFullLine();
            }
            var indentSize = selection.Text.Lines().Where(l => !string.IsNullOrWhiteSpace(l)).Min(l => l.Length - l.TrimStart().Length);

            var content = selection.Text.Lines();

            var leadingSpace = new string(' ', indentSize);
            var indent       = new string(' ', selection.Parent.IndentSize);

            var sb = new StringBuilder();

            foreach (var s in _preText)
            {
                sb.Append(leadingSpace).AppendLine(s);
            }

            foreach (var s in content)
            {
                if (_indentSelection)
                {
                    sb.Append(indent);
                }
                sb.AppendLine(s);
            }

            foreach (var s in _postText)
            {
                sb.Append(leadingSpace).AppendLine(s);
            }

            var line = selection.TopLine;
            var col  = selection.TopPoint.DisplayColumn;

            selection.Delete();
            selection.Insert(sb.ToString());

            if (_caretLine >= _preText.Length)
            {
                selection.MoveToLineAndOffset(line + _caretLine + content.Count, _caretCol + indentSize, false);
            }
            else if (_caretLine >= 0)
            {
                selection.MoveToLineAndOffset(line + _caretLine, _caretCol + indentSize, false);
            }
            else
            {
                selection.MoveToDisplayColumn(line, col, true);
            }

            if (closeUndoContext)
            {
                selection.EndUpdate();
            }
        }