use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method getAvailableChildrenAtIndex.
// returns a list of ModelQueryActions
protected List getAvailableChildrenAtIndex(Element parent, int index, int validityChecking) {
List list = new ArrayList();
CMElementDeclaration parentDecl = getCMElementDeclaration(parent);
if (parentDecl != null) {
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(parent.getOwnerDocument());
// taken from ActionManagers
// int editMode = modelQuery.getEditMode();
int editMode = ModelQuery.EDIT_MODE_UNCONSTRAINED;
int ic = (editMode == ModelQuery.EDIT_MODE_CONSTRAINED_STRICT) ? ModelQuery.INCLUDE_CHILD_NODES | ModelQuery.INCLUDE_SEQUENCE_GROUPS : ModelQuery.INCLUDE_CHILD_NODES;
modelQuery.getInsertActions(parent, parentDecl, index, ic, validityChecking, list);
}
return list;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addTagNameProposals.
protected void addTagNameProposals(ContentAssistRequest contentAssistRequest, int childPosition) {
List cmnodes = null;
Node parent = contentAssistRequest.getParent();
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
String error = null;
String matchString = contentAssistRequest.getMatchString();
if (parent.getNodeType() == Node.ELEMENT_NODE) {
// retrieve the list of children
// validActions = getAvailableChildrenAtIndex((Element) parent,
// childPosition);
cmnodes = getAvailableChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
Iterator nodeIterator = cmnodes.iterator();
// chop off any leading <'s and whitespace from the matchstring
while ((matchString.length() > 0) && (Character.isWhitespace(matchString.charAt(0)) || beginsWith(matchString, "<"))) {
// $NON-NLS-1$
matchString = matchString.substring(1);
}
if (!nodeIterator.hasNext()) {
error = NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() }));
}
while (nodeIterator.hasNext()) {
CMNode elementDecl = (CMNode) nodeIterator.next();
if (elementDecl != null) {
// only add proposals for the child element's that begin
// with the matchstring
String proposedText = null;
int cursorAdjustment = 0;
// names are in list
if (((node != null) && (node.getAttributes() != null) && (node.getAttributes().getLength() > 0) && attributeInList(node, parent, elementDecl)) || ((node.getNodeType() != Node.TEXT_NODE) && node.getFirstStructuredDocumentRegion().isEnded())) {
proposedText = getRequiredName(parent, elementDecl);
cursorAdjustment = proposedText.length();
} else {
proposedText = getRequiredName(parent, elementDecl);
cursorAdjustment = proposedText.length();
if (elementDecl instanceof CMElementDeclaration) {
CMElementDeclaration ed = (CMElementDeclaration) elementDecl;
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
StringBuffer sb = new StringBuffer();
getContentGenerator().generateTag(parent, ed, sb);
// since it's a name proposal, assume '<' is
// already there
// only return the rest of the tag
proposedText = sb.toString().substring(1);
cursorAdjustment = getCursorPositionForProposedText(proposedText);
// cursorAdjustment = proposedText.length() +
// 1;
// proposedText += "></" +
// getRequiredName(parent, elementDecl) + ">";
// //$NON-NLS-2$//$NON-NLS-1$
}
}
if (beginsWith(proposedText, matchString)) {
Image image = CMImageUtil.getImage(elementDecl);
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
}
String proposedInfo = getAdditionalInfo(getCMElementDeclaration(parent), elementDecl);
CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, getRequiredName(parent, elementDecl), null, proposedInfo, XMLRelevanceConstants.R_TAG_NAME);
contentAssistRequest.addProposal(proposal);
}
}
}
if (contentAssistRequest.getProposals().size() == 0) {
if (error != null) {
setErrorMessage(error);
} else if ((contentAssistRequest.getMatchString() != null) && (contentAssistRequest.getMatchString().length() > 0)) {
setErrorMessage(NLS.bind(XMLUIMessages.No_known_child_tag_names, (new Object[] { parent.getNodeName(), contentAssistRequest.getMatchString() })));
// $NON-NLS-1$ = "No known child tag names of <{0}> begin with \"{1}\""
} else {
setErrorMessage(NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() })));
}
}
} else if (parent.getNodeType() == Node.DOCUMENT_NODE) {
List childElements = getAvailableRootChildren((Document) parent, childPosition);
for (int i = 0; i < childElements.size(); i++) {
CMNode ed = (CMNode) childElements.get(i);
if (ed == null) {
continue;
}
String proposedText = null;
int cursorAdjustment = 0;
if (ed instanceof CMElementDeclaration) {
// proposedText = getRequiredName(parent, ed);
StringBuffer sb = new StringBuffer();
getContentGenerator().generateTag(parent, (CMElementDeclaration) ed, sb);
// tag starts w/ '<', but we want to compare to name
proposedText = sb.toString().substring(1);
if (!beginsWith(proposedText, matchString)) {
continue;
}
cursorAdjustment = getCursorPositionForProposedText(proposedText);
String proposedInfo = getAdditionalInfo(null, ed);
Image image = CMImageUtil.getImage(ed);
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
}
CustomCompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), cursorAdjustment, image, getRequiredName(parent, ed), null, proposedInfo, XMLRelevanceConstants.R_TAG_NAME);
contentAssistRequest.addProposal(proposal);
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class XMLQuickAssistProcessor method getRequiredAttrs.
private List getRequiredAttrs(Node node) {
List result = new ArrayList();
ModelQuery modelQuery = getModelQuery(node);
if (modelQuery != null) {
CMElementDeclaration elementDecl = modelQuery.getCMElementDeclaration((Element) node);
if (elementDecl != null) {
CMNamedNodeMap attrMap = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, 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);
}
}
attrMap = allAttributes;
Iterator it = attrMap.iterator();
CMAttributeDeclaration attr = null;
while (it.hasNext()) {
attr = (CMAttributeDeclaration) it.next();
if (attr.getUsage() == CMAttributeDeclaration.REQUIRED) {
result.add(attr);
}
}
}
}
return result;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class ContentModelWorkbenchAdapter method getChildren.
public Object[] getChildren(Object o) {
if (o instanceof Element) {
Element node = (Element) o;
ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
if (mq != null) {
CMElementDeclaration decl = mq.getCMElementDeclaration(node);
CMListWorkbenchAdapter adapter = new CMListWorkbenchAdapter(decl);
return new Object[] { adapter };
}
}
return EMPTY;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class ElementNodeFormatter method formatEndTag.
protected void formatEndTag(IDOMNode node, IStructuredFormatContraints formatContraints) {
if (!isEndTagMissing(node)) {
// end tag exists
IStructuredDocument structuredDocument = node.getModel().getStructuredDocument();
String lineDelimiter = structuredDocument.getLineDelimiter();
String nodeIndentation = getNodeIndent(node);
IDOMNode lastChild = (IDOMNode) node.getLastChild();
if (lastChild != null && lastChild.getNodeType() != Node.TEXT_NODE) {
if (isEndTagMissing(lastChild)) {
// find deepest child
IDOMNode deepestChild = (IDOMNode) lastChild.getLastChild();
while (deepestChild != null && deepestChild.getLastChild() != null && isEndTagMissing(deepestChild)) {
lastChild = deepestChild;
deepestChild = (IDOMNode) deepestChild.getLastChild();
}
if (deepestChild != null) {
if (deepestChild.getNodeType() == Node.TEXT_NODE) {
// Special indentation handling if lastChild's end
// tag is missing and deepestChild is a text node.
String nodeText = deepestChild.getNodeValue();
if (!nodeText.endsWith(lineDelimiter + nodeIndentation)) {
nodeText = StringUtils.appendIfNotEndWith(nodeText, lineDelimiter);
nodeText = StringUtils.appendIfNotEndWith(nodeText, nodeIndentation);
}
replaceNodeValue(deepestChild, nodeText);
} else
insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
}
} else
// indent end tag
insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
} else if (lastChild == null && firstStructuredDocumentRegionContainsLineDelimiters(node)) {
// BUG174243 do not indent end tag if node has empty content
// (otherwise new text node would be introduced)
ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node);
if ((elementDeclaration == null) || (elementDeclaration.getContentType() != CMElementDeclaration.EMPTY)) {
// indent end tag
replace(structuredDocument, node.getFirstStructuredDocumentRegion().getEndOffset(), 0, lineDelimiter + nodeIndentation);
}
}
// format end tag name
IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getLastStructuredDocumentRegion();
if (endTagStructuredDocumentRegion.getRegions().size() >= 3) {
ITextRegion endTagNameRegion = endTagStructuredDocumentRegion.getRegions().get(1);
removeRegionSpaces(node, endTagStructuredDocumentRegion, endTagNameRegion);
}
}
}
Aggregations