Skip to content

Commit

Permalink
#227 - generate named schema templates from inline schema templates
Browse files Browse the repository at this point in the history
  • Loading branch information
tfesenko committed Sep 26, 2017
1 parent e027674 commit 522b4b3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
14 changes: 1 addition & 13 deletions com.reprezen.swagedit.openapi3/resources/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,8 @@ content:
$$ref: ${model}</template>

<!--
NAMED SCHEMA
NAMED SCHEMA: generated from SCHEMA OBJECT
-->

<template name="named schema"
id="com.reprezen.swagedit.openapi3.templates.schemas.named_schema_required_property"
description="Schema definition with a required property"
context="com.reprezen.swagedit.openapi3.templates.schemas"
enabled="true">${type}:
required:
- ${property}
properties:
${property}:
type: integer
format: int64</template>

<!--
SCHEMA OBJECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import java.io.IOException;

import org.dadacoalition.yedit.YEditLog;
import org.dadacoalition.yedit.preferences.PreferenceConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.templates.ContextTypeRegistry;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.jface.text.templates.TemplateContextType;
import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData;
import org.eclipse.jface.text.templates.persistence.TemplateStore;
import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.google.common.base.Strings;
import com.reprezen.swagedit.openapi3.schema.OpenApi3Schema;
import com.reprezen.swagedit.openapi3.templates.OpenApi3ContextType;

Expand Down Expand Up @@ -88,8 +93,35 @@ public TemplateStore getTemplateStore() {
} catch (IOException e) {
YEditLog.logException(e);
}
addNamedSchemaTemplates();
}
return templateStore;
}

private void addNamedSchemaTemplates() {
Template[] schemaTemplates = templateStore.getTemplates("com.reprezen.swagedit.openapi3.templates.schema");
for (int i = 0; i < schemaTemplates.length; i++) {
Template schemaTemplate = schemaTemplates[i];
Template template = createNamedTemplate(schemaTemplate, "com.reprezen.swagedit.openapi3.templates.schemas");
templateStore.add(new TemplatePersistenceData(template, true));
}
}

private Template createNamedTemplate(Template inlineTemplate, String newTemplateId) {
String indent = Strings.repeat(" ", getTabWidth());
String newPattern = inlineTemplate.getPattern().replaceAll("\n", "\n" + indent);
Template template = new Template(inlineTemplate.getName(), //
inlineTemplate.getDescription(), //
newTemplateId, //
"${schemaName}:\n" + indent + newPattern, //
inlineTemplate.isAutoInsertable());
return template;
}

// TODO: make it reusable: a similar method is defined in QuickFixer
private int getTabWidth() {
IPreferenceStore prefs = org.dadacoalition.yedit.Activator.getDefault().getPreferenceStore();
return prefs.getInt(PreferenceConstants.SPACES_PER_TAB);
}

}

0 comments on commit 522b4b3

Please sign in to comment.