Search in sources :

Example 1 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class AbstractJSONCompletionProposalComputer method computeCompletionProposals.

@Override
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
    ITextViewer textViewer = context.getViewer();
    int documentPosition = context.getInvocationOffset();
    setErrorMessage(null);
    fTextViewer = textViewer;
    IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition <= 0 ? 0 : documentPosition - 1);
    IJSONNode node = (IJSONNode) treeNode;
    while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) {
        node = node.getParentNode();
    }
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    ITextRegion completionRegion = getCompletionRegion(documentPosition, node);
    // Fix completion region in case of JSON_OBJECT_CLOSE
    if (completionRegion != null && completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_CLOSE && documentPosition > 0) {
        completionRegion = getCompletionRegion(documentPosition, node);
    }
    String matchString = EMPTY;
    if (completionRegion != null) {
        if (isPairValue(context, node)) {
            try {
                String nodeText = getNodeText(node);
                int colonIndex = nodeText.indexOf(COLON);
                int offset = documentPosition - node.getStartOffset();
                if (colonIndex >= 0 && offset >= 0) {
                    String str = nodeText.substring(colonIndex + 1, offset);
                    // $NON-NLS-1$
                    str = str.replaceAll(",", BLANK);
                    matchString = str;
                }
            } catch (BadLocationException e) {
            // ignore
            }
        } else {
            matchString = getMatchString(sdRegion, completionRegion, documentPosition);
        }
    }
    // compute normal proposals
    contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IJSONNode) treeNode, node != null ? node.getParentNode() : null, context);
    if (contentAssistRequest == null) {
        contentAssistRequest = new ContentAssistRequest((IJSONNode) treeNode, node != null ? node.getParentNode() : null, sdRegion, completionRegion, documentPosition, 0, EMPTY);
        setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
    }
    /*
		 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=123892 Only set this
		 * error message if nothing else was already set
		 */
    if (contentAssistRequest.getProposals().size() == 0 && getErrorMessage() == null) {
        setErrorMessage(JSONUIMessages.Content_Assist_not_availab_UI_);
    }
    ICompletionProposal[] props = contentAssistRequest.getCompletionProposals();
    return (props != null) ? Arrays.asList(props) : new ArrayList(0);
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 2 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class CompletionProposalCollectorsRegistryReader method addProposals.

public void addProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context, TargetType target) {
    IJSONNode node = contentAssistRequest.getNode();
    String contentTypeId = node.getModel().getContentTypeIdentifier();
    Collection<CompletionProposalMatcher> matchersByContentType = matchers.get(contentTypeId);
    if (matchersByContentType != null) {
        for (CompletionProposalMatcher matcher : matchersByContentType) {
            matcher.addProposalsIfMatch(contentAssistRequest, context, target);
        }
    }
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 3 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class JFaceNodeAdapter method notifyChanged.

/**
 * Called by the object being adapter (the notifier) when something has
 * changed.
 */
public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
    // there's probably some optimization that can be done.
    if (notifier instanceof IJSONNode) {
        Collection listeners = fAdapterFactory.getListeners();
        Iterator iterator = listeners.iterator();
        while (iterator.hasNext()) {
            Object listener = iterator.next();
            // INodeNotifier.CHANGE && changedFeature == null))) {
            if ((listener instanceof StructuredViewer) && ((eventType == INodeNotifier.STRUCTURE_CHANGED) || (eventType == INodeNotifier.CONTENT_CHANGED) || (eventType == INodeNotifier.CHANGE))) {
                if (DEBUG) {
                    System.out.println(// $NON-NLS-1$
                    "JFaceNodeAdapter notified on event type > " + eventType);
                }
                // refresh on structural and "unknown" changes
                StructuredViewer structuredViewer = (StructuredViewer) listener;
                // https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=5230
                if (structuredViewer.getControl() != null) {
                    getRefreshJob().refresh(structuredViewer, (IJSONNode) notifier);
                }
            }
        }
    }
}
Also used : Iterator(java.util.Iterator) Collection(java.util.Collection) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) StructuredViewer(org.eclipse.jface.viewers.StructuredViewer) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 4 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class RefreshStructureJob method addRefreshRequest.

private synchronized void addRefreshRequest(IJSONNode newNodeRequest) {
    /*
		 * note: the caller must NOT pass in null node request (which, since
		 * private method, we do not need to gaurd against here, as long as we
		 * gaurd against it in calling method.
		 */
    int size = fRefreshes.size();
    for (int i = 0; i < size; i++) {
        IJSONNode existingNodeRequest = fRefreshes.get(i);
        /*
			 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=157427 If we
			 * already have a request which equals the new request, discard
			 * the new request
			 */
        if (existingNodeRequest.equals(newNodeRequest)) {
            return;
        }
        /*
			 * If we already have a request which contains the new request,
			 * discard the new request
			 */
        if (contains(existingNodeRequest, newNodeRequest)) {
            return;
        }
        /*
			 * If new request contains any existing requests, replace it with
			 * new request. ISSUE: technically, we should replace ALL
			 * contained, existing requests (such as if many siblings already
			 * que'd up when their common parent is then requested, but, I'm
			 * not sure if that occurs much, in practice, or if there's an
			 * algorithm to quickly find them all. Actually, I guess we could
			 * just go through the _rest_ of the list (i+1 to size) and remove
			 * any that are contained by new request ... in future :) .
			 */
        if (contains(newNodeRequest, existingNodeRequest)) {
            fRefreshes.set(i, newNodeRequest);
            return;
        }
    }
    /*
		 * If we get to here, either from existing request list being zero
		 * length, or no exisitng requests "matched" new request, then add the
		 * new request.
		 */
    fRefreshes.add(newNodeRequest);
}
Also used : IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 5 with IJSONNode

use of org.eclipse.wst.json.core.document.IJSONNode in project webtools.sourceediting by eclipse.

the class RefreshStructureJob method contains.

/**
 * Simple hierarchical containment relationship. Note, this method returns
 * "false" if the two nodes are equal!
 *
 * @param root
 * @param possible
 * @return if the root is parent of possible, return true, otherwise
 *         return false
 */
private boolean contains(IJSONNode root, IJSONNode possible) {
    if (DEBUG) {
        // $NON-NLS-1$
        System.out.println("==============================================================================================================");
        // $NON-NLS-1$ //$NON-NLS-2$
        System.out.println("recursive call w/ root: " + root.getNodeName() + " and possible: " + possible);
        // $NON-NLS-1$
        System.out.println("--------------------------------------------------------------------------------------------------------------");
    }
    // can't contain the child if it's null
    if (root == null) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("returning false: root is null");
        }
        return false;
    }
    // nothing can be parent of Document node
    if (possible instanceof IJSONDocument) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("returning false: possible is Document node");
        }
        return false;
    }
    // document contains everything
    if (root instanceof IJSONDocument) {
        if (DEBUG) {
            // $NON-NLS-1$
            System.out.println("returning true: root is Document node");
        }
        return true;
    }
    // check parentage
    IJSONNode current = possible;
    // loop parents
    while ((current != null) && (current.getNodeType() != IJSONNode.DOCUMENT_NODE)) {
        // found it
        if (root.equals(current)) {
            if (DEBUG) {
                // $NON-NLS-1$ //$NON-NLS-2$
                System.out.println("   !!! found: " + possible.getNodeName() + " in subelement of: " + root.getNodeName());
            }
            return true;
        }
        current = current.getParentNode();
    }
    // never found it
    return false;
}
Also used : IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Aggregations

IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)56 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)10 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)9 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)9 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)8 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)4 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1