Skip to content

Commit

Permalink
Toggle comment doesn't toggle back off comments but keeps adding comm…
Browse files Browse the repository at this point in the history
…ents chars #445

Signed-off-by: Victor Rubezhny <vrubezhny@redhat.com>
  • Loading branch information
vrubezhny committed Oct 4, 2022
1 parent da8ba92 commit 1bee731
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 13 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.1.qualifier"
version="0.5.2.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.1-SNAPSHOT</version>
<version>0.5.2-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Tests for TM4E language configuration support
Bundle-SymbolicName: org.eclipse.tm4e.languageconfiguration.tests;singleton:=true
Bundle-Version: 0.5.0.qualifier
Bundle-Version: 0.5.2.qualifier
Bundle-Vendor: Eclipse TM4E
Automatic-Module-Name: org.eclipse.tm4e.languageconfiguration.tests
Bundle-RequiredExecutionEnvironment: JavaSE-17
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.languageconfiguration.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>org.eclipse.tm4e.languageconfiguration.tests</artifactId>
<version>0.5.0-SNAPSHOT</version>
<version>0.5.2-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2021 Red Hat Inc. and others.
* Copyright (c) 2021, 2022 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -50,5 +50,11 @@ public void testIndentOnNewLine() throws Exception {
editor.getSelectionProvider().setSelection(new TextSelection(0, 5));
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals("/*a*/\n/*b*/\n/*c*/", doc.get());

// Repeatedly executed toggle comment command should remove the comments inserted previously
String text = doc.get();
editor.getSelectionProvider().setSelection(new TextSelection(0,text.length()));
service.executeCommand(ToggleLineCommentHandler.TOGGLE_LINE_COMMENT_COMMAND_ID, null);
assertEquals("a\nb\nc", doc.get());
}
}
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.1.qualifier
Bundle-Version: 0.5.2.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.1-SNAPSHOT</version>
<version>0.5.2-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2018 Red Hat Inc. and others.
* Copyright (c) 2018, 2022 Red Hat Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -13,6 +13,7 @@

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.Command;
Expand Down Expand Up @@ -106,11 +107,23 @@ public Object execute(@Nullable final ExecutionEvent event) throws ExecutionExce
} else {
final var blockComment = commentSupport.getBlockComment();
if (blockComment != null) {
final Set<Integer> lines = computeLines(textSelection, document);
Set<Integer> lines = computeLines(textSelection, document);

// As >toggleBlockComment() method performs changes on the document,
// we need to preserve some values here
final int selectionStartLine = textSelection.getStartLine();
final int selectionEndLine = textSelection.getEndLine();

// Filter out the blank lines and lines that are outside of the text selection
lines = lines.stream().filter(l ->
(l >= selectionStartLine && l <= selectionEndLine &&
!TextUtils.isBlankLine(document, l)))
.collect(Collectors.toSet());

for (final int line : lines) {
final int lineOffset = document.getLineOffset(line);
final int lineLength = document.getLineLength(line);
final var range = new TextSelection(lineOffset,
final var range = new TextSelection(document, lineOffset,
line == document.getNumberOfLines() - 1 ? lineLength : lineLength - 1);
toggleBlockComment(document, range, commentSupport, editor);
}
Expand Down Expand Up @@ -351,7 +364,7 @@ private void addBlockComment(final IDocument document, final ITextSelection sele
document.replace(selection.getOffset(), 0, blockComment.open);
document.replace(selection.getOffset() + selection.getLength() + blockComment.open.length(), 0,
blockComment.close);
final var newSelection = new TextSelection(selection.getOffset() + blockComment.close.length(),
final var newSelection = new TextSelection(selection.getOffset() + blockComment.open.length(),
selection.getLength());
editor.selectAndReveal(newSelection.getOffset(), newSelection.getLength());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* Copyright (c) 2015-2022 Angelo ZERR.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -182,4 +182,28 @@ public static TabSpacesInfo getTabSpaces(@Nullable final ITextViewer viewer) {
return new TabSpacesInfo(4, false);
}

}
/**
* Determines if all the characters at any offset of he specified document range are space or tab characters.
*
* @param document the document to search in
* @param line zero-based document line number
*
* @return <code>true</code> if all the characters of he specified document range are space or
* tab characters, otherwise returns <code>false</code>
*/
public static boolean isBlankLine(final IDocument document, int line) {
try {
int offset = document.getLineOffset(line);
final int lineEnd = offset + document.getLineLength(line);
while (offset < lineEnd) {
if (!Character.isWhitespace(document.getChar(offset))) {
return false;
}
offset++;
}
} catch (BadLocationException e) {
// Ignore, forcing a positive result
}
return true;
}
}

0 comments on commit 1bee731

Please sign in to comment.