Search in sources :

Example 1 with ContextTypeRegistry

use of org.eclipse.text.templates.ContextTypeRegistry in project eclipse.jdt.ls by eclipse.

the class JavaLanguageServerPlugin method getTemplateContextRegistry.

/**
 * Returns the template context type registry for the java plug-in.
 *
 * @return the template context type registry for the java plug-in
 */
public synchronized ContextTypeRegistry getTemplateContextRegistry() {
    if (fContextTypeRegistry == null) {
        ContextTypeRegistry registry = new ContextTypeRegistry();
        JavaContextType statementContextType = new JavaContextType();
        statementContextType.setId(JavaContextType.ID_STATEMENTS);
        statementContextType.setName(JavaContextType.ID_STATEMENTS);
        statementContextType.initializeContextTypeResolvers();
        // Todo: Some of the resolvers is defined in the XML of the jdt.ui, now we have to add them manually.
        // See: https://github.com/eclipse/eclipse.jdt.ui/blob/cf6c42522ee5a5ea21a34fcfdecf3504d4750a04/org.eclipse.jdt.ui/plugin.xml#L5619-L5625
        TemplateVariableResolver resolver = new VarResolver();
        resolver.setType("var");
        statementContextType.addResolver(resolver);
        registry.addContextType(statementContextType);
        fContextTypeRegistry = registry;
    }
    return fContextTypeRegistry;
}
Also used : JavaContextType(org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType) ContextTypeRegistry(org.eclipse.text.templates.ContextTypeRegistry) VarResolver(org.eclipse.jdt.internal.corext.template.java.VarResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 2 with ContextTypeRegistry

use of org.eclipse.text.templates.ContextTypeRegistry in project eclipse.jdt.ui by eclipse-jdt.

the class ChainCompletionTemplateBuilder method createJavaContext.

private static JavaContext createJavaContext(final JavaContentAssistInvocationContext contentAssistContext) {
    final ContextTypeRegistry templateContextRegistry = JavaPlugin.getDefault().getTemplateContextRegistry();
    final TemplateContextType templateContextType = templateContextRegistry.getContextType(JavaContextType.ID_ALL);
    final CompletionContext ctx = contentAssistContext.getCoreContext();
    final JavaContext javaTemplateContext = new JavaContext(templateContextType, contentAssistContext.getDocument(), ctx.getTokenStart(), ctx.getToken().length, contentAssistContext.getCompilationUnit());
    javaTemplateContext.setForceEvaluation(true);
    return javaTemplateContext;
}
Also used : CompletionContext(org.eclipse.jdt.core.CompletionContext) JavaContext(org.eclipse.jdt.internal.corext.template.java.JavaContext) ContextTypeRegistry(org.eclipse.text.templates.ContextTypeRegistry) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType)

Example 3 with ContextTypeRegistry

use of org.eclipse.text.templates.ContextTypeRegistry in project eclipse.jdt.ls by eclipse.

the class PreferenceManager method initialize.

/**
 * Initialize default preference values of used bundles to match server
 * functionality.
 */
