public List collectJSONNodes(IStructuredModel model, int start, int length) { List nodes = new ArrayList(); IndexedRegion startNode = model.getIndexedRegion(start); IndexedRegion endNode = model.getIndexedRegion(start + length - 1); if (startNode == null || endNode == null) { return nodes; } if (model instanceof IJSONModel && startNode instanceof IJSONNode && endNode instanceof IJSONNode) { // JSON model IJSONNode ca = getCommonAncestor((IJSONNode) startNode, (IJSONNode) endNode); if (ca != null) { for (IJSONNode node = ca.getFirstChild(); node != null && start + length < ((IndexedRegion) node).getStartOffset(); node = node.getNextSibling()) { if (start < ((IndexedRegion) node).getEndOffset()) { nodes.add(node); } } } } return nodes; }
/** * To see whether the textSelection start and end are on the same. * * @param model * @param textSelection * @return the node */ static boolean isSame(IStructuredModel model, TextSelection textSelection) { if (model != null && textSelection != null) { int t1 = textSelection.getOffset(); int t2 = textSelection.getLength() + t1; return model.getIndexedRegion(t1) == model.getIndexedRegion(t2); } return false; }
protected IndexedRegion getCorrespondingNode(IStructuredDocumentRegion sdRegion) { IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); IndexedRegion indexedRegion = null; try { if (sModel != null) indexedRegion = sModel.getIndexedRegion(sdRegion.getStart()); } finally { if (sModel != null) sModel.releaseFromRead(); } return indexedRegion; }
public void testGetIndexedRegion() throws IOException, CoreException { IStructuredModel model = getTestModel(); try { model.getIndexedRegion(0); assertTrue(true); } finally { if (model != null) { model.releaseFromEdit(); } } }
/** * Get node at indexed position. * * @param model * @param position * @return the node at position */ static Node getNodeAt(IStructuredModel model, int position) { try { IndexedRegion region = model.getIndexedRegion(position); if (region instanceof Node) { return (Node) region; } return null; } catch (Exception e) { // "Error in region node creation" _log.error("Error.EditModelQuery.1", e); // $NON-NLS-1$ return null; } }
/** * Returns the closest IndexedRegion for the offset and viewer allowing for differences between * viewer offsets and model positions. note: this method returns an IndexedRegion for read only * * @param viewer the viewer whose document is used to compute the proposals * @param documentOffset an offset within the document for which completions should be computed * @return an IndexedRegion */ public static IndexedRegion getNodeAt(ITextViewer viewer, int documentOffset) { if (viewer == null) return null; IndexedRegion node = null; IModelManager mm = StructuredModelManager.getModelManager(); IStructuredModel model = null; if (mm != null) model = mm.getExistingModelForRead(viewer.getDocument()); try { if (model != null) { int lastOffset = documentOffset; node = model.getIndexedRegion(documentOffset); while (node == null && lastOffset >= 0) { lastOffset--; node = model.getIndexedRegion(lastOffset); } } } finally { if (model != null) model.releaseFromRead(); } return node; }
/** * originally copied from org.eclipse.wst.xml.ui.internal.hyperlink.XMLHyperlinkDetector this * method grabs the IDOMModel for the IDocument, performs the passed operation on the node at the * offset and then releases the IDOMModel operation's Node value is also an instance of * IndexedRegion * * @param offset */ public static void performOnCurrentElement( IDocument document, int offset, NodeOperation<Node> operation) { assert document != null; assert operation != null; // get the current node at the offset (returns either: element, // doctype, text) IStructuredModel sModel = null; try { sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document); if (sModel != null) { IndexedRegion inode = sModel.getIndexedRegion(offset); if (inode == null) { inode = sModel.getIndexedRegion(offset - 1); } if (inode instanceof Node) { operation.process((Node) inode, sModel.getStructuredDocument()); } } } finally { if (sModel != null) { sModel.releaseFromRead(); } } }
/** * To see if the range and text selection covered the same range. * * @param model * @param range * @param textSelection * @return true if same */ static boolean isSame(IStructuredModel model, DesignRange range, TextSelection textSelection) { if (model != null && range != null && textSelection != null) { int t1 = textSelection.getOffset(); int t2 = textSelection.getLength() + t1; int r1 = getIndexedRegionLocation(DOMRangeHelper.toDOMRange(range).getStartPosition()); int r2 = getIndexedRegionLocation(DOMRangeHelper.toDOMRange(range).getEndPosition()); return (model.getIndexedRegion(t1) == model.getIndexedRegion(r1) && // model.getIndexedRegion(t2) == model.getIndexedRegion(r2)) || (model.getIndexedRegion(t2) == model.getIndexedRegion(r1) && // model.getIndexedRegion(t1) == model.getIndexedRegion(r2)); } return false; }
/** * This validate call is for the ISourceValidator partial document validation approach * * @param dirtyRegion * @param helper * @param reporter * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator */ public void validate(IRegion dirtyRegion, IValidationContext helper, IReporter reporter) { if (helper == null || fDocument == null || !fEnableSourceValidation) return; if ((reporter != null) && (reporter.isCancelled() == true)) { throw new OperationCanceledException(); } IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); if (model == null) return; // error try { IDOMDocument document = null; if (model instanceof IDOMModel) { document = ((IDOMModel) model).getDocument(); } if (document == null || !hasHTMLFeature(document)) return; // ignore ITextFileBuffer fb = FileBufferModelManager.getInstance().getBuffer(fDocument); if (fb == null) return; IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fb.getLocation()); if (file == null || !file.exists()) return; // this will be the wrong region if it's Text (instead of Element) // we don't know how to validate Text IndexedRegion ir = getCoveringNode(dirtyRegion); // model.getIndexedRegion(dirtyRegion.getOffset()); if (ir instanceof Text) { while (ir != null && ir instanceof Text) { // it's assumed that this gets the IndexedRegion to // the right of the end offset ir = model.getIndexedRegion(ir.getEndOffset()); } } if (ir instanceof INodeNotifier) { INodeAdapterFactory factory = HTMLValidationAdapterFactory.getInstance(); ValidationAdapter adapter = (ValidationAdapter) factory.adapt((INodeNotifier) ir); if (adapter == null) return; // error if (reporter != null) { HTMLValidationReporter rep = null; rep = getReporter(reporter, file, (IDOMModel) model); rep.clear(); adapter.setReporter(rep); Message mess = new LocalizedMessage( IMessage.LOW_SEVERITY, file.getFullPath().toString().substring(1)); reporter.displaySubtask(this, mess); } adapter.validate(ir); } } finally { if (model != null) model.releaseFromRead(); } }
/* * Search the RegionContainer's regions looking for JSP content. If valid * content is found, return the position >= 0 If no valid content is * found, return NO_VALID_CONTENT. If a region starts after the line's * endOffset, return END_OF_LINE. */ private static int getValidRegionPosition( IStructuredModel model, ITextRegionCollection regionContainer, int startOffset, int endOffset) { ITextRegionList regions = regionContainer.getRegions(); for (int i = 0; i < regions.size(); i++) { ITextRegion region = regions.get(i); if (region instanceof ITextRegionCollection) { int validPosition = getValidRegionPosition(model, (ITextRegionCollection) region, startOffset, endOffset); if (validPosition == END_OF_LINE || validPosition >= 0) return validPosition; } else { // region must be at least partially on selected line if (regionContainer.getEndOffset(region) > startOffset) { int regionStartOffset = regionContainer.getStartOffset(region); // if region starts after line's endOffset, we're done // searching if (regionStartOffset > endOffset) return END_OF_LINE; // If region is JSP content, make sure the language is // Java not Javascript by // checking the content assist adapter's type. if (region.getType().equals(DOMJSPRegionContexts.JSP_CONTENT)) { // DWM: this logic is not incorrect ... given changes // to adapters, etc. // but probably don't need anything here, since both // Java and JavaScript // are supported in V5. // nsd_TODO: verify this!!! // INodeNotifier notifier = // (INodeNotifier)model.getNode(region.getStartOffset()); // IAdapterFactory factory = // model.getFactoryRegistry().getFactoryFor(ContentAssistAdapter.class); // if(factory instanceof // HTMLContentAssistAdapterFactory) { // INodeAdapter adapter = // ((HTMLContentAssistAdapterFactory)factory).createAdapter(notifier, // region); // if(adapter != null && adapter instanceof // JSPJavaContentAssistAdapter) if (regionStartOffset > startOffset) return regionStartOffset; else return startOffset; // } } // a custom tag, jsp:useBean, getproperty or setproperty // statement is also a valid breakpoint location else if (region.getType().equals(DOMRegionContext.XML_TAG_NAME) && (isCustomTagRegion(model.getIndexedRegion(regionStartOffset)) || regionContainer.getText(region).equals(JSP12Namespace.ElementName.USEBEAN) || regionContainer.getText(region).equals(JSP12Namespace.ElementName.GETPROPERTY) || regionContainer .getText(region) .equals(JSP12Namespace.ElementName.SETPROPERTY))) { if (regionStartOffset > startOffset) return regionStartOffset; else return startOffset; } else { // Defect #241090, the Text Nodes inside of JSP // scriptlets, expressions, and declarations are valid // breakpoint-able locations boolean isCodeNode = false; IndexedRegion node = model.getIndexedRegion(regionStartOffset); if (node != null && node instanceof Node) { Node domNode = (Node) node; Node root = domNode.getOwnerDocument().getDocumentElement(); if (root != null && root.getNodeName().equals(JSP12Namespace.ElementName.ROOT) && domNode.getNodeType() == Node.TEXT_NODE && domNode.getParentNode() != null) { String parentName = domNode.getParentNode().getNodeName(); isCodeNode = parentName.equals(JSP12Namespace.ElementName.SCRIPTLET) || parentName.equals(JSP12Namespace.ElementName.EXPRESSION) || parentName.equals(JSP12Namespace.ElementName.DECLARATION); } } if (isCodeNode) { if (regionStartOffset > startOffset) return regionStartOffset; else return startOffset; } } } } } return NO_VALID_CONTENT; }
/** * Get a node that is at Indexed position 'pos' in 'model'. * * @param model * @param pos * @return the */ IndexedRegion getPosNode(IStructuredModel model, int pos) { IndexedRegion inode = model.getIndexedRegion(pos); return inode; }