Search in sources :

Example 1 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class QuickOutline method setInput.

@Override
public void setInput(Object input) {
    if (input instanceof Model) {
        Model model = (Model) input;
        if (model.getPath() == null) {
            IFile currentFile = null;
            IEditorInput editorInput = editor.getEditorInput();
            if (editorInput instanceof IFileEditorInput) {
                currentFile = ((IFileEditorInput) editorInput).getFile();
            }
            if (currentFile != null) {
                model.setPath(currentFile.getFullPath());
            }
        }
    }
    treeViewer.setInput(input);
    treeViewer.setSelection(null, true);
}
Also used : IFile(org.eclipse.core.resources.IFile) IFileEditorInput(org.eclipse.ui.IFileEditorInput) Model(com.reprezen.swagedit.core.model.Model) IEditorInput(org.eclipse.ui.IEditorInput)

Example 2 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonContentAssistProcessor method getContextType.

@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;
    }
}
Also used : Model(com.reprezen.swagedit.core.model.Model) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) StyledString(org.eclipse.jface.viewers.StyledString) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument)

Example 3 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonContentAssistProcessor method computeCompletionProposals.

@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
    if (!(viewer.getDocument() instanceof JsonDocument)) {
        return super.computeCompletionProposals(viewer, documentOffset);
    }
    maybeSwitchScope(documentOffset);
    final JsonDocument document = (JsonDocument) viewer.getDocument();
    final ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    int line = 0, lineOffset = 0, column = 0;
    try {
        line = document.getLineOfOffset(documentOffset);
        lineOffset = document.getLineOffset(line);
        column = selection.getOffset() - lineOffset;
    } catch (BadLocationException e) {
    }
    final String prefix = extractPrefix(viewer, documentOffset);
    // column to resolve the path
    if (!prefix.isEmpty()) {
        column -= prefix.length();
    }
    Model model = document.getModel(documentOffset - prefix.length());
    currentPath = model.getPath(line, column);
    isRefCompletion = referenceProposalProvider.canProvideProposal(model, currentPath);
    Collection<Proposal> p;
    if (isRefCompletion) {
        updateStatus(model);
        p = referenceProposalProvider.getProposals(currentPath, document, currentScope);
    } else {
        clearStatus();
        p = proposalProvider.getProposals(currentPath, model, prefix);
    }
    final Collection<ICompletionProposal> proposals = getCompletionProposals(p, prefix, documentOffset);
    // compute template proposals
    if (!isRefCompletion) {
        final ICompletionProposal[] templateProposals = super.computeCompletionProposals(viewer, documentOffset);
        if (templateProposals != null && templateProposals.length > 0) {
            proposals.addAll(Lists.newArrayList(templateProposals));
        }
    }
    return proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Model(com.reprezen.swagedit.core.model.Model) StyledString(org.eclipse.jface.viewers.StyledString) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal)

Example 4 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class LinkOperationHyperlinkDetector method doDetect.

@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    Model model = doc.getModel();
    AbstractNode node = model.find(pointer);
    List<AbstractNode> nodes = model.findByType(JsonPointer.compile("/definitions/operation"));
    Iterator<AbstractNode> it = nodes.iterator();
    AbstractNode found = null;
    while (it.hasNext() && found == null) {
        AbstractNode current = it.next();
        AbstractNode value = current.get("operationId");
        if (value != null && Objects.equals(node.asValue().getValue(), value.asValue().getValue())) {
            found = value;
        }
    }
    if (found != null) {
        IRegion target = doc.getRegion(found.getPointer());
        if (target != null) {
            return new IHyperlink[] { new SwaggerHyperlink(info.text, viewer, info.region, target) };
        }
    }
    return null;
}
Also used : SwaggerHyperlink(com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) Model(com.reprezen.swagedit.core.model.Model) IRegion(org.eclipse.jface.text.IRegion)

Example 5 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceValidator method findTarget.

protected AbstractNode findTarget(JsonDocument doc, URI baseURI, JsonReference reference) {
    Model model = doc.getModel();
    AbstractNode valueNode = model.find(reference.getPointer());
    if (valueNode == null) {
        // Try to load the referenced node from an external document
        JsonNode externalDoc = reference.getDocument(doc, baseURI);
        if (externalDoc != null) {
            try {
                Model externalModel = Model.parse(model.getSchema(), externalDoc);
                valueNode = externalModel.find(reference.getPointer());
            } catch (Exception e) {
                // fail to parse the model or the pointer
                return null;
            }
        }
    }
    return valueNode;
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) Model(com.reprezen.swagedit.core.model.Model) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

Model (com.reprezen.swagedit.core.model.Model)9 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)6 JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)3 IRegion (org.eclipse.jface.text.IRegion)3 SwaggerHyperlink (com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink)2 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)2 StyledString (org.eclipse.jface.viewers.StyledString)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Matcher (java.util.regex.Matcher)1 IFile (org.eclipse.core.resources.IFile)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 Position (org.eclipse.jface.text.Position)1 Region (org.eclipse.jface.text.Region)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1 IEditorInput (org.eclipse.ui.IEditorInput)1