Skip to content

Commit 9d507af

Browse files
authored
Automatically choose first rule in grammar as start rule (#545)
* Fixes #460 so we automatically choose first rule in grammar as start rule but can still set to other rules. Signed-off-by: Terence Parr <parrt@antlr.org> * Upon save of grammar, update start rule and start rule display. Signed-off-by: Terence Parr <parrt@antlr.org>
1 parent bcc6403 commit 9d507af

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

src/main/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
import com.intellij.openapi.editor.event.EditorMouseAdapter;
1717
import com.intellij.openapi.editor.event.EditorMouseEvent;
1818
import com.intellij.openapi.extensions.PluginId;
19-
import com.intellij.openapi.fileEditor.*;
19+
import com.intellij.openapi.fileEditor.FileDocumentManager;
20+
import com.intellij.openapi.fileEditor.FileEditorManager;
21+
import com.intellij.openapi.fileEditor.FileEditorManagerEvent;
22+
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
2023
import com.intellij.openapi.progress.ProgressIndicator;
2124
import com.intellij.openapi.progress.ProgressManager;
2225
import com.intellij.openapi.progress.Task;
@@ -242,7 +245,7 @@ public void editorReleased(@NotNull EditorFactoryEvent event) {
242245
}
243246

244247
/** The test ANTLR rule action triggers this event. This can occur
245-
* only occur when the current editor the showing a grammar, because
248+
* only occur when the current editor is showing a grammar, because
246249
* that is the only time that the action is enabled. We will see
247250
* a file changed event when the project loads the first grammar file.
248251
*/
@@ -395,6 +398,12 @@ private String updateGrammarObjectsFromFile_(VirtualFile grammarFile) {
395398
previewState.g = grammars[1];
396399
}
397400
}
401+
else {
402+
synchronized (previewState) { // build atomically
403+
previewState.lg = null;
404+
previewState.g = null;
405+
}
406+
}
398407
return grammarFileName;
399408
}
400409

@@ -579,19 +588,25 @@ private class MyVirtualFileAdapter extends VirtualFileAdapter {
579588
public void contentsChanged(VirtualFileEvent event) {
580589
final VirtualFile vfile = event.getFile();
581590
if ( !vfile.getName().endsWith(".g4") ) return;
582-
if ( !projectIsClosed && !ApplicationManager.getApplication().isUnitTestMode()) grammarFileSavedEvent(vfile);
591+
if ( !projectIsClosed && !ApplicationManager.getApplication().isUnitTestMode()) {
592+
grammarFileSavedEvent(vfile);
593+
}
583594
}
584595
}
585596

586597
private class MyFileEditorManagerAdapter implements FileEditorManagerListener {
587598
@Override
588599
public void selectionChanged(FileEditorManagerEvent event) {
589-
if ( !projectIsClosed ) currentEditorFileChangedEvent(event.getOldFile(), event.getNewFile());
600+
if ( !projectIsClosed ) {
601+
currentEditorFileChangedEvent(event.getOldFile(), event.getNewFile());
602+
}
590603
}
591604

592605
@Override
593606
public void fileClosed(FileEditorManager source, VirtualFile file) {
594-
if ( !projectIsClosed ) editorFileClosedEvent(file);
607+
if ( !projectIsClosed ) {
608+
editorFileClosedEvent(file);
609+
}
595610
}
596611
}
597612

src/main/java/org/antlr/intellij/plugin/preview/InputPanel.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.intellij.openapi.editor.event.CaretListener;
1010
import com.intellij.openapi.editor.event.DocumentAdapter;
1111
import com.intellij.openapi.editor.event.DocumentEvent;
12-
import com.intellij.openapi.editor.event.EditorMouseEvent;
1312
import com.intellij.openapi.editor.ex.EditorEx;
1413
import com.intellij.openapi.editor.ex.EditorMarkupModel;
1514
import com.intellij.openapi.editor.markup.*;
@@ -235,6 +234,12 @@ public void documentChanged(DocumentEvent event) {
235234

236235
public void grammarFileSaved() {
237236
clearParseErrors();
237+
if ( previewState.startRuleName!=null ) {
238+
setStartRuleName(previewState.grammarFile, previewState.startRuleName);
239+
}
240+
else {
241+
resetStartRuleLabel();
242+
}
238243
}
239244

240245
public void switchToGrammar(PreviewState previewState, VirtualFile grammarFile) {

src/main/java/org/antlr/intellij/plugin/preview/PreviewEditorMouseListener.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ else if ( mouseEvent.isAltDown() ) {
5555
InputPanel.clearDecisionEventHighlighters(editor);
5656
}
5757

58-
public void rightClick(final PreviewState previewState, Editor editor, int offset)
59-
{
58+
public void rightClick(final PreviewState previewState, Editor editor, int offset) {
6059
if (previewState.parsingResult == null) return;
6160
final List<RangeHighlighter> highlightersAtOffset = MyActionUtils.getRangeHighlightersAtOffset(editor, offset);
6261
if (highlightersAtOffset.size() == 0) {
@@ -80,7 +79,7 @@ else if ( lookaheadInfo != null ) {
8079
}
8180

8281
@Override
83-
public void mouseMoved(EditorMouseEvent e){
82+
public void mouseMoved(EditorMouseEvent e) {
8483
int offset = getEditorCharOffsetAndRemoveTokenHighlighters(e);
8584
if ( offset<0 ) return;
8685

src/main/java/org/antlr/intellij/plugin/preview/PreviewPanel.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.antlr.intellij.plugin.parsing.ParsingUtils;
2222
import org.antlr.intellij.plugin.parsing.PreviewParser;
2323
import org.antlr.intellij.plugin.profiler.ProfilerPanel;
24+
import org.antlr.v4.misc.OrderedHashMap;
2425
import org.antlr.v4.runtime.CommonToken;
2526
import org.antlr.v4.runtime.ParserRuleContext;
2627
import org.antlr.v4.runtime.Token;
@@ -273,6 +274,8 @@ public void grammarFileSaved(VirtualFile grammarFile) {
273274
ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(project);
274275
PreviewState previewState = controller.getPreviewState(grammarFile);
275276

277+
autoSetStartRule(previewState);
278+
276279
ensureStartRuleExists(grammarFile);
277280
inputPanel.grammarFileSaved();
278281

@@ -315,6 +318,8 @@ private void switchToGrammar(VirtualFile grammarFile) {
315318
ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(project);
316319
PreviewState previewState = controller.getPreviewState(grammarFile);
317320

321+
autoSetStartRule(previewState);
322+
318323
inputPanel.switchToGrammar(previewState, grammarFile);
319324
profilerPanel.switchToGrammar(previewState, grammarFile);
320325

@@ -328,6 +333,20 @@ private void switchToGrammar(VirtualFile grammarFile) {
328333
setEnabled(previewState.g!=null || previewState.lg==null);
329334
}
330335

336+
/** From 1.18, automatically set the start rule name to the first rule in the grammar
337+
* if none has been specified
338+
*/
339+
protected void autoSetStartRule(PreviewState previewState) {
340+
if ( previewState.g==null || previewState.g.rules.size()==0 ) {
341+
// If there is no grammar all of a sudden, we need to unset the previous rule name
342+
previewState.startRuleName = null;
343+
}
344+
else if ( previewState.startRuleName==null ) {
345+
OrderedHashMap<String, Rule> rules = previewState.g.rules;
346+
previewState.startRuleName = rules.getElement(0).name;
347+
}
348+
}
349+
331350
@Override
332351
public void setEnabled(boolean enabled) {
333352
super.setEnabled(enabled);

0 commit comments

Comments
 (0)