com.intellij.codeInsight.template.TemplateContextType Java Examples

The following examples show how to use com.intellij.codeInsight.template.TemplateContextType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TemplateContext.java    From consulo with Apache License 2.0 6 votes vote down vote up
Map<TemplateContextType, Boolean> getDifference(@Nullable TemplateContext defaultContext) {
  Map<TemplateContextType, Boolean> result = ContainerUtil.newLinkedHashMap();
  synchronized (myContextStates) {
    //noinspection NestedSynchronizedStatement
    synchronized (defaultContext == null ? myContextStates : defaultContext.myContextStates) {
      for (TemplateContextType contextType : TemplateContextType.EP_NAME.getExtensions()) {
        String context = contextType.getContextId();
        Boolean myStateInContext = myContextStates.get(context);
        if (myStateInContext != null && differsFromDefault(defaultContext, context, myStateInContext)) {
          result.put(contextType, myStateInContext);
        }
      }
    }
  }
  return result;
}
 
Example #2
Source File: LiveTemplateSettingsEditor.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void addContextNode(MultiMap<TemplateContextType, TemplateContextType> hierarchy,
                            CheckedTreeNode parent,
                            TemplateContextType type) {
  final Collection<TemplateContextType> children = hierarchy.get(type);
  final String name = UIUtil.removeMnemonic(type.getPresentableName());
  final CheckedTreeNode node = new CheckedTreeNode(Pair.create(children.isEmpty() ? type : null, name));
  parent.add(node);

  if (children.isEmpty()) {
    node.setChecked(myContext.get(type));
  }
  else {
    for (TemplateContextType child : children) {
      addContextNode(hierarchy, node, child);
    }
    final CheckedTreeNode other = new CheckedTreeNode(Pair.create(type, "Other"));
    other.setChecked(myContext.get(type));
    node.add(other);
  }
}
 
Example #3
Source File: TemplateListPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void createTemplateEditor(final TemplateImpl template,
                                  String shortcut,
                                  Map<TemplateOptionalProcessor, Boolean> options,
                                  Map<TemplateContextType, Boolean> context) {
  myCurrentTemplateEditor = new LiveTemplateSettingsEditor(template, shortcut, options, context, new Runnable() {
    @Override
    public void run() {
      DefaultMutableTreeNode node = getNode(getSingleSelectedIndex());
      if (node != null) {
        ((DefaultTreeModel)myTree.getModel()).nodeChanged(node);
        TemplateSettings.getInstance().setLastSelectedTemplate(template.getGroupName(), template.getKey());
      }
    }
  }, TemplateSettings.getInstance().getTemplate(template.getKey(), template.getGroupName()) != null);
  for (Component component : myDetailsPanel.getComponents()) {
    if (component instanceof LiveTemplateSettingsEditor) {
      myDetailsPanel.remove(component);
    }
  }

  myDetailsPanel.add(myCurrentTemplateEditor, TEMPLATE_SETTINGS);
}
 
Example #4
Source File: LiveTemplateSettingsEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void updateHighlighter() {
  List<TemplateContextType> applicableContexts = getApplicableContexts();
  if (!applicableContexts.isEmpty()) {
    TemplateContext contextByType = new TemplateContext();
    contextByType.setEnabled(applicableContexts.get(0), true);
    TemplateEditorUtil.setHighlighter(myTemplateEditor, contextByType);
    return;
  }
  ((EditorEx) myTemplateEditor).repaint(0, myTemplateEditor.getDocument().getTextLength());
}
 
Example #5
Source File: TemplateContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
void writeTemplateContext(Element element, @Nullable TemplateContext defaultContext) throws WriteExternalException {
  Map<TemplateContextType, Boolean> diff = getDifference(defaultContext);
  for (TemplateContextType type : diff.keySet()) {
    Element optionElement = new Element("option");
    optionElement.setAttribute("name", type.getContextId());
    optionElement.setAttribute("value", diff.get(type).toString());
    element.addContent(optionElement);
  }
}
 
