Skip to content

Commit

Permalink
Bring back deep macro evaluation in tooltips
Browse files Browse the repository at this point in the history
For example, hovering over BAR in:
> #define FOO 42
> #define BAR FOO + 56
> BAR

Will show a tooltip with "42 + 56" instead of "FOO + 56" as it would before.

The implementation is not perfect, it (ab)uses the IElementType like the old implementation did.
However making it the clean way would require implementing remapping system which allows using text.
And that could take some time.
  • Loading branch information
Darkyenus committed Oct 3, 2015
1 parent 683f9c6 commit e13ddd2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
5 changes: 2 additions & 3 deletions src/glslplugin/GLSLHighlighter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase;
import com.intellij.psi.tree.IElementType;
import glslplugin.lang.elements.GLSLElementTypes;
import glslplugin.lang.scanner.GLSLFlexAdapter;
import org.jetbrains.annotations.NotNull;

import static glslplugin.lang.elements.GLSLElementTypes.RedefinedTokenElementType;
import static glslplugin.lang.elements.GLSLTokenTypes.*;
import static glslplugin.lang.elements.GLSLElementTypes.*;

public class GLSLHighlighter extends SyntaxHighlighterBase {

Expand Down Expand Up @@ -97,7 +96,7 @@ public TextAttributesKey[] getTokenHighlights(IElementType type) {
if (type == PREPROCESSOR_STRING) return GLSL_STRING;
if (type == UNKNOWN) return GLSL_UNKNOWN;
if (type == RESERVED_KEYWORD) return GLSL_FLOW_KEYWORDS;
if (type == REDEFINED_TOKEN) return GLSL_REDEFINED_TOKEN;
if (type instanceof RedefinedTokenElementType /*== REDEFINED_TOKEN*/) return GLSL_REDEFINED_TOKEN;
return GLSL_TEXT;
}
}
16 changes: 13 additions & 3 deletions src/glslplugin/annotation/impl/RedefinedTokenAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.intellij.lang.annotation.Annotation;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.psi.tree.IElementType;
import glslplugin.GLSLHighlighter;
import glslplugin.annotation.Annotator;
import glslplugin.lang.elements.GLSLElementTypes;
import glslplugin.lang.elements.preprocessor.GLSLDefineDirective;
import glslplugin.lang.elements.preprocessor.GLSLRedefinedToken;
import glslplugin.lang.elements.reference.GLSLMacroReference;
Expand All @@ -16,9 +18,17 @@
public class RedefinedTokenAnnotation extends Annotator<GLSLRedefinedToken> {
@Override
public void annotate(GLSLRedefinedToken identifier, AnnotationHolder holder) {
GLSLMacroReference reference = identifier.getReference();
GLSLDefineDirective referent = (reference != null) ? reference.resolve() : null;
String definition = (referent != null) ? referent.getBoundText() : null;
String definition;

final IElementType identifierType = identifier.getNode().getElementType();
if(identifierType instanceof GLSLElementTypes.RedefinedTokenElementType){
definition = ((GLSLElementTypes.RedefinedTokenElementType) identifierType).text;
}else{
GLSLMacroReference reference = identifier.getReference();
GLSLDefineDirective referent = (reference != null) ? reference.resolve() : null;
definition = (referent != null) ? referent.getBoundText() : null;
}

Annotation annotation = holder.createInfoAnnotation(identifier, definition);
annotation.setTextAttributes(GLSLHighlighter.GLSL_REDEFINED_TOKEN[0]);
}
Expand Down
29 changes: 5 additions & 24 deletions src/glslplugin/lang/elements/GLSLElementTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class GLSLElementTypes {
public static final IFileElementType FILE = new IFileElementType(Language.findInstance(GLSLLanguage.class));

public static final IElementType PREPROCESSOR_DIRECTIVE = new GLSLElementType("PREPROCESSOR_DIRECTIVE");
public static final IElementType REDEFINED_TOKEN = new GLSLElementType("REDEFINED_TOKEN");
//Workaround before proper text redefinition of remapped tokens is implemented, see RedefinedTokenElementType below
//public static final IElementType REDEFINED_TOKEN = new GLSLElementType("REDEFINED_TOKEN");

public static final IElementType VARIABLE_NAME_EXPRESSION = new GLSLElementType("VARIABLE_NAME_EXPRESSION");
public static final IElementType EXPRESSION = new GLSLElementType("EXPRESSION");
Expand Down Expand Up @@ -116,32 +117,12 @@ public class GLSLElementTypes {
public static final IElementType CONDITION = new GLSLElementType("CONDITION");

//Preprocessor dropins
public static final class PreprocessedExpressionElementType extends GLSLElementType {
public static final class RedefinedTokenElementType extends GLSLElementType {

public final String text;

public PreprocessedExpressionElementType(String text) {
super("PREPROCESSED_EXPRESSION", false);
this.text = text;
}
}
public static final class PreprocessedLiteralElementType extends GLSLElementType {

public final GLSLLiteral.Type type;
public final String text;

public PreprocessedLiteralElementType(GLSLLiteral.Type type, String text) {
super("PREPROCESSED_LITERAL", false);
this.type = type;
this.text = text;
}
}
public static final IElementType PREPROCESSED_EMPTY = new GLSLElementType("PREPROCESSED_EMPTY");
public static final class PreprocessedUnknownElementType extends GLSLElementType {
public final String text;

public PreprocessedUnknownElementType(String text) {
super("PREPROCESSED_UNKNOWN", false);
public RedefinedTokenElementType(String text) {
super("REDEFINED_TOKEN", false);
this.text = text;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/glslplugin/lang/elements/GLSLPsiElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public GLSLElement create(ASTNode node) {
}
IElementType type = node.getElementType();

if (type == GLSLElementTypes.REDEFINED_TOKEN) return new GLSLRedefinedToken(node);
if (type instanceof GLSLElementTypes.RedefinedTokenElementType /*== GLSLElementTypes.REDEFINED_TOKEN*/) return new GLSLRedefinedToken(node);
if (type == GLSLTokenTypes.PREPROCESSOR_DEFINE) return new GLSLDefineDirective(node);
if (GLSLTokenTypes.PREPROCESSOR_DIRECTIVES.contains(type)) return new GLSLPreprocessorDirective(node);

Expand Down
7 changes: 7 additions & 0 deletions src/glslplugin/lang/parser/GLSLParsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,20 @@ protected final void parsePreprocessor() {
b.advanceLexer();//Get past identifier

List<IElementType> definition = new ArrayList<IElementType>();
StringBuilder definitionText = new StringBuilder();

while (b.getTokenType() != PREPROCESSOR_END && !b.eof()) {
//Suppressed warning that getTokenType/Text may be null, because it won't be (.eof() is checked).
//noinspection ConstantConditions
definition.add(new ForeignLeafType(b.getTokenType(), b.getTokenText()));
definitionText.append(b.getTokenText()).append(' ');
b.advanceLexer();
}
definitions.put(defineIdentifier, definition);
if(definitionText.length() >= 1){
definitionText.setLength(definitionText.length()-1);
}
definitionTexts.put(defineIdentifier, definitionText.toString());
}else{
//Invalid
b.error("Identifier expected.");
Expand All @@ -121,6 +127,7 @@ protected final void parsePreprocessor() {
//Valid
final String defineIdentifier = b.getTokenText();
definitions.remove(defineIdentifier);
definitionTexts.remove(defineIdentifier);

b.advanceLexer();//Get past IDENTIFIER
}else{
Expand Down
9 changes: 6 additions & 3 deletions src/glslplugin/lang/parser/GLSLParsingBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ abstract class GLSLParsingBase {
protected final GLSLPsiBuilderAdapter b;

protected Map<String, List<IElementType>> definitions = new HashMap<String, List<IElementType>>();
protected Map<String, String> definitionTexts = new HashMap<String, String>();

GLSLParsingBase(PsiBuilder builder) {
b = new GLSLPsiBuilderAdapter(builder);
Expand Down Expand Up @@ -77,12 +78,14 @@ public void advanceLexer(boolean checkForPreprocessor, boolean remapTokens){
}

public void advanceLexer_remapTokens(){
if (definitions.get(getTokenText()) != null) {
final String tokenText = getTokenText();
final List<IElementType> definition = definitions.get(tokenText);
if (definition != null) {
Marker macro = mark();
remapCurrentTokenAdvanceLexer_redefineTokens = false;
remapCurrentToken(definitions.get(getTokenText()));
remapCurrentToken(definition);
remapCurrentTokenAdvanceLexer_redefineTokens = true;
macro.done(REDEFINED_TOKEN);
macro.done(new RedefinedTokenElementType(definitionTexts.get(tokenText)));
advanceLexer_remapTokens();
}
}
Expand Down

0 comments on commit e13ddd2

Please sign in to comment.