Skip to content

Commit

Permalink
Fix Toggle Block Comments actions
Browse files Browse the repository at this point in the history
The PR makes the Add/Remove actions to behave as much closer as possible to the Java ones.

Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
  • Loading branch information
vrubezhny committed Dec 6, 2022
1 parent ed13ec5 commit 598576c
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 157 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="org.eclipse.tm4e.feature"
label="%featureName"
version="0.5.2.qualifier"
version="0.5.3.qualifier"
provider-name="%featureProvider"
license-feature="org.eclipse.license"
license-feature-version="0.0.0">
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

<artifactId>org.eclipse.tm4e.feature</artifactId>
<packaging>eclipse-feature</packaging>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.3-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
package org.eclipse.tm4e.languageconfiguration.tests;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import java.io.ByteArrayInputStream;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.tm4e.languageconfiguration.internal.ToggleLineCommentHandler;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
Expand All @@ -23,7 +26,6 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;


public class TestComment {

@AfterEach
Expand Down Expand Up @@ -51,12 +53,48 @@ public void testToggleLineCommentUseBlockComment() throws Exception {
editor.getSelectionProvider().setSelection(new TextSelection(0, text.length()));
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals("/*a*/\n\n/*b*/\n\n/*c*/", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 2, 15);

// Repeatedly executed toggle comment command should remove the comments inserted previously
text = doc.get();
editor.getSelectionProvider().setSelection(new TextSelection(0,text.length()));
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals("a\n\nb\n\nc", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 7);
}

@Test
public void testToggleLineCommentUseBlockCommentnPartiallyIncludedEnds() 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.noLineComment");
String text = "/* a */";
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(1,5)); // [* a *]
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);

text = doc.get();
assertEquals(" a ", text);
ISelection selection = editor.getSelectionProvider().getSelection();
assertNotNull(selection);
assertInstanceOf(ITextSelection.class, selection);
ITextSelection textSelection = (ITextSelection)selection;
assertEquals(0, textSelection.getOffset());
assertEquals(3, textSelection.getLength());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 3);

// Repeatedly executed toggle comment command should remove the comments inserted previously
editor.getSelectionProvider().setSelection(new TextSelection(0,text.length()));
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals("/* a */", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 2, 3);
}

/**
Expand All @@ -76,16 +114,16 @@ public void testToggleLineCommentUseBlockCommentAndWindowsEOL() throws Exception
"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();
editor.getSelectionProvider().setSelection(new TextSelection(0, 0)); // No matter the selection length
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals("/*a*/\r\n\r\nb\r\n\r\nc", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 2, 0);

// Repeatedly executed toggle comment command should remove the comments inserted previously
text = doc.get();
editor.getSelectionProvider().setSelection(new TextSelection(0, 0)); // No matter the selection length
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals("a\r\n\r\nb\r\n\r\nc", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 0);
}

@Test
Expand All @@ -105,11 +143,163 @@ public void testToggleBlockCommentUseLineComment() throws Exception {
editor.getSelectionProvider().setSelection(new TextSelection(0, text.length()));
service.executeCommand(ToggleLineCommentHandler.ADD_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals("//a\n//\n//b\n//\n//c", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 2, 15);

// Repeatedly executed toggle comment command should remove the comments inserted previously
text = doc.get();
editor.getSelectionProvider().setSelection(new TextSelection(0,text.length()));
service.executeCommand(ToggleLineCommentHandler.REMOVE_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals("a\n\nb\n\nc", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 7);
}

@Test
public void testRemoveBlockComment() throws Exception {
final String text = "/* a */";
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.noLineComment");
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(0, text.length()));
service.executeCommand(ToggleLineCommentHandler.REMOVE_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals(" a ", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 0);
}

@Test
public void testRemoveBlockCommentMultiplesComments() throws Exception {
final String text = "/* a */ b /* c */";
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.noLineComment");
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(0, text.length()));
service.executeCommand(ToggleLineCommentHandler.REMOVE_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals(" a b c ", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 0);
}

@Test
public void testRemoveBlockCommentPartiallyIncludedEnds() throws Exception {
final String text = "/* a */";
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.noLineComment");
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(1, text.length() - 2));
service.executeCommand(ToggleLineCommentHandler.REMOVE_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals(" a ", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 0, 0);
}

@Test
public void testRemoveBlockCommentMultiplesCommentsBrokenEnds() throws Exception {
final String text = "/* a */ b /* c */";
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.noLineComment");
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(5, 7)); // [*/ b /*]
service.executeCommand(ToggleLineCommentHandler.REMOVE_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals(" a b c ", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 3, 0);
}

@Test
public void testRemoveBlockCommentMultiplesCommentsBrokenPartiallyIncludedEnds() throws Exception {
final String text = "/* a */ b /* c */";
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.noLineComment");
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(6,5)); // [/ b /]
service.executeCommand(ToggleLineCommentHandler.REMOVE_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals(" a b c ", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 3, 0);
}

@Test
public void testAddBlockComment() throws Exception {
final String text = "a b c";
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.noLineComment");
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(2,1)); // [b]
service.executeCommand(ToggleLineCommentHandler.ADD_BLOCK_COMMENT_COMMAND_ID, null);
assertEquals("a /*b*/ c", doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 4, 0);
}

@Test
public void testAddBlockCommentInsideExistingBockComment() throws Exception {
final String text = "/*a b c*/";
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.noLineComment");
file.create(new ByteArrayInputStream(text.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);
editor.getSelectionProvider().setSelection(new TextSelection(4,1)); // [b]
service.executeCommand(ToggleLineCommentHandler.ADD_BLOCK_COMMENT_COMMAND_ID, null);

// No comment is to be added because the selection is already inside a block comment
assertEquals(text, doc.get());
checktTextSelection(editor.getSelectionProvider().getSelection(), 4, 1);
}

private void checktTextSelection(ISelection selection, int expectedOffset, int expectedLength) {
assertNotNull(selection);
assertInstanceOf(ITextSelection.class, selection);
ITextSelection textSelection = (ITextSelection)selection;
assertEquals(expectedOffset, textSelection.getOffset());
assertEquals(expectedLength, textSelection.getLength());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tm4e.languageconfiguration;singleton:=true
Bundle-Version: 0.5.2.qualifier
Bundle-Version: 0.5.3.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-17
Require-Bundle: org.eclipse.jface.text,
org.eclipse.ui.genericeditor,
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.languageconfiguration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

<artifactId>org.eclipse.tm4e.languageconfiguration</artifactId>
<packaging>eclipse-plugin</packaging>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.3-SNAPSHOT</version>
</project>
Loading

0 comments on commit 598576c

Please sign in to comment.