@Override public void performPaste(@NotNull DataContext dataContext) { TemplateGroup group = myConfigurable.getSingleSelectedGroup(); assert group != null; String buffer = CopyPasteManager.getInstance().getContents(DataFlavor.stringFlavor); assert buffer != null; try { for (Element templateElement : JDOMUtil.load(new StringReader("<root>" + buffer + "</root>")) .getChildren(TemplateSettings.TEMPLATE)) { TemplateImpl template = TemplateSettings.readTemplateFromElement( group.getName(), templateElement, getClass().getClassLoader()); while (group.containsTemplate(template.getKey(), template.getId())) { template.setKey(template.getKey() + "1"); if (template.getId() != null) { template.setId(template.getId() + "1"); } } myConfigurable.addTemplate(template); } } catch (JDOMException ignore) { } catch (IOException ignore) { } }
private static List<TemplateImpl> listApplicableTemplates(PsiFile file, int offset) { Set<TemplateContextType> contextTypes = TemplateManagerImpl.getApplicableContextTypes(file, offset); final ArrayList<TemplateImpl> result = ContainerUtil.newArrayList(); for (final TemplateImpl template : TemplateSettings.getInstance().getTemplates()) { if (!template.isDeactivated() && TemplateManagerImpl.isApplicable(template, contextTypes)) { result.add(template); } } return result; }
public static List<TemplateImpl> listApplicableTemplates( PsiFile file, int offset, boolean selectionOnly) { Set<TemplateContextType> contextTypes = getApplicableContextTypes(file, offset); final ArrayList<TemplateImpl> result = ContainerUtil.newArrayList(); for (final TemplateImpl template : TemplateSettings.getInstance().getTemplates()) { if (!template.isDeactivated() && (!selectionOnly || template.isSelectionTemplate()) && isApplicable(template, contextTypes)) { result.add(template); } } return result; }
public static List<TemplateImpl> findMatchingTemplates( CharSequence text, int caretOffset, @Nullable Character shortcutChar, TemplateSettings settings, boolean hasArgument) { List<TemplateImpl> candidates = Collections.emptyList(); for (int i = settings.getMaxKeyLength(); i >= 1; i--) { int wordStart = caretOffset - i; if (wordStart < 0) { continue; } String key = text.subSequence(wordStart, caretOffset).toString(); if (Character.isJavaIdentifierStart(key.charAt(0))) { if (wordStart > 0 && Character.isJavaIdentifierPart(text.charAt(wordStart - 1))) { continue; } } candidates = settings.collectMatchingCandidates(key, shortcutChar, hasArgument); if (!candidates.isEmpty()) break; } return candidates; }
@Nullable public Runnable prepareTemplate( final Editor editor, char shortcutChar, @Nullable final PairProcessor<String, String> processor) { if (editor.getSelectionModel().hasSelection()) { return null; } PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, myProject); if (file == null) return null; Map<TemplateImpl, String> template2argument = findMatchingTemplates(file, editor, shortcutChar, TemplateSettings.getInstance()); List<CustomLiveTemplate> customCandidates = ContainerUtil.findAll( CustomLiveTemplate.EP_NAME.getExtensions(), customLiveTemplate -> shortcutChar == customLiveTemplate.getShortcut() && (editor.getCaretModel().getCaretCount() <= 1 || supportsMultiCaretMode(customLiveTemplate))); if (!customCandidates.isEmpty()) { int caretOffset = editor.getCaretModel().getOffset(); PsiFile fileCopy = insertDummyIdentifierIfNeeded(file, caretOffset, caretOffset, ""); Document document = editor.getDocument(); for (final CustomLiveTemplate customLiveTemplate : customCandidates) { if (isApplicable(customLiveTemplate, editor, fileCopy)) { final String key = customLiveTemplate.computeTemplateKey(new CustomTemplateCallback(editor, fileCopy)); if (key != null) { int offsetBeforeKey = caretOffset - key.length(); CharSequence text = document.getImmutableCharSequence(); if (template2argument == null || !containsTemplateStartingBefore( template2argument, offsetBeforeKey, caretOffset, text)) { return () -> customLiveTemplate.expand(key, new CustomTemplateCallback(editor, file)); } } } } } return startNonCustomTemplates(template2argument, editor, processor); }