public void closeGrammar(VirtualFile grammarFile) {
    String grammarFileName = grammarFile.getPath();
    LOG.info("closeGrammar " + grammarFileName + " " + project.getName());

    inputPanel.resetStartRuleLabel();
    inputPanel.clearErrorConsole();
    clearParseTree(); // wipe tree

    ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(project);
    PreviewState previewState = controller.getPreviewState(grammarFile);
    inputPanel.releaseEditor(previewState);
  }
 public void updateParseTreeFromDoc(VirtualFile grammarFile) {
   ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(project);
   PreviewState previewState = controller.getPreviewState(grammarFile);
   LOG.info("updateParseTreeFromDoc " + grammarFile + " rule " + previewState.startRuleName);
   try {
     final String inputText = previewState.getEditor().getDocument().getText();
     ParsingResult results = controller.parseText(grammarFile, inputText);
     if (results != null) {
       updateTreeViewer(previewState, results);
     } else if (previewState.startRuleName == null) {
       indicateNoStartRuleInParseTreePane();
     } else {
       indicateInvalidGrammarInParseTreePane();
     }
   } catch (IOException ioe) {
     ioe.printStackTrace();
   }
 }
  /** Notify the preview tool window contents that the grammar file has changed */
  public void grammarFileSaved(VirtualFile grammarFile) {
    String grammarFileName = grammarFile.getPath();
    LOG.info("grammarFileSaved " + grammarFileName + " " + project.getName());
    ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(project);
    PreviewState previewState = controller.getPreviewState(grammarFile);

    ensureStartRuleExists(grammarFile);
    inputPanel.grammarFileSaved(grammarFile);

    // if the saved grammar is not a pure lexer and there is a start rule, reparse
    // means that switching grammars must refresh preview
    if (previewState.g != null && previewState.startRuleName != null) {
      updateParseTreeFromDoc(previewState.grammarFile);
    } else {
      setParseTree(Collections.<String>emptyList(), null); // blank tree
    }

    profilerPanel.grammarFileSaved(previewState, grammarFile);
  }
 public void ensureStartRuleExists(VirtualFile grammarFile) {
   PreviewState previewState =
       ANTLRv4PluginController.getInstance(project).getPreviewState(grammarFile);
   // if start rule no longer exists, reset display/state.
   if (previewState.g != ParsingUtils.BAD_PARSER_GRAMMAR && previewState.startRuleName != null) {
     Rule rule = previewState.g.getRule(previewState.startRuleName);
     if (rule == null) {
       previewState.startRuleName = null;
       inputPanel.resetStartRuleLabel();
     }
   }
 }
  /** Load grammars and set editor component. */
  public void switchToGrammar(VirtualFile oldFile, VirtualFile grammarFile) {
    String grammarFileName = grammarFile.getPath();
    LOG.info("switchToGrammar " + grammarFileName + " " + project.getName());
    ANTLRv4PluginController controller = ANTLRv4PluginController.getInstance(project);
    PreviewState previewState = controller.getPreviewState(grammarFile);

    inputPanel.switchToGrammar(grammarFile);

    if (previewState.startRuleName != null) {
      updateParseTreeFromDoc(grammarFile);
    } else {
      setParseTree(Collections.<String>emptyList(), null); // blank tree
    }

    profilerPanel.switchToGrammar(previewState, grammarFile);

    if (previewState.g == null && previewState.lg != null) {
      setEnabled(false);
    } else {
      setEnabled(true);
    }
  }