Search in sources :

Example 46 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class StructuredRegionProcessor method _getOuterRegion.

/**
 * Assumes that when this method is called, region1's node start offset >=
 * region2's node start offset. Determines if region1 contains region2 or
 * vice versa. Returns region1 if:
 * <ul>
 * <li>region1's node region == region2's node region</li>
 * <li>region1's node region contains region2's node region</li>
 * </ul>
 * Returns region2 if:
 * <ul>
 * <li>region1's node region and region2's node region starts at same
 * offset but region2's node region is longer</li>
 * </ul>
 * Returns null otherwise.
 *
 * @param region1
 * @param region2
 * @param sModel
 * @param region1NodeStart
 * @param region2NodeStart
 * @return outer dirty region or null if none exists.
 */
private DirtyRegion _getOuterRegion(DirtyRegion region1, DirtyRegion region2, IStructuredModel sModel, int region1NodeStart, int region2NodeStart) {
    DirtyRegion outer = null;
    int region1NodeEnd = -1;
    int region2NodeEnd = -1;
    // then check if region1's end appears after
    // region2's end
    IndexedRegion region1EndNode = sModel.getIndexedRegion(region1.getOffset() + region1.getLength());
    if (region1EndNode == null) {
        // if no end, just assume region spans all the
        // way to the end so it includes other region
        outer = region1;
    } else {
        region1NodeEnd = region1EndNode.getEndOffset();
        IndexedRegion region2EndNode = sModel.getIndexedRegion(region2.getOffset() + region2.getLength());
        region2NodeEnd = region2EndNode != null ? region2EndNode.getEndOffset() : getDocument().getLength();
        if (region1NodeEnd >= region2NodeEnd) {
            // root contains or is equal to possible
            outer = region1;
        } else if (region1NodeStart == region2NodeStart && region2NodeEnd >= region1NodeEnd) {
            // possible contains root because they
            // both start at same place but possible
            // is longer
            outer = region2;
        }
    }
    if (DEBUG) {
        if (outer != null)
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
            System.out.println("checking if [" + region1NodeStart + ":" + region1NodeEnd + "] contains [" + region2NodeStart + ":" + region2NodeEnd + "] ... " + outer.toString());
        else
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
            System.out.println("checking if [" + region1NodeStart + ":" + region1NodeEnd + "] contains [" + region2NodeStart + ":" + region2NodeEnd + "] ... NO CONTAIN");
    }
    return outer;
}
Also used : DirtyRegion(org.eclipse.jface.text.reconciler.DirtyRegion) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 47 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class JSPContentSourceValidator method validate.

/**
 * 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)
        // error
        return;
    try {
        IDOMDocument document = null;
        if (model instanceof IDOMModel) {
            document = ((IDOMModel) model).getDocument();
        }
        if (document == null || !hasHTMLFeature(document))
            // ignore
            return;
        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
        // model.getIndexedRegion(dirtyRegion.getOffset());
        IndexedRegion ir = getCoveringNode(dirtyRegion);
        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)
                // error
                return;
            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();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IMessage(org.eclipse.wst.validation.internal.provisional.core.IMessage) Message(org.eclipse.wst.validation.internal.core.Message) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Text(org.w3c.dom.Text) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) INodeAdapterFactory(org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ValidationAdapter(org.eclipse.wst.sse.core.internal.validate.ValidationAdapter) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 48 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class CleanupProcessorCSS method cleanupModel.

public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
    CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
    if (structuredModel instanceof ICSSModel) {
        ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
        CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
        StringBuffer buf = formatter.cleanup(doc);
        if (buf != null) {
            int startOffset = ((IndexedRegion) doc).getStartOffset();
            int endOffset = ((IndexedRegion) doc).getEndOffset();
            formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
        }
    } else if (structuredModel instanceof IDOMModel) {
        List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
        if (cssnodes != null && !cssnodes.isEmpty()) {
            ICSSModel model = null;
            for (int i = 0; i < cssnodes.size(); i++) {
                ICSSNode node = (ICSSNode) cssnodes.get(i);
                CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
                StringBuffer buf = formatter.cleanup(node);
                if (buf != null) {
                    int startOffset = ((IndexedRegion) node).getStartOffset();
                    int endOffset = ((IndexedRegion) node).getEndOffset();
                    if (model == null) {
                        model = node.getOwnerDocument().getModel();
                    }
                    formatUtil.replaceSource(model, startOffset, endOffset - startOffset, buf.toString());
                }
            }
        }
    }
}
Also used : CSSSourceFormatter(org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter) CSSFormatUtil(org.eclipse.wst.css.core.internal.formatter.CSSFormatUtil) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) List(java.util.List) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 49 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class CleanupProcessorJSON method cleanupModel.

public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
    JSONFormatUtil formatUtil = JSONFormatUtil.getInstance();
    if (structuredModel instanceof IJSONModel) {
        IJSONDocument doc = ((IJSONModel) structuredModel).getDocument();
        IJSONSourceFormatter formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
        StringBuilder buf = formatter.cleanup(doc);
        if (buf != null) {
            int startOffset = ((IndexedRegion) doc).getStartOffset();
            int endOffset = ((IndexedRegion) doc).getEndOffset();
            formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
        }
    }
}
Also used : JSONFormatUtil(org.eclipse.wst.json.core.internal.format.JSONFormatUtil) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IJSONSourceFormatter(org.eclipse.wst.json.core.internal.format.IJSONSourceFormatter) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 50 with IndexedRegion

use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.

the class JSPPropertyCompletionProposalComputer method addBeanNameProposals.

/**
 * <p>Add bean name propoasals to the given {@link ContentAssistRequest}</p>
 *
 * @param contentAssistRequest
 * @param node
 * @param matchString
 */
