mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-08 02:06:01 +02:00
Fix inactive code highlighting for nested conditionals and includes.
This commit is contained in:
parent
3606664371
commit
c63a200ba9
2 changed files with 70 additions and 57 deletions
|
@ -2640,12 +2640,12 @@ abstract class BaseScanner implements IScanner {
|
||||||
definitions,
|
definitions,
|
||||||
getLineNumber(bufferPos[bufferStackPos]),
|
getLineNumber(bufferPos[bufferStackPos]),
|
||||||
getCurrentFilename()) == 0) {
|
getCurrentFilename()) == 0) {
|
||||||
processIf(pos, bufferPos[bufferStackPos], true);
|
processIf(pos, bufferPos[bufferStackPos], false);
|
||||||
skipOverConditionalCode(true);
|
skipOverConditionalCode(true);
|
||||||
if (isLimitReached())
|
if (isLimitReached())
|
||||||
handleInvalidCompletion();
|
handleInvalidCompletion();
|
||||||
} else {
|
} else {
|
||||||
processIf(pos, bufferPos[bufferStackPos], false);
|
processIf(pos, bufferPos[bufferStackPos], true);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
case ppElse:
|
case ppElse:
|
||||||
|
|
|
@ -14,6 +14,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.IProgressMonitor;
|
import org.eclipse.core.runtime.IProgressMonitor;
|
||||||
|
@ -103,7 +104,9 @@ public class CSourceViewerDecorationSupport
|
||||||
inactiveCodePositionsChanged(inactiveCodePositions);
|
inactiveCodePositionsChanged(inactiveCodePositions);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
fViewer.getTextWidget().getDisplay().asyncExec(updater);
|
if (fViewer != null && !monitor.isCanceled()) {
|
||||||
|
fViewer.getTextWidget().getDisplay().asyncExec(updater);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -201,11 +204,12 @@ public class CSourceViewerDecorationSupport
|
||||||
* @see org.eclipse.ui.texteditor.SourceViewerDecorationSupport#dispose()
|
* @see org.eclipse.ui.texteditor.SourceViewerDecorationSupport#dispose()
|
||||||
*/
|
*/
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
super.dispose();
|
|
||||||
fViewer = null;
|
|
||||||
if (fUpdateJob != null) {
|
if (fUpdateJob != null) {
|
||||||
fUpdateJob.cancel();
|
fUpdateJob.cancel();
|
||||||
}
|
}
|
||||||
|
fTranslationUnit = null;
|
||||||
|
fASTTranslationUnit = null;
|
||||||
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -246,7 +250,7 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the color for the cursor line painter.
|
* Update the color for the cursor line highlighting.
|
||||||
*/
|
*/
|
||||||
private void updateCLPColor() {
|
private void updateCLPColor() {
|
||||||
if (fLineBackgroundPainter != null) {
|
if (fLineBackgroundPainter != null) {
|
||||||
|
@ -258,12 +262,12 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hide cursor line painter.
|
* Hide cursor line highlighting.
|
||||||
*/
|
*/
|
||||||
private void hideCLP() {
|
private void hideCLP() {
|
||||||
if (fLineBackgroundPainter != null) {
|
if (fLineBackgroundPainter != null) {
|
||||||
if (!isInactiveCodePositionsActive()) {
|
if (!isInactiveCodePositionsActive()) {
|
||||||
uninstallInactiveCodePainter();
|
uninstallLineBackgroundPainter();
|
||||||
} else {
|
} else {
|
||||||
fLineBackgroundPainter.enableCursorLine(false);
|
fLineBackgroundPainter.enableCursorLine(false);
|
||||||
fLineBackgroundPainter.redraw();
|
fLineBackgroundPainter.redraw();
|
||||||
|
@ -272,10 +276,10 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show cursor line painter.
|
* Show cursor line highlighting.
|
||||||
*/
|
*/
|
||||||
private void showCLP() {
|
private void showCLP() {
|
||||||
installInactiveCodePainter();
|
installLineBackgroundPainter();
|
||||||
if (fLineBackgroundPainter != null) {
|
if (fLineBackgroundPainter != null) {
|
||||||
fLineBackgroundPainter.enableCursorLine(true);
|
fLineBackgroundPainter.enableCursorLine(true);
|
||||||
fLineBackgroundPainter.redraw();
|
fLineBackgroundPainter.redraw();
|
||||||
|
@ -283,7 +287,7 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if cursor line painter is active.
|
* @return true if cursor line highlighting is active.
|
||||||
*/
|
*/
|
||||||
private boolean isCLPActive() {
|
private boolean isCLPActive() {
|
||||||
if (fPrefStore != null) {
|
if (fPrefStore != null) {
|
||||||
|
@ -293,7 +297,7 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if inactive code painter is active.
|
* @return true if inactive code highlighting is active.
|
||||||
*/
|
*/
|
||||||
private boolean isInactiveCodePositionsActive() {
|
private boolean isInactiveCodePositionsActive() {
|
||||||
if (fPrefStore != null) {
|
if (fPrefStore != null) {
|
||||||
|
@ -344,14 +348,14 @@ public class CSourceViewerDecorationSupport
|
||||||
* @see org.eclipse.ui.texteditor.SourceViewerDecorationSupport#uninstall()
|
* @see org.eclipse.ui.texteditor.SourceViewerDecorationSupport#uninstall()
|
||||||
*/
|
*/
|
||||||
public void uninstall() {
|
public void uninstall() {
|
||||||
uninstallInactiveCodePainter();
|
uninstallLineBackgroundPainter();
|
||||||
super.uninstall();
|
super.uninstall();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Install inactive code/cursor line painter.
|
* Install line background painter (inactive code/cursor line).
|
||||||
*/
|
*/
|
||||||
private void installInactiveCodePainter() {
|
private void installLineBackgroundPainter() {
|
||||||
if (fLineBackgroundPainter == null) {
|
if (fLineBackgroundPainter == null) {
|
||||||
if (fViewer instanceof ITextViewerExtension2) {
|
if (fViewer instanceof ITextViewerExtension2) {
|
||||||
fLineBackgroundPainter = new LineBackgroundPainter(fViewer);
|
fLineBackgroundPainter = new LineBackgroundPainter(fViewer);
|
||||||
|
@ -364,14 +368,16 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uninstall inactive code/cursor line painter.
|
* Uninstall line background painter (inactive code/cursor line).
|
||||||
*/
|
*/
|
||||||
private void uninstallInactiveCodePainter() {
|
private void uninstallLineBackgroundPainter() {
|
||||||
if (fLineBackgroundPainter != null) {
|
if (fLineBackgroundPainter != null) {
|
||||||
((ITextViewerExtension2)fViewer).removePainter(fLineBackgroundPainter);
|
if (fViewer instanceof ITextViewerExtension2) {
|
||||||
fLineBackgroundPainter.deactivate(true);
|
((ITextViewerExtension2)fViewer).removePainter(fLineBackgroundPainter);
|
||||||
fLineBackgroundPainter.dispose();
|
fLineBackgroundPainter.deactivate(true);
|
||||||
fLineBackgroundPainter = null;
|
fLineBackgroundPainter.dispose();
|
||||||
|
fLineBackgroundPainter = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -379,7 +385,7 @@ public class CSourceViewerDecorationSupport
|
||||||
* Show inactive code positions.
|
* Show inactive code positions.
|
||||||
*/
|
*/
|
||||||
private void showInactiveCodePositions() {
|
private void showInactiveCodePositions() {
|
||||||
installInactiveCodePainter();
|
installLineBackgroundPainter();
|
||||||
updateInactiveCodePositions();
|
updateInactiveCodePositions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -389,7 +395,7 @@ public class CSourceViewerDecorationSupport
|
||||||
private void hideInactiveCodePositions() {
|
private void hideInactiveCodePositions() {
|
||||||
if (fLineBackgroundPainter != null) {
|
if (fLineBackgroundPainter != null) {
|
||||||
if (!isCLPActive()) {
|
if (!isCLPActive()) {
|
||||||
uninstallInactiveCodePainter();
|
uninstallLineBackgroundPainter();
|
||||||
} else {
|
} else {
|
||||||
fLineBackgroundPainter.setHighlightPositions(Collections.EMPTY_LIST);
|
fLineBackgroundPainter.setHighlightPositions(Collections.EMPTY_LIST);
|
||||||
}
|
}
|
||||||
|
@ -422,52 +428,62 @@ public class CSourceViewerDecorationSupport
|
||||||
* @return a {@link List} of {@link IRegion}s
|
* @return a {@link List} of {@link IRegion}s
|
||||||
*/
|
*/
|
||||||
private static List collectInactiveCodePositions(IASTTranslationUnit translationUnit) {
|
private static List collectInactiveCodePositions(IASTTranslationUnit translationUnit) {
|
||||||
List positions = new ArrayList();
|
|
||||||
if (translationUnit == null) {
|
if (translationUnit == null) {
|
||||||
return positions;
|
return Collections.EMPTY_LIST;
|
||||||
}
|
}
|
||||||
|
String fileName = translationUnit.getContainingFilename();
|
||||||
|
if (fileName == null) {
|
||||||
|
return Collections.EMPTY_LIST;
|
||||||
|
}
|
||||||
|
List positions = new ArrayList();
|
||||||
int inactiveCodeStart = -1;
|
int inactiveCodeStart = -1;
|
||||||
boolean inInactiveCode = false;
|
boolean inInactiveCode = false;
|
||||||
|
Stack inactiveCodeStack = new Stack();
|
||||||
|
// TLETODO [performance] This does not look very efficient
|
||||||
IASTPreprocessorStatement[] preprocStmts = translationUnit.getAllPreprocessorStatements();
|
IASTPreprocessorStatement[] preprocStmts = translationUnit.getAllPreprocessorStatements();
|
||||||
for (int i = 0; i < preprocStmts.length; i++) {
|
for (int i = 0; i < preprocStmts.length; i++) {
|
||||||
IASTPreprocessorStatement statement = preprocStmts[i];
|
IASTPreprocessorStatement statement = preprocStmts[i];
|
||||||
|
if (!fileName.equals(statement.getContainingFilename())) {
|
||||||
|
// preprocessor directive is from a different file
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (statement instanceof IASTPreprocessorIfStatement) {
|
if (statement instanceof IASTPreprocessorIfStatement) {
|
||||||
IASTPreprocessorIfStatement ifStmt = (IASTPreprocessorIfStatement)statement;
|
IASTPreprocessorIfStatement ifStmt = (IASTPreprocessorIfStatement)statement;
|
||||||
if (!inInactiveCode && !ifStmt.taken()) {
|
inactiveCodeStack.push(Boolean.valueOf(inInactiveCode));
|
||||||
IASTNodeLocation nodeLocation = ifStmt.getNodeLocations()[0];
|
if (!ifStmt.taken()) {
|
||||||
inactiveCodeStart = nodeLocation.getNodeOffset();
|
if (!inInactiveCode) {
|
||||||
inInactiveCode = true;
|
IASTNodeLocation nodeLocation = ifStmt.getNodeLocations()[0];
|
||||||
} else if (inInactiveCode && ifStmt.taken()) {
|
inactiveCodeStart = nodeLocation.getNodeOffset();
|
||||||
// should not happen!
|
inInactiveCode = true;
|
||||||
assert false;
|
}
|
||||||
}
|
}
|
||||||
} else if (statement instanceof IASTPreprocessorIfdefStatement) {
|
} else if (statement instanceof IASTPreprocessorIfdefStatement) {
|
||||||
IASTPreprocessorIfdefStatement ifdefStmt = (IASTPreprocessorIfdefStatement)statement;
|
IASTPreprocessorIfdefStatement ifdefStmt = (IASTPreprocessorIfdefStatement)statement;
|
||||||
if (!inInactiveCode && !ifdefStmt.taken()) {
|
inactiveCodeStack.push(Boolean.valueOf(inInactiveCode));
|
||||||
IASTNodeLocation nodeLocation = ifdefStmt.getNodeLocations()[0];
|
if (!ifdefStmt.taken()) {
|
||||||
inactiveCodeStart = nodeLocation.getNodeOffset();
|
if (!inInactiveCode) {
|
||||||
inInactiveCode = true;
|
IASTNodeLocation nodeLocation = ifdefStmt.getNodeLocations()[0];
|
||||||
} else if (inInactiveCode && ifdefStmt.taken()) {
|
inactiveCodeStart = nodeLocation.getNodeOffset();
|
||||||
// should not happen!
|
inInactiveCode = true;
|
||||||
assert false;
|
}
|
||||||
}
|
}
|
||||||
} else if (statement instanceof IASTPreprocessorIfndefStatement) {
|
} else if (statement instanceof IASTPreprocessorIfndefStatement) {
|
||||||
IASTPreprocessorIfndefStatement ifndefStmt = (IASTPreprocessorIfndefStatement)statement;
|
IASTPreprocessorIfndefStatement ifndefStmt = (IASTPreprocessorIfndefStatement)statement;
|
||||||
if (!inInactiveCode && !ifndefStmt.taken()) {
|
inactiveCodeStack.push(Boolean.valueOf(inInactiveCode));
|
||||||
IASTNodeLocation nodeLocation = ifndefStmt.getNodeLocations()[0];
|
if (!ifndefStmt.taken()) {
|
||||||
inactiveCodeStart = nodeLocation.getNodeOffset();
|
if (!inInactiveCode) {
|
||||||
inInactiveCode = true;
|
IASTNodeLocation nodeLocation = ifndefStmt.getNodeLocations()[0];
|
||||||
} else if (inInactiveCode && ifndefStmt.taken()) {
|
inactiveCodeStart = nodeLocation.getNodeOffset();
|
||||||
// should not happen!
|
inInactiveCode = true;
|
||||||
assert false;
|
}
|
||||||
}
|
}
|
||||||
} else if (statement instanceof IASTPreprocessorElseStatement) {
|
} else if (statement instanceof IASTPreprocessorElseStatement) {
|
||||||
IASTPreprocessorElseStatement elseStmt = (IASTPreprocessorElseStatement)statement;
|
IASTPreprocessorElseStatement elseStmt = (IASTPreprocessorElseStatement)statement;
|
||||||
if (!inInactiveCode && !elseStmt.taken()) {
|
if (!elseStmt.taken() && !inInactiveCode) {
|
||||||
IASTNodeLocation nodeLocation = elseStmt.getNodeLocations()[0];
|
IASTNodeLocation nodeLocation = elseStmt.getNodeLocations()[0];
|
||||||
inactiveCodeStart = nodeLocation.getNodeOffset();
|
inactiveCodeStart = nodeLocation.getNodeOffset();
|
||||||
inInactiveCode = true;
|
inInactiveCode = true;
|
||||||
} else if (inInactiveCode && elseStmt.taken()) {
|
} else if (elseStmt.taken() && inInactiveCode) {
|
||||||
IASTNodeLocation nodeLocation = elseStmt.getNodeLocations()[0];
|
IASTNodeLocation nodeLocation = elseStmt.getNodeLocations()[0];
|
||||||
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
||||||
positions.add(new ReusableRegion(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
|
positions.add(new ReusableRegion(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
|
||||||
|
@ -475,11 +491,11 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
} else if (statement instanceof IASTPreprocessorElifStatement) {
|
} else if (statement instanceof IASTPreprocessorElifStatement) {
|
||||||
IASTPreprocessorElifStatement elifStmt = (IASTPreprocessorElifStatement)statement;
|
IASTPreprocessorElifStatement elifStmt = (IASTPreprocessorElifStatement)statement;
|
||||||
if (!inInactiveCode && !elifStmt.taken()) {
|
if (!elifStmt.taken() && !inInactiveCode) {
|
||||||
IASTNodeLocation nodeLocation = elifStmt.getNodeLocations()[0];
|
IASTNodeLocation nodeLocation = elifStmt.getNodeLocations()[0];
|
||||||
inactiveCodeStart = nodeLocation.getNodeOffset();
|
inactiveCodeStart = nodeLocation.getNodeOffset();
|
||||||
inInactiveCode = true;
|
inInactiveCode = true;
|
||||||
} else if (inInactiveCode && elifStmt.taken()) {
|
} else if (elifStmt.taken() && inInactiveCode) {
|
||||||
IASTNodeLocation nodeLocation = elifStmt.getNodeLocations()[0];
|
IASTNodeLocation nodeLocation = elifStmt.getNodeLocations()[0];
|
||||||
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
||||||
positions.add(new ReusableRegion(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
|
positions.add(new ReusableRegion(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
|
||||||
|
@ -487,20 +503,17 @@ public class CSourceViewerDecorationSupport
|
||||||
}
|
}
|
||||||
} else if (statement instanceof IASTPreprocessorEndifStatement) {
|
} else if (statement instanceof IASTPreprocessorEndifStatement) {
|
||||||
IASTPreprocessorEndifStatement endifStmt = (IASTPreprocessorEndifStatement)statement;
|
IASTPreprocessorEndifStatement endifStmt = (IASTPreprocessorEndifStatement)statement;
|
||||||
if (inInactiveCode) {
|
boolean wasInInactiveCode = ((Boolean)inactiveCodeStack.pop()).booleanValue();
|
||||||
|
if (inInactiveCode && !wasInInactiveCode) {
|
||||||
IASTNodeLocation nodeLocation = endifStmt.getNodeLocations()[0];
|
IASTNodeLocation nodeLocation = endifStmt.getNodeLocations()[0];
|
||||||
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
int inactiveCodeEnd = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
|
||||||
positions.add(new ReusableRegion(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
|
positions.add(new ReusableRegion(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
|
||||||
inInactiveCode = false;
|
|
||||||
}
|
}
|
||||||
|
inInactiveCode = wasInInactiveCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (inInactiveCode) {
|
if (inInactiveCode) {
|
||||||
IASTNodeLocation[] nodeLocations = translationUnit.getNodeLocations();
|
// handle dangling #if?
|
||||||
IASTNodeLocation lastNode = nodeLocations[nodeLocations.length - 1];
|
|
||||||
int inactiveCodeEnd = lastNode.getNodeOffset() + lastNode.getNodeLength();
|
|
||||||
positions.add(new ReusableRegion(inactiveCodeStart, inactiveCodeEnd - inactiveCodeStart));
|
|
||||||
inInactiveCode = false;
|
|
||||||
}
|
}
|
||||||
return positions;
|
return positions;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue