Search in sources :

Example 6 with TemplateContextType

use of com.intellij.codeInsight.template.TemplateContextType in project intellij-community by JetBrains.

the class LiveTemplateSettingsEditor method addContextNode.

private static void addContextNode(MultiMap<TemplateContextType, TemplateContextType> hierarchy, CheckedTreeNode parent, TemplateContextType type, TemplateContext context) {
    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(context.isEnabled(type));
    } else {
        for (TemplateContextType child : children) {
            addContextNode(hierarchy, node, child, context);
        }
        final CheckedTreeNode other = new CheckedTreeNode(Pair.create(type, "Other"));
        other.setChecked(context.isEnabled(type));
        node.add(other);
    }
}
Also used : TemplateContextType(com.intellij.codeInsight.template.TemplateContextType)

Example 7 with TemplateContextType

use of com.intellij.codeInsight.template.TemplateContextType in project intellij-community by JetBrains.

the class LiveTemplateSettingsEditor method createShortContextPanel.

private JPanel createShortContextPanel(final boolean allowNoContexts) {
    JPanel panel = new JPanel(new BorderLayout());
    final JLabel ctxLabel = new JLabel();
    final JLabel change = new JLabel();
    change.setForeground(PlatformColors.BLUE);
    change.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    panel.add(ctxLabel, BorderLayout.CENTER);
    panel.add(change, BorderLayout.EAST);
    final Runnable updateLabel = () -> {
        myExpandByCombo.setEnabled(isExpandableFromEditor());
        updateHighlighter();
        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);
        }
        String contexts = "Applicable in " + sb.toString();
        change.setText("Change");
        final boolean noContexts = sb.length() == 0;
        if (noContexts) {
            if (!allowNoContexts) {
                ctxLabel.setForeground(JBColor.RED);
            }
            contexts = "No applicable contexts" + (allowNoContexts ? "" : " yet");
            ctxLabel.setIcon(AllIcons.General.BalloonWarning);
            change.setText("Define");
        } else {
            ctxLabel.setForeground(UIUtil.getLabelForeground());
            ctxLabel.setIcon(null);
        }
        ctxLabel.setText(StringUtil.first(contexts + ". ", 100, true));
        myTemplateOptionsPanel.removeAll();
        myTemplateOptionsPanel.add(createTemplateOptionsPanel());
    };
    new ClickListener() {

        @Override
        public boolean onClick(@NotNull MouseEvent e, int clickCount) {
            if (disposeContextPopup())
                return false;
            final JPanel content = createPopupContextPanel(updateLabel, myContext);
            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;
}
Also used : EverywhereContextType(com.intellij.codeInsight.template.EverywhereContextType) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) TemplateContextType(com.intellij.codeInsight.template.TemplateContextType)

Example 8 with TemplateContextType

use of com.intellij.codeInsight.template.TemplateContextType in project intellij-community by JetBrains.

the class TemplateContext method writeTemplateContext.

@VisibleForTesting
@Nullable
public Element writeTemplateContext(@Nullable TemplateContext defaultContext, @NotNull Lazy<Map<String, TemplateContextType>> idToType) {
    if (myContextStates.isEmpty()) {
        return null;
    }
    Element element = new Element(TemplateSettings.CONTEXT);
    List<Map.Entry<String, Boolean>> entries = new ArrayList<>(myContextStates.entrySet());
    entries.sort(Comparator.comparing(Map.Entry::getKey));
    for (Map.Entry<String, Boolean> entry : entries) {
        Boolean ownValue = entry.getValue();
        if (ownValue == null) {
            continue;
        }
        TemplateContextType type = idToType.getValue().get(entry.getKey());
        if (type == null) {
            // https://youtrack.jetbrains.com/issue/IDEA-155623#comment=27-1721029
            JdomKt.addOptionTag(element, entry.getKey(), ownValue.toString());
        } else if (isValueChanged(ownValue, type, defaultContext)) {
            JdomKt.addOptionTag(element, type.getContextId(), ownValue.toString());
        }
    }
    return element;
}
Also used : Element(org.jdom.Element) THashMap(gnu.trove.THashMap) TemplateContextType(com.intellij.codeInsight.template.TemplateContextType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

TemplateContextType (com.intellij.codeInsight.template.TemplateContextType)8 RelativePoint (com.intellij.ui.awt.RelativePoint)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 OffsetKey (com.intellij.codeInsight.completion.OffsetKey)1 OffsetsInFile (com.intellij.codeInsight.completion.OffsetsInFile)1 EverywhereContextType (com.intellij.codeInsight.template.EverywhereContextType)1 Template (com.intellij.codeInsight.template.Template)1 TemplateManager (com.intellij.codeInsight.template.TemplateManager)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 RangeMarker (com.intellij.openapi.editor.RangeMarker)1 PlainSyntaxHighlighter (com.intellij.openapi.fileTypes.PlainSyntaxHighlighter)1 SyntaxHighlighter (com.intellij.openapi.fileTypes.SyntaxHighlighter)1 Project (com.intellij.openapi.project.Project)1 JBPopupAdapter (com.intellij.openapi.ui.popup.JBPopupAdapter)1 LightweightWindowEvent (com.intellij.openapi.ui.popup.LightweightWindowEvent)1 Pair (com.intellij.openapi.util.Pair)1 TextRange (com.intellij.openapi.util.TextRange)1