/** * Adds a context type to the registry. If there already is a context type * with the same ID registered, it is replaced. * * @param contextType the context type to add */ public void addContextType(TemplateContextType contextType) { fContextTypes.put(contextType.getId(), contextType); }
public void validate(String pattern) throws TemplateException { super.validate(pattern); if (fIsComment) { if (!isValidComment(pattern)) { throw new TemplateException(JavaTemplateMessages.CodeTemplateContextType_validate_invalidcomment); } } }
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, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateContextType.class.getName(), e)); //$NON-NLS-1$ } }
/** * Registers the given Java template context. * * @param registry the template context type registry * @param id the context type id * @param parent the parent context type * @since 3.4 */ private static void registerJavaContext(ContributionContextTypeRegistry registry, String id, TemplateContextType parent) { TemplateContextType contextType= registry.getContextType(id); Iterator<TemplateVariableResolver> iter= parent.resolvers(); while (iter.hasNext()) contextType.addResolver(iter.next()); }
@Override public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { ITextSelection selection= (ITextSelection) viewer.getSelectionProvider().getSelection(); // adjust offset to end of normalized selection if (selection.getOffset() == offset) offset= selection.getOffset() + selection.getLength(); String prefix= extractPrefix(viewer, offset); Region region= new Region(offset - prefix.length(), prefix.length()); TemplateContext context= createContext(viewer, region); if (context == null) return new ICompletionProposal[0]; context.setVariable("selection", selection.getText()); // name of the selection variables {line, word}_selection //$NON-NLS-1$ Template[] templates= getTemplates(context.getContextType().getId()); List<ICompletionProposal> matches= new ArrayList<>(); for (Template template : templates) { try { context.getContextType().validate(template.getPattern()); } catch (TemplateException e) { continue; } if (template.matches(prefix, context.getContextType().getId())) matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); } Collections.sort(matches, fgProposalComparator); return matches.toArray(new ICompletionProposal[matches.size()]); }
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { if (fContextType == null) return null; List proposals= new ArrayList(); String text= viewer.getDocument().get(); int start= getStart(text, documentOffset); int end= documentOffset; String string= text.substring(start, end); String prefix= (string.length() >= 2) ? string.substring(2) : null; int offset= start; int length= end - start; for (Iterator iterator= fContextType.resolvers(); iterator.hasNext(); ) { TemplateVariableResolver variable= (TemplateVariableResolver) iterator.next(); if (prefix == null || variable.getType().startsWith(prefix)) proposals.add(new TemplateVariableProposal(variable, offset, length, viewer)); } Collections.sort(proposals, fgTemplateVariableProposalComparator); return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]); }
private String getContextColumnText(TemplateContextType contextType, int columnIndex) { switch (columnIndex) { case 0: return contextType.getName(); default: return ""; //$NON-NLS-1$ } } }
public TemplateVariable getTemplateVariable(String name) { TemplateVariable variable= fVariables.get(name); if (variable != null && !variable.isResolved()) getContextType().resolve(variable, this); return variable; }
@Override public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { // test that all variables are defined Iterator<TemplateVariableResolver> iterator= getContextType().resolvers(); while (iterator.hasNext()) { TemplateVariableResolver var= iterator.next(); if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) { Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$ } } if (!canEvaluate(template)) return null; String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter); TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(pattern); getContextType().resolve(buffer, this); return buffer; }
/** * 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) { EditorsPlugin.log(e); type= null; } return type; }
/** * Validates a pattern, a <code>TemplateException</code> is thrown if * validation fails. * * @param pattern the template pattern to validate * @throws TemplateException if the pattern is invalid */ public void validate(String pattern) throws TemplateException { TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(pattern); validateVariables(buffer.getVariables()); }
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, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateContextType.class.getName(), e)); //$NON-NLS-1$ } }
@Override public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) { if (fContextType == null) return null; List<TemplateVariableProposal> proposals= new ArrayList<>(); String text= viewer.getDocument().get(); int start= getStart(text, documentOffset); int end= documentOffset; String string= text.substring(start, end); String prefix= (string.length() >= 2) ? string.substring(2) : null; int offset= start; int length= end - start; for (Iterator<TemplateVariableResolver> iterator= fContextType.resolvers(); iterator.hasNext(); ) { TemplateVariableResolver variable= iterator.next(); if (prefix == null || variable.getType().startsWith(prefix)) proposals.add(new TemplateVariableProposal(variable, offset, length, viewer)); } Collections.sort(proposals, fgTemplateVariableProposalComparator); return proposals.toArray(new ICompletionProposal[proposals.size()]); }
private String getContextColumnText(TemplateContextType contextType, int columnIndex) { switch (columnIndex) { case 0: return contextType.getName(); default: return ""; //$NON-NLS-1$ } } }
public TemplateVariable getTemplateVariable(String name) { TemplateVariable variable= fVariables.get(name); if (variable != null && !variable.isResolved()) getContextType().resolve(variable, this); return variable; }
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException { // test that all variables are defined Iterator iterator= getContextType().resolvers(); while (iterator.hasNext()) { TemplateVariableResolver var= (TemplateVariableResolver) iterator.next(); if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) { Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined"); //$NON-NLS-1$ //$NON-NLS-2$ } } if (!canEvaluate(template)) return null; String pattern= changeLineDelimiter(template.getPattern(), fLineDelimiter); TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(pattern); getContextType().resolve(buffer, this); return buffer; }
/** * Registers the given Java template context. * * @param registry the template context type registry * @param id the context type id * @param parent the parent context type * @since 3.4 */ private static void registerJavaContext(ContributionContextTypeRegistry registry, String id, TemplateContextType parent) { TemplateContextType contextType= registry.getContextType(id); Iterator<TemplateVariableResolver> iter= parent.resolvers(); while (iter.hasNext()) contextType.addResolver(iter.next()); }
/** * 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) { EditorsPlugin.log(e); type= null; } return type; }
/** * Validates a pattern, a <code>TemplateException</code> is thrown if * validation fails. * * @param pattern the template pattern to validate * @throws TemplateException if the pattern is invalid */ public void validate(String pattern) throws TemplateException { TemplateTranslator translator= new TemplateTranslator(); TemplateBuffer buffer= translator.translate(pattern); validateVariables(buffer.getVariables()); }