Search in sources :

Example 1 with JsonDocument

use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.

the class AbstractJsonHyperlinkDetector method detectHyperlinks.

@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    JsonDocument document = (JsonDocument) textViewer.getDocument();
    JsonPointer basePath = document.getPath(region);
    if (!canDetect(basePath)) {
        return null;
    }
    HyperlinkInfo info = getHyperlinkInfo(textViewer, region);
    if (info == null) {
        return null;
    }
    return doDetect(document, textViewer, info, basePath);
}
Also used : JsonPointer(com.fasterxml.jackson.core.JsonPointer) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument)

Example 2 with JsonDocument

use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.

the class AbstractJsonHyperlinkDetector method getHyperlinkInfo.

protected HyperlinkInfo getHyperlinkInfo(ITextViewer viewer, IRegion region) {
    final JsonDocument document = (JsonDocument) viewer.getDocument();
    IRegion line;
    try {
        line = document.getLineInformationOfOffset(region.getOffset());
    } catch (BadLocationException e) {
        return null;
    }
    String lineContent;
    try {
        lineContent = document.get(line.getOffset(), line.getLength());
    } catch (BadLocationException e) {
        return null;
    }
    if (lineContent == null || emptyToNull(lineContent) == null) {
        return null;
    }
    final int column = region.getOffset() - line.getOffset();
    final IRegion selected = getSelectedRegion(line, lineContent, column);
    String text;
    try {
        text = document.get(selected.getOffset(), selected.getLength());
    } catch (BadLocationException e) {
        return null;
    }
    if (emptyToNull(text) == null || text.trim().equals(":") || text.trim().equals("$ref:")) {
        return null;
    }
    return new HyperlinkInfo(selected, text, column);
}
Also used : JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with JsonDocument

use of com.reprezen.swagedit.core.editor.JsonDocument 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 4 with JsonDocument

use of com.reprezen.swagedit.core.editor.JsonDocument 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 5 with JsonDocument

use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonContentOutlinePage method update.

protected void update() {
    final IDocument document = documentProvider.getDocument(currentInput);
    if (document instanceof JsonDocument) {
        final Model model = ((JsonDocument) document).getModel();
        if (model == null) {
            return;
        }
        final TreeViewer viewer = getTreeViewer();
        if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed()) {
            // we keep all elements that have been previously expanded
            // so the tree stay in the same state between updates.
            final Object[] expandedElements = viewer.getExpandedElements();
            final List<Object> elements = expandedElements != null ? Arrays.asList(expandedElements) : null;
            viewer.setInput(model);
            if (elements != null && !elements.isEmpty()) {
                Iterable<AbstractNode> newElements = filter(model.allNodes(), new Predicate<AbstractNode>() {

                    @Override
                    public boolean apply(AbstractNode node) {
                        return elements.contains(node);
                    }
                });
                viewer.setExpandedElements(toArray(newElements, AbstractNode.class));
            }
        }
    }
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Model(com.reprezen.swagedit.core.model.Model) IDocument(org.eclipse.jface.text.IDocument) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument)

Aggregations

JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)6 Model (com.reprezen.swagedit.core.model.Model)3 BadLocationException (org.eclipse.jface.text.BadLocationException)2 StyledString (org.eclipse.jface.viewers.StyledString)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)1 IOException (java.io.IOException)1 IDocument (org.eclipse.jface.text.IDocument)1 IRegion (org.eclipse.jface.text.IRegion)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1