private void addBeanNameProposals(ContentAssistRequest contentAssistRequest, IDOMNode node, String matchString) {
    // will not catch useBeans specified using other than actual DOM Nodes
    NodeList useBeans = node.getOwnerDocument().getElementsByTagName(JSP11Namespace.ElementName.USEBEAN);
    if (useBeans != null) {
        // $NON-NLS-1$
        String id = "";
        String displayString = null;
        String classOrType = null;
        String imageName = JSPEditorPluginImages.IMG_OBJ_CLASS_OBJ;
        for (int j = 0; j < useBeans.getLength(); j++) {
            if (useBeans.item(j).getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Element useBean = (Element) useBeans.item(j);
            if (useBean instanceof IndexedRegion && ((IndexedRegion) useBean).getStartOffset() < node.getStartOffset() && useBean.hasAttribute(JSP11Namespace.ATTR_NAME_ID)) {
                id = useBean.hasAttribute(JSP11Namespace.ATTR_NAME_ID) ? StringUtils.strip(useBean.getAttribute(JSP11Namespace.ATTR_NAME_ID)) : null;
                displayString = null;
                classOrType = null;
                imageName = JSPEditorPluginImages.IMG_OBJ_CLASS_OBJ;
                // set the Image based on whether the class, type, or beanName attribute is present
                if (useBean.hasAttribute(JSP11Namespace.ATTR_NAME_CLASS))
                    classOrType = useBean.getAttribute(JSP11Namespace.ATTR_NAME_CLASS);
                if ((classOrType == null || classOrType.length() < 1) && useBean.hasAttribute(JSP11Namespace.ATTR_NAME_TYPE)) {
                    classOrType = useBean.getAttribute(JSP11Namespace.ATTR_NAME_TYPE);
                    imageName = JSPEditorPluginImages.IMG_OBJ_PUBLIC;
                }
                if ((classOrType == null || classOrType.length() < 1) && useBean.hasAttribute(JSP11Namespace.ATTR_NAME_BEAN_NAME)) {
                    classOrType = useBean.getAttribute(JSP11Namespace.ATTR_NAME_BEAN_NAME);
                    imageName = JSPEditorPluginImages.IMG_OBJ_PUBLIC;
                }
                if (classOrType != null && classOrType.length() > 0) {
                    // $NON-NLS-1$
                    displayString = id + " - " + classOrType;
                } else {
                    displayString = id;
                }
                // https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=2341
                if (id != null) {
                    // filter
                    if (matchString.length() == 0 || id.startsWith(matchString)) {
                        CustomCompletionProposal proposal = new // $NON-NLS-1$ //$NON-NLS-2$
                        CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
                        "\"" + id + "\"", contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), id.length() + 2, JSPEditorPluginImageHelper.getInstance().getImage(imageName), displayString, null, null, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Aggregations

IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)148 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)32 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)31 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)27 Node (org.w3c.dom.Node)27 Region (org.eclipse.jface.text.Region)21 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)21 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)21 CSSCleanupStrategy (org.eclipse.wst.css.core.internal.cleanup.CSSCleanupStrategy)20 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)17 ArrayList (java.util.ArrayList)11 List (java.util.List)11 ITextSelection (org.eclipse.jface.text.ITextSelection)11 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)8 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)8 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)8 Preferences (org.eclipse.core.runtime.Preferences)7 IDocument (org.eclipse.jface.text.IDocument)7