Antlr4 Eclipse based editor
The editor use a the antlr v4 grammar from the antlr.org examples site: github.com/antlr/grammars-v4/tree/master/antlrv4
The translated grammar is used to provide the tokens for the eclipse editor.
This editor implements a number of features.
Use the ANTLR parse tree visitor and listeners to extract details from the parsed antlr grammar.
To capture changes to the document a document listener is added in AntlrDocument
The scanner and parser is invoked from LexerHelper and the visitor ANTLRv4Visitor produces
- Map of all parser rules
- Map of all lexer rules
- Map of all lexer modes
- List of all error messages
The error messages are added to the file as resource markers.
The text attributes are provided in ANTLRv4Scanner and IANTLRv4ColorConstants.
For example, for the Lexer keyword FRAGMENT:
Color STMTrgb = new Color(Display.getCurrent(),153, 0, 153);
TextAttribute STMT = new TextAttribute(STMTrgb, null, SWT.ITALIC);
IToken stmtTextToken = new org.eclipse.jface.text.rules.Token(IANTLRv4ColorConstants.STMT);
hilite.put(ANTLRv4Lexer.FRAGMENT, stmtTextToken);
The Outline view uses the lexer rules and parser rules maps that the scanner produced. The outline content provider AntlrDocOutlineContentProvider takes the content of the maps and add to the outline content.
Supported content in the outline view
- Lexer Modes
- Lexer Rules
- Parser Rules
The project nature and build will eventually request the plugin to build the antlr g4 file.
package org.github.antlr4ide.builder;
public class AntlrBuilder extends IncrementalProjectBuilder { ... }
Folding is implemented using the projection framework based on the article Folding
The scanner creates the maps for Lexer modes, Lexer rules and Parser rules. The maps has the relevant name as key and a position as value. A position org.eclipse.jface.text.Position
is start offset and length. The positions can be used directly by the projection framework.
For now only initial folding of Parser Rules and Lexer Modes is implemented.
Tool preference page has an option to cache the internal antlr parse tree. This require a version of the antlr tool that supports Serialization. If the parse tree is still valid and if enabled the editor will read the parse tree from the cache instead of regenerating the parse tree. Currently this fork of Antlr supports caching parse trees for Java target.
If there is interest to support this for other targets please raise an issue antlride-eclipse-issue
This section is for various snippets collected while searching for answers
Currently the editor and document providers are cached. There might be a better way
public String getCurrentEditorContent() {
final IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActiveEditor();
if (!(editor instanceof ITextEditor)) return null;
ITextEditor ite = (ITextEditor)editor;
IDocument doc = ite.getDocumentProvider().getDocument(ite.getEditorInput());
return doc.get();
}
// get all open editors
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences()
Searching for adding "folding" to editor context menu.
https://github.com/mickaelistria/eclipse-ide-parallel-builds-demo https://stackoverflow.com/questions/33718708/is-it-possible-to-generate-builder-code-automatically-in-eclipse-according-to-bu https://www.eclipse.org/articles/Article-Builders/builders.html
https://www.eclipse.org/swt/ https://www.eclipse.org/swt/snippets/ https://www.eclipse.org/swt/javadoc.php SWT: A Native Widget Toolkit for Java - Part 1 of 2 SWT: A Native Widget Toolkit for Java - Part 2 of 2