Search in sources :

Example 1 with CustomTemplateCallback

use of com.intellij.codeInsight.template.CustomTemplateCallback 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 CustomTemplateCallback

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

the class PostfixLiveTemplate method getLookupElements.

@NotNull
@Override
public Collection<? extends CustomLiveTemplateLookupElement> getLookupElements(@NotNull PsiFile file, @NotNull Editor editor, int offset) {
    Collection<CustomLiveTemplateLookupElement> result = ContainerUtil.newHashSet();
    CustomTemplateCallback callback = new CustomTemplateCallback(editor, file);
    for (PostfixTemplateProvider provider : LanguagePostfixTemplate.LANG_EP.allForLanguage(getLanguage(callback))) {
        String key = computeTemplateKeyWithoutContextChecking(callback);
        if (key != null && editor.getCaretModel().getCaretCount() == 1) {
            Condition<PostfixTemplate> isApplicationTemplateFunction = createIsApplicationTemplateFunction(provider, key, file, editor);
            for (PostfixTemplate postfixTemplate : provider.getTemplates()) {
                if (isApplicationTemplateFunction.value(postfixTemplate)) {
                    result.add(new PostfixTemplateLookupElement(this, postfixTemplate, postfixTemplate.getKey(), provider, false));
                }
            }
        }
    }
    return result;
}
Also used : CustomLiveTemplateLookupElement(com.intellij.codeInsight.template.impl.CustomLiveTemplateLookupElement) PostfixTemplateLookupElement(com.intellij.codeInsight.template.postfix.completion.PostfixTemplateLookupElement) CustomTemplateCallback(com.intellij.codeInsight.template.CustomTemplateCallback) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with CustomTemplateCallback

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

the class PostfixTemplatesCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    Editor editor = parameters.getEditor();
    if (!isCompletionEnabled(parameters) || LiveTemplateCompletionContributor.shouldShowAllTemplates() || editor.getCaretModel().getCaretCount() != 1) {
        /*
        disabled or covered with {@link com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor}
       */
        return;
    }
    PsiFile originalFile = parameters.getOriginalFile();
    PostfixLiveTemplate postfixLiveTemplate = getPostfixLiveTemplate(originalFile, editor);
    if (postfixLiveTemplate != null) {
        postfixLiveTemplate.addCompletions(parameters, result.withPrefixMatcher(new MyPrefixMatcher(result.getPrefixMatcher().getPrefix())));
        String possibleKey = postfixLiveTemplate.computeTemplateKeyWithoutContextChecking(new CustomTemplateCallback(editor, originalFile));
        if (possibleKey != null) {
            result = result.withPrefixMatcher(possibleKey);
            result.restartCompletionOnPrefixChange(StandardPatterns.string().oneOf(postfixLiveTemplate.getAllTemplateKeys(originalFile, parameters.getOffset())));
        }
    }
}
Also used : PostfixTemplateCompletionContributor.getPostfixLiveTemplate(com.intellij.codeInsight.template.postfix.completion.PostfixTemplateCompletionContributor.getPostfixLiveTemplate) PostfixLiveTemplate(com.intellij.codeInsight.template.postfix.templates.PostfixLiveTemplate) PsiFile(com.intellij.psi.PsiFile) CustomTemplateCallback(com.intellij.codeInsight.template.CustomTemplateCallback) Editor(com.intellij.openapi.editor.Editor)

Example 4 with CustomTemplateCallback

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

the class WrapWithCustomTemplateAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final Document document = myEditor.getDocument();
    final VirtualFile file = FileDocumentManager.getInstance().getFile(document);
    if (file != null) {
        ReadonlyStatusHandler.getInstance(myFile.getProject()).ensureFilesWritable(file);
    }
    String selection = myEditor.getSelectionModel().getSelectedText(true);
    if (selection != null) {
        selection = selection.trim();
        PsiDocumentManager.getInstance(myFile.getProject()).commitAllDocuments();
        myTemplate.wrap(selection, new CustomTemplateCallback(myEditor, myFile));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) CustomTemplateCallback(com.intellij.codeInsight.template.CustomTemplateCallback) Document(com.intellij.openapi.editor.Document)

Aggregations

CustomTemplateCallback (com.intellij.codeInsight.template.CustomTemplateCallback)4 CustomLiveTemplate (com.intellij.codeInsight.template.CustomLiveTemplate)1 CustomLiveTemplateBase (com.intellij.codeInsight.template.CustomLiveTemplateBase)1 CustomLiveTemplateLookupElement (com.intellij.codeInsight.template.impl.CustomLiveTemplateLookupElement)1 PostfixTemplateCompletionContributor.getPostfixLiveTemplate (com.intellij.codeInsight.template.postfix.completion.PostfixTemplateCompletionContributor.getPostfixLiveTemplate)1 PostfixTemplateLookupElement (com.intellij.codeInsight.template.postfix.completion.PostfixTemplateLookupElement)1 PostfixLiveTemplate (com.intellij.codeInsight.template.postfix.templates.PostfixLiveTemplate)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 NotNull (org.jetbrains.annotations.NotNull)1