Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Improve performance by using a single parser instead of 3 (yaml / jackson / custom model) (DO NOT MERGE) #453

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions com.reprezen.swagedit.core/.classpath
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="lib/com.fasterxml.jackson.core.jackson-annotations_2.9.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/com.fasterxml.jackson.core.jackson-core_2.9.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/com.fasterxml.jackson.core.jackson-databind_2.9.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/com.fasterxml.jackson.dataformat.jackson-dataformat-yaml_2.9.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/org.yaml.snakeyaml_1.18.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/org.apache.commons.lang3_3.2.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/org.mozilla.rhino_1.0.0.7R4.jar"/>
<classpathentry exported="true" kind="lib" path="lib/com.github.fge.uri-template_0.9.0.jar"/>
Expand Down
21 changes: 14 additions & 7 deletions com.reprezen.swagedit.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ui.ide,
org.eclipse.core.filesystem,
org.eclipse.ui.views,
com.fasterxml.jackson.core.jackson-core;bundle-version="2.5.0",
com.fasterxml.jackson.core.jackson-databind;bundle-version="2.5.0",
com.fasterxml.jackson.dataformat.jackson-dataformat-yaml;bundle-version="2.5.0",
org.yaml.snakeyaml;bundle-version="1.14.0",
org.slf4j.api;bundle-version="1.7.2",
org.eclipse.ui.workbench.texteditor
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy
Export-Package: com.github.fge.jsonschema.core.exceptions,
Export-Package: com.fasterxml.jackson.core,
com.fasterxml.jackson.databind,
com.fasterxml.jackson.databind.node,
com.fasterxml.jackson.dataformat.yaml,
com.github.fge.jackson.jsonpointer,
com.github.fge.jsonschema.core.exceptions,
com.github.fge.jsonschema.core.load.configuration,
com.github.fge.jsonschema.core.report,
com.github.fge.jsonschema.main,
Expand All @@ -34,8 +36,8 @@ Export-Package: com.github.fge.jsonschema.core.exceptions,
com.reprezen.swagedit.core.editor.outline,
com.reprezen.swagedit.core.handlers,
com.reprezen.swagedit.core.hyperlinks,
com.reprezen.swagedit.core.json.references,
com.reprezen.swagedit.core.model,
com.reprezen.swagedit.core.json,
com.reprezen.swagedit.core.json.references,
com.reprezen.swagedit.core.preferences,
com.reprezen.swagedit.core.quickfix,
com.reprezen.swagedit.core.schema,
Expand All @@ -54,5 +56,10 @@ Bundle-ClassPath: .,
lib/com.googlecode.libphonenumber_6.2.0.jar,
lib/com.github.fge.uri-template_0.9.0.jar,
lib/org.mozilla.rhino_1.0.0.7R4.jar,
lib/org.apache.commons.lang3_3.2.1.jar
lib/org.apache.commons.lang3_3.2.1.jar,
lib/com.fasterxml.jackson.core.jackson-annotations_2.9.0.jar,
lib/com.fasterxml.jackson.core.jackson-core_2.9.2.jar,
lib/com.fasterxml.jackson.core.jackson-databind_2.9.2.jar,
lib/com.fasterxml.jackson.dataformat.jackson-dataformat-yaml_2.9.2.jar,
lib/org.yaml.snakeyaml_1.18.0.jar
Bundle-Activator: com.reprezen.swagedit.core.Activator
20 changes: 7 additions & 13 deletions com.reprezen.swagedit.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ source.. = src/
output.. = target/classes/
bin.includes = .,\
META-INF/,\
lib/,\
lib/com.github.fge.json-schema-core_1.2.5.jar,\
lib/com.github.fge.json-schema-validator_2.2.6.jar,\
lib/com.github.fge.btf_1.2.0.jar,\
lib/com.github.fge.jackson-coreutils_1.8.0.jar,\
lib/com.github.fge.msg-simple_1.1.0.jar,\
lib/joda-time_2.3.0.jar,\
lib/javax.mail.api_1.4.3.jar,\
lib/com.googlecode.libphonenumber_6.2.0.jar,\
lib/com.github.fge.uri-template_0.9.0.jar,\
lib/org.mozilla.rhino_1.0.0.7R4.jar,\
lib/org.apache.commons.lang3_3.2.1.jar,\
icons/,\
plugin.xml,\
resources/
resources/,\
lib/,\
lib/com.fasterxml.jackson.core.jackson-annotations_2.9.0.jar,\
lib/com.fasterxml.jackson.core.jackson-core_2.9.2.jar,\
lib/com.fasterxml.jackson.core.jackson-databind_2.9.2.jar,\
lib/com.fasterxml.jackson.dataformat.jackson-dataformat-yaml_2.9.2.jar,\
lib/org.yaml.snakeyaml_1.18.0.jar
jars.compile.order = .
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.eclipse.ui.IWorkbenchCommandConstants.EDIT_CONTENT_ASSIST;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -52,8 +53,9 @@
import com.reprezen.swagedit.core.Activator.Icons;
import com.reprezen.swagedit.core.assist.contexts.ContextType;
import com.reprezen.swagedit.core.editor.JsonDocument;
import com.reprezen.swagedit.core.json.JsonModel;
import com.reprezen.swagedit.core.json.JsonRegion;
import com.reprezen.swagedit.core.json.references.Messages;
import com.reprezen.swagedit.core.model.Model;
import com.reprezen.swagedit.core.templates.SwaggerTemplateContext;
import com.reprezen.swagedit.core.utils.SwaggerFileFinder.Scope;

