@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; }
public void wrap(final String selection, @NotNull final CustomTemplateCallback callback) { InputValidatorEx validator = new InputValidatorEx() { public String getErrorText(String inputString) { if (!checkTemplateKey(inputString, callback)) { return XmlBundle.message("zen.coding.incorrect.abbreviation.error"); } return null; } public boolean checkInput(String inputString) { return getErrorText(inputString) == null; } public boolean canClose(String inputString) { return checkInput(inputString); } }; final String abbreviation = Messages.showInputDialog( callback.getProject(), XmlBundle.message("zen.coding.enter.abbreviation.dialog.label"), XmlBundle.message("zen.coding.title"), Messages.getQuestionIcon(), "", validator); if (abbreviation != null) { doWrap(selection, abbreviation, callback); } }
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); } }); }
@Nullable @Override public String computeTemplateKeyWithoutContextChecking(@NotNull CustomTemplateCallback callback) { Editor editor = callback.getEditor(); return computeTemplateKeyWithoutContextChecking( editor.getDocument().getCharsSequence(), editor.getCaretModel().getOffset()); }
@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); }
@Nullable private ZenCodingNode parseTemplate() { final ZenCodingToken token = nextToken(); String templateKey = isHtml(myCallback) ? DEFAULT_TAG : null; boolean mustHaveSelector = true; if (token instanceof IdentifierToken) { templateKey = ((IdentifierToken) token).getText(); mustHaveSelector = false; myIndex++; } if (templateKey == null) { return null; } final TemplateImpl template = myCallback.findApplicableTemplate(templateKey); if (template == null && !isXML11ValidQName(templateKey)) { return null; } final List<Pair<String, String>> attrList = parseSelectors(); if (mustHaveSelector && attrList.size() == 0) { return null; } final TemplateToken templateToken = new TemplateToken(templateKey, attrList); if (!setTemplate(templateToken, template)) { return null; } return new TemplateNode(templateToken); }
public static boolean isApplicable( @NotNull CustomLiveTemplate customLiveTemplate, @NotNull Editor editor, @NotNull PsiFile file, boolean wrapping) { return customLiveTemplate.isApplicable( file, CustomTemplateCallback.getOffset(editor), wrapping); }
@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 void expand( ZenCodingNode node, ZenCodingGenerator generator, List<ZenCodingFilter> filters, String surroundedText, CustomTemplateCallback callback) { if (surroundedText != null) { surroundedText = surroundedText.trim(); } List<GenerationNode> genNodes = node.expand(-1, surroundedText, callback, true); LiveTemplateBuilder builder = new LiveTemplateBuilder(); int end = -1; for (int i = 0, genNodesSize = genNodes.size(); i < genNodesSize; i++) { GenerationNode genNode = genNodes.get(i); TemplateImpl template = genNode.generate(callback, generator, filters, true); int e = builder.insertTemplate(builder.length(), template, null); if (end == -1 && end < builder.length()) { end = e; } } callback.startTemplate( builder.buildTemplate(), null, new TemplateEditingAdapter() { private TextRange myEndVarRange; private Editor myEditor; @Override public void beforeTemplateFinished(TemplateState state, Template template) { int variableNumber = state.getCurrentVariableNumber(); if (variableNumber >= 0 && template instanceof TemplateImpl) { TemplateImpl t = (TemplateImpl) template; while (variableNumber < t.getVariableCount()) { String varName = t.getVariableNameAt(variableNumber); if (LiveTemplateBuilder.isEndVariable(varName)) { myEndVarRange = state.getVariableRange(varName); myEditor = state.getEditor(); break; } variableNumber++; } } } @Override public void templateFinished(Template template, boolean brokenOff) { if (brokenOff && myEndVarRange != null && myEditor != null) { int offset = myEndVarRange.getStartOffset(); if (offset >= 0 && offset != myEditor.getCaretModel().getOffset()) { myEditor.getCaretModel().moveToOffset(offset); myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } } } }); }
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; }
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); }
@NotNull private static XmlFile parseXmlFileInTemplate( String templateString, CustomTemplateCallback callback, boolean createPhysicalFile) { XmlFile xmlFile = (XmlFile) PsiFileFactory.getInstance(callback.getProject()) .createFileFromText( "dummy.xml", StdFileTypes.XML, templateString, LocalTimeCounter.currentTime(), createPhysicalFile); VirtualFile vFile = xmlFile.getVirtualFile(); if (vFile != null) { vFile.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE); } return xmlFile; }
@Nullable protected ZenCodingNode parseTemplate() { ZenCodingToken token = getToken(); if (!(token instanceof IdentifierToken)) { return null; } String templateKey = ((IdentifierToken) token).getText(); advance(); TemplateImpl template = myCallback.findApplicableTemplate(templateKey); if (template == null && !ZenCodingUtil.isXML11ValidQName(templateKey)) { return null; } final TemplateToken templateToken = new TemplateToken(templateKey); if (!setTemplate(templateToken, template)) { return null; } return new TemplateNode(templateToken); }
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 boolean isHtml(CustomTemplateCallback callback) { FileType type = callback.getFileType(); return type == StdFileTypes.HTML || type == StdFileTypes.XHTML; }
public String computeTemplateKey(@NotNull CustomTemplateCallback callback) { ZenCodingGenerator generator = findApplicableDefaultGenerator(callback.getContext(), false); if (generator == null) return null; return generator.computeTemplateKey(callback); }
public void expand(String key, @NotNull CustomTemplateCallback callback) { ZenCodingGenerator defaultGenerator = findApplicableDefaultGenerator(callback.getContext(), false); assert defaultGenerator != null; expand(key, callback, null, defaultGenerator); }
public static boolean checkTemplateKey(String inputString, CustomTemplateCallback callback) { ZenCodingGenerator generator = findApplicableDefaultGenerator(callback.getContext(), true); assert generator != null; return checkTemplateKey(inputString, callback, generator); }