private static List<TemplateContextType> getContextTypes() { List<TemplateContextType> contextTypes = new ArrayList<>(); contextTypes.add(new CodeTemplateContextType(CATCHBLOCK_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(METHODBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(CONSTRUCTORBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(GETTERBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(SETTERBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(NEWTYPE_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(CLASSBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(INTERFACEBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(ENUMBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(ANNOTATIONBODY_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(FILECOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(TYPECOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(FIELDCOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(METHODCOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(CONSTRUCTORCOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(OVERRIDECOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(DELEGATECOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(GETTERCOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(SETTERCOMMENT_CONTEXTTYPE)); contextTypes.add(new CodeTemplateContextType(CATCHBODY_CONTEXTTYPE)); return contextTypes; }
@Override protected void createTemplates(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) { if (templateContext.getContextType().getId().equals("com.avaloq.tools.ddk.checkcfg.CheckCfg.ConfiguredCheck")) { //$NON-NLS-1$ addConfiguredCheckTemplates(templateContext, context, acceptor); return; } else if (templateContext.getContextType().getId().equals("com.avaloq.tools.ddk.checkcfg.CheckCfg.kw_catalog")) { //$NON-NLS-1$ addCatalogConfigurations(templateContext, context, acceptor); } TemplateContextType contextType = templateContext.getContextType(); Template[] templates = templateStore.getTemplates(contextType.getId()); for (Template template : templates) { if (!acceptor.canAcceptMoreTemplates()) { return; } if (validate(template, templateContext)) { acceptor.accept(createProposal(template, templateContext, context, getImage(template), getRelevance(template))); } } }
public String getHoverInfo(ITextViewer textViewer, IRegion subject) { try { IDocument doc= textViewer.getDocument(); int offset= subject.getOffset(); if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$ String varName= doc.get(offset, subject.getLength()); TemplateContextType contextType= fProcessor.getContextType(); if (contextType != null) { Iterator iter= contextType.resolvers(); while (iter.hasNext()) { TemplateVariableResolver var= (TemplateVariableResolver) iter.next(); if (varName.equals(var.getType())) { return var.getDescription(); } } } } } catch (BadLocationException e) { } return null; }
public String getColumnText(Object element, int columnIndex) { TemplatePersistenceData data = (TemplatePersistenceData) element; Template template= data.getTemplate(); switch (columnIndex) { case 0: return template.getName(); case 1: TemplateContextType type= fContextTypeRegistry.getContextType(template.getContextTypeId()); if (type != null) return type.getName(); return template.getContextTypeId(); case 2: return template.getDescription(); case 3: return template.isAutoInsertable() ? TemplatesMessages.TemplatePreferencePage_on : ""; //$NON-NLS-1$ default: return ""; //$NON-NLS-1$ } }
private void add() { Iterator it= fContextTypeRegistry.contextTypes(); if (it.hasNext()) { Template template= new Template("", "", ((TemplateContextType) it.next()).getId(), "", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ Template newTemplate= editTemplate(template, false, true); if (newTemplate != null) { TemplatePersistenceData data= new TemplatePersistenceData(newTemplate, true); fTemplateStore.add(data); fTableViewer.refresh(); fTableViewer.setChecked(data, true); fTableViewer.setSelection(new StructuredSelection(data)); } } }
/** * Returns the template context type registry for the java plug-in. * * @return the template context type registry for the java plug-in * @since 3.0 */ public synchronized ContextTypeRegistry getTemplateContextRegistry() { if (fContextTypeRegistry == null) { ContributionContextTypeRegistry registry = new ContributionContextTypeRegistry(ID_CU_EDITOR); TemplateContextType all_contextType = registry.getContextType(JavaContextType.ID_ALL); ((AbstractJavaContextType) all_contextType).initializeContextTypeResolvers(); registerJavaContext(registry, JavaContextType.ID_MEMBERS, all_contextType); registerJavaContext(registry, JavaContextType.ID_STATEMENTS, all_contextType); // registerJavaContext(registry, SWTContextType.ID_ALL, all_contextType); // all_contextType= registry.getContextType(SWTContextType.ID_ALL); // // registerJavaContext(registry, SWTContextType.ID_MEMBERS, all_contextType); // registerJavaContext(registry, SWTContextType.ID_STATEMENTS, all_contextType); fContextTypeRegistry = registry; } return fContextTypeRegistry; }
/** * Tries to create a context type given an id. Contributions to the <code> * org.eclipse.ui.editors.templates</code> extension point are searched for the given identifier * and the specified context type instantiated if it is found. Any contributed {@link * org.eclipse.jface.text.templates.TemplateVariableResolver}s are also instantiated and added to * the context type. * * @param id the id for the context type as specified in XML * @return the instantiated and configured context type, or <code>null</code> if it is not found * or cannot be instantiated */ public static TemplateContextType createContextType(String id) { Assert.isNotNull(id); IConfigurationElement[] extensions = getTemplateExtensions(); TemplateContextType type; try { type = createContextType(extensions, id); if (type != null) { TemplateVariableResolver[] resolvers = createResolvers(extensions, id); for (int i = 0; i < resolvers.length; i++) type.addResolver(resolvers[i]); } } catch (CoreException e) { JavaPlugin.log(e); type = null; } return type; }
private static TemplateContextType createContextType(IConfigurationElement element) throws CoreException { String id = element.getAttribute(ID); try { TemplateContextType contextType = (TemplateContextType) element.createExecutableExtension(CLASS); String name = element.getAttribute(NAME); if (name == null) name = id; if (contextType.getId() == null) contextType.setId(id); if (contextType.getName() == null) contextType.setName(name); return contextType; } catch (ClassCastException e) { throw new CoreException( new Status( IStatus.ERROR, "org.eclipse.ui.editors", IStatus.OK, "extension does not implement " + TemplateContextType.class.getName(), e)); // $NON-NLS-1$ } }
public TemplateContextType getContextType(final Model model, final String path) { if (OpenApi3ContextTypeProvider.RootContextType.isRoot(path)) { return new RootContextType(); } return Iterables .getFirst(Iterables.filter(allContextTypes(), new Predicate<TemplateContextType>() { @Override public boolean apply(TemplateContextType input) { if (input instanceof SchemaBasedTemplateContextType) { return ((SchemaBasedTemplateContextType) input).matches(model, path); } return false; } }), null); }
@Override protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) { try { String prefix = viewer.getDocument().get(region.getOffset(), region.getLength()); int parameterCount = parseParameters(prefix).size(); TemplateContextType type = ParametrizedTemplateTypeDescriptor.findTemplateType(parameterCount); if (type != null) { return type; } } catch (BadLocationException ex) { BfActivator.getDefault().logError("Context type could not be evaluated", ex); } return ParametrizedTemplateTypeDescriptor.NoParameters.templateType; }
@Override protected TemplateContext createContext(ITextViewer viewer, IRegion region) { TemplateContext context = null; TemplateContextType contextType= getContextType(viewer, region); if (contextType != null) { IDocument document= viewer.getDocument(); context = new BfTemplateContext(contextType, document, region.getOffset(), region.getLength()); } if (context == null) { return null; } try { String prefix = viewer.getDocument().get(region.getOffset(), region.getLength()); int i = 0; for (String param : parseParameters(prefix)) { context.setVariable("x" + (i++), param); } } catch (BadLocationException ex) { BfActivator.getDefault().logError("Prefix for Template could not be computed", ex); } return context; }
/** * Evaluates a 'java' template in the context of a compilation unit * * @param template the template to be evaluated * @param compilationUnit the compilation unit in which to evaluate the template * @param position the position inside the compilation unit for which to evaluate the template * @return the evaluated template * @throws CoreException in case the template is of an unknown context type * @throws BadLocationException in case the position is invalid in the compilation unit * @throws TemplateException in case the evaluation fails */ public static String evaluateTemplate(Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException { TemplateContextType contextType= JavaPlugin.getDefault().getTemplateContextRegistry().getContextType(template.getContextTypeId()); if (!(contextType instanceof CompilationUnitContextType)) throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null)); IDocument document= new Document(); if (compilationUnit != null && compilationUnit.exists()) document.set(compilationUnit.getSource()); CompilationUnitContext context= ((CompilationUnitContextType) contextType).createContext(document, position, 0, compilationUnit); context.setForceEvaluation(true); TemplateBuffer buffer= context.evaluate(template); if (buffer == null) return null; return buffer.getString(); }
/** * Returns the template context type registry for the java plug-in. * * @return the template context type registry for the java plug-in * @since 3.0 */ public synchronized ContextTypeRegistry getTemplateContextRegistry() { if (fContextTypeRegistry == null) { ContributionContextTypeRegistry registry= new ContributionContextTypeRegistry(JavaUI.ID_CU_EDITOR); TemplateContextType all_contextType= registry.getContextType(JavaContextType.ID_ALL); ((AbstractJavaContextType) all_contextType).initializeContextTypeResolvers(); registerJavaContext(registry, JavaContextType.ID_MEMBERS, all_contextType); registerJavaContext(registry, JavaContextType.ID_STATEMENTS, all_contextType); registerJavaContext(registry, SWTContextType.ID_ALL, all_contextType); all_contextType= registry.getContextType(SWTContextType.ID_ALL); registerJavaContext(registry, SWTContextType.ID_MEMBERS, all_contextType); registerJavaContext(registry, SWTContextType.ID_STATEMENTS, all_contextType); fContextTypeRegistry= registry; } return fContextTypeRegistry; }
@Override protected void updatePatternViewer(Template template) { if (template == null) { getPatternViewer().getDocument().set(""); //$NON-NLS-1$ return ; } String contextId= template.getContextTypeId(); TemplateContextType type= getContextTypeRegistry().getContextType(contextId); fTemplateProcessor.setContextType(type); IDocument doc= getPatternViewer().getDocument(); String start= null; if ("javadoc".equals(contextId)) { //$NON-NLS-1$ start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$ } else start= ""; //$NON-NLS-1$ doc.set(start + template.getPattern()); int startLen= start.length(); getPatternViewer().setDocument(doc, startLen, doc.getLength() - startLen); }
public String getHoverInfo(ITextViewer textViewer, IRegion subject) { try { IDocument doc= textViewer.getDocument(); int offset= subject.getOffset(); if (offset >= 2 && "${".equals(doc.get(offset-2, 2))) { //$NON-NLS-1$ String varName= doc.get(offset, subject.getLength()); TemplateContextType contextType= fProcessor.getContextType(); if (contextType != null) { Iterator<TemplateVariableResolver> iter= contextType.resolvers(); while (iter.hasNext()) { TemplateVariableResolver var= iter.next(); if (varName.equals(var.getType())) { return var.getDescription(); } } } } } catch (BadLocationException e) { } return null; }
public JavaStatementPostfixContext( TemplateContextType type, IDocument document, int offset, int length, ICompilationUnit compilationUnit, ASTNode currentNode, ASTNode parentNode) { super(type, document, offset, length, compilationUnit); this.nodeRegions = new HashMap<>(); this.currentCompletionNode = currentNode; nodeRegions.put(currentNode, calculateNodeRegion(currentNode)); this.currentCompletionNodeParent = parentNode; nodeRegions.put(parentNode, calculateNodeRegion(parentNode)); this.selectedNode = currentNode; outOfRangeOffsets = new HashMap<>(); }
/** * Returns template string to insert. * * @return String to insert or null if none is to be inserted */ String getTemplateString() { String templateString = null; Template template = getSelectedTemplate(); if (template != null) { TemplateContextType contextType = GlassmakerUIPlugin.getDefault().getTemplateContextRegistry().getContextType(CardContextType.CONTEXT_TYPE); IDocument document = new Document(); TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0); try { TemplateBuffer buffer = context.evaluate(template); templateString = buffer.getString(); } catch (Exception e) { GlassmakerUIPlugin.logError("Could not create template for new html", e); } } return templateString; }
public SilverStripeTemplatesPreferencePage() { setPreferenceStore(SilverStripePDTPlugin.getDefault().getPreferenceStore()); //Build registry Iterator contexts=SilverStripePDTPlugin.getDefault().getTemplateContextRegistry().contextTypes(); ContributionContextTypeRegistry registry = new ContributionContextTypeRegistry(); registry.addContextType(new CodeTemplateContextType(NewSilverStripeClassWizardTemplatePage.NEW_CLASS_CONTEXTTYPE)); while(contexts.hasNext()) { registry.addContextType((TemplateContextType) contexts.next()); } setTemplateStore(new SilverStripeTemplateStore(registry, this.getPreferenceStore(), "ca.edchipman.silverstripepdt.SilverStripe.templates")); try { this.getTemplateStore().load(); } catch (IOException e) { e.printStackTrace(); } setContextTypeRegistry(registry); }
/** * Returns template string to insert. * * @return String to insert or null if none is to be inserted */ public String getTemplateString() { String templateString = null; Template template = getSelectedTemplate(); if (template != null) { TemplateContextType contextType=SilverStripePDTPlugin.getDefault().getTemplateContextRegistry().getContextType(this._languageProvider.getTemplateContext()); IDocument document = new Document(); TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0); try { TemplateBuffer buffer = context.evaluate(template); templateString = buffer.getString(); } catch (Exception e) { Logger.log(Logger.WARNING_DEBUG, "Could not create template for new html", e); //$NON-NLS-1$ } } return templateString; }
@Override protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) { SQLEditorTemplateManager manager = SQLEditorTemplateManager .getInstance(); return manager.getContextTypeRegistry().getContextType( SQLEditorTemplateContextType.CONTEXT_TYPE); }
/** * Returns the template context type registry for the code generation * templates. * * @return the template context type registry for the code generation * templates */ public static Map<String, TemplateContextType> getCodeTemplateContextRegistry() { if (codeTemplateContextTypeRegistry == null) { codeTemplateContextTypeRegistry = new LinkedHashMap<>(); for (TemplateContextType contextType : getContextTypes()) { codeTemplateContextTypeRegistry.put(contextType.getId(), contextType); } } return codeTemplateContextTypeRegistry; }
protected void updateViewerInput() { IStructuredSelection selection= (IStructuredSelection) getTableViewer().getSelection(); SourceViewer viewer= getViewer(); if (selection.size() == 1 && selection.getFirstElement() instanceof TemplatePersistenceData) { TemplatePersistenceData data= (TemplatePersistenceData) selection.getFirstElement(); Template template= data.getTemplate(); String contextId= template.getContextTypeId(); TemplateContextType type= JSDTTypeScriptUIPlugin.getDefault().getTemplateContextRegistry().getContextType(contextId); fTemplateProcessor.setContextType(type); IDocument doc= viewer.getDocument(); String start= null; if ("javadoc".equals(contextId)) { //$NON-NLS-1$ start= "/**" + doc.getLegalLineDelimiters()[0]; //$NON-NLS-1$ } else start= ""; //$NON-NLS-1$ doc.set(start + template.getPattern()); int startLen= start.length(); viewer.setDocument(doc, startLen, doc.getLength() - startLen); } else { viewer.getDocument().set(""); //$NON-NLS-1$ } }
protected void updateSourceViewerInput(List selection) { if (fPatternViewer == null || fPatternViewer.getTextWidget().isDisposed()) { return; } if (selection.size() == 1 && selection.get(0) instanceof TemplatePersistenceData) { TemplatePersistenceData data = (TemplatePersistenceData) selection.get(0); Template template = data.getTemplate(); TemplateContextType type = JavaScriptPlugin.getDefault().getCodeTemplateContextRegistry() .getContextType(template.getContextTypeId()); fTemplateProcessor.setContextType(type); fPatternViewer.getDocument().set(template.getPattern()); } else { fPatternViewer.getDocument().set(""); //$NON-NLS-1$ } }
protected void doSourceChanged(IDocument document) { String text = document.get(); fValidationStatus.setOK(); TemplateContextType contextType = fContextTypeRegistry.getContextType(getContextId()); if (contextType != null) { try { contextType.validate(text); } catch (TemplateException e) { fValidationStatus.setError(e.getLocalizedMessage()); } } updateUndoAction(); updateStatusAndButtons(); }
private void doSourceChanged(IDocument document) { String text= document.get(); fValidationStatus.setOK(); TemplateContextType contextType= fContextTypeRegistry.getContextType(getContextId()); if (contextType != null) { try { contextType.validate(text); } catch (TemplateException e) { fValidationStatus.setError(e.getLocalizedMessage()); } } updateAction(ITextEditorActionConstants.UNDO); updateButtons(); }
private int computeMinimumContextColumnWidth(GC gc) { int width= gc.stringExtent(TemplatesMessages.TemplatePreferencePage_column_context).x; Iterator iter= getContextTypeRegistry().contextTypes(); while (iter.hasNext()) { TemplateContextType contextType= (TemplateContextType)iter.next(); width= Math.max(width, gc.stringExtent(contextType.getName()).x); } return width; }
@Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof TemplateContextType)) return false; if (obj == this) return true; TemplateContextType contextType = (TemplateContextType) obj; return getId().equals(contextType.getId()); }
public int compareTo(TemplateContextType templateContextType) { int result = getName().compareTo(templateContextType.getName()); if (result == 0) { return getId().compareTo(templateContextType.getId()); } return result; }
@Override protected void createTemplates(TemplateContext templateContext, ContentAssistContext context, ITemplateAcceptor acceptor) { TemplateContextType contextType = templateContext.getContextType(); Template[] templates = templateStore.getTemplates(contextType.getId()); for (Template template : templates) { if (!acceptor.canAcceptMoreTemplates()) return; if (validate(template, templateContext)) { acceptor.accept(createProposal(template, templateContext, context, getImage(template), getRelevance(template))); } } }
@Override protected TemplateContextType[] getContextTypes(final ContentAssistContext context) { final Set<TemplateContextType> result = Sets.newLinkedHashSet(); IFollowElementAcceptor acceptor = createFollowElementAcceptor(result); List<AbstractElement> grammarElements = context.getFirstSetGrammarElements(); for(AbstractElement element: grammarElements) acceptor.accept(element); return result.toArray(new TemplateContextType[result.size()]); }
protected TemplateContext[] createTemplateContexts(ContentAssistContext context) { TemplateContextType[] contextTypes = getContextTypes(context); if (contextTypes != null && contextTypes.length != 0) { TemplateContext[] result = new TemplateContext[contextTypes.length]; for(int i = 0; i < contextTypes.length; i++) { result[i] = doCreateTemplateContext(contextTypes[i], context); } return result; } return null; }
/** * Creates a compilation unit context. * * @param type the context type * @param document the document * @param completionOffset the completion position within the document * @param completionLength the completion length within the document * @param compilationUnit the compilation unit (may be <code>null</code>) */ protected CompilationUnitContext( TemplateContextType type, IDocument document, int completionOffset, int completionLength, ICompilationUnit compilationUnit) { super(type, document, completionOffset, completionLength); fCompilationUnit = compilationUnit; fIsManaged = false; }
/** * Creates a compilation unit context. * * @param type the context type * @param document the document * @param completionPosition the position defining the completion offset and length * @param compilationUnit the compilation unit (may be <code>null</code>) * @since 3.2 */ protected CompilationUnitContext( TemplateContextType type, IDocument document, Position completionPosition, ICompilationUnit compilationUnit) { super(type, document, completionPosition); fCompilationUnit = compilationUnit; fIsManaged = true; }
/** * Evaluates a 'java' template in the context of a compilation unit * * @param template the template to be evaluated * @param compilationUnit the compilation unit in which to evaluate the template * @param position the position inside the compilation unit for which to evaluate the template * @return the evaluated template * @throws CoreException in case the template is of an unknown context type * @throws BadLocationException in case the position is invalid in the compilation unit * @throws TemplateException in case the evaluation fails */ public static String evaluateTemplate( Template template, ICompilationUnit compilationUnit, int position) throws CoreException, BadLocationException, TemplateException { TemplateContextType contextType = JavaPlugin.getDefault() .getTemplateContextRegistry() .getContextType(template.getContextTypeId()); if (!(contextType instanceof CompilationUnitContextType)) throw new CoreException( new Status( IStatus.ERROR, JavaPlugin.ID_PLUGIN, IStatus.ERROR, JavaTemplateMessages.JavaContext_error_message, null)); IDocument document = new Document(); if (compilationUnit != null && compilationUnit.exists()) document.set(compilationUnit.getSource()); CompilationUnitContext context = ((CompilationUnitContextType) contextType) .createContext(document, position, 0, compilationUnit); context.setForceEvaluation(true); TemplateBuffer buffer = context.evaluate(template); if (buffer == null) return null; return buffer.getString(); }
private static TemplateContextType createContextType( IConfigurationElement[] extensions, String contextTypeId) throws CoreException { for (int i = 0; i < extensions.length; i++) { // TODO create half-order over contributions if (extensions[i].getName().equals(CONTEXT_TYPE)) { String id = extensions[i].getAttribute(ID); if (contextTypeId.equals(id)) return createContextType(extensions[i]); } } return null; }