forked from eclipse-jdtls/eclipse.jdt.ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial changes to support semantic highlighting
Closes eclipse-jdtls#715. Signed-off-by: Akos Kitta <kittaakos@typefox.io>
- Loading branch information
Akos Kitta
committed
Jul 4, 2018
1 parent
b695ed2
commit a9ebadd
Showing
10 changed files
with
2,470 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
....ls.core/src/org/eclipse/jdt/ls/core/internal/semantichighlight/SemanticHighlighting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 TypeFox and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Copied from https://github.com/eclipse/eclipse.jdt.ui/blob/d41fa3326c5b75a6419c81fcecb37d7d7fb3ac43/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlighting.java | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.jdt.ls.core.internal.semantichighlight; | ||
|
||
import java.util.Collection; | ||
|
||
import com.google.common.collect.Iterables; | ||
|
||
/** | ||
* Semantic highlighting | ||
*/ | ||
public interface SemanticHighlighting { | ||
|
||
public abstract Collection<String> getScopes(); | ||
|
||
/** | ||
* @return the display name | ||
*/ | ||
public default String getDisplayName() { | ||
final StringBuilder sb = new StringBuilder(); | ||
sb.append(this.getClass().getSimpleName()); | ||
sb.append("["); | ||
sb.append(Iterables.toString(getScopes())); | ||
sb.append("]"); | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* Returns <code>true</code> iff the semantic highlighting consumes the semantic | ||
* token. | ||
* <p> | ||
* NOTE: Implementors are not allowed to keep a reference on the token or on any | ||
* object retrieved from the token. | ||
* </p> | ||
* | ||
* @param token | ||
* the semantic token for a | ||
* {@link org.eclipse.jdt.core.dom.SimpleName} | ||
* @return <code>true</code> iff the semantic highlighting consumes the semantic | ||
* token | ||
*/ | ||
public boolean consumes(SemanticToken token); | ||
|
||
/** | ||
* Returns <code>true</code> iff the semantic highlighting consumes the semantic | ||
* token. | ||
* <p> | ||
* NOTE: Implementors are not allowed to keep a reference on the token or on any | ||
* object retrieved from the token. | ||
* </p> | ||
* | ||
* @param token | ||
* the semantic token for a | ||
* {@link org.eclipse.jdt.core.dom.NumberLiteral}, | ||
* {@link org.eclipse.jdt.core.dom.BooleanLiteral} or | ||
* {@link org.eclipse.jdt.core.dom.CharacterLiteral} | ||
* @return <code>true</code> iff the semantic highlighting consumes the semantic | ||
* token | ||
*/ | ||
public default boolean consumesLiteral(SemanticToken token) { | ||
return false; | ||
} | ||
|
||
} |
184 changes: 184 additions & 0 deletions
184
...e/src/org/eclipse/jdt/ls/core/internal/semantichighlight/SemanticHighlightingManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 TypeFox and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Copied from https://github.com/eclipse/eclipse.jdt.ui/blob/d41fa3326c5b75a6419c81fcecb37d7d7fb3ac43/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/SemanticHighlightingManager.java | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.ls.core.internal.semantichighlight; | ||
|
||
import java.util.Collection; | ||
|
||
import org.eclipse.jface.text.Position; | ||
import org.eclipse.jface.text.Region; | ||
|
||
/** | ||
* Semantic highlighting manager | ||
* | ||
* @since 3.0 | ||
*/ | ||
public class SemanticHighlightingManager { | ||
|
||
/** | ||
* Highlighted Positions. | ||
*/ | ||
static class HighlightedPosition extends Position { | ||
|
||
/** Highlighting (TM) scopes of the position */ | ||
private Collection<String> fStyle; | ||
|
||
/** Lock object */ | ||
private Object fLock; | ||
|
||
/** | ||
* Initialize the styled positions with the given offset, length and foreground | ||
* color. | ||
* | ||
* @param offset | ||
* The position offset | ||
* @param length | ||
* The position length | ||
* @param highlighting | ||
* The position's TM scopes for the highlighting | ||
* @param lock | ||
* The lock object | ||
*/ | ||
public HighlightedPosition(int offset, int length, Collection<String> highlighting, Object lock) { | ||
super(offset, length); | ||
fStyle= highlighting; | ||
fLock= lock; | ||
} | ||
|
||
/** | ||
* Uses reference equality for the highlighting. | ||
* | ||
* @param off | ||
* The offset | ||
* @param len | ||
* The length | ||
* @param highlighting | ||
* The highlighting scopes for the position | ||
* @return <code>true</code> iff the given offset, length and highlighting are | ||
* equal to the internal ones. | ||
*/ | ||
public boolean isEqual(int off, int len, Collection<String> highlighting) { | ||
synchronized (fLock) { | ||
return !isDeleted() && getOffset() == off && getLength() == len && fStyle == highlighting; | ||
} | ||
} | ||
|
||
/** | ||
* Is this position contained in the given range (inclusive)? Synchronizes on position updater. | ||
* | ||
* @param off The range offset | ||
* @param len The range length | ||
* @return <code>true</code> iff this position is not delete and contained in the given range. | ||
*/ | ||
public boolean isContained(int off, int len) { | ||
synchronized (fLock) { | ||
return !isDeleted() && off <= getOffset() && off + len >= getOffset() + getLength(); | ||
} | ||
} | ||
|
||
public void update(int off, int len) { | ||
synchronized (fLock) { | ||
super.setOffset(off); | ||
super.setLength(len); | ||
} | ||
} | ||
|
||
/* | ||
* @see org.eclipse.jface.text.Position#setLength(int) | ||
*/ | ||
@Override | ||
public void setLength(int length) { | ||
synchronized (fLock) { | ||
super.setLength(length); | ||
} | ||
} | ||
|
||
/* | ||
* @see org.eclipse.jface.text.Position#setOffset(int) | ||
*/ | ||
@Override | ||
public void setOffset(int offset) { | ||
synchronized (fLock) { | ||
super.setOffset(offset); | ||
} | ||
} | ||
|
||
/* | ||
* @see org.eclipse.jface.text.Position#delete() | ||
*/ | ||
@Override | ||
public void delete() { | ||
synchronized (fLock) { | ||
super.delete(); | ||
} | ||
} | ||
|
||
/* | ||
* @see org.eclipse.jface.text.Position#undelete() | ||
*/ | ||
@Override | ||
public void undelete() { | ||
synchronized (fLock) { | ||
super.undelete(); | ||
} | ||
} | ||
|
||
/** | ||
* @return Returns the highlighting (TM) scopes. | ||
*/ | ||
public Collection<String> getHighlightingScopes() { | ||
return fStyle; | ||
} | ||
} | ||
|
||
/** | ||
* Highlighted ranges. | ||
*/ | ||
public static class HighlightedRange extends Region { | ||
/** The highlighting key as returned by {@link SemanticHighlighting#getPreferenceKey()}. */ | ||
private String fKey; | ||
|
||
/** | ||
* Initialize with the given offset, length and highlighting key. | ||
* | ||
* @param offset the offset | ||
* @param length the length | ||
* @param key the highlighting key as returned by | ||
* {@link SemanticHighlighting#getPreferenceKey()} | ||
*/ | ||
public HighlightedRange(int offset, int length, String key) { | ||
super(offset, length); | ||
fKey= key; | ||
} | ||
|
||
/** | ||
* @return the highlighting key as returned by {@link SemanticHighlighting#getPreferenceKey()} | ||
*/ | ||
public String getKey() { | ||
return fKey; | ||
} | ||
|
||
/* | ||
* @see org.eclipse.jface.text.Region#equals(java.lang.Object) | ||
*/ | ||
@Override | ||
public boolean equals(Object o) { | ||
return super.equals(o) && o instanceof HighlightedRange && fKey.equals(((HighlightedRange)o).getKey()); | ||
} | ||
|
||
/* | ||
* @see org.eclipse.jface.text.Region#hashCode() | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
return super.hashCode() | fKey.hashCode(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.