Skip to content

Commit

Permalink
Recolorize only tokens if damage region is setted (by hyperlink,
Browse files Browse the repository at this point in the history
folding, etc)
  • Loading branch information
angelozerr committed Dec 23, 2016
1 parent 169249a commit 698a4e0
Showing 1 changed file with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public void textChanged(TextEvent e) {
// 1) when the content of the document has changed. In this case,
// colorization must not be done here. The colorization is done with
// modelTokensChanged listener.
// 2) when TextViewer#invalidateTextPresentation is called (because of folding, etc)
// 2) when TextViewer#invalidateTextPresentation is called (because
// of folding, etc)
if (e.getDocumentEvent() != null) {
// case 1), ignore colorization
return;
Expand Down Expand Up @@ -312,12 +313,10 @@ private void colorize(int fromLineNumber, int toLineNumber, IRegion damage, TMMo
System.err.println("Render from: " + fromLineNumber + " to: " + toLineNumber);
try {
IDocument document = model.getDocument();
if (damage == null) {
damage = DocumentHelper.getRegion(document, fromLineNumber, toLineNumber);
}
TextPresentation presentation = new TextPresentation(damage, 1000);
TextPresentation presentation = new TextPresentation(
damage != null ? damage : DocumentHelper.getRegion(document, fromLineNumber, toLineNumber), 1000);

int lastStart = damage.getOffset();
int lastStart = presentation.getExtent().getOffset();
int length = 0;
boolean firstToken = true;
IToken lastToken = Token.UNDEFINED;
Expand All @@ -329,6 +328,20 @@ private void colorize(int fromLineNumber, int toLineNumber, IRegion damage, TMMo
int i = 0;
int startLineOffset = document.getLineOffset(line);
for (TMToken t : tokens) {
if (damage != null) {
// Damage region is setted (this case comes from when
// hyperlink, occurences, folding are processed and call
// TextViewer#invalidateTextPresentation)
if (isBeforeRegion(t, startLineOffset, damage)) {
// The token is before the damage region, ignore it
i++;
continue;
} else if (isAfterRegion(t, startLineOffset, damage)) {
// The token is after the damage region, stop the
// colorization process
break;
}
}
IToken token = toToken(t);
TextAttribute attribute = getTokenTextAttribute(token);
if (lastAttribute != null && lastAttribute.equals(attribute)) {
Expand All @@ -354,6 +367,32 @@ private void colorize(int fromLineNumber, int toLineNumber, IRegion damage, TMMo
}
}

/**
* Return true if the given token is before the given region and false
* otherwise.
*
* @param token
* @param startLineOffset
* @param damage
* @return
*/
private boolean isBeforeRegion(TMToken token, int startLineOffset, IRegion damage) {
return token.startIndex + startLineOffset < damage.getOffset();
}

/**
* Return true if the given token is after the given region and false
* otherwise.
*
* @param t
* @param startLineOffset
* @param damage
* @return
*/
private boolean isAfterRegion(TMToken t, int startLineOffset, IRegion damage) {
return t.startIndex + startLineOffset >= damage.getOffset() + damage.getLength();
}

private IToken toToken(TMToken t) {
IToken token = getTokenProvider().getToken(t.type);
if (token != null) {
Expand Down

0 comments on commit 698a4e0

Please sign in to comment.