Search in sources :

Example 61 with IStructuredModel

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

the class CSSContentAssistProcessor method computeCompletionProposals.

/**
 * Return a list of proposed code completions based on the specified
 * location within the document that corresponds to the current cursor
 * position within the text-editor control.
 *
 * @param documentPosition
 *            a location within the document
 * @return an array of code-assist items
 */
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentPosition) {
    IndexedRegion indexedNode = ContentAssistUtils.getNodeAt(viewer, documentPosition + fDocumentOffset);
    IDOMNode xNode = null;
    IDOMNode parent = null;
    CSSProposalArranger arranger = null;
    boolean isEmptyDocument = false;
    // If there is a selected region, we'll need to replace the text
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    boolean selected = (selection != null && selection.getText() != null && selection.getText().trim().length() > 0);
    // if(indexedNode == null) return new ICompletionProposal[0];
    if (indexedNode instanceof IDOMNode) {
        xNode = (IDOMNode) indexedNode;
        parent = (IDOMNode) xNode.getParentNode();
    }
    // case
    if ((xNode != null) && xNode.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag w/out region
        IStructuredModel cssModel = getCSSModel(xNode);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = documentPosition + fDocumentOffset;
            int pos = 0;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, (char) 0, selected);
        }
    } else if ((parent != null) && parent.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
        // now we know the cursor is in a <style> tag with a region
        // use the parent because that will be the <style> tag
        IStructuredModel cssModel = getCSSModel(parent);
        if (cssModel != null) {
            // adjust offsets for embedded style
            int offset = indexedNode.getStartOffset();
            int pos = documentPosition - offset;
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, (char) 0, selected);
        }
    } else if (indexedNode instanceof IDOMNode) {
        // get model for node w/ style attribute
        IStructuredModel cssModel = getCSSModel((IDOMNode) indexedNode);
        if (cssModel != null) {
            IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
            if (keyIndexedNode == null) {
                keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
            }
            if (keyIndexedNode instanceof ICSSNode) {
                // inline style for a tag, not embedded
                arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote, selected);
            }
        }
    } else if (indexedNode instanceof ICSSNode) {
        // when editing external CSS using CSS Designer, ICSSNode is
        // passed.
        ICSSDocument cssdoc = ((ICSSNode) indexedNode).getOwnerDocument();
        if (cssdoc != null) {
            IStructuredModel cssModel = cssdoc.getModel();
            if (cssModel != null) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote, selected);
                }
            }
        }
    } else if ((indexedNode == null) && isViewerEmpty(viewer)) {
        isEmptyDocument = true;
        // the top of empty CSS Document
        IStructuredModel cssModel = null;
        try {
            cssModel = StructuredModelManager.getModelManager().getExistingModelForRead(viewer.getDocument());
            if (cssModel instanceof ICSSModel) {
                IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
                if (keyIndexedNode == null) {
                    keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
                }
                if (keyIndexedNode instanceof ICSSNode) {
                    // inline style for a tag, not embedded
                    arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote);
                }
            }
        } finally {
            if (cssModel != null)
                cssModel.releaseFromRead();
        }
    }
    ICompletionProposal[] proposals = new ICompletionProposal[0];
    if (arranger != null) {
        fDocumentOffset = 0;
        proposals = arranger.getProposals();
        ICompletionProposal[] newfileproposals = new ICompletionProposal[0];
        ICompletionProposal[] anyproposals = new ICompletionProposal[0];
        // add template proposals
        if (getTemplateCompletionProcessor() != null) {
            if (isEmptyDocument) {
                getTemplateCompletionProcessor().setContextType(TemplateContextTypeIdsCSS.NEW);
                newfileproposals = getTemplateCompletionProcessor().computeCompletionProposals(viewer, documentPosition);
            }
            getTemplateCompletionProcessor().setContextType(TemplateContextTypeIdsCSS.ALL);
            anyproposals = getTemplateCompletionProcessor().computeCompletionProposals(viewer, documentPosition);
        }
        // add end tag if parent is not closed
        ICompletionProposal endTag = XMLContentAssistUtilities.computeXMLEndTagProposal(viewer, documentPosition, indexedNode, HTML40Namespace.ElementName.STYLE, SharedXMLEditorPluginImageHelper.IMG_OBJ_TAG_GENERIC);
        // add the additional proposals
        int additionalLength = newfileproposals.length + anyproposals.length;
        additionalLength = (endTag != null) ? ++additionalLength : additionalLength;
        if (additionalLength > 0) {
            ICompletionProposal[] plusOnes = new ICompletionProposal[proposals.length + additionalLength];
            int appendPos = proposals.length;
            // add end tag proposal
            if (endTag != null) {
                System.arraycopy(proposals, 0, plusOnes, 1, proposals.length);
                plusOnes[0] = endTag;
                ++appendPos;
            } else {
                System.arraycopy(proposals, 0, plusOnes, 0, proposals.length);
            }
            // add items in newfileproposals
            for (int i = 0; i < newfileproposals.length; ++i) {
                plusOnes[appendPos + i] = newfileproposals[i];
            }
            // add items in anyproposals
            appendPos = appendPos + newfileproposals.length;
            for (int i = 0; i < anyproposals.length; ++i) {
                plusOnes[appendPos + i] = anyproposals[i];
            }
            proposals = plusOnes;
        }
    }
    return proposals;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSDocument(org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 62 with IStructuredModel

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

the class CSSHyperlinkDetector method getNode.

private ICSSNode getNode(IDocument document, IRegion region) {
    if (!(document instanceof IStructuredDocument))
        return null;
    IStructuredModel model = null;
    ICSSNode node = null;
    try {
        model = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument) document);
        node = (ICSSNode) model.getIndexedRegion(region.getOffset());
    } finally {
        if (model != null) {
            model.releaseFromRead();
        }
    }
    return node;
}
Also used : IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)

