use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class StyleSheetFormatter method formatPre.
/**
*/
protected void formatPre(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode node, java.lang.StringBuffer source) {
int start = ((IndexedRegion) node).getStartOffset();
int end = (node.getFirstChild() != null && ((IndexedRegion) node.getFirstChild()).getEndOffset() > 0) ? ((IndexedRegion) node.getFirstChild()).getStartOffset() : getChildInsertPos(node);
if (end > 0) {
CSSCleanupStrategy stgy = getCleanupStrategy(node);
IStructuredDocument structuredDocument = node.getOwnerDocument().getModel().getStructuredDocument();
CompoundRegion[] regions = getRegionsWithoutWhiteSpaces(structuredDocument, new FormatRegion(start, end - start), stgy);
for (int i = 0; i < regions.length; i++) {
if (i != 0 || regions[i].getType() == CSSRegionContexts.CSS_CDO || node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)
appendDelimBefore(node, regions[i], source);
source.append(decoratedRegion(regions[i], 0, stgy));
}
if (node.getLastChild() != null && (source.length() > 0 || node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED)) {
appendDelimBefore(node, null, source);
}
} else if (node.getOwnerDocument().getModel() != null && node.getOwnerDocument().getModel().getStyleSheetType() == ICSSModel.EMBEDDED) {
appendDelimBefore(node, null, source);
// source.append("<!--");
// appendDelimBefore(node,null, source);
} else
// nothing
return;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class HTMLContentAssistProcessor 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 textViewer, int documentPosition) {
fTemplateContexts.clear();
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition);
IDOMNode node = (IDOMNode) treeNode;
setErrorMessage(null);
// check if it's in a comment node
IStructuredDocument structuredDocument = (IStructuredDocument) textViewer.getDocument();
IStructuredDocumentRegion fn = structuredDocument.getRegionAtCharacterOffset(documentPosition);
if (fn != null && fn.getType() == DOMRegionContext.XML_COMMENT_TEXT && documentPosition != fn.getStartOffset()) {
return new ICompletionProposal[0];
}
// if it's a </script> tag, bounce back to JS ca processor...
if (fn != null && fn.getType() == DOMRegionContext.XML_TAG_NAME && documentPosition == fn.getStartOffset()) {
ITextRegionList v = fn.getRegions();
if (v.size() > 1) {
// determine that it's a close tag
if ((v.get(0)).getType() == DOMRegionContext.XML_END_TAG_OPEN) {
Iterator it = v.iterator();
ITextRegion region = null;
// search for script tag name
while (it.hasNext()) {
region = (ITextRegion) it.next();
if (fn.getText(region).equalsIgnoreCase("script")) {
// $NON-NLS-1$
IContentAssistProcessor jsProcessor = getJSContentAssistProcessor();
if (jsProcessor != null) {
return jsProcessor.computeCompletionProposals(textViewer, documentPosition);
}
return new ICompletionProposal[0];
}
}
}
}
}
isXHTML = getXHTML(node);
// force reload of content generator
fGenerator = null;
// handle blank HTML document case
if (treeNode == null || isViewerEmpty(textViewer)) {
// cursor is at the EOF
ICompletionProposal htmlTagProposal = getHTMLTagProposal(textViewer, documentPosition);
ICompletionProposal[] superResults = super.computeCompletionProposals(textViewer, documentPosition);
if (superResults != null && superResults.length > 0 && htmlTagProposal != null) {
ICompletionProposal[] blankHTMLDocResults = new ICompletionProposal[superResults.length + 1];
blankHTMLDocResults[0] = htmlTagProposal;
System.arraycopy(superResults, 0, blankHTMLDocResults, 1, superResults.length);
return blankHTMLDocResults;
}
}
if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
// check embedded CSS proposals at the beginning of the STYLE end
// tag
Element element = (Element) node;
String tagName = element.getTagName();
if (tagName != null && tagName.equalsIgnoreCase(HTML40Namespace.ATTR_NAME_STYLE)) {
// $NON-NLS-1$
IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
if (endStructuredDocumentRegion != null && endStructuredDocumentRegion.getStartOffset() == documentPosition) {
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
if (startStructuredDocumentRegion != null) {
int offset = startStructuredDocumentRegion.getEndOffset();
int pos = documentPosition - offset;
ICompletionProposal[] proposals = getCSSProposals(textViewer, pos, node, offset, (char) 0);
if (proposals != null)
return proposals;
}
}
}
// check inline CSS proposals
// need to find attr region from sd region
IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(textViewer, documentPosition);
Iterator regions = sdRegion.getRegions().iterator();
ITextRegion styleNameRegion = null;
ITextRegion styleValueRegion = null;
while (regions.hasNext()) {
styleNameRegion = (ITextRegion) regions.next();
if (styleNameRegion.getType().equals(DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) && sdRegion.getText(styleNameRegion).equalsIgnoreCase(HTML40Namespace.ATTR_NAME_STYLE)) {
// the next region should be "="
if (regions.hasNext()) {
// skip the "="
regions.next();
// next region should be attr value region
if (regions.hasNext()) {
styleValueRegion = (ITextRegion) regions.next();
break;
}
}
}
}
if (styleValueRegion != null) {
int offset = sdRegion.getStartOffset(styleValueRegion);
int end = sdRegion.getTextEndOffset(styleValueRegion);
if (documentPosition >= offset && documentPosition <= end) {
boolean askCSS = true;
char quote = (char) 0;
String text = sdRegion.getText(styleValueRegion);
int length = (text != null ? text.length() : 0);
if (length > 0) {
char firstChar = text.charAt(0);
if (firstChar == '"' || firstChar == '\'') {
if (documentPosition == offset) {
// before quote
askCSS = false;
} else {
offset++;
quote = firstChar;
}
}
if (documentPosition == end) {
if (length > 1 && text.charAt(length - 1) == quote) {
// after quote
askCSS = false;
}
}
}
if (askCSS) {
int pos = documentPosition - offset;
ICompletionProposal[] proposals = getCSSProposals(textViewer, pos, node, offset, quote);
if (proposals != null)
return proposals;
}
}
}
}
return super.computeCompletionProposals(textViewer, documentPosition);
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class AnchorHyperlinkDetector method getCurrentNode.
/**
* Returns the node the cursor is currently on in the document. null if no
* node is selected
*
* @param offset
* @return Node either element, doctype, text, or null
*/
private Node getCurrentNode(IDocument document, int offset) {
// get the current node at the offset (returns either: element,
// doctype, text)
IndexedRegion inode = null;
IStructuredModel sModel = null;
try {
sModel = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (sModel != null) {
inode = sModel.getIndexedRegion(offset);
if (inode == null) {
inode = sModel.getIndexedRegion(offset - 1);
}
}
} finally {
if (sModel != null)
sModel.releaseFromRead();
}
if (inode instanceof Node) {
return (Node) inode;
}
return null;
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class ViewerTestHTML method followSelection.
/**
* Hooks up the viewer to follow the selection made in the active editor
*/
void followSelection() {
ITextEditor editor = getActiveEditor();
if (editor != null) {
setupViewerForEditor(editor);
if (fHighlightRangeListener == null)
fHighlightRangeListener = new NodeRangeSelectionListener();
fContentOutlinePage = (editor.getAdapter(IContentOutlinePage.class));
if (fContentOutlinePage != null) {
fContentOutlinePage.addSelectionChangedListener(fHighlightRangeListener);
if (!fContentOutlinePage.getSelection().isEmpty() && fContentOutlinePage.getSelection() instanceof IStructuredSelection) {
fSourceViewer.resetVisibleRegion();
Object[] nodes = ((IStructuredSelection) fContentOutlinePage.getSelection()).toArray();
IndexedRegion startNode = (IndexedRegion) nodes[0];
IndexedRegion endNode = (IndexedRegion) nodes[nodes.length - 1];
if (startNode instanceof Attr)
startNode = (IndexedRegion) ((Attr) startNode).getOwnerElement();
if (endNode instanceof Attr)
endNode = (IndexedRegion) ((Attr) endNode).getOwnerElement();
int start = startNode.getStartOffset();
int end = endNode.getEndOffset();
fSourceViewer.setVisibleRegion(start, end - start);
fSourceViewer.setSelectedRange(start, 0);
}
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.IndexedRegion in project webtools.sourceediting by eclipse.
the class AbstractStructuredFoldingStrategy method getIndexedRegions.
/**
* <p>Gets all of the {@link IndexedRegion}s from the given {@link IStructuredModel} spanned by the given
* {@link IStructuredDocumentRegion}s.</p>
*
* @param model the {@link IStructuredModel} used to get the {@link IndexedRegion}s
* @param structRegions get the {@link IndexedRegion}s spanned by these {@link IStructuredDocumentRegion}s
* @return the {@link Set} of {@link IndexedRegion}s from the given {@link IStructuredModel} spanned by the
* given {@link IStructuredDocumentRegion}s.
*/
private Set getIndexedRegions(IStructuredModel model, IStructuredDocumentRegion[] structRegions) {
Set indexedRegions = new HashSet(structRegions.length);
// for each text region in each structured document region find the indexed region it spans/is in
for (int structRegionIndex = 0; structRegionIndex < structRegions.length && fProjectionAnnotationModel != null; ++structRegionIndex) {
IStructuredDocumentRegion structuredDocumentRegion = structRegions[structRegionIndex];
int offset = structuredDocumentRegion.getStartOffset();
IndexedRegion indexedRegion = model.getIndexedRegion(offset);
indexedRegions.add(indexedRegion);
if (structuredDocumentRegion.getEndOffset() <= indexedRegion.getEndOffset())
continue;
ITextRegionList textRegions = structuredDocumentRegion.getRegions();
int textRegionCount = textRegions.size();
for (int textRegionIndex = 1; textRegionIndex < textRegionCount; ++textRegionIndex) {
offset = structuredDocumentRegion.getStartOffset(textRegions.get(textRegionIndex));
indexedRegion = model.getIndexedRegion(offset);
indexedRegions.add(indexedRegion);
}
}
return indexedRegions;
}
Aggregations