use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.
the class JSPReParser method isBreakingWithNestedTag.
/**
* Verifies that the regions given, representing the regions touched by a
* text change have: 1) ...an insertion at the textEndOffset of an
* XML_TAG_OPEN that's in it's own IStructuredDocumentRegion and preceded
* by an unended IStructuredDocumentRegion 2) ...a deletion happening in
* an XML_EMPTY_TAG_CLOSE that ends a ITextRegionContainer 3) ...an
* insertion happening with a ' <' character somewhere in an XML attribute
* name or value 4) ...a deletion of a normal XML_TAG_CLOSE since
* subsequent tags become attribute values
*/
private boolean isBreakingWithNestedTag(boolean changesIncludeA_lt, boolean delsIncludeA_gt, IStructuredDocumentRegion parent, ITextRegion region) {
boolean result = false;
IStructuredDocumentRegion previous = parent.getPrevious();
// case 1 test
if (parent.getRegions().size() == 1 && region.getType() == DOMRegionContext.XML_TAG_OPEN && (previous == null || (!previous.isEnded() || previous.getType() == DOMRegionContext.XML_CONTENT))) {
result = true;
}
// case 2 test
if (region instanceof ITextRegionContainer) {
ITextRegionContainer container = (ITextRegionContainer) region;
ITextRegion internal = container.getRegions().get(container.getRegions().size() - 1);
if (internal.getType() == DOMRegionContext.WHITE_SPACE && container.getRegions().size() >= 2)
internal = container.getRegions().get(container.getRegions().size() - 2);
if (internal.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
result = true;
}
}
// case 3 test
if (changesIncludeA_lt && (region.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || region.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)) {
result = true;
}
// case 4 test
if (delsIncludeA_gt && region.getType() == DOMRegionContext.XML_TAG_CLOSE) {
result = true;
}
return result;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.
the class JSPReParser method isTaglibOrInclude.
/**
* Verifies that the regions given, representing the contents of a
* IStructuredDocumentRegion, includes a valid taglib directive or include
* directive
*/
private boolean isTaglibOrInclude(IStructuredDocumentRegion node, ITextRegionList regions) {
boolean sizeAndTypesMatch = (regions.size() > 1) && (regions.get(1).getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_NAME) && (regions.get(0).getType() == DOMJSPRegionContexts.JSP_DIRECTIVE_OPEN || regions.get(0).getType() == DOMRegionContext.XML_TAG_OPEN);
if (!sizeAndTypesMatch)
return false;
ITextRegion region = regions.get(1);
String directiveName = node.getText(region);
return sizeAndTypesMatch && (directiveName.equals(JSP11TLDNames.TAGLIB) || directiveName.equals(JSP11TLDNames.INCLUDE) || directiveName.equals(JSP12Namespace.ElementName.DIRECTIVE_TAGLIB) || directiveName.equals(JSP12Namespace.ElementName.DIRECTIVE_INCLUDE));
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.
the class JSPJavaCompletionProposalComputer method isValidContext.
/**
* <p>Determines if the context is a valid one for JSP Java proposals.
* The default result is <code>true</code></p>
*
* @param context check this context to see if it is valid for JSP
* Java proposals
* @return <code>true</code> if the given context is a valid one for
* JSP Java proposals, <code>false</code> otherwise. <code>true</code>
* is the default response if a specific case for <code>false</code> is
* not found.
*/
private boolean isValidContext(CompletionProposalInvocationContext context) {
ITextViewer viewer = context.getViewer();
int documentPosition = context.getInvocationOffset();
String partitionType = getPartitionType(viewer, documentPosition);
if (partitionType == IJSPPartitions.JSP_CONTENT_JAVA)
return true;
IStructuredDocument structuredDocument = (IStructuredDocument) viewer.getDocument();
IStructuredDocumentRegion fn = structuredDocument.getRegionAtCharacterOffset(documentPosition);
IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
// check for xml-jsp tags...
if (partitionType == IJSPPartitions.JSP_DIRECTIVE && fn != null) {
IStructuredDocumentRegion possibleXMLJSP = ((fn.getType() == DOMRegionContext.XML_CONTENT) && fn.getPrevious() != null) ? fn.getPrevious() : fn;
ITextRegionList regions = possibleXMLJSP.getRegions();
if (regions.size() > 1) {
// check bounds cases
ITextRegion xmlOpenOrClose = regions.get(0);
if (xmlOpenOrClose.getType() != DOMRegionContext.XML_TAG_OPEN && documentPosition != possibleXMLJSP.getStartOffset() && xmlOpenOrClose.getType() != DOMRegionContext.XML_END_TAG_OPEN && documentPosition <= possibleXMLJSP.getStartOffset()) {
// possible xml-jsp
ITextRegion nameRegion = regions.get(1);
String name = possibleXMLJSP.getText(nameRegion);
if (name.equals("jsp:scriptlet") || name.equals("jsp:expression") || name.equals("jsp:declaration")) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return true;
}
}
}
}
// check for XML-JSP in a <script> region
if (partitionType == IJSPPartitions.JSP_CONTENT_JAVASCRIPT || partitionType == IHTMLPartitions.SCRIPT) {
// fn should be block text
IStructuredDocumentRegion decodedSDRegion = decodeScriptBlock(fn.getFullText());
// decodedSDRegion.getEndOffset()));
if (decodedSDRegion != null) {
IStructuredDocumentRegion sdr = decodedSDRegion;
while (sdr != null) {
// sdr.getEndOffset()));
if (sdr.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
if (documentPosition >= fn.getStartOffset() + sdr.getStartOffset() && documentPosition <= fn.getStartOffset() + sdr.getEndOffset()) {
return true;
}
} else if (sdr.getType() == DOMRegionContext.XML_TAG_NAME) {
if (documentPosition > fn.getStartOffset() + sdr.getStartOffset() && documentPosition < fn.getStartOffset() + sdr.getEndOffset()) {
return false;
} else if (documentPosition == fn.getStartOffset() + sdr.getEndOffset() && sdr.getNext() != null && sdr.getNext().getType() == DOMJSPRegionContexts.JSP_CONTENT) {
// <jsp:scriptlet>| blah </jsp:scriptlet>
return true;
} else if (documentPosition == fn.getStartOffset() + sdr.getStartOffset() && sdr.getPrevious() != null && sdr.getPrevious().getType() == DOMRegionContext.XML_TAG_NAME) {
return true;
}
}
sdr = sdr.getNext();
}
}
}
// check special JSP delimiter cases
if (fn != null && partitionType == IJSPPartitions.JSP_CONTENT_DELIMITER) {
IStructuredDocumentRegion fnDelim = fn;
// if it's a nested JSP region, need to get the correct
// StructuredDocumentRegion
// not sure why this check was there...
// if (fnDelim.getType() == XMLRegionContext.BLOCK_TEXT) {
Iterator blockRegions = fnDelim.getRegions().iterator();
ITextRegion temp = null;
ITextRegionContainer trc;
while (blockRegions.hasNext()) {
temp = (ITextRegion) blockRegions.next();
// we hit a nested
if (temp instanceof ITextRegionContainer) {
trc = (ITextRegionContainer) temp;
// it's in this region
if (documentPosition >= trc.getStartOffset() && documentPosition < trc.getEndOffset()) {
Iterator nestedJSPRegions = trc.getRegions().iterator();
while (nestedJSPRegions.hasNext()) {
temp = (ITextRegion) nestedJSPRegions.next();
if (XMLContentAssistUtilities.isJSPOpenDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
// adapter
if (documentPosition > 0) {
partitionType = getPartitionType(viewer, documentPosition - 1);
break;
}
} else if (XMLContentAssistUtilities.isJSPCloseDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
// JSP content assist
return true;
}
}
}
}
// }
}
// take care of XML-JSP delimter cases
if (XMLContentAssistUtilities.isXMLJSPDelimiter(fnDelim)) {
// since it's a delimiter, we know it's a ITextRegionContainer
ITextRegion firstRegion = fnDelim.getRegions().get(0);
if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN)) {
// |<jsp:scriptlet> </jsp:scriptlet>
// (pa) commented out so that we get regular behavior JSP
// macros etc...
// return getHTMLCompletionProposals(viewer,
// documentPosition);
} else if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_END_TAG_OPEN)) {
// adapter get the proposals
if (documentPosition > 0) {
String checkType = getPartitionType(viewer, documentPosition - 1);
if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
// check is failing for XML-JSP (region is not javascript...)
return true;
}
partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
}
} else if ((firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN) && documentPosition >= fnDelim.getEndOffset()) {
// anything else inbetween
return true;
}
} else if (XMLContentAssistUtilities.isJSPDelimiter(fnDelim)) {
// the delimiter <%, <%=, <%!, ...
if (XMLContentAssistUtilities.isJSPCloseDelimiter(fnDelim)) {
if (documentPosition == fnDelim.getStartOffset()) {
// JAVASCRIPT adapter get the proposals
if (documentPosition > 0) {
String checkType = getPartitionType(viewer, documentPosition - 1);
if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
return true;
}
partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
}
}
} else if (XMLContentAssistUtilities.isJSPOpenDelimiter(fnDelim)) {
// use embedded HTML results
if (documentPosition == fnDelim.getEndOffset()) {
// it's at the EOF <%|
return true;
}
}
}
}
// <!-- <% |%> -->
if (fn != null && (fn.getType() == DOMRegionContext.XML_CDATA_TEXT || fn.getType() == DOMRegionContext.XML_COMMENT_TEXT)) {
if (fn instanceof ITextRegionContainer) {
Object[] cdataRegions = fn.getRegions().toArray();
ITextRegion r = null;
ITextRegion jspRegion = null;
for (int i = 0; i < cdataRegions.length; i++) {
r = (ITextRegion) cdataRegions[i];
if (r instanceof ITextRegionContainer) {
// CDATA embedded container, or comment container
Object[] jspRegions = ((ITextRegionContainer) r).getRegions().toArray();
for (int j = 0; j < jspRegions.length; j++) {
jspRegion = (ITextRegion) jspRegions[j];
if (jspRegion.getType() == DOMJSPRegionContexts.JSP_CLOSE) {
if (sdRegion.getStartOffset(jspRegion) == documentPosition) {
return true;
}
}
}
}
}
}
}
// check if it's in an attribute value, if so, don't add CDATA
// proposal
ITextRegion attrContainer = (fn != null) ? fn.getRegionAtCharacterOffset(documentPosition) : null;
if (attrContainer != null && attrContainer instanceof ITextRegionContainer) {
if (attrContainer.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
// test location of the cursor
// return null if it's in the middle of an open/close delimiter
Iterator attrRegions = ((ITextRegionContainer) attrContainer).getRegions().iterator();
ITextRegion testRegion = null;
while (attrRegions.hasNext()) {
testRegion = (ITextRegion) attrRegions.next();
// need to check for other valid attribute regions
if (XMLContentAssistUtilities.isJSPOpenDelimiter(testRegion.getType())) {
if (!(((ITextRegionContainer) attrContainer).getEndOffset(testRegion) <= documentPosition))
return false;
} else if (XMLContentAssistUtilities.isJSPCloseDelimiter(testRegion.getType())) {
if (!(((ITextRegionContainer) attrContainer).getStartOffset(testRegion) >= documentPosition))
return false;
}
}
// TODO: handle non-Java code such as nested tags
if (testRegion.getType().equals(DOMJSPRegionContexts.JSP_CONTENT)) {
return true;
}
return false;
}
}
return true;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.
the class TaglibHelper method extractTagData.
/**
* Returns all attribute -> value pairs for the tag in a Hashtable.
*
* @param customTag
* @return
*/
private Hashtable extractTagData(ITextRegionCollection customTag) {
Hashtable tagDataTable = new Hashtable();
ITextRegionList regions = customTag.getRegions();
ITextRegion r = null;
// $NON-NLS-1$
String attrName = "";
// $NON-NLS-1$
String attrValue = "";
final int size = regions.size();
for (int i = 2; i < size; i++) {
r = regions.get(i);
// check if attr name
if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
attrName = customTag.getText(r);
// check equals is next region
if (size > ++i) {
r = regions.get(i);
if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS && size > ++i) {
// get attr value
r = regions.get(i);
final String type = r.getType();
if (type == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
// attributes in our document have quotes, so we
// need to strip them
attrValue = StringUtils.stripQuotes(customTag.getText(r));
tagDataTable.put(attrName, attrValue);
} else if (type == DOMJSPRegionContexts.JSP_TAG_ATTRIBUTE_VALUE_DQUOTE || type == DOMJSPRegionContexts.JSP_TAG_ATTRIBUTE_VALUE_SQUOTE) {
final StringBuffer buffer = new StringBuffer();
while (size > ++i && (r = regions.get(i)).getType() != type) {
buffer.append(customTag.getText(r));
}
tagDataTable.put(attrName, buffer.toString());
}
}
}
}
}
// $NON-NLS-1$
tagDataTable.put("jsp:id", customTag.getText(regions.get(1)) + "_" + customTag.getStartOffset());
return tagDataTable;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion in project webtools.sourceediting by eclipse.
the class TaglibHyperlinkDetector method getHyperlinkRegion.
// the below methods were copied from URIHyperlinkDetector
private IRegion getHyperlinkRegion(Node node, IRegion boundingRegion) {
IRegion hyperRegion = null;
if (node != null) {
short nodeType = node.getNodeType();
if (nodeType == Node.DOCUMENT_TYPE_NODE) {
// handle doc type node
IDOMNode docNode = (IDOMNode) node;
hyperRegion = new Region(docNode.getStartOffset(), docNode.getEndOffset() - docNode.getStartOffset());
} else if (nodeType == Node.ATTRIBUTE_NODE) {
// handle attribute nodes
IDOMAttr att = (IDOMAttr) node;
// do not include quotes in attribute value region
int regOffset = att.getValueRegionStartOffset();
ITextRegion valueRegion = att.getValueRegion();
if (valueRegion != null) {
int regLength = valueRegion.getTextLength();
String attValue = att.getValueRegionText();
if (StringUtils.isQuoted(attValue)) {
++regOffset;
regLength = regLength - 2;
}
hyperRegion = new Region(regOffset, regLength);
}
}
if (nodeType == Node.ELEMENT_NODE) {
// Handle doc type node
IDOMNode docNode = (IDOMNode) node;
hyperRegion = getNameRegion(docNode.getFirstStructuredDocumentRegion());
if (hyperRegion == null) {
hyperRegion = new Region(docNode.getStartOffset(), docNode.getFirstStructuredDocumentRegion().getTextLength());
}
}
}
/**
* Only return a hyperlink region that overlaps the search region.
* This will help us to not underline areas not under the cursor.
*/
if (hyperRegion != null && intersects(hyperRegion, boundingRegion))
return hyperRegion;
return null;
}
Aggregations