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)); }
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(); } }