Search in sources :

Example 1 with Injectable

use of com.intellij.psi.injection.Injectable in project intellij-community by JetBrains.

the class InjectLanguageAction method getAllInjectables.

public static List<Injectable> getAllInjectables() {
    Language[] languages = InjectedLanguage.getAvailableLanguages();
    List<Injectable> list = new ArrayList<>();
    for (Language language : languages) {
        list.add(Injectable.fromLanguage(language));
    }
    list.addAll(Arrays.asList(ReferenceInjector.EXTENSION_POINT_NAME.getExtensions()));
    Collections.sort(list);
    return list;
}
Also used : Injectable(com.intellij.psi.injection.Injectable) Language(com.intellij.lang.Language) PlainTextLanguage(com.intellij.openapi.fileTypes.PlainTextLanguage) ArrayList(java.util.ArrayList)

Example 2 with Injectable

use of com.intellij.psi.injection.Injectable in project intellij-community by JetBrains.

the class InjectLanguageAction method doChooseLanguageToInject.

public static boolean doChooseLanguageToInject(Editor editor, final Processor<Injectable> onChosen) {
    final List<Injectable> injectables = getAllInjectables();
    final JList list = new JBList(injectables);
    list.setCellRenderer(new ColoredListCellRendererWrapper<Injectable>() {

        @Override
        protected void doCustomize(JList list, Injectable language, int index, boolean selected, boolean hasFocus) {
            setIcon(language.getIcon());
            append(language.getDisplayName());
            String description = language.getAdditionalDescription();
            if (description != null) {
                append(description, SimpleTextAttributes.GRAYED_ATTRIBUTES);
            }
        }
    });
    Dimension minSize = new JLabel(PlainTextLanguage.INSTANCE.getDisplayName(), EmptyIcon.ICON_16, SwingConstants.LEFT).getMinimumSize();
    minSize.height *= 4;
    list.setMinimumSize(minSize);
    JBPopup popup = new PopupChooserBuilder(list).setItemChoosenCallback(() -> {
        Injectable value = (Injectable) list.getSelectedValue();
        if (value != null) {
            onChosen.process(value);
            PropertiesComponent.getInstance().setValue(LAST_INJECTED_LANGUAGE, value.getId());
        }
    }).setFilteringEnabled(language -> ((Injectable) language).getDisplayName()).setMinSize(minSize).createPopup();
    final String lastInjected = PropertiesComponent.getInstance().getValue(LAST_INJECTED_LANGUAGE);
    if (lastInjected != null) {
        Injectable injectable = ContainerUtil.find(injectables, injectable1 -> lastInjected.equals(injectable1.getId()));
        list.setSelectedValue(injectable, true);
    }
    popup.showInBestPositionFor(editor);
    return true;
}
Also used : Injectable(com.intellij.psi.injection.Injectable) JBList(com.intellij.ui.components.JBList) JBPopup(com.intellij.openapi.ui.popup.JBPopup) PopupChooserBuilder(com.intellij.openapi.ui.popup.PopupChooserBuilder)

Aggregations

Injectable (com.intellij.psi.injection.Injectable)2 Language (com.intellij.lang.Language)1 PlainTextLanguage (com.intellij.openapi.fileTypes.PlainTextLanguage)1 JBPopup (com.intellij.openapi.ui.popup.JBPopup)1 PopupChooserBuilder (com.intellij.openapi.ui.popup.PopupChooserBuilder)1 JBList (com.intellij.ui.components.JBList)1 ArrayList (java.util.ArrayList)1