-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow retrieving rule element types by Context name instead of …
…rule index (#26)
- Loading branch information
1 parent
100850e
commit bec9161
Showing
4 changed files
with
87 additions
and
3 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
32 changes: 32 additions & 0 deletions
32
src/main/java/org/antlr/intellij/adaptor/parser/ClassNameIElementTypeMapper.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,32 @@ | ||
package org.antlr.intellij.adaptor.parser; | ||
|
||
import com.intellij.psi.tree.IElementType; | ||
import org.antlr.intellij.adaptor.lexer.RuleIElementType; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
import org.apache.commons.lang.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public class ClassNameIElementTypeMapper implements IElementTypeMapper { | ||
|
||
private final Map<String, RuleIElementType> elementTypes; | ||
|
||
public ClassNameIElementTypeMapper(List<RuleIElementType> elementTypes) { | ||
this.elementTypes = elementTypes.stream() | ||
.collect(Collectors.toMap(RuleIElementType::getDebugName, el -> el)); | ||
} | ||
|
||
@Override | ||
public IElementType toIElementType(ParserRuleContext ctx) { | ||
String elementTypeName = StringUtils.removeEnd(ctx.getClass().getSimpleName(), "Context"); | ||
return elementTypes.get(elementTypeName); | ||
} | ||
|
||
@Override | ||
public List<RuleIElementType> getRuleElementTypes() { | ||
return new ArrayList<>(elementTypes.values()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/org/antlr/intellij/adaptor/parser/IElementTypeMapper.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,16 @@ | ||
package org.antlr.intellij.adaptor.parser; | ||
|
||
import com.intellij.psi.tree.IElementType; | ||
import org.antlr.intellij.adaptor.lexer.RuleIElementType; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Maps a {@link org.antlr.v4.runtime.ParserRuleContext} to an {@link com.intellij.psi.tree.IElementType}. | ||
*/ | ||
public interface IElementTypeMapper { | ||
IElementType toIElementType(ParserRuleContext ctx); | ||
|
||
List<RuleIElementType> getRuleElementTypes(); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/antlr/intellij/adaptor/parser/RuleIndexIElementTypeMapper.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,30 @@ | ||
package org.antlr.intellij.adaptor.parser; | ||
|
||
import com.intellij.psi.tree.IElementType; | ||
import org.antlr.intellij.adaptor.lexer.RuleIElementType; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Maps a {@link org.antlr.v4.runtime.ParserRuleContext} to an {@link com.intellij.psi.tree.IElementType} using | ||
* a rule index. | ||
*/ | ||
public class RuleIndexIElementTypeMapper implements IElementTypeMapper { | ||
|
||
private final List<RuleIElementType> ruleElementTypes; | ||
|
||
public RuleIndexIElementTypeMapper(List<RuleIElementType> elementTypes) { | ||
this.ruleElementTypes = elementTypes; | ||
} | ||
|
||
@Override | ||
public IElementType toIElementType(ParserRuleContext ctx) { | ||
return ruleElementTypes.get(ctx.getRuleIndex()); | ||
} | ||
|
||
@Override | ||
public List<RuleIElementType> getRuleElementTypes() { | ||
return ruleElementTypes; | ||
} | ||
} |