use of org.eclipse.jface.text.templates.TemplateProposal in project linuxtools by eclipse.
the class SpecfileCompletionProcessor method computeTemplateProposals.
/**
* Compute the templates proposals, these proposals are contextual on
* sections. Return an array of template proposals for the given viewer,
* region, specfile, prefix.
*
* @param viewer
* the viewer for which the context is created
* @param region
* the region into <code>document</code> for which the context is
* created
* @param specfile
* the specfile element
* @param prefix
* the prefix string
* @return a ICompletionProposal[]
*/
private List<? extends ICompletionProposal> computeTemplateProposals(ITextViewer viewer, IRegion region, Specfile specfile, String prefix) {
TemplateContext context = createContext(viewer, region, specfile);
List<TemplateProposal> matches = new ArrayList<>();
if (context == null) {
return matches;
}
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// $NON-NLS-1$
context.setVariable("selection", selection.getText());
String id = context.getContextType().getId();
Template[] templates = Activator.getDefault().getTemplateStore().getTemplates(id);
for (Template template : templates) {
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
int relevance = getRelevance(template, prefix);
if (relevance > 0) {
matches.add(new TemplateProposal(template, context, region, Activator.getDefault().getImage(TEMPLATE_ICON), relevance));
}
}
Collections.sort(matches, (t1, t2) -> (t2.getRelevance() - t1.getRelevance()));
return matches;
}
use of org.eclipse.jface.text.templates.TemplateProposal in project liferay-ide by liferay.
the class StructuresContentAssistProcessor method getTemplateProposals.
private ICompletionProposal[] getTemplateProposals(IProject eclipseprj, ITextViewer viewer, int offset, String contextTypeId, Node currentNode, String prefix) {
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, contextTypeId);
if (context == null) {
return new ICompletionProposal[0];
}
// name of the selection variables {line, word}_selection
// $NON-NLS-1$
context.setVariable("selection", selection.getText());
StructuresTemplateContext templateContext = StructuresTemplateContext.fromId(contextTypeId);
// add the user defined templates - separate them from the rest of the templates
// so that we know what they are and can assign proper icon to them.
Image image = PortalImages.IMG_USER_TEMPLATE;
List<TemplateProposal> matches = new ArrayList<TemplateProposal>();
TemplateStore store = PortalUI.getDefault().getTemplateStore();
if (store != null) {
Template[] templates = store.getTemplates(contextTypeId);
for (Template template : templates) {
TemplateProposal proposal = createProposalForTemplate(prefix, region, context, image, template, true);
if (proposal != null) {
matches.add(proposal);
}
}
}
image = null;
Template[] templates = templateContext.getTemplates(eclipseprj, currentNode, prefix);
for (Template template : templates) {
TemplateProposal proposal = createProposalForTemplate(prefix, region, context, image, template, false);
if (proposal != null) {
matches.add(proposal);
}
}
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.jface.text.templates.TemplateProposal in project dsl-devkit by dsldevkit.
the class CheckCfgTemplateProposalProvider method addCatalogConfigurations.
/**
* Adds the populated check configuration.
*
* @param templateContext
* the template context
* @param context
* the context
* @param acceptor
* the acceptor
*/
@SuppressWarnings("all")
private void addCatalogConfigurations(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) {
// $NON-NLS-1$
final String templateName = "Add all registered catalogs";
// $NON-NLS-1$
final String templateDescription = "configures all missing catalogs";
final String contextTypeId = templateContext.getContextType().getId();
if (context.getRootModel() instanceof CheckConfiguration) {
final CheckConfiguration conf = (CheckConfiguration) context.getRootModel();
List<IEObjectDescription> allElements = Lists.newArrayList(scopeProvider.getScope(conf, CheckcfgPackage.Literals.CONFIGURED_CATALOG__CATALOG).getAllElements());
StringBuilder builder = new StringBuilder();
for (IEObjectDescription description : allElements) {
if (description.getEObjectOrProxy() instanceof CheckCatalog) {
CheckCatalog catalog = (CheckCatalog) description.getEObjectOrProxy();
if (catalog.eIsProxy()) {
catalog = (CheckCatalog) EcoreUtil.resolve(catalog, conf);
}
if (isCatalogConfigured(conf, catalog)) {
continue;
} else if (allElements.indexOf(description) > 0) {
builder.append(Strings.newLine());
}
final String catalogName = qualifiedNameValueConverter.toString(description.getQualifiedName().toString());
// $NON-NLS-1$ //$NON-NLS-2$
builder.append("catalog ").append(catalogName).append(" {}").append(Strings.newLine());
}
}
if (builder.length() > 0) {
// $NON-NLS-1$
builder.append("${cursor}");
Template t = new Template(templateName, templateDescription, contextTypeId, builder.toString(), true);
TemplateProposal tp = createProposal(t, templateContext, context, images.forConfiguredCatalog(), getRelevance(t));
acceptor.accept(tp);
}
}
}
use of org.eclipse.jface.text.templates.TemplateProposal in project statecharts by Yakindu.
the class SGenTemplateProposalProvider method createFeatureConfigurationTemplates.
private void createFeatureConfigurationTemplates(TemplateContext templateContext, ContentAssistContext context, ITemplateAcceptor acceptor) {
GeneratorModel model = (GeneratorModel) EcoreUtil2.getRootContainer(context.getCurrentModel());
IGeneratorDescriptor generatorDescriptor = GeneratorExtensions.getGeneratorDescriptor(model.getGeneratorId());
Iterable<ILibraryDescriptor> libraryDescriptor = LibraryExtensions.getLibraryDescriptors(generatorDescriptor.getLibraryIDs());
for (ILibraryDescriptor desc : libraryDescriptor) {
ResourceSet set = new ResourceSetImpl();
Resource resource = set.getResource(desc.getURI(), true);
FeatureTypeLibrary lib = (FeatureTypeLibrary) resource.getContents().get(0);
EList<FeatureType> types = lib.getTypes();
for (FeatureType featureType : types) {
Template template = new Template(featureType.getName() + " feature", "Creates feature " + featureType.getName(), featureType.getName(), creator.createProposal(featureType, desc.createFeatureValueProvider(SGenActivator.getInstance().getInjector(SGenActivator.ORG_YAKINDU_SCT_GENERATOR_GENMODEL_SGEN)), context.getCurrentModel()), false);
TemplateProposal proposal = createProposal(template, templateContext, context, getImage(template), getRelevance(template));
acceptor.accept(proposal);
}
}
}
use of org.eclipse.jface.text.templates.TemplateProposal in project titan.EclipsePlug-ins by eclipse.
the class ProposalCollector method addTemplateProposal.
/**
* Adds a new TemplateProposal to the ones already know.
*
* @param prefixString
* the prefix of TemplateProposal, using which we can
* decide if the proposal is valid in the given context.
* @param candidate
* the proposal candidate to be added
* @param image
* the image to use for this proposal in the completion
* popup
*/
public void addTemplateProposal(final String prefixString, final Template candidate, final Image image) {
int prefixLength = prefixLength(prefixString);
if (prefixLength != -1) {
Region region = new Region(replacementOffset, 0);
proposalList.add(new TemplateProposal(candidate, templatecontext, region, image));
}
}
Aggregations