1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 01:36:01 +02:00

Bug 323456 - Uncomment code separated by empty lines

Patch from Marc-Andre Laperle
This commit is contained in:
Anton Leherbauer 2010-10-13 13:23:25 +00:00
parent 64778c5409
commit 196ab5c67e

View file

@ -155,14 +155,18 @@ public final class ToggleCommentAction extends TextEditorAction {
} }
// Perform the check // Perform the check
boolean hasComment= false;
for (int i = 0, j = 0; i < regions.length; i++, j += 2) { for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
String[] prefixes= fPrefixesMap.get(regions[i].getType()); String[] prefixes= fPrefixesMap.get(regions[i].getType());
if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0 && if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0) {
!isBlockCommented(lines[j], lines[j + 1], prefixes, document)) { if (isBlockCommented(lines[j], lines[j + 1], prefixes, document)) {
return false; hasComment= true;
} else if (!isBlockEmpty(lines[j], lines[j + 1], document)) {
return false;
}
} }
} }
return true; return hasComment;
} catch (BadLocationException e) { } catch (BadLocationException e) {
CUIPlugin.log(e); // Should not happen CUIPlugin.log(e); // Should not happen
} }
@ -179,25 +183,18 @@ public final class ToggleCommentAction extends TextEditorAction {
* @param selection The selection to use * @param selection The selection to use
* @param document The document * @param document The document
* @return the region describing the text block comprising the given selection * @return the region describing the text block comprising the given selection
* @throws BadLocationException
*/ */
private IRegion getTextBlockFromSelection(ITextSelection selection, IDocument document) { private IRegion getTextBlockFromSelection(ITextSelection selection, IDocument document) throws BadLocationException {
try { int start= document.getLineOffset(selection.getStartLine());
// Until https://bugs.eclipse.org/bugs/show_bug.cgi?id=325438 is fixed, work around it int end;
int start= document.getLineOffset(selection.getStartLine()); int endLine= selection.getEndLine();
int endLine= selection.getEndLine(); if (document.getNumberOfLines() > endLine+1) {
IRegion endLineInfo= document.getLineInformation(endLine); end= document.getLineOffset(endLine+1);
int end= endLineInfo.getOffset() + endLineInfo.getLength(); } else {
return new Region(start, end - start); end= document.getLength();
// IRegion line= document.getLineInformationOfOffset(selection.getOffset());
// int length= selection.getLength() == 0 ?
// line.getLength() : selection.getLength() + (selection.getOffset() - line.getOffset());
// return new Region(line.getOffset(), length);
} catch (BadLocationException e) {
CUIPlugin.log(e); // Should not happen
} }
return null; return new Region(start, end - start);
} }
/** /**
@ -239,29 +236,63 @@ public final class ToggleCommentAction extends TextEditorAction {
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
boolean hasComment = false;
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());
boolean isEmptyLine = text.trim().length() == 0;
if(isEmptyLine) {
continue;
}
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;
} }
hasComment = true;
}
return hasComment;
} catch (BadLocationException e) {
CUIPlugin.log(e); // Should not happen
}
return false;
}
/**
* Determines whether each line is empty
*
* @param startLine Start line in document
* @param endLine End line in document
* @param document The document
* @return <code>true</code> if each line from <code>startLine</code>
* to and including <code>endLine</code> is empty
*/
private boolean isBlockEmpty(int startLine, int endLine, IDocument document) {
try {
for (int i= startLine; i <= endLine; i++) {
IRegion line= document.getLineInformation(i);
String text= document.get(line.getOffset(), line.getLength());
boolean isEmptyLine = text.trim().length() == 0;
if(!isEmptyLine) {
return false;
}
} }
return true; return true;
} catch (BadLocationException e) { } catch (BadLocationException e) {
CUIPlugin.log(e); // Should not happen CUIPlugin.log(e); // Should not happen
} }
return false; return false;
} }