/// <summary> /// Attempt to uncomment a span of text using a particular block comment format. /// </summary> /// <param name="span">The span of text to uncomment.</param> /// <param name="edit">The <see cref="ITextEdit"/> instance to use for applying changes.</param> /// <param name="format">The block comment format to use for uncommenting the code.</param> /// <param name="result">Upon return, a <see cref="VirtualSnapshotSpan"/> containing the uncommented /// code.</param> /// <returns> /// <para><see langword="true"/> if the span of text was uncommented using the specified block comment /// <paramref name="format"/>.</para> /// <para>-or-</para> /// <para>false if the span of text was not a complete block comment with the specified /// <paramref name="format"/>.</para> /// </returns> /// <exception cref="ArgumentNullException">If <paramref name="edit"/> is <see langword="null"/>.</exception> protected virtual bool TryUncommentBlock(VirtualSnapshotSpan span, ITextEdit edit, BlockCommentFormat format, out VirtualSnapshotSpan result) { Contract.Requires <ArgumentNullException>(edit != null, "edit"); Contract.Requires <ArgumentNullException>(format != null, "format"); string blockStart = format.StartText; string blockEnd = format.EndText; int startLen = span.Start.Position.GetContainingLine().Length; int endLen = span.End.Position.GetContainingLine().Length; TrimSpan(ref span); //special case no selection, try and uncomment the current line. if (span.IsEmpty) { VirtualSnapshotPoint start = new VirtualSnapshotPoint(span.Start.Position.GetContainingLine().Start + ScanToNonWhitespaceChar(span.Start.Position.GetContainingLine())); VirtualSnapshotPoint end = span.IsInVirtualSpace ? span.End : new VirtualSnapshotPoint(span.End.Position.GetContainingLine().End); span = new VirtualSnapshotSpan(start, end); } // Check that comment start and end blocks are possible. if ((span.Start.Position - span.Start.Position.GetContainingLine().Start) + blockStart.Length <= startLen && (span.End.Position - span.End.Position.GetContainingLine().Start) - blockStart.Length >= 0) { string startText = span.Snapshot.GetText(span.Start.Position, blockStart.Length); if (startText.Equals(blockStart, StringComparison.Ordinal)) { SnapshotSpan linespan = span.SnapshotSpan; linespan = new SnapshotSpan(span.End.Position - blockEnd.Length, span.End.Position); string endText = linespan.GetText(); if (endText.Equals(blockEnd, StringComparison.Ordinal)) { //yes, block comment selected; remove it edit.Delete(linespan); edit.Delete(span.Start.Position, blockStart.Length); result = new VirtualSnapshotSpan(span.SnapshotSpan); return(true); } } } result = default(VirtualSnapshotSpan); return(false); }
/// <summary> /// Comment out a span of text using the specified block comment format. If the <paramref name="span"/> is /// empty, the entire line containing the span's start point is commented. /// </summary> /// <param name="span">The span of text to comment.</param> /// <param name="edit">The <see cref="ITextEdit"/> to use for applying changes.</param> /// <param name="format">The block comment format.</param> /// <returns>A <see cref="VirtualSnapshotSpan"/> containing the commented code.</returns> /// <exception cref="ArgumentNullException"> /// <para>If <paramref name="edit"/> is <see langword="null"/>.</para> /// <para>-or-</para> /// <para>If <paramref name="format"/> is <see langword="null"/>.</para> /// </exception> protected virtual VirtualSnapshotSpan CommentBlock(VirtualSnapshotSpan span, ITextEdit edit, BlockCommentFormat format) { Contract.Requires <ArgumentNullException>(edit != null, "edit"); Contract.Requires <ArgumentNullException>(format != null, "format"); //special case no selection if (span.IsEmpty) { VirtualSnapshotPoint start = new VirtualSnapshotPoint(span.Start.Position.GetContainingLine().Start + ScanToNonWhitespaceChar(span.Start.Position.GetContainingLine())); VirtualSnapshotPoint end = span.IsInVirtualSpace ? span.End : new VirtualSnapshotPoint(span.End.Position.GetContainingLine().End); span = new VirtualSnapshotSpan(start, end); } // add start comment edit.Insert(span.Start.Position, format.StartText); // add end comment edit.Insert(span.End.Position, format.EndText); return(span); }
/// <summary> /// Comment out a span of text using the specified block comment format. If the <paramref name="span"/> is /// empty, the entire line containing the span's start point is commented. /// </summary> /// <param name="span">The span of text to comment.</param> /// <param name="edit">The <see cref="ITextEdit"/> to use for applying changes.</param> /// <param name="format">The block comment format.</param> /// <returns>A <see cref="VirtualSnapshotSpan"/> containing the commented code.</returns> /// <exception cref="ArgumentNullException"> /// <para>If <paramref name="edit"/> is <see langword="null"/>.</para> /// <para>-or-</para> /// <para>If <paramref name="format"/> is <see langword="null"/>.</para> /// </exception> protected virtual VirtualSnapshotSpan CommentBlock(VirtualSnapshotSpan span, ITextEdit edit, BlockCommentFormat format) { Contract.Requires<ArgumentNullException>(edit != null, "edit"); Contract.Requires<ArgumentNullException>(format != null, "format"); //special case no selection if (span.IsEmpty) { VirtualSnapshotPoint start = new VirtualSnapshotPoint(span.Start.Position.GetContainingLine().Start + ScanToNonWhitespaceChar(span.Start.Position.GetContainingLine())); VirtualSnapshotPoint end = span.IsInVirtualSpace ? span.End : new VirtualSnapshotPoint(span.End.Position.GetContainingLine().End); span = new VirtualSnapshotSpan(start, end); } // add start comment edit.Insert(span.Start.Position, format.StartText); // add end comment edit.Insert(span.End.Position, format.EndText); return span; }
/// <summary> /// Attempt to uncomment a span of text using a particular block comment format. /// </summary> /// <param name="span">The span of text to uncomment.</param> /// <param name="edit">The <see cref="ITextEdit"/> instance to use for applying changes.</param> /// <param name="format">The block comment format to use for uncommenting the code.</param> /// <param name="result">Upon return, a <see cref="VirtualSnapshotSpan"/> containing the uncommented /// code.</param> /// <returns> /// <para><see langword="true"/> if the span of text was uncommented using the specified block comment /// <paramref name="format"/>.</para> /// <para>-or-</para> /// <para>false if the span of text was not a complete block comment with the specified /// <paramref name="format"/>.</para> /// </returns> /// <exception cref="ArgumentNullException">If <paramref name="edit"/> is <see langword="null"/>.</exception> protected virtual bool TryUncommentBlock(VirtualSnapshotSpan span, ITextEdit edit, BlockCommentFormat format, out VirtualSnapshotSpan result) { Contract.Requires<ArgumentNullException>(edit != null, "edit"); Contract.Requires<ArgumentNullException>(format != null, "format"); string blockStart = format.StartText; string blockEnd = format.EndText; int startLen = span.Start.Position.GetContainingLine().Length; int endLen = span.End.Position.GetContainingLine().Length; TrimSpan(ref span); //special case no selection, try and uncomment the current line. if (span.IsEmpty) { VirtualSnapshotPoint start = new VirtualSnapshotPoint(span.Start.Position.GetContainingLine().Start + ScanToNonWhitespaceChar(span.Start.Position.GetContainingLine())); VirtualSnapshotPoint end = span.IsInVirtualSpace ? span.End : new VirtualSnapshotPoint(span.End.Position.GetContainingLine().End); span = new VirtualSnapshotSpan(start, end); } // Check that comment start and end blocks are possible. if ((span.Start.Position - span.Start.Position.GetContainingLine().Start) + blockStart.Length <= startLen && (span.End.Position - span.End.Position.GetContainingLine().Start) - blockStart.Length >= 0) { string startText = span.Snapshot.GetText(span.Start.Position, blockStart.Length); if (startText.Equals(blockStart, StringComparison.Ordinal)) { SnapshotSpan linespan = span.SnapshotSpan; linespan = new SnapshotSpan(span.End.Position - blockEnd.Length, span.End.Position); string endText = linespan.GetText(); if (endText.Equals(blockEnd, StringComparison.Ordinal)) { //yes, block comment selected; remove it edit.Delete(linespan); edit.Delete(span.Start.Position, blockStart.Length); result = new VirtualSnapshotSpan(span.SnapshotSpan); return true; } } } result = default(VirtualSnapshotSpan); return false; }
private void TestSimpleBlockComment(string initialText, string finalText, int caretPosition) { ITextBuffer textBuffer = TextBufferFactoryService.CreateTextBuffer(initialText, ContentTypeRegistryService.GetContentType(TestContentType)); BlockCommentFormat blockCommentFormat = new BlockCommentFormat("/*", "*/"); FormatCommenter commenter = new FormatCommenter(textBuffer, blockCommentFormat); VirtualSnapshotPoint caret = new VirtualSnapshotPoint(textBuffer.CurrentSnapshot.Lines.First(), caretPosition); VirtualSnapshotSpan caretSpan = new VirtualSnapshotSpan(caret, caret); ReadOnlyCollection<VirtualSnapshotSpan> commentSpans = commenter.CommentSpans(new ReadOnlyCollection<VirtualSnapshotSpan>(new[] { caretSpan })); Assert.AreEqual(finalText, textBuffer.CurrentSnapshot.GetText()); Assert.AreEqual(1, commentSpans.Count); Assert.AreEqual(finalText.Length - finalText.TrimStart().Length, commentSpans[0].Start.Position); Assert.AreEqual(initialText.Length + blockCommentFormat.StartText.Length + blockCommentFormat.EndText.Length, commentSpans[0].End.Position); }