use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project liferay-ide by liferay.
the class UITestsUtils method getRegion.
public static IRegion getRegion(Node node) {
if (node != null) {
switch(node.getNodeType()) {
case Node.ELEMENT_NODE:
IDOMElement element = (IDOMElement) node;
int endOffset;
if (element.hasEndTag() && element.isClosed()) {
endOffset = element.getStartEndOffset();
} else {
endOffset = element.getEndOffset();
}
if (element.getFirstChild() == null || element.getFirstChild().getTextContent().isEmpty()) {
return new Region(endOffset, 0);
}
return new Region(endOffset, element.getTextContent().length());
case Node.ATTRIBUTE_NODE:
IDOMAttr att = (IDOMAttr) node;
int regOffset = att.getValueRegionStartOffset();
int regLength = att.getValueRegionText().length();
String attValue = att.getValueRegionText();
if (StringUtil.isQuoted(attValue)) {
regOffset++;
regLength -= 2;
}
return new Region(regOffset, regLength);
case Node.TEXT_NODE:
IDOMText text = (IDOMText) node;
int startOffset = text.getStartOffset();
int length = text.getLength();
return new Region(startOffset, length);
}
}
return null;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.
the class EmbeddedCSSFormatter method formatNode.
/**
*/
protected void formatNode(IDOMNode node, HTMLFormatContraints contraints) {
if (node == null)
return;
IDOMText text = (IDOMText) node;
String source = getCSSContent(node);
if (source == null) {
// fallback
source = text.getSource();
}
int offset = text.getStartOffset();
int length = text.getEndOffset() - offset;
replaceSource(text.getModel(), offset, length, source);
setWidth(contraints, source);
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.
the class JSPTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset)
offset = selection.getOffset() + selection.getLength();
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
// If there's no prefix, check if we're in a tag open region
if (prefix.trim().length() == 0) {
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, offset);
if (treeNode instanceof IDOMText) {
IDOMNode node = (IDOMNode) treeNode;
// Check each region in the node, if the offset is after a tag region, replace it with the template
IStructuredDocumentRegion cursor = node.getFirstStructuredDocumentRegion();
IStructuredDocumentRegion end = node.getLastStructuredDocumentRegion();
do {
if (cursor != null && DOMRegionContext.XML_TAG_OPEN.equals(cursor.getType()) && cursor.getStartOffset() == offset - 1) {
// We have a tag to replace
offset = cursor.getStartOffset();
region = new Region(cursor.getStartOffset(), cursor.getLength());
break;
}
} while (cursor != end && (cursor = cursor.getNext()) != null);
}
}
TemplateContext context = createContext(viewer, region, offset);
if (context == null)
return new ICompletionProposal[0];
// name of the selection variables {line, word}_selection
// //$NON-NLS-1$
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
List matches = new ArrayList();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
if (template.matches(prefix, context.getContextType().getId()))
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
Collections.sort(matches, fgProposalComparator);
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method updateFormattingConstraints.
/**
* Given the provided information (parentConstraints & currentDOMRegion),
* update the formatting constraints (for this & child)
*
* @param parentConstraints
* can be null
* @param thisConstraints
* can be null
* @param childConstraints
* can be null
* @param currentDOMRegion
* cannot be null
*/
protected void updateFormattingConstraints(XMLFormattingConstraints parentConstraints, XMLFormattingConstraints thisConstraints, XMLFormattingConstraints childConstraints, DOMRegion currentDOMRegion) {
IStructuredDocumentRegion currentRegion = currentDOMRegion.documentRegion;
IDOMNode currentNode = currentDOMRegion.domNode;
// default to whatever parent's constraint said to do
if (parentConstraints != null) {
if (thisConstraints != null) {
thisConstraints.copyConstraints(parentConstraints);
}
if (childConstraints != null) {
childConstraints.copyConstraints(parentConstraints);
// defaults are taken instead
if (parentConstraints.isWhitespaceStrategyAHint())
childConstraints.setWhitespaceStrategy(null);
}
}
// set up constraints for direct children of document root
Node parentNode = currentNode.getParentNode();
if (parentNode != null && parentNode.getNodeType() == Node.DOCUMENT_NODE) {
if (thisConstraints != null) {
thisConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
thisConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
thisConstraints.setIndentLevel(0);
}
if (childConstraints != null) {
childConstraints.setWhitespaceStrategy(null);
childConstraints.setIndentStrategy(null);
childConstraints.setIndentLevel(0);
}
}
// other conditions to check when setting up child constraints
if (childConstraints != null) {
XMLFormattingPreferences preferences = getFormattingPreferences();
// on a new line and have an indent level of 0
if (currentNode.getNodeType() == Node.DOCUMENT_NODE) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
childConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
childConstraints.setIndentLevel(0);
} else {
// BUG108074 & BUG84688 - preserve whitespace in xsl:text &
// xsl:attribute
String nodeNamespaceURI = currentNode.getNamespaceURI();
if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
String nodeName = ((Element) currentNode).getLocalName();
if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
}
} else {
// search within current tag for xml:space attribute
ITextRegionList textRegions = currentRegion.getRegions();
int i = 0;
boolean xmlSpaceFound = false;
boolean preserveFound = false;
while (i < textRegions.size() && !xmlSpaceFound) {
ITextRegion textRegion = textRegions.get(i);
if (DOMRegionContext.XML_TAG_ATTRIBUTE_NAME.equals(textRegion.getType())) {
String regionText = currentRegion.getText(textRegion);
if (XML_SPACE.equals(regionText)) {
if ((i + 1) < textRegions.size()) {
++i;
textRegion = textRegions.get(i);
if (DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS.equals(textRegion.getType()) && ((i + 1) < textRegions.size())) {
++i;
textRegion = textRegions.get(i);
regionText = currentRegion.getText(textRegion);
if (PRESERVE.equals(regionText) || PRESERVE_QUOTED.equals(regionText)) {
preserveFound = true;
}
}
}
xmlSpaceFound = true;
}
}
++i;
}
if (xmlSpaceFound) {
if (preserveFound) {
// preserve was found so set the strategy
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
} else {
// xml:space was found but it was not collapse, so
// use default whitespace strategy
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
}
} else {
// how to hande nodes that have nonwhitespace text
// content
NodeList nodeList = currentNode.getChildNodes();
int length = nodeList.getLength();
int index = 0;
boolean textNodeFound = false;
// still reflect the parent constraints
while (index < length && !textNodeFound && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
Node childNode = nodeList.item(index);
if (childNode.getNodeType() == Node.TEXT_NODE) {
textNodeFound = !((IDOMText) childNode).isElementContentWhitespace();
}
++index;
}
if (textNodeFound) {
if (length > 1) {
// more in here than just text, so consider
// this mixed content
childConstraints.setWhitespaceStrategy(preferences.getMixedWhitespaceStrategy());
childConstraints.setIndentStrategy(preferences.getMixedIndentStrategy());
} else {
// there's only text
childConstraints.setWhitespaceStrategy(preferences.getTextWhitespaceStrategy());
childConstraints.setIndentStrategy(preferences.getTextIndentStrategy());
}
childConstraints.setIsWhitespaceStrategyAHint(true);
childConstraints.setIsIndentStrategyAHint(true);
}
// try referring to content model for information on
// whitespace & indent strategy
ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) currentNode.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(currentNode);
if (elementDeclaration != null) {
// follow whitespace strategy preference for
// pcdata content
int contentType = elementDeclaration.getContentType();
String facetValue = null;
if (elementDeclaration.getDataType() != null)
facetValue = (String) elementDeclaration.getDataType().getProperty(PROPERTY_WHITESPACE_FACET);
if (facetValue != null) {
if (PRESERVE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
else // For XSD types, "collapse" corresponds to the IGNOREANDTRIM strategy
if (COLLAPSE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNOREANDTRIM);
else if (REPLACE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.REPLACE);
} else if (contentType == CMElementDeclaration.PCDATA && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
childConstraints.setWhitespaceStrategy(preferences.getPCDataWhitespaceStrategy());
} else if (contentType == CMElementDeclaration.ELEMENT && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
childConstraints.setIndentStrategy(XMLFormattingConstraints.INDENT);
childConstraints.setIsWhitespaceStrategyAHint(true);
childConstraints.setIsIndentStrategyAHint(true);
} else {
// look for xml:space in content model
CMNamedNodeMap cmAttributes = elementDeclaration.getAttributes();
// Not needed - we're looking for xml:space
// CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(cmAttributes);
// List nodes = ModelQueryUtil.getModelQuery(currentNode.getOwnerDocument()).getAvailableContent((Element) currentNode, elementDeclaration, ModelQuery.INCLUDE_ATTRIBUTES);
// for (int k = 0; k < nodes.size(); k++) {
// CMNode cmnode = (CMNode) nodes.get(k);
// if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
// allAttributes.put(cmnode);
// }
// }
// cmAttributes = allAttributes;
// Check implied values from the DTD way.
CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) cmAttributes.getNamedItem(XML_SPACE);
if (attributeDeclaration != null) {
// CMAttributeDeclaration found, check
// it
// out.
// BUG214516/196544 - Fixed NPE that was caused by an attr having
// a null attr type
String defaultValue = null;
CMDataType attrType = attributeDeclaration.getAttrType();
if (attrType != null) {
if ((attrType.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && attrType.getImpliedValue() != null)
defaultValue = attrType.getImpliedValue();
else if ((attrType.getEnumeratedValues() != null) && (attrType.getEnumeratedValues().length > 0)) {
defaultValue = attrType.getEnumeratedValues()[0];
}
}
// default.
if (PRESERVE.equals(defaultValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
else
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
} else // If the node has no attributes, inherit the parents whitespace strategy
{
if (parentConstraints != null)
childConstraints.setWhitespaceStrategy(parentConstraints.getWhitespaceStrategy());
else
childConstraints.setWhitespaceStrategy(null);
}
}
}
}
}
}
// set default values according to preferences
if (childConstraints.getWhitespaceStrategy() == null) {
childConstraints.setWhitespaceStrategy(preferences.getElementWhitespaceStrategy());
}
if (childConstraints.getIndentStrategy() == null) {
childConstraints.setIndentStrategy(preferences.getElementIndentStrategy());
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMText in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method formatContent.
// private XMLFormattingConstraints getRegionConstraints(IDOMNode currentNode) {
// IDOMNode iterator = (IDOMNode) currentNode.getParentNode();
// XMLFormattingConstraints result = new XMLFormattingConstraints();
// DOMRegion region = new DOMRegion();
//
// /* Iterate through the ancestry to find if any explicit whitespace strategy has
// * been defined
// */
// while(iterator != null && iterator.getNodeType() != Node.DOCUMENT_NODE) {
// region.domNode = iterator;
// region.documentRegion = iterator.getFirstStructuredDocumentRegion();
//
// updateFormattingConstraints(null, null, result, region);
//
// /* A parent who has defined a specific whitespace strategy was found */
// if(XMLFormattingConstraints.PRESERVE == result.getWhitespaceStrategy() || XMLFormattingConstraints.DEFAULT == result.getWhitespaceStrategy())
// return result;
//
// iterator = (IDOMNode) iterator.getParentNode();
// }
//
// return null;
// }
/**
* Formats the given xml content region
*
* @param textEdit
* @param formatRange
* @param parentConstraints
* @param currentDOMRegion
* @param previousRegion
*/
private void formatContent(TextEdit textEdit, Position formatRange, XMLFormattingConstraints parentConstraints, DOMRegion currentDOMRegion, IStructuredDocumentRegion previousRegion) {
IStructuredDocumentRegion currentRegion = currentDOMRegion.documentRegion;
String fullText = currentDOMRegion.domNode.getSource();
// check if in preserve space mode, if so, don't touch anything but
// make sure to update available line width
String whitespaceMode = parentConstraints.getWhitespaceStrategy();
if (XMLFormattingConstraints.PRESERVE.equals(whitespaceMode)) {
int availableLineWidth = parentConstraints.getAvailableLineWidth();
availableLineWidth = updateLineWidthWithLastLine(fullText, availableLineWidth);
// update available line width in constraints
parentConstraints.setAvailableLineWidth(availableLineWidth);
// A text node can contain multiple structured document regions - sync the documentRegion
// with the last region of the node since the text from all regions was formatted
currentDOMRegion.documentRegion = currentDOMRegion.domNode.getLastStructuredDocumentRegion();
return;
}
// if content is just whitespace and there's something after it
// just skip over this region because region will take care of it
boolean isAllWhitespace = ((IDOMText) currentDOMRegion.domNode).isElementContentWhitespace();
IStructuredDocumentRegion nextDocumentRegion = null;
if (isAllWhitespace) {
parentConstraints.setAvailableLineWidth(fPreferences.getMaxLineWidth());
nextDocumentRegion = currentRegion.getNext();
if (nextDocumentRegion != null)
return;
}
// special handling if text follows an entity or cdata region
if (!XMLFormattingConstraints.COLLAPSE.equals(whitespaceMode) && previousRegion != null) {
String previouRegionType = previousRegion.getType();
if (DOMRegionContext.XML_ENTITY_REFERENCE.equals(previouRegionType) || DOMRegionContext.XML_CDATA_TEXT.equals(previouRegionType))
whitespaceMode = XMLFormattingConstraints.COLLAPSE;
}
// also, special handling if text is before an entity or cdata region
if (!XMLFormattingConstraints.COLLAPSE.equals(whitespaceMode)) {
// get next document region if dont already have it
if (nextDocumentRegion == null)
nextDocumentRegion = currentRegion.getNext();
if (nextDocumentRegion != null) {
String nextRegionType = nextDocumentRegion.getType();
if (DOMRegionContext.XML_ENTITY_REFERENCE.equals(nextRegionType) || DOMRegionContext.XML_CDATA_TEXT.equals(nextRegionType))
whitespaceMode = XMLFormattingConstraints.COLLAPSE;
}
}
final IStructuredDocumentRegion lastRegion = currentDOMRegion.domNode.getLastStructuredDocumentRegion();
formatTextInContent(textEdit, parentConstraints, currentRegion, lastRegion != null ? lastRegion.getNext() : null, fullText, whitespaceMode);
// A text node can contain multiple structured document regions - sync the documentRegion
// with the last region of the node since the text from all regions was formatted
currentDOMRegion.documentRegion = lastRegion;
}
Aggregations