Search in sources :

Example 1 with RunANTLROnGrammarFile

use of org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile in project intellij-plugin-v4 by antlr.

the class GenerateParserAction method actionPerformed.

@Override
public void actionPerformed(final AnActionEvent e) {
    Project project = e.getData(PlatformDataKeys.PROJECT);
    if (project == null) {
        LOG.error("actionPerformed no project for " + e);
        // whoa!
        return;
    }
    VirtualFile grammarFile = MyActionUtils.getGrammarFileFromEvent(e);
    LOG.info("actionPerformed " + (grammarFile == null ? "NONE" : grammarFile));
    if (grammarFile == null)
        return;
    String title = "ANTLR Code Generation";
    boolean canBeCancelled = true;
    // commit changes to PSI and file system
    PsiDocumentManager psiMgr = PsiDocumentManager.getInstance(project);
    FileDocumentManager docMgr = FileDocumentManager.getInstance();
    Document doc = docMgr.getDocument(grammarFile);
    if (doc == null)
        return;
    boolean unsaved = !psiMgr.isCommitted(doc) || docMgr.isDocumentUnsaved(doc);
    if (unsaved) {
        // save event triggers ANTLR run if autogen on
        psiMgr.commitDocument(doc);
        docMgr.saveDocument(doc);
    }
    // from action, they really mean it
    boolean forceGeneration = true;
    RunANTLROnGrammarFile gen = new RunANTLROnGrammarFile(grammarFile, project, title, canBeCancelled, forceGeneration);
    boolean autogen = ANTLRv4GrammarPropertiesStore.getGrammarProperties(project, grammarFile).shouldAutoGenerateParser();
    if (!unsaved || !autogen) {
        // if everything already saved (not stale) then run ANTLR
        // if had to be saved and autogen NOT on, then run ANTLR
        // Otherwise, the save file event will have or will run ANTLR.
        // , "Generating", canBeCancelled, e.getData(PlatformDataKeys.PROJECT));
        ProgressManager.getInstance().run(gen);
        // refresh from disk to see new files
        Set<File> generatedFiles = new HashSet<>();
        generatedFiles.add(new File(gen.getOutputDirName()));
        LocalFileSystem.getInstance().refreshIoFiles(generatedFiles, true, true, null);
        // pop up a notification
        Notification notification = new Notification(RunANTLROnGrammarFile.groupDisplayId, "parser for " + grammarFile.getName() + " generated", "to " + gen.getOutputDirName(), NotificationType.INFORMATION);
        Notifications.Bus.notify(notification, project);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) RunANTLROnGrammarFile(org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) RunANTLROnGrammarFile(org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile) Notification(com.intellij.notification.Notification) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) HashSet(java.util.HashSet)

Example 2 with RunANTLROnGrammarFile

use of org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile in project intellij-plugin-v4 by antlr.

the class ANTLRv4PluginController method runANTLRTool.

/**
 * Make sure to run after updating grammars in previewState
 */
public void runANTLRTool(final VirtualFile grammarFile) {
    String title = "ANTLR Code Generation";
    boolean canBeCancelled = true;
    boolean forceGeneration = false;
    Task gen = new RunANTLROnGrammarFile(grammarFile, project, title, canBeCancelled, forceGeneration);
    ProgressManager.getInstance().run(gen);
}
Also used : Task(com.intellij.openapi.progress.Task) RunANTLROnGrammarFile(org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile)

Aggregations

RunANTLROnGrammarFile (org.antlr.intellij.plugin.parsing.RunANTLROnGrammarFile)2 Notification (com.intellij.notification.Notification)1 Document (com.intellij.openapi.editor.Document)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 File (java.io.File)1 HashSet (java.util.HashSet)1