Skip to content

Commit

Permalink
fix: minor bugfix to ToggleLineCommentHandler.
Browse files Browse the repository at this point in the history
As written, the removeLineComments() algorithm expects the commented
lines to be processed in order, otherwise the document can become
corrupted as the wrong characters get removed. This fix ensures that the
lines are processed in order.
  • Loading branch information
andrewL-avlq committed Apr 27, 2023
1 parent 84c448f commit 8cec6c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ public void testToggleLineCommentUseBlockCommentAndWindowsEOL() throws Exception
assertEquals("a\r\n\r\nb\r\n\r\nc", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 0);
}

@Test
public void testToggleLineComment() throws Exception {
final var now = System.currentTimeMillis();
final var proj = ResourcesPlugin.getWorkspace().getRoot().getProject(getClass().getName() + now);
proj.create(null);
proj.open(null);
final var file = proj.getFile("whatever.lc-test");
// smallest padding so that the selected lines would not come out of a HashSet in order.
String padding = "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n";
String input = padding + "a\nb\nc\n" + padding;
file.create(new ByteArrayInputStream(input.getBytes()), true, null);
final var editor = (ITextEditor) IDE.openEditor(
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), file,
"org.eclipse.ui.genericeditor.GenericEditor");
final var doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
final var service = PlatformUI.getWorkbench().getService(IHandlerService.class);
String text = doc.get();
int indexOfA = text.indexOf('a');
int lengthToC = text.indexOf('c') + 1 - indexOfA;
editor.getSelectionProvider().setSelection(new TextSelection(indexOfA, lengthToC));
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
String commented = padding + "//a\n//b\n//c\n" + padding;
assertEquals(commented, doc.get());

// Repeatedly executed toggle comment command should remove the comments inserted previously
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals(input, doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), indexOfA, lengthToC);
}

@Test
public void testToggleBlockCommentUseLineComment() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ private static void removeLineComments(final IDocument document, final ITextSele
// Filter out the blank lines and lines that are outside of the text selection
final Set<Integer> lines = computeLines(selection, document).stream()
.filter(l -> l >= selection.getStartLine() && l <= selection.getEndLine())
.collect(Collectors.toSet());
.collect(Collectors.toCollection(TreeSet::new));

boolean isFirstLineUpdated = false;
for (final int lineNumber : lines) {
Expand Down

0 comments on commit 8cec6c3

Please sign in to comment.