use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.
the class ContributionContextTypeRegistry method createContextType.
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) {
// $NON-NLS-1$
throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateContextType.class.getName(), e));
}
}
use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.
the class GlobalTemplateVariablesDateTest method setUp.
@Before
public void setUp() {
fTranslator = new TemplateTranslator();
fType = new TemplateContextType();
fType.addResolver(new GlobalTemplateVariables.Date());
fContext = new DocumentTemplateContext(fType, new Document(), 0, 0);
}
use of org.eclipse.jface.text.templates.TemplateContextType in project xtext-eclipse by eclipse.
the class CodetemplatesProposalProvider method completeVariable_Type.
@Override
public void completeVariable_Type(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
if ((mode & NORMAL) != 0) {
super.completeVariable_Name(model, assignment, context, acceptor);
TemplateData data = new TemplateData(model);
if (data.doCreateProposals()) {
ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language);
if (helper != null) {
String contextTypeId = helper.getId(data.rule);
ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language);
TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
if (contextType != null) {
Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
while (resolvers.hasNext()) {
TemplateVariableResolver resolver = resolvers.next();
String type = resolver.getType();
StyledString displayString = new StyledString(type).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
acceptor.accept(createCompletionProposal(type, displayString, null, context));
}
}
}
}
}
}
use of org.eclipse.jface.text.templates.TemplateContextType in project xtext-eclipse by eclipse.
the class CodetemplatesProposalProvider method completeNestedCrossReference.
public void completeNestedCrossReference(CrossReference crossReference, ContentAssistContext context, ICompletionProposalAcceptor acceptor, TemplateData data) {
if (data.doCreateProposals()) {
ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language);
if (helper != null) {
String contextTypeId = helper.getId(data.rule);
ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language);
TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
TemplateVariableResolver crossRefResolver = getResolver(contextType, "CrossReference");
if (crossRefResolver != null) {
Assignment assignment = (Assignment) crossReference.eContainer();
EReference reference = GrammarUtil.getReference(crossReference);
if (reference != null) {
String proposalText = "${" + assignment.getFeature() + ":CrossReference(" + reference.getEContainingClass().getName() + "." + reference.getName() + ")}";
StyledString displayText = new StyledString("${", StyledString.DECORATIONS_STYLER).append(assignment.getFeature()).append(":CrossReference(", StyledString.DECORATIONS_STYLER).append(reference.getEContainingClass().getName() + "." + reference.getName(), StyledString.COUNTER_STYLER).append(")}", StyledString.DECORATIONS_STYLER).append(" - Create a new template variable", StyledString.QUALIFIER_STYLER);
ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
if (proposal instanceof ConfigurableCompletionProposal) {
ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
configurable.setSelectionStart(configurable.getReplacementOffset() + 2);
configurable.setSelectionLength(assignment.getFeature().length());
configurable.setAutoInsertable(false);
configurable.setSimpleLinkedMode(context.getViewer(), '\t');
configurable.setPriority(configurable.getPriority() * 2);
}
acceptor.accept(proposal);
}
}
}
}
}
use of org.eclipse.jface.text.templates.TemplateContextType in project xtext-eclipse by eclipse.
the class CodetemplatesProposalProvider method completeVariable_Name.
@Override
public void completeVariable_Name(EObject model, Assignment assignment, final ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
if ((mode & NORMAL) != 0) {
{
String proposalText = "variable";
StyledString displayText = new StyledString(proposalText).append(" - Create a new template variable", StyledString.QUALIFIER_STYLER);
ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
if (proposal instanceof ConfigurableCompletionProposal) {
ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
configurable.setSelectionStart(configurable.getReplacementOffset());
configurable.setSelectionLength(proposalText.length());
configurable.setAutoInsertable(false);
configurable.setSimpleLinkedMode(context.getViewer(), '\t', ' ');
}
acceptor.accept(proposal);
}
TemplateData data = new TemplateData(model);
if (data.doCreateProposals()) {
ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language);
if (helper != null) {
String contextTypeId = helper.getId(data.rule);
ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language);
TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
if (contextType != null) {
Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
while (resolvers.hasNext()) {
final TemplateVariableResolver resolver = resolvers.next();
IInspectableTemplateVariableResolver inspectableResolver = templateVariableResolverRegistry.toInspectableResolver(resolver);
if (inspectableResolver != null) {
if (!inspectableResolver.hasMandatoryParameters()) {
String type = resolver.getType();
StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
acceptor.accept(createCompletionProposal(type, displayString, null, context));
}
String proposalText = "variable:" + resolver.getType();
StyledString displayText = new StyledString("variable").append(":" + resolver.getType(), StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
if (inspectableResolver.hasMandatoryParameters())
proposalText = proposalText + "()";
Builder builder = context.copy();
PrefixMatcher newMatcher = new PrefixMatcher() {
@Override
public boolean isCandidateMatchingPrefix(String name, String prefix) {
return context.getMatcher().isCandidateMatchingPrefix(name, prefix) || context.getMatcher().isCandidateMatchingPrefix("variable:" + resolver.getType(), prefix) || context.getMatcher().isCandidateMatchingPrefix(resolver.getType(), prefix);
}
};
builder.setMatcher(newMatcher);
ContentAssistContext myContext = builder.toContext();
ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, myContext);
if (proposal instanceof ConfigurableCompletionProposal) {
ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
configurable.setSelectionStart(configurable.getReplacementOffset());
configurable.setSelectionLength("variable".length());
configurable.setAutoInsertable(false);
if (inspectableResolver.hasMandatoryParameters()) {
configurable.setCursorPosition(proposalText.length() - 1);
}
configurable.setSimpleLinkedMode(myContext.getViewer(), '\t');
}
acceptor.accept(proposal);
} else {
String type = resolver.getType();
StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
acceptor.accept(createCompletionProposal(type, displayString, null, context));
}
}
}
}
if (data.template.getBody() != null) {
for (Variable variable : Iterables.filter(data.template.getBody().getParts(), Variable.class)) {
if (variable != model && variable.getName() != null) {
String proposalText = variable.getName();
StyledString displayText = new StyledString(proposalText).append(" - existing variable", StyledString.QUALIFIER_STYLER);
if (variable.getType() != null)
displayText = displayText.append(" of type " + variable.getType(), StyledString.QUALIFIER_STYLER);
ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
acceptor.accept(proposal);
}
}
}
}
}
}
Aggregations