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);
}
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);
}
}
}
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);
}
}
}
}
}
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);
}
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;
}
Aggregations