Search in sources :

Example 1 with CustomLiveTemplate

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

the class ListTemplatesHandler method getCustomTemplatesLookupItems.

public static MultiMap<String, CustomLiveTemplateLookupElement> getCustomTemplatesLookupItems(@NotNull Editor editor, @NotNull PsiFile file, int offset) {
    final MultiMap<String, CustomLiveTemplateLookupElement> result = MultiMap.create();
    CustomTemplateCallback customTemplateCallback = new CustomTemplateCallback(editor, file);
    for (CustomLiveTemplate customLiveTemplate : TemplateManagerImpl.listApplicableCustomTemplates(editor, file, false)) {
        if (customLiveTemplate instanceof CustomLiveTemplateBase) {
            String customTemplatePrefix = ((CustomLiveTemplateBase) customLiveTemplate).computeTemplateKeyWithoutContextChecking(customTemplateCallback);
            if (customTemplatePrefix != null) {
                result.putValues(customTemplatePrefix, ((CustomLiveTemplateBase) customLiveTemplate).getLookupElements(file, editor, offset));
            }
        }
    }
    return result;
}
Also used : CustomLiveTemplate(com.intellij.codeInsight.template.CustomLiveTemplate) CustomTemplateCallback(com.intellij.codeInsight.template.CustomTemplateCallback) CustomLiveTemplateBase(com.intellij.codeInsight.template.CustomLiveTemplateBase)

Example 2 with CustomLiveTemplate

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

the class LiveTemplateCompletionContributor method showCustomLiveTemplates.

private static void showCustomLiveTemplates(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
    PsiFile file = parameters.getPosition().getContainingFile();
    Editor editor = parameters.getEditor();
    for (CustomLiveTemplate customLiveTemplate : TemplateManagerImpl.listApplicableCustomTemplates(editor, file, false)) {
        if (customLiveTemplate instanceof CustomLiveTemplateBase) {
            ((CustomLiveTemplateBase) customLiveTemplate).addCompletions(parameters, result);
        }
    }
}
Also used : CustomLiveTemplate(com.intellij.codeInsight.template.CustomLiveTemplate) PsiFile(com.intellij.psi.PsiFile) CustomLiveTemplateBase(com.intellij.codeInsight.template.CustomLiveTemplateBase) Editor(com.intellij.openapi.editor.Editor)

Example 3 with CustomLiveTemplate

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

the class SurroundWithTemplateHandler method createActionGroup.

@Nullable
public static DefaultActionGroup createActionGroup(Project project, Editor editor, PsiFile file) {
    if (!editor.getSelectionModel().hasSelection()) {
        editor.getSelectionModel().selectLineAtCaret();
        if (!editor.getSelectionModel().hasSelection())
            return null;
    }
    List<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
    List<TemplateImpl> templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true);
    if (templates.isEmpty() && customTemplates.isEmpty()) {
        HintManager.getInstance().showErrorHint(editor, CodeInsightBundle.message("templates.surround.no.defined"));
        return null;
    }
    Set<Character> usedMnemonicsSet = new HashSet<>();
    DefaultActionGroup group = new DefaultActionGroup();
    for (TemplateImpl template : templates) {
        group.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet));
    }
    for (CustomLiveTemplate customTemplate : customTemplates) {
        group.add(new WrapWithCustomTemplateAction(customTemplate, editor, file, usedMnemonicsSet));
    }
    return group;
}
Also used : CustomLiveTemplate(com.intellij.codeInsight.template.CustomLiveTemplate) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with CustomLiveTemplate

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

the class SurroundWithHandler method doBuildSurroundActions.

@Nullable
private static List<AnAction> doBuildSurroundActions(Project project, Editor editor, PsiFile file, Map<Surrounder, PsiElement[]> surrounders) {
    final List<AnAction> applicable = new ArrayList<>();
    boolean hasEnabledSurrounders = false;
    Set<Character> usedMnemonicsSet = new HashSet<>();
    int index = 0;
    for (Map.Entry<Surrounder, PsiElement[]> entry : surrounders.entrySet()) {
        Surrounder surrounder = entry.getKey();
        PsiElement[] elements = entry.getValue();
        if (surrounder.isApplicable(elements)) {
            char mnemonic;
            if (index < 9) {
                mnemonic = (char) ('0' + index + 1);
            } else if (index == 9) {
                mnemonic = '0';
            } else {
                mnemonic = (char) ('A' + index - 10);
            }
            index++;
            usedMnemonicsSet.add(Character.toUpperCase(mnemonic));
            applicable.add(new InvokeSurrounderAction(surrounder, project, editor, elements, mnemonic));
            hasEnabledSurrounders = true;
        }
    }
    List<CustomLiveTemplate> customTemplates = TemplateManagerImpl.listApplicableCustomTemplates(editor, file, true);
    List<TemplateImpl> templates = TemplateManagerImpl.listApplicableTemplateWithInsertingDummyIdentifier(editor, file, true);
    if (!templates.isEmpty() || !customTemplates.isEmpty()) {
        applicable.add(new Separator("Live templates"));
    }
    for (TemplateImpl template : templates) {
        applicable.add(new InvokeTemplateAction(template, editor, project, usedMnemonicsSet));
        hasEnabledSurrounders = true;
    }
    for (CustomLiveTemplate customTemplate : customTemplates) {
        applicable.add(new WrapWithCustomTemplateAction(customTemplate, editor, file, usedMnemonicsSet));
        hasEnabledSurrounders = true;
    }
    if (!templates.isEmpty() || !customTemplates.isEmpty()) {
        applicable.add(Separator.getInstance());
        applicable.add(new ConfigureTemplatesAction());
    }
    return hasEnabledSurrounders ? applicable : null;
}
Also used : TemplateImpl(com.intellij.codeInsight.template.impl.TemplateImpl) CustomLiveTemplate(com.intellij.codeInsight.template.CustomLiveTemplate) InvokeTemplateAction(com.intellij.codeInsight.template.impl.InvokeTemplateAction) WrapWithCustomTemplateAction(com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction) Surrounder(com.intellij.lang.surroundWith.Surrounder) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CustomLiveTemplate (com.intellij.codeInsight.template.CustomLiveTemplate)4 CustomLiveTemplateBase (com.intellij.codeInsight.template.CustomLiveTemplateBase)2 Nullable (org.jetbrains.annotations.Nullable)2 CustomTemplateCallback (com.intellij.codeInsight.template.CustomTemplateCallback)1 InvokeTemplateAction (com.intellij.codeInsight.template.impl.InvokeTemplateAction)1 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 WrapWithCustomTemplateAction (com.intellij.codeInsight.template.impl.WrapWithCustomTemplateAction)1 Surrounder (com.intellij.lang.surroundWith.Surrounder)1 DefaultActionGroup (com.intellij.openapi.actionSystem.DefaultActionGroup)1 Editor (com.intellij.openapi.editor.Editor)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 HashSet (java.util.HashSet)1