Example #6
Source File: TemplateImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public Map<TemplateContextType, Boolean> createContext(){

    Map<TemplateContextType, Boolean> context = new LinkedHashMap<TemplateContextType, Boolean>();
    for (TemplateContextType processor : TemplateContextType.EP_NAME.getExtensions()) {
      context.put(processor, getTemplateContext().isEnabled(processor));
    }
    return context;

  }
 
Example #7
Source File: TemplateContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
public boolean isEnabled(TemplateContextType contextType) {
  Boolean storedValue = isEnabledBare(contextType);
  if (storedValue == null) {
    TemplateContextType baseContextType = contextType.getBaseContextType();
    if (baseContextType != null && !(baseContextType instanceof EverywhereContextType)) {
      return isEnabled(baseContextType);
    }
    return false;
  }
  return storedValue.booleanValue();
}
 
Example #8
Source File: TemplateListPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void copyRow() {
  int selected = getSingleSelectedIndex();
  if (selected < 0) return;

  TemplateImpl orTemplate = getTemplate(selected);
  LOG.assertTrue(orTemplate != null);
  TemplateImpl template = orTemplate.copy();
  template.setKey(ABBREVIATION);
  myTemplateOptions.put(getKey(template), new HashMap<TemplateOptionalProcessor, Boolean>(getTemplateOptions(orTemplate)));
  myTemplateContext.put(getKey(template), new HashMap<TemplateContextType, Boolean>(getTemplateContext(orTemplate)));
  registerTemplate(template);

  updateTemplateDetails(true);
}
 
Example #9
Source File: TemplateListPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean areContextsEqual(final TemplateImpl newTemplate, final TemplateImpl originalTemplate) {
  Map<TemplateContextType, Boolean> templateContext = getTemplateContext(newTemplate);
  for (TemplateContextType processor : templateContext.keySet()) {
    if (originalTemplate.getTemplateContext().isEnabled(processor) != templateContext.get(processor).booleanValue())
      return false;
  }
  return true;
}
 
Example #10
Source File: LiveTemplateSettingsEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isExpandableFromEditor() {
  boolean hasNonExpandable = false;
  for (TemplateContextType type : getApplicableContexts()) {
    if (type.isExpandableFromEditor()) {
      return true;
    }
    hasNonExpandable = true;
  }
  
  return !hasNonExpandable;
}
 
Example #11
Source File: LiveTemplateSettingsEditor.java    From consulo with Apache License 2.0 5 votes vote down vote up
private List<TemplateContextType> getApplicableContexts() {
  ArrayList<TemplateContextType> result = new ArrayList<TemplateContextType>();
  for (TemplateContextType type : myContext.keySet()) {
    if (myContext.get(type).booleanValue()) {
      result.add(type);
    }
  }
  return result;
}
 
Example #12
Source File: TemplateEditorUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static Document createDocument(CharSequence text, @Nullable Map<TemplateContextType, Boolean> context, Project project) {
  if (context != null) {
    for (Map.Entry<TemplateContextType, Boolean> entry : context.entrySet()) {
      if (entry.getValue()) {
        return entry.getKey().createDocument(text, project);
      }
    }
  }

  return EditorFactory.getInstance().createDocument(text);
}
 
Example #13
Source File: EditVariableDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
public EditVariableDialog(Editor editor, Component parent, ArrayList<Variable> variables, List<TemplateContextType> contextTypes) {
  super(parent, true);
  myContextTypes = contextTypes;
  myVariables = variables;
  myEditor = editor;
  init();
  setTitle(CodeInsightBundle.message("templates.dialog.edit.variables.title"));
  setOKButtonText(CommonBundle.getOkButtonText());
}
 
