diff --git a/Nodejs/Product/Nodejs/EditFilter.cs b/Nodejs/Product/Nodejs/EditFilter.cs index 7509928c6..0439152a9 100644 --- a/Nodejs/Product/Nodejs/EditFilter.cs +++ b/Nodejs/Product/Nodejs/EditFilter.cs @@ -120,19 +120,6 @@ _intellisenseStack.TopSession is ICompletionSession && break; case VSConstants.VSStd2KCmdID.PASTE: return Paste(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut, out hr); - - case VSConstants.VSStd2KCmdID.COMMENT_BLOCK: - case VSConstants.VSStd2KCmdID.COMMENTBLOCK: - if (_textView.CommentOrUncommentBlock(comment: true)) { - return VSConstants.S_OK; - } - break; - case VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK: - case VSConstants.VSStd2KCmdID.UNCOMMENTBLOCK: - if (_textView.CommentOrUncommentBlock(comment: false)) { - return VSConstants.S_OK; - } - break; } } else if (pguidCmdGroup == VSConstants.GUID_VSStandardCommandSet97) { switch ((VSConstants.VSStd97CmdID)nCmdID) { diff --git a/Nodejs/Product/Nodejs/Editor/EditorExtensions.cs b/Nodejs/Product/Nodejs/Editor/EditorExtensions.cs index c6a1ca00d..70c6d209e 100644 --- a/Nodejs/Product/Nodejs/Editor/EditorExtensions.cs +++ b/Nodejs/Product/Nodejs/Editor/EditorExtensions.cs @@ -25,52 +25,6 @@ namespace Microsoft.NodejsTools.Editor.Core { internal static class EditorExtensions { - public static bool CommentOrUncommentBlock(this ITextView view, bool comment) { - SnapshotPoint start, end; - SnapshotPoint? mappedStart, mappedEnd; - - // select the region to comment. - // if the selected area is non-empty, let's comment the group of lines, - // otherwise just comment the current line - if (view.Selection.IsActive && !view.Selection.IsEmpty) { - // comment every line in the selection - start = view.Selection.Start.Position; - end = view.Selection.End.Position; - mappedStart = MapPoint(view, start); - - var endLine = end.GetContainingLine(); - - // If we grabbed the last line by just the start, don't comment it as it isn't actually selected. - if (endLine.Start == end) { - end = end.Snapshot.GetLineFromLineNumber(endLine.LineNumber - 1).End; - } - - mappedEnd = MapPoint(view, end); - } else { - // comment the current line - start = end = view.Caret.Position.BufferPosition; - mappedStart = mappedEnd = MapPoint(view, start); - } - - // Now that we have selected the region to comment, let's do the work. - if (mappedStart != null && mappedEnd != null && - mappedStart.Value <= mappedEnd.Value) { - if (comment) { - CommentRegion(view, mappedStart.Value, mappedEnd.Value); - } else { - UncommentRegion(view, mappedStart.Value, mappedEnd.Value); - } - - // After commenting, update the selection to the complete commented area. - if (view.TextBuffer.IsNodeJsContent()) { - UpdateSelection(view, start, end); - } - return true; - } - - return false; - } - /// /// Find if the current insertion point is a comment and, if it is, insert a * on the following /// line at the correct indentation. @@ -199,107 +153,5 @@ internal static bool IsNodeJsContent(this ITextSnapshot buffer) { internal static bool IsNodeJsContent(this ITextBuffer buffer) { return buffer.ContentType.IsOfType(NodejsConstants.Nodejs); } - - /// - /// Adds comment characters (//) to the start of each line. If there is a selection the comment is applied - /// to each selected line. Otherwise the comment is applied to the current line. - /// - private static void CommentRegion(ITextView view, SnapshotPoint start, SnapshotPoint end) { - Debug.Assert(start.Snapshot == end.Snapshot); - var snapshot = start.Snapshot; - - using (var edit = snapshot.TextBuffer.CreateEdit()) { - int minColumn = Int32.MaxValue; - - // First pass, determine the position to place the comment. - // This is done as we want all of the comment lines to line up at the end. - // We also should ignore whitelines in this calculation. - for (int i = start.GetContainingLine().LineNumber; i <= end.GetContainingLine().LineNumber; i++) { - var curLine = snapshot.GetLineFromLineNumber(i); - var text = curLine.GetText(); - - int firstNonWhitespace = IndexOfNonWhitespaceCharacter(text); - if (firstNonWhitespace >= 0 && firstNonWhitespace < minColumn) { - minColumn = firstNonWhitespace; - } - } - - // Second pass, place the comment. - for (int i = start.GetContainingLine().LineNumber; i <= end.GetContainingLine().LineNumber; i++) { - var curLine = snapshot.GetLineFromLineNumber(i); - if (String.IsNullOrWhiteSpace(curLine.GetText())) { - continue; - } - - Debug.Assert(curLine.Length >= minColumn); - - edit.Insert(curLine.Start.Position + minColumn, "//"); - } - - edit.Apply(); - } - } - - /// - /// Removes a comment character (//) from the start of each line. If there is a selection the character is - /// removed from each selected line. Otherwise the character is removed from the current line. Uncommented - /// lines are ignored. - /// - private static void UncommentRegion(ITextView view, SnapshotPoint start, SnapshotPoint end) { - Debug.Assert(start.Snapshot == end.Snapshot); - var snapshot = start.Snapshot; - - using (var edit = snapshot.TextBuffer.CreateEdit()) { - for (int i = start.GetContainingLine().LineNumber; i <= end.GetContainingLine().LineNumber; i++) { - var curLine = snapshot.GetLineFromLineNumber(i); - - DeleteCommentChars(edit, curLine); - } - - edit.Apply(); - } - } - - private static SnapshotPoint? MapPoint(ITextView view, SnapshotPoint point) { - return view.BufferGraph.MapDownToFirstMatch( - point, - PointTrackingMode.Positive, - IsNodeJsContent, - PositionAffinity.Successor - ); - } - - private static void UpdateSelection(ITextView view, SnapshotPoint start, SnapshotPoint end) { - // Select the full region that is commented, do not select if in projection buffer - // (the selection might span non-language buffer regions) - view.Selection.Select( - new SnapshotSpan( - start.GetContainingLine().Start.TranslateTo(view.TextBuffer.CurrentSnapshot, PointTrackingMode.Negative), - end.GetContainingLine().End.TranslateTo(view.TextBuffer.CurrentSnapshot, PointTrackingMode.Positive) - ), - false - ); - } - - private static void DeleteCommentChars(ITextEdit edit, ITextSnapshotLine curLine) { - var text = curLine.GetText(); - for (int j = 0; j < text.Length; j++) { - if (!Char.IsWhiteSpace(text[j])) { - if (text.Substring(j, 2) == "//") { - edit.Delete(curLine.Start.Position + j, 2); - } - break; - } - } - } - - private static int IndexOfNonWhitespaceCharacter(string text) { - for (int j = 0; j < text.Length; j++) { - if (!Char.IsWhiteSpace(text[j])) { - return j; - } - } - return -1; - } } } diff --git a/Nodejs/Tests/Core/CommentBlockTests.cs b/Nodejs/Tests/Core/CommentBlockTests.cs deleted file mode 100644 index 623701aa8..000000000 --- a/Nodejs/Tests/Core/CommentBlockTests.cs +++ /dev/null @@ -1,315 +0,0 @@ -//*********************************************************// -// Copyright (c) Microsoft. All rights reserved. -// -// Apache 2.0 License -// -// You may obtain a copy of the License at -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or -// implied. See the License for the specific language governing -// permissions and limitations under the License. -// -//*********************************************************// - -using Microsoft.NodejsTools.Editor.Core; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Microsoft.VisualStudio.Text; -using MockTextView = TestUtilities.Mocks.MockTextView; -using MockTextBuffer = NodejsTests.Mocks.MockTextBuffer; - -namespace NodejsTests { - [TestClass] - public class CommentBlockTests { - [TestMethod, Priority(0)] - public void CommentCurrentLine() { - var view = new MockTextView( - new MockTextBuffer(@"console.log('Hello'); -console.log('Goodbye');")); - - view.Caret.MoveTo(view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(0).Start); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"//console.log('Hello'); -console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - - view.Caret.MoveTo(view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).Start); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"//console.log('Hello'); -//console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void UnCommentCurrentLine() { - var view = new MockTextView( - new MockTextBuffer(@"//console.log('Hello'); -//console.log('Goodbye');")); - - view.Caret.MoveTo(view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(0).Start); - - view.CommentOrUncommentBlock(false); - - Assert.AreEqual(@"console.log('Hello'); -//console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - - view.Caret.MoveTo(view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).Start); - - view.CommentOrUncommentBlock(false); - - Assert.AreEqual(@"console.log('Hello'); -console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void Comment() { - var view = new MockTextView( - new MockTextBuffer(@"console.log('Hello'); -console.log('Goodbye');")); - - view.Selection.Select( - new SnapshotSpan(view.TextBuffer.CurrentSnapshot, new Span(0, view.TextBuffer.CurrentSnapshot.Length)), - false - ); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"//console.log('Hello'); -//console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentEmptyLine() { - var view = new MockTextView( - new MockTextBuffer(@"console.log('Hello'); - -console.log('Goodbye');")); - - view.Selection.Select( - new SnapshotSpan(view.TextBuffer.CurrentSnapshot, new Span(0, view.TextBuffer.CurrentSnapshot.Length)), - false - ); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"//console.log('Hello'); - -//console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentWhiteSpaceLine() { - var view = new MockTextView( - new MockTextBuffer(@"console.log('Hello'); - -console.log('Goodbye');")); - - view.Selection.Select( - new SnapshotSpan(view.TextBuffer.CurrentSnapshot, new Span(0, view.TextBuffer.CurrentSnapshot.Length)), - false - ); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"//console.log('Hello'); - -//console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentIndented() { - var view = new MockTextView( - new MockTextBuffer(@"function f(){ - console.log('Hello'); - console.log('Still here'); - console.log('Goodbye'); -}")); - - view.Selection.Select( - new SnapshotSpan( - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).Start, - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(2).End - ), - false - ); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"function f(){ - //console.log('Hello'); - //console.log('Still here'); - console.log('Goodbye'); -}", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentIndentedBlankLine() { - var view = new MockTextView( - new MockTextBuffer(@"function f(){ - console.log('Hello'); - - console.log('Still here'); - console.log('Goodbye'); -}")); - - view.Selection.Select( - new SnapshotSpan( - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).Start, - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(3).End - ), - false - ); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"function f(){ - //console.log('Hello'); - - //console.log('Still here'); - console.log('Goodbye'); -}", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentBlankLine() { - var view = new MockTextView( - new MockTextBuffer(@"console.log('hi'); - -console.log('bye');")); - - view.Caret.MoveTo(view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).Start); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"console.log('hi'); - -console.log('bye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentIndentedWhiteSpaceLine() { - var view = new MockTextView( - new MockTextBuffer(@"function f(){ - console.log('Hello'); - - console.log('Still here'); - console.log('Goodbye'); -}")); - - view.Selection.Select( - new SnapshotSpan( - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).Start, - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(3).End - ), - false - ); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"function f(){ - //console.log('Hello'); - - //console.log('Still here'); - console.log('Goodbye'); -}", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void UnCommentIndented() { - var view = new MockTextView( - new MockTextBuffer(@"function f(){ - //console.log('Hello'); - //console.log('Still here'); - console.log('Goodbye'); -}")); - - view.Selection.Select( - new SnapshotSpan( - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1).Start, - view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(2).End - ), - false - ); - - view.CommentOrUncommentBlock(false); - - Assert.AreEqual(@"function f(){ - console.log('Hello'); - console.log('Still here'); - console.log('Goodbye'); -}", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0), TestCategory("Ignore")] - public void UnComment() { - var view = new MockTextView( - new MockTextBuffer(@"//console.log('Hello'); -//console.log('Goodbye');")); - - view.Selection.Select( - new SnapshotSpan(view.TextBuffer.CurrentSnapshot, new Span(0, view.TextBuffer.CurrentSnapshot.Length)), - false - ); - - view.CommentOrUncommentBlock(false); - - Assert.AreEqual(@"console.log('Hello'); -console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentStartOfLastLine() { - var view = new MockTextView( - new MockTextBuffer(@"console.log('Hello'); -console.log('Goodbye');")); - - view.Selection.Select( - new SnapshotSpan(view.TextBuffer.CurrentSnapshot, new Span(0, view.TextBuffer.CurrentSnapshot.GetText().IndexOf("console.log('Goodbye');"))), - false - ); - - view.CommentOrUncommentBlock(true); - - Assert.AreEqual(@"//console.log('Hello'); -console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - - [TestMethod, Priority(0)] - public void CommentAfterCodeIsNotUncommented() { - var view = new MockTextView( - new MockTextBuffer(@"console.log('Hello');//comment that should stay a comment; -//console.log('Still here');//another comment that should stay a comment; -console.log('Goodbye');")); - - view.Selection.Select( - new SnapshotSpan(view.TextBuffer.CurrentSnapshot, new Span(0, view.TextBuffer.CurrentSnapshot.GetText().IndexOf("console.log('Goodbye');"))), - false - ); - - view.CommentOrUncommentBlock(false); - - Assert.AreEqual(@"console.log('Hello');//comment that should stay a comment; -console.log('Still here');//another comment that should stay a comment; -console.log('Goodbye');", - view.TextBuffer.CurrentSnapshot.GetText()); - } - } -} diff --git a/Nodejs/Tests/Core/NodejsTests.csproj b/Nodejs/Tests/Core/NodejsTests.csproj index 2e9ab6de5..92df17427 100644 --- a/Nodejs/Tests/Core/NodejsTests.csproj +++ b/Nodejs/Tests/Core/NodejsTests.csproj @@ -87,7 +87,6 @@ -