Example 63 with IStructuredModel

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

the class CleanupDocumentHandler method execute.

public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
    ITextEditor textEditor = null;
    if (editorPart instanceof ITextEditor)
        textEditor = (ITextEditor) editorPart;
    else {
        Object o = editorPart.getAdapter(ITextEditor.class);
        if (o != null)
            textEditor = (ITextEditor) o;
    }
    if (textEditor != null) {
        final ITextEditor editor = textEditor;
        Dialog cleanupDialog = new CleanupDialogCSS(editor.getSite().getShell());
        if (cleanupDialog.open() == Window.OK) {
            // setup runnable
            Runnable runnable = new Runnable() {

                public void run() {
                    IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor();
                    if (cleanupProcessor != null) {
                        IStructuredModel model = null;
                        try {
                            model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                            if (model != null)
                                cleanupProcessor.cleanupModel(model);
                        } finally {
                            if (model != null)
                                model.releaseFromEdit();
                        }
                    }
                }
            };
            // TODO: make independent of 'model'.
            IStructuredModel model = null;
            try {
                model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                if (model != null) {
                    // begin recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    // $NON-NLS-1$ //$NON-NLS-2$
                    model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength());
                    // tell the model that we are about to make a big
                    // model change
                    model.aboutToChangeModel();
                    // run
                    BusyIndicator.showWhile(editor.getEditorSite().getWorkbenchWindow().getShell().getDisplay(), runnable);
                }
            } finally {
                if (model != null) {
                    // tell the model that we are done with the big
                    // model
                    // change
                    model.changedModel();
                    // end recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    model.endRecording(this, selection.getOffset(), selection.getLength());
                    model.releaseFromEdit();
                }
            }
        }
    }
    return null;
}
Also used : IStructuredCleanupProcessor(org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Dialog(org.eclipse.jface.dialogs.Dialog) IEditorPart(org.eclipse.ui.IEditorPart) CleanupDialogCSS(org.eclipse.wst.css.ui.internal.edit.ui.CleanupDialogCSS) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 64 with IStructuredModel

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

the class CSSTextPropertyDescriptor method createPropertyEditor.

/**
 * @return org.eclipse.jface.viewers.CellEditor
 * @param parent
 *            org.eclipse.swt.widgets.Composite
 */
public CellEditor createPropertyEditor(Composite parent) {
    ICSSModel model = fNode.getOwnerDocument().getModel();
    if (model == null)
        return null;
    if (model.getStyleSheetType() == ICSSModel.EXTERNAL && findEditor(model) == null)
        return null;
    // check whether IFile is readonly to prohibit editing before
    // validateEdit()
    IStructuredModel structuredModel = model;
    if (model.getStyleSheetType() != ICSSModel.EXTERNAL) {
        structuredModel = ((IDOMNode) model.getOwnerDOMNode()).getModel();
        if (structuredModel == null)
            return null;
    }
    IFile file = CSSPathService.location2File(structuredModel.getBaseLocation());
    if (file == null || file.isReadOnly())
        return null;
    return super.createPropertyEditor(parent);
}
Also used : IFile(org.eclipse.core.resources.IFile) ICSSModel(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)

Example 65 with IStructuredModel

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

the class CleanupActionCSSDelegate method run.

public void run(IAction action) {
    if (fEditor instanceof ITextEditor) {
        final ITextEditor editor = (ITextEditor) fEditor;
        Dialog cleanupDialog = new CleanupDialogCSS(editor.getSite().getShell());
        if (cleanupDialog.open() == Window.OK) {
            // setup runnable
            Runnable runnable = new Runnable() {

                public void run() {
                    IStructuredCleanupProcessor cleanupProcessor = getCleanupProcessor();
                    if (cleanupProcessor != null) {
                        IStructuredModel model = null;
                        try {
                            model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                            if (model != null)
                                cleanupProcessor.cleanupModel(model);
                        } finally {
                            if (model != null)
                                model.releaseFromEdit();
                        }
                    }
                }
            };
            // TODO: make independent of 'model'.
            IStructuredModel model = null;
            try {
                model = StructuredModelManager.getModelManager().getExistingModelForEdit(editor.getDocumentProvider().getDocument(editor.getEditorInput()));
                if (model != null) {
                    // begin recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    // $NON-NLS-1$ //$NON-NLS-2$
                    model.beginRecording(this, SSEUIMessages.Cleanup_Document_UI_, SSEUIMessages.Cleanup_Document_UI_, selection.getOffset(), selection.getLength());
                    // tell the model that we are about to make a big
                    // model change
                    model.aboutToChangeModel();
                    // run
                    BusyIndicator.showWhile(fEditor.getEditorSite().getWorkbenchWindow().getShell().getDisplay(), runnable);
                }
            } finally {
                if (model != null) {
                    // tell the model that we are done with the big
                    // model
                    // change
                    model.changedModel();
                    // end recording
                    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
                    model.endRecording(this, selection.getOffset(), selection.getLength());
                    model.releaseFromEdit();
                }
            }
        }
    }
}
Also used : IStructuredCleanupProcessor(org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupProcessor) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) Dialog(org.eclipse.jface.dialogs.Dialog) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ITextSelection(org.eclipse.jface.text.ITextSelection)

Aggregations

IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)482 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)110 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)102 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)81 IFile (org.eclipse.core.resources.IFile)75 IOException (java.io.IOException)69 CoreException (org.eclipse.core.runtime.CoreException)49 IDocument (org.eclipse.jface.text.IDocument)46 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)42 InputStream (java.io.InputStream)40 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)36 Path (org.eclipse.core.runtime.Path)35 BadLocationException (org.eclipse.jface.text.BadLocationException)34 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)32 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)32 IProject (org.eclipse.core.resources.IProject)31 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)31 IPath (org.eclipse.core.runtime.IPath)27 ByteArrayInputStream (java.io.ByteArrayInputStream)22 Document (org.w3c.dom.Document)19