@NotNull private static PsiElement deleteTemplateKey( @NotNull final PsiFile file, @NotNull final Document document, final int currentOffset, @NotNull final String key) { ApplicationManager.getApplication().assertIsDispatchThread(); final int startOffset = currentOffset - key.length(); ApplicationManager.getApplication() .runWriteAction( new Runnable() { @Override public void run() { CommandProcessor.getInstance() .runUndoTransparentAction( new Runnable() { public void run() { document.deleteString(startOffset, currentOffset); PsiDocumentManager.getInstance(file.getProject()) .commitDocument(document); } }); } }); return CustomTemplateCallback.getContext(file, startOffset > 0 ? startOffset - 1 : startOffset); }
protected static void doWrap( final String selection, final String abbreviation, final CustomTemplateCallback callback) { final ZenCodingGenerator defaultGenerator = findApplicableDefaultGenerator(callback.getContext(), true); assert defaultGenerator != null; ApplicationManager.getApplication() .runWriteAction( new Runnable() { public void run() { CommandProcessor.getInstance() .executeCommand( callback.getProject(), new Runnable() { public void run() { callback.fixInitialState(true); ZenCodingNode node = parse(abbreviation, callback, defaultGenerator); assert node != null; PsiElement context = callback.getContext(); ZenCodingGenerator generator = findApplicableGenerator(node, context, true); List<ZenCodingFilter> filters = getFilters(node, context); EditorModificationUtil.deleteSelectedText(callback.getEditor()); PsiDocumentManager.getInstance(callback.getProject()) .commitAllDocuments(); expand(node, generator, filters, selection, callback); } }, CodeInsightBundle.message("insert.code.template.command"), null); } }); }
public boolean isApplicable(PsiFile file, int offset, boolean wrapping) { WebEditorOptions webEditorOptions = WebEditorOptions.getInstance(); if (!webEditorOptions.isZenCodingEnabled()) { return false; } if (file == null) { return false; } PsiDocumentManager.getInstance(file.getProject()).commitAllDocuments(); PsiElement element = CustomTemplateCallback.getContext(file, offset); return findApplicableDefaultGenerator(element, wrapping) != null; }
@Nullable @Override public String computeTemplateKey(@NotNull CustomTemplateCallback callback) { Editor editor = callback.getEditor(); String key = computeTemplateKeyWithoutContextChecking( editor.getDocument().getCharsSequence(), editor.getCaretModel().getOffset()); if (key == null) return null; return isApplicableTemplate( getTemplateByKey(key), key, callback.getContext().getContainingFile(), editor) ? key : null; }
@Override public void expand(@NotNull final String key, @NotNull final CustomTemplateCallback callback) { ApplicationManager.getApplication().assertIsDispatchThread(); final PostfixTemplate template = getTemplateByKey(key); final Editor editor = callback.getEditor(); final PsiFile file = callback.getContext().getContainingFile(); if (isApplicableTemplate(template, key, file, editor)) { int currentOffset = editor.getCaretModel().getOffset(); PsiElement newContext = deleteTemplateKey(file, editor.getDocument(), currentOffset, key); newContext = addSemicolonIfNeeded( editor, editor.getDocument(), newContext, currentOffset - key.length()); expandTemplate(template, editor, newContext); } else { LOG.error("Template not found by key: " + key); } }
private static Condition<PostfixTemplate> createIsApplicationTemplateFunction( @NotNull String key, @NotNull PsiFile file, @NotNull Editor editor) { int currentOffset = editor.getCaretModel().getOffset(); final int newOffset = currentOffset - key.length(); CharSequence fileContent = editor.getDocument().getCharsSequence(); StringBuilder fileContentWithoutKey = new StringBuilder(); fileContentWithoutKey.append(fileContent.subSequence(0, newOffset)); fileContentWithoutKey.append(fileContent.subSequence(currentOffset, fileContent.length())); PsiFile copyFile = copyFile(file, fileContentWithoutKey); Document copyDocument = copyFile.getViewProvider().getDocument(); if (copyDocument == null) { //noinspection unchecked return Condition.FALSE; } if (isSemicolonNeeded(copyFile, editor)) { fileContentWithoutKey.insert(newOffset, ';'); copyFile = copyFile(file, fileContentWithoutKey); copyDocument = copyFile.getViewProvider().getDocument(); if (copyDocument == null) { //noinspection unchecked return Condition.FALSE; } } final PsiElement context = CustomTemplateCallback.getContext(copyFile, newOffset > 0 ? newOffset - 1 : newOffset); final Document finalCopyDocument = copyDocument; return new Condition<PostfixTemplate>() { @Override public boolean value(PostfixTemplate template) { return template != null && template.isEnabled() && template.isApplicable(context, finalCopyDocument, newOffset); } }; }
private static void expand( String key, @NotNull CustomTemplateCallback callback, String surroundedText, @NotNull ZenCodingGenerator defaultGenerator) { ZenCodingNode node = parse(key, callback, defaultGenerator); assert node != null; if (surroundedText == null) { if (node instanceof TemplateNode) { if (key.equals(((TemplateNode) node).getTemplateToken().getKey()) && callback.findApplicableTemplates(key).size() > 1) { callback.startTemplate(); return; } } callback.deleteTemplateKey(key); } PsiElement context = callback.getContext(); ZenCodingGenerator generator = findApplicableGenerator(node, context, false); List<ZenCodingFilter> filters = getFilters(node, context); expand(node, generator, filters, surroundedText, callback); }
public String computeTemplateKey(@NotNull CustomTemplateCallback callback) { ZenCodingGenerator generator = findApplicableDefaultGenerator(callback.getContext(), false); if (generator == null) return null; return generator.computeTemplateKey(callback); }
public static boolean checkTemplateKey(String inputString, CustomTemplateCallback callback) { ZenCodingGenerator generator = findApplicableDefaultGenerator(callback.getContext(), true); assert generator != null; return checkTemplateKey(inputString, callback, generator); }
public void expand(String key, @NotNull CustomTemplateCallback callback) { ZenCodingGenerator defaultGenerator = findApplicableDefaultGenerator(callback.getContext(), false); assert defaultGenerator != null; expand(key, callback, null, defaultGenerator); }