public static void initialize() {
    // Update JavaCore options
    initializeJavaCoreOptions();
    // Initialize default preferences
    IEclipsePreferences defEclipsePrefs = DefaultScope.INSTANCE.getNode(IConstants.PLUGIN_ID);
    defEclipsePrefs.put("org.eclipse.jdt.ui.typefilter.enabled", "");
    defEclipsePrefs.put(CodeStyleConfiguration.ORGIMPORTS_IMPORTORDER, String.join(";", Preferences.JAVA_IMPORT_ORDER_DEFAULT));
    // $NON-NLS-1$
    defEclipsePrefs.put(MembersOrderPreferenceCacheCommon.APPEARANCE_MEMBER_SORT_ORDER, "T,SF,SI,SM,F,I,C,M");
    defEclipsePrefs.put(CodeGenerationSettingsConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, Boolean.TRUE.toString());
    defEclipsePrefs.put(StubUtility.CODEGEN_KEYWORD_THIS, Boolean.FALSE.toString());
    defEclipsePrefs.put(StubUtility.CODEGEN_IS_FOR_GETTERS, Boolean.TRUE.toString());
    // $NON-NLS-1$
    defEclipsePrefs.put(StubUtility.CODEGEN_EXCEPTION_VAR_NAME, "e");
    defEclipsePrefs.put(StubUtility.CODEGEN_ADD_COMMENTS, Boolean.FALSE.toString());
    ContextTypeRegistry registry = new ContextTypeRegistry();
    // Register standard context types from JDT
    CodeTemplateContextType.registerContextTypes(registry);
    // Register additional context types
    registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.CLASSSNIPPET_CONTEXTTYPE));
    registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.INTERFACESNIPPET_CONTEXTTYPE));
    registry.addContextType(new CodeTemplateContextType(CodeTemplatePreferences.RECORDSNIPPET_CONTEXTTYPE));
    // These should be upstreamed into CodeTemplateContextType & GlobalVariables
    TemplateContextType tmp = registry.getContextType(CodeTemplateContextType.TYPECOMMENT_CONTEXTTYPE);
    tmp.addResolver(new CodeTemplatePreferences.Month());
    tmp.addResolver(new CodeTemplatePreferences.ShortMonth());
    tmp.addResolver(new CodeTemplatePreferences.Day());
    tmp.addResolver(new CodeTemplatePreferences.Hour());
    tmp.addResolver(new CodeTemplatePreferences.Minute());
    JavaManipulation.setCodeTemplateContextRegistry(registry);
    // Initialize templates
    templates.put(CodeTemplatePreferences.CODETEMPLATE_FIELDCOMMENT, CodeGenerationTemplate.FIELDCOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_METHODCOMMENT, CodeGenerationTemplate.METHODCOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_CONSTRUCTORCOMMENT, CodeGenerationTemplate.CONSTRUCTORCOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_CONSTRUCTORBODY, CodeGenerationTemplate.CONSTRUCTORBODY.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_DELEGATECOMMENT, CodeGenerationTemplate.DELEGATECOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_OVERRIDECOMMENT, CodeGenerationTemplate.OVERRIDECOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_TYPECOMMENT, CodeGenerationTemplate.TYPECOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_GETTERCOMMENT, CodeGenerationTemplate.GETTERCOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_SETTERCOMMENT, CodeGenerationTemplate.SETTERCOMMENT.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_GETTERBODY, CodeGenerationTemplate.GETTERBODY.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_SETTERBODY, CodeGenerationTemplate.SETTERBOY.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_CATCHBODY, CodeGenerationTemplate.CATCHBODY.createTemplate());
    templates.put(CodeTemplatePreferences.CODETEMPLATE_METHODBODY, CodeGenerationTemplate.METHODBODY.createTemplate());
    reloadTemplateStore();
}
Also used : CodeTemplateContextType(org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) ContextTypeRegistry(org.eclipse.text.templates.ContextTypeRegistry) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) CodeTemplateContextType(org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType)

Example 4 with ContextTypeRegistry

use of org.eclipse.text.templates.ContextTypeRegistry in project eclipse.platform.text by eclipse-platform.

the class TemplateStore method getRegistry.

@Override
protected final org.eclipse.jface.text.templates.ContextTypeRegistry getRegistry() {
    ContextTypeRegistry registry = super.getRegistry();
    if (registry == null) {
        return null;
    }
    org.eclipse.jface.text.templates.ContextTypeRegistry res = new org.eclipse.jface.text.templates.ContextTypeRegistry();
    registry.contextTypes().forEachRemaining(t -> res.addContextType(t));
    return res;
}
Also used : ContextTypeRegistry(org.eclipse.text.templates.ContextTypeRegistry)

Aggregations

ContextTypeRegistry (org.eclipse.text.templates.ContextTypeRegistry)4 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)2 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 CompletionContext (org.eclipse.jdt.core.CompletionContext)1 CodeTemplateContextType (org.eclipse.jdt.internal.core.manipulation.CodeTemplateContextType)1 JavaContext (org.eclipse.jdt.internal.corext.template.java.JavaContext)1 VarResolver (org.eclipse.jdt.internal.corext.template.java.VarResolver)1 JavaContextType (org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType)1 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)1