Example #14
Source File: EditVariableDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
private JComponent createVariablesTable() {
  final String[] names = {
    CodeInsightBundle.message("templates.dialog.edit.variables.table.column.name"),
    CodeInsightBundle.message("templates.dialog.edit.variables.table.column.expression"),
    CodeInsightBundle.message("templates.dialog.edit.variables.table.column.default.value"),
    CodeInsightBundle.message("templates.dialog.edit.variables.table.column.skip.if.defined")
  };

  // Create a model of the data.
  TableModel dataModel = new VariablesModel(names);

  // Create the table
  myTable = new JBTable(dataModel);
  myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  myTable.setPreferredScrollableViewportSize(new Dimension(500, myTable.getRowHeight() * 8));
  myTable.getColumn(names[0]).setPreferredWidth(120);
  myTable.getColumn(names[1]).setPreferredWidth(200);
  myTable.getColumn(names[2]).setPreferredWidth(200);
  myTable.getColumn(names[3]).setPreferredWidth(100);
  if (myVariables.size() > 0) {
    myTable.getSelectionModel().setSelectionInterval(0, 0);
  }

  JComboBox comboField = new JComboBox();
  Macro[] macros = MacroFactory.getMacros();
  Arrays.sort(macros, new Comparator<Macro> () {
    @Override
    public int compare(Macro m1, Macro m2) {
      return m1.getPresentableName().compareTo(m2.getPresentableName());
    }
  });
  eachMacro:
  for (Macro macro : macros) {
    for (TemplateContextType contextType : myContextTypes) {
      if (macro.isAcceptableInContext(contextType)) {
        comboField.addItem(macro.getPresentableName());
        continue eachMacro;
      }
    }
  }
  comboField.setEditable(true);
  DefaultCellEditor cellEditor = new DefaultCellEditor(comboField);
  cellEditor.setClickCountToStart(1);
  myTable.getColumn(names[1]).setCellEditor(cellEditor);
  myTable.setRowHeight(comboField.getPreferredSize().height);

  JTextField textField = new JTextField();

  /*textField.addMouseListener(
    new PopupHandler(){
      public void invokePopup(Component comp,int x,int y){
        showCellPopup((JTextField)comp,x,y);
      }
    }
  );*/

  cellEditor = new DefaultCellEditor(textField);
  cellEditor.setClickCountToStart(1);
  myTable.setDefaultEditor(String.class, cellEditor);

  final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myTable).disableAddAction().disableRemoveAction();
  return decorator.createPanel();
}
 
Example #15
Source File: TemplateImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void applyContext(final Map<TemplateContextType, Boolean> context) {
  for (Map.Entry<TemplateContextType, Boolean> entry : context.entrySet()) {
    getTemplateContext().setEnabled(entry.getKey(), entry.getValue().booleanValue());
  }
}
 
Example #16
Source File: TemplateEditorUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static Editor createEditor(boolean isReadOnly, CharSequence text, @Nullable Map<TemplateContextType, Boolean> context) {
  final Project project = DataManager.getInstance().getDataContext().getData(CommonDataKeys.PROJECT);
  return createEditor(isReadOnly, createDocument(text, context, project), project);
}
 
Example #17
Source File: RmlContextType.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
RmlContextType(@NotNull String id, @NotNull String presentableName, @Nullable Class<? extends TemplateContextType> baseContextType) {
    super(id, presentableName, baseContextType);
}
 
Example #18
Source File: TemplateContext.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void setEnabled(TemplateContextType contextType, boolean value) {
  synchronized (myContextStates) {
    myContextStates.put(contextType.getContextId(), value);
  }
}
 
Example #19
Source File: TemplateContext.java    From consulo with Apache License 2.0 4 votes vote down vote up
private Boolean isEnabledBare(TemplateContextType contextType) {
  synchronized (myContextStates) {
    return myContextStates.get(contextType.getContextId());
  }
}
 
Example #20
Source File: TemplateListPanel.java    From consulo with Apache License 2.0 4 votes vote down vote up
private Map<TemplateContextType, Boolean> getTemplateContext(final TemplateImpl newTemplate) {
  return myTemplateContext.get(getKey(newTemplate));
}
 
