Skip to content

Commit

Permalink
Merge pull request #95 from parrt/dont-rely-on-current-file
Browse files Browse the repository at this point in the history
Dont rely on current file
  • Loading branch information
parrt committed Apr 23, 2014
2 parents fd4d43c + e4ea17d commit 805e5ac
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 148 deletions.
102 changes: 56 additions & 46 deletions src/java/org/antlr/intellij/plugin/ANTLRv4PluginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void projectOpened() {
}

public void createToolWindows() {
LOG.info("createToolWindows");
LOG.info("createToolWindows "+project.getName());
ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);

previewPanel = new PreviewPanel(project);
Expand All @@ -136,14 +136,13 @@ public void createToolWindows() {

@Override
public void projectClosed() {
LOG.info("projectClosed "+project.getName());
console.dispose();

ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(project);
for (PreviewState it : grammarToPreviewState.values() ) {
LOG.info("projectClosed still has state " + it.grammarFileName);
synchronized (controller.previewStateLock) {
if (it.editor != null) {
LOG.info("projectClosed still has editor " + it.grammarFileName);
final EditorFactory factory = EditorFactory.getInstance();
factory.releaseEditor(it.editor);
it.editor = null;
Expand All @@ -165,7 +164,7 @@ public String getComponentName() {
// ------------------------------

public void installListeners() {
LOG.info("installListeners");
LOG.info("installListeners "+project.getName());
// Listen for .g4 file saves
VirtualFileManager.getInstance().addVirtualFileListener(
new VirtualFileAdapter() {
Expand All @@ -185,53 +184,57 @@ public void contentsChanged(VirtualFileEvent event) {
public void selectionChanged(FileEditorManagerEvent event) {
currentEditorFileChangedEvent(event.getOldFile(), event.getNewFile());
}

@Override
public void fileClosed(FileEditorManager source, VirtualFile file) {
currentEditorFileClosedEvent(file);
editorFileClosedEvent(file);
}
}
);
// for now let's leave the grammar loading in the selectionChanged event
// as jetbrains says that I should not rely on event order.
// msgBus.subscribe(FileEditorManagerListener.Before.FILE_EDITOR_MANAGER,
// new FileEditorManagerListener.Before.Adapter() {
// @Override
// public void beforeFileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
// fileOpenedEvent(file);
// }
// }
// );
}

/** The test ANTLR rule action triggers this event. This can occur
* only occur when the current editor the showing a grammar, because
* that is the only time that the action is enabled. We will see
* a file changed event when the project loads the first grammar file.
*/
public void setStartRuleNameEvent(String startRuleName) {
LOG.info("setStartRuleNameEvent " + startRuleName);
PreviewState previewState = getPreviewState();
if ( previewState==null ) {
LOG.error("setStartRuleNameEvent called without grammar in current editor; current file="+
getCurrentEditorFile(project));
return;
}
public void setStartRuleNameEvent(VirtualFile grammarFile, String startRuleName) {
LOG.info("setStartRuleNameEvent " + startRuleName+" "+project.getName());
PreviewState previewState = getPreviewState(grammarFile.getPath());
previewState.startRuleName = startRuleName;
if ( previewPanel!=null ) {
previewPanel.setStartRuleName(startRuleName); // notify the view
previewPanel.updateParseTreeFromDoc();
previewPanel.setStartRuleName(grammarFile, startRuleName); // notify the view
previewPanel.updateParseTreeFromDoc(grammarFile);
}
else {
LOG.error("setStartRuleNameEvent called before preview panel created");
}
}

public void grammarFileSavedEvent(VirtualFile vfile) {
LOG.info("grammarFileSavedEvent "+vfile.getPath());
updateGrammarObjectsFromFile(vfile.getPath()); // force reload
public void grammarFileSavedEvent(VirtualFile grammarFile) {
LOG.info("grammarFileSavedEvent "+grammarFile.getPath()+" "+project.getName());
updateGrammarObjectsFromFile(grammarFile.getPath()); // force reload
if ( previewPanel!=null ) {
previewPanel.grammarFileSaved(vfile);
previewPanel.grammarFileSaved(grammarFile);
}
else {
LOG.error("grammarFileSavedEvent called before preview panel created");
}
runANTLRTool(vfile);
runANTLRTool(grammarFile);
}

public void currentEditorFileChangedEvent(VirtualFile oldFile, VirtualFile newFile) {
LOG.info("currentEditorFileChangedEvent "+(oldFile!=null?oldFile.getPath():"none")+
" -> "+(newFile!=null?newFile.getPath():"none"));
" -> "+(newFile!=null?newFile.getPath():"none")+" "+project.getName());
if ( newFile==null ) { // all files must be closed I guess
return;
}
Expand All @@ -253,18 +256,32 @@ public void currentEditorFileChangedEvent(VirtualFile oldFile, VirtualFile newFi
}
}

public void currentEditorFileClosedEvent(VirtualFile vfile) {
// public void fileOpenedEvent(VirtualFile vfile) {
// String grammarFileName = vfile.getPath();
// LOG.info("fileOpenedEvent "+ grammarFileName+" "+project.getName());
// if ( !vfile.getName().endsWith(".g4") ) {
// ApplicationManager.getApplication().invokeLater(
// new Runnable() {
// @Override
// public void run() {
// previewWindow.hide(null);
// }
// }
// );
// }
// }
//
public void editorFileClosedEvent(VirtualFile vfile) {
String grammarFileName = vfile.getPath();
LOG.info("currentEditorFileClosedEvent "+ grammarFileName);
LOG.info("editorFileClosedEvent "+ grammarFileName+" "+project.getName());
if ( !vfile.getName().endsWith(".g4") ) {
previewWindow.hide(null);
return;
}

// Dispose of state, editor, and such for this file
PreviewState previewState = grammarToPreviewState.get(grammarFileName);
if ( previewState==null ) {
LOG.error("currentEditorFileClosedEvent no state for "+ grammarFileName);
if ( previewState==null ) { // project closing must have done already
return;
}

Expand All @@ -276,12 +293,12 @@ public void currentEditorFileClosedEvent(VirtualFile vfile) {
previewWindow.hide(null);
}

public void runANTLRTool(final VirtualFile vfile) {
LOG.info("runANTLRTool launch on "+vfile.getPath());
public void runANTLRTool(final VirtualFile grammarFile) {
LOG.info("runANTLRTool launch on "+grammarFile.getPath()+" "+project.getName());
String title = "ANTLR Code Generation";
boolean canBeCancelled = true;
Task.Backgroundable gen =
new RunANTLROnGrammarFile(new VirtualFile[]{vfile},
new RunANTLROnGrammarFile(grammarFile,
project,
title,
canBeCancelled,
Expand All @@ -306,23 +323,17 @@ public void updateGrammarObjectsFromFile(String grammarFileName) {
}
}

public Object[] parseText(String inputText) throws IOException {
public Object[] parseText(VirtualFile grammarFile, String inputText) throws IOException {
// TODO:Try to reuse the same parser and lexer.
if ( getCurrentGrammarFile()==null ) {
LOG.error("parseText current editor not grammar " + getCurrentEditorFile(project));
return null; // weird. not at a grammar file.
}

VirtualFile currentGrammarFile = getCurrentGrammarFile();
String grammarFileName = currentGrammarFile.getPath();
String grammarFileName = grammarFile.getPath();
PreviewState previewState = getPreviewState(grammarFileName);
if (!new File(grammarFileName).exists()) {
LOG.error("parseText grammar doesn't exit " + grammarFileName);
return null;
}

// Wipes out the console and also any error annotations
previewPanel.clearParseErrors();
previewPanel.clearParseErrors(grammarFile);

if ( previewState.g == BAD_PARSER_GRAMMAR ||
previewState.lg == BAD_LEXER_GRAMMAR )
Expand Down Expand Up @@ -350,7 +361,7 @@ public Object[] parseText(String inputText) throws IOException {
}
ParseTree t = parser.parse(start.index);

previewPanel.showParseErrors(previewState.syntaxErrorListener.getSyntaxErrors());
previewPanel.showParseErrors(grammarFile, previewState.syntaxErrorListener.getSyntaxErrors());

if ( t!=null ) {
return new Object[] {parser, t};
Expand All @@ -360,7 +371,7 @@ public Object[] parseText(String inputText) throws IOException {

/** Get lexer and parser grammars */
public Grammar[] loadGrammars(String grammarFileName) {
LOG.info("loadGrammars open "+grammarFileName);
LOG.info("loadGrammars open "+grammarFileName+" "+project.getName());
Tool antlr = new Tool();
antlr.errMgr = new PluginIgnoreMissingTokensFileErrorManager(antlr);
antlr.errMgr.setFormat("antlr");
Expand All @@ -375,7 +386,7 @@ public Grammar[] loadGrammars(String grammarFileName) {
// so that I can check for an empty AST coming back.
GrammarRootAST grammarRootAST = antlr.parseGrammar(grammarFileName);
if ( grammarRootAST==null ) {
LOG.info("Empty or bad grammar "+grammarFileName);
LOG.info("Empty or bad grammar "+grammarFileName+" "+project.getName());
return null;
}
Grammar g = antlr.createGrammar(grammarRootAST);
Expand Down Expand Up @@ -477,11 +488,6 @@ public ToolWindow getPreviewWindow() {
return previewWindow;
}

public String getInputText() {
if ( previewPanel==null ) return "";
return getPreviewState().editor.getDocument().getText();
}

public @NotNull PreviewState getPreviewState(String grammarFileName) {
// Have we seen this grammar before?
PreviewState stateForCurrentGrammar = grammarToPreviewState.get(grammarFileName);
Expand Down Expand Up @@ -513,6 +519,10 @@ public String getInputText() {
return getPreviewState(currentGrammarFileName);
}

// These "get current editor file" routines should only be used
// when you are sure the user is in control and is viewing the
// right file (i.e., don't use these during project loading etc...)

public static VirtualFile getCurrentEditorFile(Project project) {
FileEditorManager fmgr = FileEditorManager.getInstance(project);
VirtualFile files[] = fmgr.getSelectedFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import org.antlr.intellij.plugin.ANTLRv4PluginController;
import org.antlr.intellij.plugin.configdialogs.ConfigANTLRPerGrammar;

public class ConfigureANTLRAction extends AnAction implements DumbAware {
public static final Logger LOG = Logger.getInstance("ANTLR ConfigureANTLRAction");

@Override
public void update(AnActionEvent e) {
GenerateAction.selectedFileIsGrammar(e);
MyActionUtils.selectedFileIsGrammar(e);
}

@Override
Expand All @@ -24,20 +22,17 @@ public void actionPerformed(AnActionEvent e) {
LOG.error("actionPerformed no project for "+e);
return; // whoa!
}
VirtualFile currentGrammarFile = ANTLRv4PluginController.getCurrentGrammarFile(e.getProject());
LOG.info("actionPerformed "+currentGrammarFile);
VirtualFile[] files = LangDataKeys.VIRTUAL_FILE_ARRAY.getData(e.getDataContext());
if ( files==null || files[0]==null ) return; // no files?
VirtualFile grammarFile = MyActionUtils.getGrammarFileFromEvent(e);
if ( grammarFile==null ) return;
LOG.info("actionPerformed "+grammarFile);

String name = files[0].getPath();
ConfigANTLRPerGrammar configDialog = new ConfigANTLRPerGrammar(e.getProject(), name);
String fileName = files[0].getName();
configDialog.getPeer().setTitle("Configure ANTLR for "+ fileName);
ConfigANTLRPerGrammar configDialog = new ConfigANTLRPerGrammar(e.getProject(), grammarFile.getPath());
configDialog.getPeer().setTitle("Configure ANTLR for "+ grammarFile.getName());

configDialog.show();

if ( configDialog.getExitCode()==DialogWrapper.OK_EXIT_CODE ) {
configDialog.saveValues(e.getProject(), name);
configDialog.saveValues(e.getProject(), grammarFile.getPath());
}
}
}
20 changes: 5 additions & 15 deletions src/java/org/antlr/intellij/plugin/actions/GenerateAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.progress.ProgressManager;
Expand All @@ -11,7 +10,6 @@
import com.intellij.openapi.vcs.changes.BackgroundFromStartOption;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import org.antlr.intellij.plugin.ANTLRv4PluginController;

/** Generate parser from ANTLR grammar;
* learned how to do from Grammar-Kit by Gregory Shrago.
Expand All @@ -21,7 +19,7 @@ public class GenerateAction extends AnAction implements DumbAware {

@Override
public void update(AnActionEvent e) {
selectedFileIsGrammar(e);
MyActionUtils.selectedFileIsGrammar(e);
}

@Override
Expand All @@ -30,10 +28,9 @@ public void actionPerformed(final AnActionEvent e) {
LOG.error("actionPerformed no project for "+e);
return; // whoa!
}
VirtualFile currentGrammarFile = ANTLRv4PluginController.getCurrentGrammarFile(e.getProject());
LOG.info("actionPerformed "+currentGrammarFile);
VirtualFile[] files = LangDataKeys.VIRTUAL_FILE_ARRAY.getData(e.getDataContext());
if ( files==null ) return; // no files?
VirtualFile grammarFile = MyActionUtils.getGrammarFileFromEvent(e);
if ( grammarFile==null ) return;
LOG.info("actionPerformed "+grammarFile);
String title = "ANTLR Code Generation";
boolean canBeCancelled = true;

Expand All @@ -42,18 +39,11 @@ public void actionPerformed(final AnActionEvent e) {
FileDocumentManager.getInstance().saveAllDocuments();

Task.Backgroundable gen =
new RunANTLROnGrammarFile(files,
new RunANTLROnGrammarFile(grammarFile,
e.getProject(),
title,
canBeCancelled,
new BackgroundFromStartOption());
ProgressManager.getInstance().run(gen);
}

public static void selectedFileIsGrammar(AnActionEvent e) {
VirtualFile currentGrammarFile = ANTLRv4PluginController.getCurrentGrammarFile(e.getProject());
boolean grammarFound = currentGrammarFile!=null;
e.getPresentation().setEnabled(grammarFound); // enable action if we're looking at grammar file
e.getPresentation().setVisible(grammarFound);
}
}
24 changes: 24 additions & 0 deletions src/java/org/antlr/intellij/plugin/actions/MyActionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.antlr.intellij.plugin.actions;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.vfs.VirtualFile;

public class MyActionUtils {
public static void selectedFileIsGrammar(AnActionEvent e) {
VirtualFile vfile = getGrammarFileFromEvent(e);
if ( vfile==null ) return;
e.getPresentation().setEnabled(true); // enable action if we're looking at grammar file
e.getPresentation().setVisible(true);
}

public static VirtualFile getGrammarFileFromEvent(AnActionEvent e) {
VirtualFile[] files = LangDataKeys.VIRTUAL_FILE_ARRAY.getData(e.getDataContext());
if ( files==null || files.length==0 ) return null;
VirtualFile vfile = files[0];
if ( vfile!=null && vfile.getName().endsWith(".g4") ) {
return vfile;
}
return null;
}
}
Loading

0 comments on commit 805e5ac

Please sign in to comment.