mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Cosmetics.
This commit is contained in:
parent
6bb5d3014f
commit
d8406010a1
1 changed files with 12 additions and 24 deletions
|
@ -42,7 +42,6 @@ import org.eclipse.cdt.ui.CUIPlugin;
|
||||||
* @since 4.0.0
|
* @since 4.0.0
|
||||||
*/
|
*/
|
||||||
public final class ToggleCommentAction extends TextEditorAction {
|
public final class ToggleCommentAction extends TextEditorAction {
|
||||||
|
|
||||||
/** The text operation target */
|
/** The text operation target */
|
||||||
private ITextOperationTarget fOperationTarget;
|
private ITextOperationTarget fOperationTarget;
|
||||||
/** The document partitioning */
|
/** The document partitioning */
|
||||||
|
@ -122,15 +121,14 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
IDocument document= getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
|
IDocument document= getTextEditor().getDocumentProvider().getDocument(getTextEditor().getEditorInput());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
IRegion block= getTextBlockFromSelection(textSelection, document);
|
IRegion block= getTextBlockFromSelection(textSelection, document);
|
||||||
ITypedRegion[] regions= TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
|
ITypedRegion[] regions= TextUtilities.computePartitioning(document, fDocumentPartitioning, block.getOffset(), block.getLength(), false);
|
||||||
|
|
||||||
int lineCount= 0;
|
int lineCount= 0;
|
||||||
int[] lines= new int[regions.length * 2]; // [startline, endline, startline, endline, ...]
|
int[] lines= new int[regions.length * 2]; // [startline, endline, startline, endline, ...]
|
||||||
|
|
||||||
//For each partition in the text selection, figure out what are the startlines and endlines for
|
// For each partition in the text selection, figure out what are the startlines and endlines for
|
||||||
//each partition. Count the number of lines that are selected.
|
// each partition. Count the number of lines that are selected.
|
||||||
for (int i = 0, j = 0; i < regions.length; i++, j+= 2) {
|
for (int i = 0, j = 0; i < regions.length; i++, j+= 2) {
|
||||||
// start line of region
|
// start line of region
|
||||||
lines[j]= getFirstCompleteLineOfRegion(regions[i], document);
|
lines[j]= getFirstCompleteLineOfRegion(regions[i], document);
|
||||||
|
@ -140,12 +138,12 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
if (length > 0)
|
if (length > 0)
|
||||||
offset--;
|
offset--;
|
||||||
|
|
||||||
//if there is no startline for this region (startline = -1)
|
// If there is no startline for this region (startline = -1),
|
||||||
//then there is no endline
|
// then there is no endline,
|
||||||
//otherwise, get the line number of the endline and store it in the array
|
// otherwise, get the line number of the endline and store it in the array.
|
||||||
lines[j + 1]= (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
|
lines[j + 1]= (lines[j] == -1 ? -1 : document.getLineOfOffset(offset));
|
||||||
|
|
||||||
//count the number of lines that are selected in this region
|
// Count the number of lines that are selected in this region
|
||||||
lineCount += lines[j + 1] - lines[j] + 1;
|
lineCount += lines[j + 1] - lines[j] + 1;
|
||||||
|
|
||||||
assert i < regions.length;
|
assert i < regions.length;
|
||||||
|
@ -159,9 +157,7 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
|
if (!isBlockCommented(lines[j], lines[j + 1], prefixes, document))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (BadLocationException x) {
|
} catch (BadLocationException x) {
|
||||||
// should not happen
|
// should not happen
|
||||||
CUIPlugin.log(x);
|
CUIPlugin.log(x);
|
||||||
|
@ -179,7 +175,6 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
* @return the region describing the text block comprising the given selection
|
* @return the region describing the text block comprising the given selection
|
||||||
*/
|
*/
|
||||||
private IRegion getTextBlockFromSelection(ITextSelection selection, IDocument document) {
|
private IRegion getTextBlockFromSelection(ITextSelection selection, IDocument document) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
IRegion line= document.getLineInformationOfOffset(selection.getOffset());
|
IRegion line= document.getLineInformationOfOffset(selection.getOffset());
|
||||||
int length= selection.getLength() == 0 ? line.getLength() : selection.getLength() + (selection.getOffset() - line.getOffset());
|
int length= selection.getLength() == 0 ? line.getLength() : selection.getLength() + (selection.getOffset() - line.getOffset());
|
||||||
|
@ -189,7 +184,6 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
// should not happen
|
// should not happen
|
||||||
CUIPlugin.log(x);
|
CUIPlugin.log(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,9 +195,7 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
* @return the first line whose start index is in the given range, -1 if there is no such line
|
* @return the first line whose start index is in the given range, -1 if there is no such line
|
||||||
*/
|
*/
|
||||||
private int getFirstCompleteLineOfRegion(IRegion region, IDocument document) {
|
private int getFirstCompleteLineOfRegion(IRegion region, IDocument document) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
int startLine= document.getLineOfOffset(region.getOffset());
|
int startLine= document.getLineOfOffset(region.getOffset());
|
||||||
|
|
||||||
int offset= document.getLineOffset(startLine);
|
int offset= document.getLineOffset(startLine);
|
||||||
|
@ -212,7 +204,6 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
|
|
||||||
offset= document.getLineOffset(startLine + 1);
|
offset= document.getLineOffset(startLine + 1);
|
||||||
return (offset > region.getOffset() + region.getLength() ? -1 : startLine + 1);
|
return (offset > region.getOffset() + region.getLength() ? -1 : startLine + 1);
|
||||||
|
|
||||||
} catch (BadLocationException x) {
|
} catch (BadLocationException x) {
|
||||||
// should not happen
|
// should not happen
|
||||||
CUIPlugin.log(x);
|
CUIPlugin.log(x);
|
||||||
|
@ -234,31 +225,27 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
* begin of line
|
* begin of line
|
||||||
*/
|
*/
|
||||||
private boolean isBlockCommented(int startLine, int endLine, String[] prefixes, IDocument document) {
|
private boolean isBlockCommented(int startLine, int endLine, String[] prefixes, IDocument document) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// check for occurrences of prefixes in the given lines
|
// check for occurrences of prefixes in the given lines
|
||||||
for (int i= startLine; i <= endLine; i++) {
|
for (int i= startLine; i <= endLine; i++) {
|
||||||
|
|
||||||
IRegion line= document.getLineInformation(i);
|
IRegion line= document.getLineInformation(i);
|
||||||
String text= document.get(line.getOffset(), line.getLength());
|
String text= document.get(line.getOffset(), line.getLength());
|
||||||
|
|
||||||
int[] found= TextUtilities.indexOf(prefixes, text, 0);
|
int[] found= TextUtilities.indexOf(prefixes, text, 0);
|
||||||
|
|
||||||
if (found[0] == -1)
|
if (found[0] == -1) {
|
||||||
// found a line which is not commented
|
// found a line which is not commented
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
String s= document.get(line.getOffset(), found[0]);
|
String s= document.get(line.getOffset(), found[0]);
|
||||||
s= s.trim();
|
s= s.trim();
|
||||||
if (s.length() != 0)
|
if (s.length() != 0) {
|
||||||
// found a line which is not commented
|
// found a line which is not commented
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (BadLocationException x) {
|
} catch (BadLocationException x) {
|
||||||
// should not happen
|
// should not happen
|
||||||
CUIPlugin.log(x);
|
CUIPlugin.log(x);
|
||||||
|
@ -314,9 +301,10 @@ public final class ToggleCommentAction extends TextEditorAction {
|
||||||
String[] prefixes= configuration.getDefaultPrefixes(sourceViewer, type);
|
String[] prefixes= configuration.getDefaultPrefixes(sourceViewer, type);
|
||||||
if (prefixes != null && prefixes.length > 0) {
|
if (prefixes != null && prefixes.length > 0) {
|
||||||
int emptyPrefixes= 0;
|
int emptyPrefixes= 0;
|
||||||
for (int j= 0; j < prefixes.length; j++)
|
for (int j= 0; j < prefixes.length; j++) {
|
||||||
if (prefixes[j].length() == 0)
|
if (prefixes[j].length() == 0)
|
||||||
emptyPrefixes++;
|
emptyPrefixes++;
|
||||||
|
}
|
||||||
|
|
||||||
if (emptyPrefixes > 0) {
|
if (emptyPrefixes > 0) {
|
||||||
String[] nonemptyPrefixes= new String[prefixes.length - emptyPrefixes];
|
String[] nonemptyPrefixes= new String[prefixes.length - emptyPrefixes];
|
||||||
|
|
Loading…
Add table
Reference in a new issue