Example #21
Source File: LiveTemplateSettingsEditor.java    From consulo with Apache License 2.0 4 votes vote down vote up
private JPanel createShortContextPanel(final boolean allowNoContexts) {
  JPanel panel = new JPanel(new BorderLayout());

  final JLabel ctxLabel = new JLabel();
  final JLabel change = new JLabel();
  change.setForeground(JBColor.BLUE);
  change.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  panel.add(ctxLabel, BorderLayout.CENTER);
  panel.add(change, BorderLayout.EAST);

  final Runnable updateLabel = new Runnable() {
    @Override
    public void run() {
      StringBuilder sb = new StringBuilder();
      String oldPrefix = "";
      for (TemplateContextType type : getApplicableContexts()) {
        final TemplateContextType base = type.getBaseContextType();
        String ownName = UIUtil.removeMnemonic(type.getPresentableName());
        String prefix = "";
        if (base != null && !(base instanceof EverywhereContextType)) {
          prefix = UIUtil.removeMnemonic(base.getPresentableName()) + ": ";
          ownName = StringUtil.decapitalize(ownName);
        }
        if (type instanceof EverywhereContextType) {
          ownName = "Other";
        }
        if (sb.length() > 0) {
          sb.append(oldPrefix.equals(prefix) ? ", " : "; ");
        }
        if (!oldPrefix.equals(prefix)) {
          sb.append(prefix);
          oldPrefix = prefix;
        }
        sb.append(ownName);
      }
      final boolean noContexts = sb.length() == 0;
      ctxLabel.setText((noContexts ? "No applicable contexts" + (allowNoContexts ? "" : " yet") : "Applicable in " + sb.toString()) + ".  ");
      ctxLabel.setForeground(noContexts ? allowNoContexts ? JBColor.GRAY : JBColor.RED : UIUtil.getLabelForeground());
      change.setText(noContexts ? "Define" : "Change");
    }
  };

  new ClickListener() {
    @Override
    public boolean onClick(MouseEvent e, int clickCount) {
      if (disposeContextPopup()) return false;

      final JPanel content = createPopupContextPanel(updateLabel);
      Dimension prefSize = content.getPreferredSize();
      if (myLastSize != null && (myLastSize.width > prefSize.width || myLastSize.height > prefSize.height)) {
        content.setPreferredSize(new Dimension(Math.max(prefSize.width, myLastSize.width), Math.max(prefSize.height, myLastSize.height)));
      }
      myContextPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(content, null).setResizable(true).createPopup();
      myContextPopup.show(new RelativePoint(change, new Point(change.getWidth() , -content.getPreferredSize().height - 10)));
      myContextPopup.addListener(new JBPopupAdapter() {
        @Override
        public void onClosed(LightweightWindowEvent event) {
          myLastSize = content.getSize();
        }
      });
      return true;
    }
  }.installOn(change);

  updateLabel.run();

  return panel;
}
 
Example #22
Source File: BuckCodeContexts.java    From buck with Apache License 2.0 4 votes vote down vote up
BaseTemplateContext(
    @NotNull @NonNls String id,
    @NotNull String presentableName,
    @Nullable Class<? extends TemplateContextType> baseContextType) {
  super(id, presentableName, baseContextType);
}
 
Example #23
Source File: ANTLRLiveTemplateContext.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public ANTLRLiveTemplateContext(@NotNull @NonNls String id,
								@NotNull String presentableName,
								@Nullable Class<? extends TemplateContextType> baseContextType)
{
	super(id, presentableName, baseContextType);
}
 
Example #24
Source File: XQueryContextType.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
protected XQueryContextType(@NotNull @NonNls String id, @NotNull String presentableName,
                            @Nullable Class<? extends TemplateContextType> baseContextType) {
    super(id, presentableName, baseContextType);
}