Expand All @@ -63,9 +65,9 @@
public abstract class JsonContentAssistProcessor extends TemplateCompletionProcessor
implements IContentAssistProcessor, ICompletionListener {

private final JsonProposalProvider proposalProvider;
private final JsonReferenceProposalProvider referenceProposalProvider;
private final ContentAssistant contentAssistant;
private final JsonProposalProvider proposalProvider;
private final JsonReferenceProposalProvider referenceProposalProvider;
private final ContentAssistant contentAssistant;

/**
* The pointer that helps us locate the current position of the cursor inside the document.
Expand All @@ -85,25 +87,25 @@ public abstract class JsonContentAssistProcessor extends TemplateCompletionProce
private boolean isRefCompletion = false;

private String[] textMessages;

public JsonContentAssistProcessor(ContentAssistant ca, String fileContentType) {
this(ca, new JsonProposalProvider(),
new JsonReferenceProposalProvider(ContextType.emptyContentTypeCollection(), fileContentType));
}

public JsonContentAssistProcessor(ContentAssistant ca, JsonProposalProvider proposalProvider, JsonReferenceProposalProvider referenceProposalProvider) {

public JsonContentAssistProcessor(ContentAssistant ca, String fileContentType) {
this(ca, new JsonProposalProvider(),
new JsonReferenceProposalProvider(ContextType.emptyContentTypeCollection(), fileContentType));
}

public JsonContentAssistProcessor(ContentAssistant ca, JsonProposalProvider proposalProvider,
JsonReferenceProposalProvider referenceProposalProvider) {
this.contentAssistant = ca;
this.proposalProvider = proposalProvider;
this.referenceProposalProvider = referenceProposalProvider;
this.textMessages = initTextMessages(null);
}

protected abstract TemplateStore getTemplateStore();

protected abstract ContextTypeRegistry getContextTypeRegistry();

protected abstract String getContextTypeId(Model model, String path);

protected abstract String getContextTypeId(JsonDocument doc, String path);

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
Expand Down Expand Up @@ -131,19 +133,31 @@ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int
column -= prefix.length();
}

Model model = document.getModel(documentOffset - prefix.length());
currentPath = model.getPath(line, column);
JsonModel model = null;
try {
model = new JsonModel(document.getSchema(), document.get(), false);
} catch (Exception e) {
try {
model = new JsonModel(document.getSchema(), document.get(0, documentOffset - prefix.length()), true);
} catch (BadLocationException | IOException ee) {
ee.printStackTrace();
}
}

JsonRegion range = model.findRegion(line + 1, column + 1);
currentPath = JsonPointer.compile(range.pointer.toString());

isRefCompletion = referenceProposalProvider.canProvideProposal(model, currentPath);

Collection<Proposal> p;
if (isRefCompletion) {
updateStatus(model);
p = referenceProposalProvider.getProposals(currentPath, document, currentScope);
p = referenceProposalProvider.getProposals(currentPath, model, currentScope);
} else {
clearStatus();
p = proposalProvider.getProposals(currentPath, model, prefix);
}

final Collection<ICompletionProposal> proposals = getCompletionProposals(p, prefix, documentOffset);
// compute template proposals
if (!isRefCompletion) {
Expand All @@ -164,10 +178,10 @@ private void maybeSwitchScope(int documentOffset) {
currentOffset = documentOffset;
}

protected void updateStatus(Model model) {
protected void updateStatus(JsonModel doc) {
if (contentAssistant != null) {
if (textMessages == null) {
textMessages = initTextMessages(model);
textMessages = initTextMessages(doc);
}
contentAssistant.setStatusLineVisible(true);
contentAssistant.setStatusMessage(textMessages[currentScope.getValue()]);
Expand All @@ -180,17 +194,17 @@ protected void clearStatus() {
}
}

protected String[] initTextMessages(Model model) {
IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
String bindingKey = bindingService.getBestActiveBindingFormattedFor(EDIT_CONTENT_ASSIST);
ContextType contextType = referenceProposalProvider.getContextTypes().get(model, getCurrentPath());
String context = contextType != null ? contextType.label() : "";
protected String[] initTextMessages(JsonModel doc) {
IBindingService bindingService = (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class);
String bindingKey = bindingService.getBestActiveBindingFormattedFor(EDIT_CONTENT_ASSIST);
ContextType contextType = referenceProposalProvider.getContextTypes().get(doc, getCurrentPath());
String context = contextType != null ? contextType.label() : "";

return new String[] { //
String.format(Messages.content_assist_proposal_project, bindingKey, context),
String.format(Messages.content_assist_proposal_workspace, bindingKey, context),
String.format(Messages.content_assist_proposal_local, bindingKey, context) };
}
return new String[] { //
String.format(Messages.content_assist_proposal_project, bindingKey, context),
String.format(Messages.content_assist_proposal_workspace, bindingKey, context),
String.format(Messages.content_assist_proposal_local, bindingKey, context) };
}

protected Collection<ICompletionProposal> getCompletionProposals(Collection<Proposal> proposals, String prefix,
int offset) {
Expand Down Expand Up @@ -254,15 +268,15 @@ public IContextInformationValidator getContextInformationValidator() {
return null;
}

@Override
protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region,
int relevance) {
@Override
protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region,
int relevance) {
if (context instanceof DocumentTemplateContext) {
context = new SwaggerTemplateContext((DocumentTemplateContext) context);
}
return new StyledTemplateProposal(template, context, region, getImage(template), getTemplateLabel(template),
relevance);
}
return new StyledTemplateProposal(template, context, region, getImage(template), getTemplateLabel(template),
relevance);
}

@Override
protected Template[] getTemplates(String contextTypeId) {
Expand All @@ -271,22 +285,23 @@ protected Template[] getTemplates(String contextTypeId) {

@Override
protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
Model model = null;
if (viewer.getDocument() instanceof JsonDocument) {
model = ((JsonDocument)viewer.getDocument()).getModel();
}
String contextType = getContextTypeId(model, currentPath.toString());
ContextTypeRegistry registry = getContextTypeRegistry();
if (registry != null) {
return registry.getContextType(contextType);
} else {
return null;
}
JsonModel model = null;
// if (viewer.getDocument() instanceof JsonDocument) {
// model = ((JsonDocument)viewer.getDocument()).getModel();
// }
// String contextType = getContextTypeId(model, currentPath.toString());
// ContextTypeRegistry registry = getContextTypeRegistry();
// if (registry != null) {
// return registry.getContextType(contextType);
// } else {
// return null;
// }
return null;
}

@Override
protected Image getImage(Template template) {
return Activator.getDefault().getImage(Icons.template_item);
return Activator.getDefault().getImage(Icons.template_item);
}

protected StyledString getTemplateLabel(Template template) {
Expand Down Expand Up @@ -323,9 +338,9 @@ private void resetScope() {
textMessages = null;
currentOffset = -1;
}

protected JsonPointer getCurrentPath() {
return currentPath;
return currentPath;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.reprezen.swagedit.core.assist.ext.ContentAssistExt;
import com.reprezen.swagedit.core.model.AbstractNode;
import com.reprezen.swagedit.core.model.Model;
import com.reprezen.swagedit.core.json.JsonModel;
import com.reprezen.swagedit.core.schema.ArrayTypeDefinition;
import com.reprezen.swagedit.core.schema.ComplexTypeDefinition;
import com.reprezen.swagedit.core.schema.JsonType;
Expand Down Expand Up @@ -58,36 +57,15 @@ public JsonProposalProvider(ContentAssistExt... extensions) {
* @param prefix
* @return proposals
*/
public Collection<Proposal> getProposals(JsonPointer pointer, Model model, String prefix) {
final AbstractNode node = model.find(pointer);
public Collection<Proposal> getProposals(JsonPointer pointer, JsonModel document, String prefix) {
JsonNode node = document.getContent().at(pointer);
if (node == null) {
return Collections.emptyList();
}
return getProposals(node.getType(), node, prefix);
return getProposals(document.getType(pointer), node, prefix);
}

/**
* Returns all proposals for the node inside the given model located at the given pointer.
*
* @param pointer
* @param model
* @return proposals
*/
public Collection<Proposal> getProposals(JsonPointer pointer, Model model) {
return getProposals(pointer, model, null);
}

/**
* Returns all proposals for the current node.
*
* @param node
* @return proposals
*/
public Collection<Proposal> getProposals(AbstractNode node) {
return getProposals(node.getType(), node, null);
}

protected Collection<Proposal> getProposals(TypeDefinition type, AbstractNode node, String prefix) {
protected Collection<Proposal> getProposals(TypeDefinition type, JsonNode node, String prefix) {
if (type instanceof ReferenceTypeDefinition) {
type = ((ReferenceTypeDefinition) type).resolve();
}
Expand Down Expand Up @@ -167,7 +145,7 @@ protected Proposal createPropertyProposal(String key, TypeDefinition type) {
return new Proposal(key + ":", key, type.getDescription(), labelType);
}

protected Collection<Proposal> createObjectProposals(ObjectTypeDefinition type, AbstractNode element,
protected Collection<Proposal> createObjectProposals(ObjectTypeDefinition type, JsonNode element,
String prefix) {
final Collection<Proposal> proposals = new LinkedHashSet<>();

Expand Down Expand Up @@ -209,7 +187,7 @@ protected Collection<Proposal> createObjectProposals(ObjectTypeDefinition type,
return proposals;
}

protected Collection<Proposal> createArrayProposals(ArrayTypeDefinition type, AbstractNode node) {
protected Collection<Proposal> createArrayProposals(ArrayTypeDefinition type, JsonNode node) {
Collection<Proposal> proposals = new LinkedHashSet<>();

if (type.itemsType.getType() == JsonType.ENUM) {
Expand All @@ -225,7 +203,7 @@ protected Collection<Proposal> createArrayProposals(ArrayTypeDefinition type, Ab
return proposals;
}

protected Collection<Proposal> createComplextTypeProposals(ComplexTypeDefinition type, AbstractNode node,
protected Collection<Proposal> createComplextTypeProposals(ComplexTypeDefinition type, JsonNode node,
String prefix) {
final Collection<Proposal> proposals = new LinkedHashSet<>();

Expand All @@ -244,7 +222,7 @@ protected Collection<String> enumLiterals(TypeDefinition type) {
return literals;
}

protected Collection<Proposal> createEnumProposals(TypeDefinition type, AbstractNode node) {
protected Collection<Proposal> createEnumProposals(TypeDefinition type, JsonNode node) {
final Collection<Proposal> proposals = new LinkedHashSet<>();
final String subType = type.asJson().has("type") ? //
type.asJson().get("type").asText() : //
Expand Down