コード例 #1
0
        static void RemoveCharBeforeCaret(TextEditorData data)
        {
            int offset = data.Caret.Offset;

            if (offset <= 0)
            {
                return;
            }
            var version = data.Version;
            var o       = data.Caret.Offset;

            if (CaretMoveActions.IsLowSurrogateMarkerSet(data.GetCharAt(offset - 1)))
            {
                data.Remove(offset - 2, 2);
            }
            else
            {
                data.Remove(offset - 1, 1);
            }
            data.Caret.Location = data.OffsetToLocation(version.MoveOffsetTo(data.Version, offset));
        }
コード例 #2
0
            public SnapshotSpan GetTextElementSpan(SnapshotPoint bufferPosition)
            {
                bufferPosition = this.FixBufferPosition(bufferPosition);
                if (!this.ContainsBufferPosition(bufferPosition))
                {
                    throw new ArgumentOutOfRangeException(nameof(bufferPosition));
                }

                if (bufferPosition >= ExtentIncludingLineBreak.End - LineBreakLength)
                {
                    return(new SnapshotSpan(ExtentIncludingLineBreak.End - LineBreakLength, LineBreakLength));
                }
                var line       = textEditor.GetLineByOffset(bufferPosition.Position);
                var lineOffset = line.Offset;

                var highlightedLine = this.textEditor.Document.SyntaxMode.GetHighlightedLineAsync(line, default(CancellationToken)).WaitAndGetResult();

                if (highlightedLine != null)
                {
                    foreach (var seg in highlightedLine.Segments)
                    {
                        if (seg.Contains(bufferPosition - lineOffset))
                        {
                            return(new SnapshotSpan(bufferPosition.Snapshot, lineOffset + seg.Offset, seg.Length));
                        }
                    }
                }

                var c = textEditor.GetCharAt(bufferPosition.Position);

                if (CaretMoveActions.IsLowSurrogateMarkerSet(c))
                {
                    return(new SnapshotSpan(bufferPosition.Snapshot, bufferPosition.Position, 2));
                }
                if (CaretMoveActions.IsHighSurrogateMarkerSet(c))
                {
                    return(new SnapshotSpan(bufferPosition.Snapshot, bufferPosition.Position - 1, 2));
                }
                return(new SnapshotSpan(bufferPosition, 1));
            }
コード例 #3
0
        public static void InsertNewLine(TextEditorData data)
        {
            if (!data.CanEditSelection)
            {
                return;
            }

            using (var undo = data.OpenUndoGroup()) {
                if (data.IsSomethingSelected)
                {
                    var end = data.MainSelection.End;
                    data.DeleteSelectedText();
                    if (end.Column == 1)
                    {
                        CaretMoveActions.InternalCaretMoveHome(data, true, false);
                        return;
                    }
                }
                switch (data.Options.IndentStyle)
                {
                case IndentStyle.None:
                    data.InsertAtCaret(data.EolMarker);
                    break;

                case IndentStyle.Auto:
                    data.EnsureCaretIsNotVirtual();
                    var indent = data.Document.GetLineIndent(data.Caret.Line);
                    data.InsertAtCaret(data.EolMarker);
                    data.EnsureCaretIsNotVirtual();
                    if (data.GetLine(data.Caret.Line).Length == 0)
                    {
                        data.InsertAtCaret(indent);
                    }
                    break;

                case IndentStyle.Smart:
                    if (!data.HasIndentationTracker)
                    {
                        goto case IndentStyle.Auto;
                    }
                    NewLineSmartIndent(data);
                    break;

                case IndentStyle.Virtual:
                    if (!data.HasIndentationTracker)
                    {
                        goto case IndentStyle.Auto;
                    }
                    var oldLine   = data.Caret.Line;
                    var curLine   = data.GetLine(oldLine);
                    var indentCol = data.GetVirtualIndentationColumn(data.Caret.Location);
                    if (curLine.Length >= data.Caret.Column)
                    {
                        NewLineSmartIndent(data);
                        data.FixVirtualIndentation();
                        data.FixVirtualIndentation(oldLine);
                        break;
                    }
                    data.Insert(data.Caret.Offset, data.EolMarker);
                    data.FixVirtualIndentation(oldLine);
                    data.Caret.Column = indentCol;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
コード例 #4
0
        public static void Delete(TextEditorData data)
        {
            if (!data.CanEditSelection)
            {
                return;
            }

            using (var undoGroup = data.OpenUndoGroup()) {
                if (data.IsSomethingSelected)
                {
                    // case: zero width block selection
                    if (data.MainSelection.SelectionMode == SelectionMode.Block && data.MainSelection.Anchor.Column == data.MainSelection.Lead.Column)
                    {
                        var col = data.MainSelection.Lead.Column;
                        if (col <= DocumentLocation.MinColumn)
                        {
                            data.ClearSelection();
                            return;
                        }
                        bool preserve = data.Caret.PreserveSelection;
                        data.Caret.PreserveSelection = true;
                        col--;
                        var changes = new List <Microsoft.CodeAnalysis.Text.TextChange> ();

                        for (int lineNumber = data.MainSelection.MinLine; lineNumber <= data.MainSelection.MaxLine; lineNumber++)
                        {
                            DocumentLine lineSegment = data.Document.GetLine(lineNumber);
                            if (col < lineSegment.Length)
                            {
                                changes.Add(new Microsoft.CodeAnalysis.Text.TextChange(new Microsoft.CodeAnalysis.Text.TextSpan(lineSegment.Offset + col, 1), ""));
                            }
                        }
                        data.Document.ApplyTextChanges(changes);

                        data.Caret.PreserveSelection = preserve;
                        data.Document.CommitMultipleLineUpdate(data.MainSelection.MinLine, data.MainSelection.MaxLine);
                        return;
                    }
                    data.DeleteSelectedText(data.MainSelection.SelectionMode != SelectionMode.Block);
                    return;
                }
                if (data.Caret.Offset >= data.Document.Length)
                {
                    return;
                }

                data.EnsureCaretIsNotVirtual();

                DocumentLine line = data.Document.GetLine(data.Caret.Line);
                if (data.Caret.Column == line.Length + 1)
                {
                    if (data.Caret.Line < data.Document.LineCount)
                    {
                        var deletionLength = line.DelimiterLength;
                        // smart backspace (delete indentation)
                        if (data.Options.IndentStyle == IndentStyle.Smart || data.Options.IndentStyle == IndentStyle.Virtual)
                        {
                            var next = line.NextLine;
                            if (next != null)
                            {
                                if (data.HasIndentationTracker)
                                {
                                    var lineIndentation = next.GetIndentation(data.Document);
                                    if (lineIndentation.StartsWith(data.IndentationTracker.GetIndentationString(next.LineNumber)))
                                    {
                                        deletionLength += lineIndentation.Length;
                                    }
                                }
                            }
                        }

                        data.Remove(line.EndOffsetIncludingDelimiter - line.DelimiterLength, deletionLength);
                        if (line.EndOffsetIncludingDelimiter == data.Document.Length)
                        {
                            line.UnicodeNewline = UnicodeNewline.Unknown;
                        }
                    }
                }
                else
                {
                    var o = data.Caret.Offset;
                    if (CaretMoveActions.IsHighSurrogateMarkerSet(data.GetCharAt(o)))
                    {
                        data.Remove(o, 2);
                    }
                    else
                    {
                        data.Remove(o, 1);
                    }
                    data.Document.CommitLineUpdate(data.Caret.Line);
                }
                data.FixVirtualIndentation();
            }
        }