Search in sources :

Example 6 with TemplateContextType

use of org.eclipse.jface.text.templates.TemplateContextType in project xtext-eclipse by eclipse.

the class AdvancedTemplatesPreferencePage method updateViewerInput.

@Override
protected void updateViewerInput() {
    IStructuredSelection selection = (IStructuredSelection) getTableViewer().getSelection();
    if (selection.size() == 1) {
        TemplatePersistenceData data = (TemplatePersistenceData) selection.getFirstElement();
        Template template = data.getTemplate();
        String name = template.getName();
        TemplateContextType contextType = getContextTypeRegistry().getContextType(template.getContextTypeId());
        if (contextType != null) {
            String prefix = "templates for " + grammarAccess.getGrammar().getName() + "'" + name + "'" + " for " + getContextTypeForGrammar(contextType) + ">>";
            String editablePart = template.getPattern();
            String suffix = "";
            partialEditor.updateModel(prefix, editablePart, suffix);
        } else {
            partialEditor.updateModel("", template.getPattern(), "");
        }
    } else {
        partialEditor.updateModel("", "", "");
    }
}
Also used : TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) Template(org.eclipse.jface.text.templates.Template)

Example 7 with TemplateContextType

use of org.eclipse.jface.text.templates.TemplateContextType in project xtext-eclipse by eclipse.

the class TemplateValidator method checkParameters.

@Check
public void checkParameters(Variable variable) {
    Codetemplate template = EcoreUtil2.getContainerOfType(variable, Codetemplate.class);
    Codetemplates templates = EcoreUtil2.getContainerOfType(template, Codetemplates.class);
    if (templates != null && template != null) {
        Grammar language = templates.getLanguage();
        AbstractRule rule = template.getContext();
        ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(language);
        if (helper != null && rule != null && !rule.eIsProxy() && rule instanceof ParserRule) {
            String contextTypeId = helper.getId(rule);
            ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(language);
            TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
            if (contextType != null) {
                Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
                String type = variable.getType();
                if (type == null)
                    type = variable.getName();
                while (resolvers.hasNext()) {
                    final TemplateVariableResolver resolver = resolvers.next();
                    if (resolver.getType().equals(type)) {
                        IInspectableTemplateVariableResolver inspectableResolver = registry.toInspectableResolver(resolver);
                        if (inspectableResolver != null) {
                            inspectableResolver.validateParameters(variable, this);
                        }
                    }
                }
            }
        }
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) ContextTypeIdHelper(org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver) Check(org.eclipse.xtext.validation.Check)

Example 8 with TemplateContextType

use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.

the class ContributionContextTypeRegistry method addContextType.

/**
 * Tries to create a context type given an id. If there is already a context
 * type registered under the given id, nothing happens. Otherwise,
 * 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.
 *
 * @param id the id for the context type as specified in XML
 */
public void addContextType(String id) {
    Assert.isNotNull(id);
    if (getContextType(id) != null)
        return;
    TemplateContextType type = createContextType(id);
    if (type != null)
        addContextType(type);
}
Also used : TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType)

Example 9 with TemplateContextType

use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.

the class ContributionContextTypeRegistry method createContextType.

/**
 * 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;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 10 with TemplateContextType

use of org.eclipse.jface.text.templates.TemplateContextType in project eclipse.platform.text by eclipse.

the class TemplateVariablesWordSelectionTest method setUp.

@Before
public void setUp() {
    fTranslator = new TemplateTranslator();
    fType = new TemplateContextType();
    fType.addResolver(new GlobalTemplateVariables.WordSelection());
    fContext = new DocumentTemplateContext(fType, new Document(), 0, 0);
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) GlobalTemplateVariables(org.eclipse.jface.text.templates.GlobalTemplateVariables) Document(org.eclipse.jface.text.Document) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator) Before(org.junit.Before)

Aggregations

TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)54 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)17 Template (org.eclipse.jface.text.templates.Template)14 Document (org.eclipse.jface.text.Document)11 IDocument (org.eclipse.jface.text.IDocument)11 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)9 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)9 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)8 CoreException (org.eclipse.core.runtime.CoreException)7 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)7 IStatus (org.eclipse.core.runtime.IStatus)4 Status (org.eclipse.core.runtime.Status)4 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)4 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)4 TemplateStore (org.eclipse.jface.text.templates.persistence.TemplateStore)4 ContextTypeIdHelper (org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper)4 IFile (org.eclipse.core.resources.IFile)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 StyledString (org.eclipse.jface.viewers.StyledString)3