use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addAttributeNameProposals.
protected void addAttributeNameProposals(ContentAssistRequest contentAssistRequest) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
IStructuredDocumentRegion sdRegion = contentAssistRequest.getDocumentRegion();
// retrieve the list of attributes
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
if (elementDecl != null) {
CMNamedNodeMap attributes = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
if (node.getNodeType() == Node.ELEMENT_NODE) {
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);
}
}
}
attributes = allAttributes;
String matchString = contentAssistRequest.getMatchString();
// check whether an attribute really exists for the replacement
// offsets AND if it possesses a value
boolean attrAtLocationHasValue = false;
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
AttrImpl existingAttr = (AttrImpl) attrs.item(i);
ITextRegion name = existingAttr.getNameRegion();
if ((sdRegion.getStartOffset(name) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(name) + name.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength()) && (existingAttr.getValueRegion() != null)) {
attrAtLocationHasValue = true;
break;
}
}
// the matchstring
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) attributes.item(i);
int isRequired = 0;
if (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED) {
isRequired = XMLRelevanceConstants.R_REQUIRED;
}
boolean showAttribute = true;
showAttribute = showAttribute && beginsWith(getRequiredName(node, attrDecl), matchString.trim());
AttrImpl attr = (AttrImpl) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
ITextRegion nameRegion = attr != null ? attr.getNameRegion() : null;
// nameRegion.getEndOffset() + 1 is required to allow for
// matches against the full name of an existing Attr
showAttribute = showAttribute && (attr == null || nameRegion == null || (nameRegion != null && (sdRegion.getStartOffset(nameRegion) <= contentAssistRequest.getReplacementBeginPosition()) && (sdRegion.getStartOffset(nameRegion) + nameRegion.getLength() >= contentAssistRequest.getReplacementBeginPosition() + contentAssistRequest.getReplacementLength())));
if (showAttribute) {
Image attrImage = CMImageUtil.getImage(attrDecl);
if (attrImage == null) {
if (isRequired > 0) {
attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
} else {
attrImage = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
}
}
String proposedText = null;
String proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
CustomCompletionProposal proposal = null;
// attribute is at this location and already exists
if (attrAtLocationHasValue) {
// only propose the name
proposedText = getRequiredName(node, attrDecl);
proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), proposedText.length(), attrImage, proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired, true);
} else // no attribute exists or is elsewhere, generate
// minimally
{
Attr existingAttrNode = (Attr) node.getAttributes().getNamedItem(getRequiredName(node, attrDecl));
String value = null;
if (existingAttrNode != null && existingAttrNode.getSpecified()) {
value = existingAttrNode.getNodeValue();
}
if ((value != null) && (value.length() > 0)) {
proposedText = getRequiredName(node, attrDecl);
} else {
proposedText = getRequiredText(node, attrDecl);
}
proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), attrDecl.getNodeName().length() + 2, attrImage, // and there is no single quote that may be encasing a double quote
(showValues && (proposedText.lastIndexOf('\"') - proposedText.indexOf('\"') == 1 && proposedText.indexOf('\'') == -1)) ? getRequiredName(node, attrDecl) : proposedText, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_NAME + isRequired);
}
contentAssistRequest.addProposal(proposal);
}
}
}
} else {
setErrorMessage(NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { node.getNodeName() })));
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addEndTagNameProposals.
/**
* Add the proposals for the name in an end tag
*/
protected void addEndTagNameProposals(ContentAssistRequest contentAssistRequest) {
if (contentAssistRequest.getStartOffset() + contentAssistRequest.getRegion().getTextLength() < contentAssistRequest.getReplacementBeginPosition()) {
CustomCompletionProposal proposal = new // $NON-NLS-1$
CustomCompletionProposal(// $NON-NLS-1$
">", // $NON-NLS-1$
contentAssistRequest.getReplacementBeginPosition(), // $NON-NLS-1$
contentAssistRequest.getReplacementLength(), // $NON-NLS-1$
1, // $NON-NLS-1$
XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC), // $NON-NLS-1$
NLS.bind(XMLUIMessages.Close_with__, (new Object[] { " '>'" })), null, null, XMLRelevanceConstants.R_END_TAG_NAME);
contentAssistRequest.addProposal(proposal);
} else {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
Node aNode = contentAssistRequest.getNode();
String matchString = contentAssistRequest.getMatchString();
if (matchString.startsWith("</")) {
matchString = matchString.substring(2);
}
while (aNode != null) {
if (aNode.getNodeType() == Node.ELEMENT_NODE) {
if (aNode.getNodeName().startsWith(matchString)) {
IDOMNode aXMLNode = (IDOMNode) aNode;
CMElementDeclaration ed = modelQuery.getCMElementDeclaration((Element) aNode);
if ((aXMLNode.getEndStructuredDocumentRegion() == null) && ((ed == null) || (ed.getContentType() != CMElementDeclaration.EMPTY))) {
String replacementText = aNode.getNodeName();
String displayText = replacementText;
String proposedInfo = (ed != null) ? getAdditionalInfo(null, ed) : null;
if (!contentAssistRequest.getDocumentRegion().isEnded()) {
// $NON-NLS-1$
replacementText += ">";
}
CustomCompletionProposal proposal = null;
// double check to see if the region acted upon is
// a tag name; replace it if so
Image image = CMImageUtil.getImage(ed);
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
}
if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_NAME) {
proposal = new CustomCompletionProposal(replacementText, contentAssistRequest.getStartOffset(), contentAssistRequest.getRegion().getTextLength(), replacementText.length(), image, displayText, null, proposedInfo, XMLRelevanceConstants.R_END_TAG_NAME);
} else {
proposal = new // $NON-NLS-1$ //$NON-NLS-2$
CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
replacementText, // $NON-NLS-1$ //$NON-NLS-2$
contentAssistRequest.getReplacementBeginPosition(), // $NON-NLS-1$ //$NON-NLS-2$
contentAssistRequest.getReplacementLength(), // $NON-NLS-1$ //$NON-NLS-2$
replacementText.length(), // $NON-NLS-1$ //$NON-NLS-2$
image, // $NON-NLS-1$ //$NON-NLS-2$
NLS.bind(XMLUIMessages.Close_with__, (new Object[] { "'" + displayText + "'" })), null, proposedInfo, XMLRelevanceConstants.R_END_TAG_NAME);
}
contentAssistRequest.addProposal(proposal);
}
}
}
aNode = aNode.getParentNode();
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addTagCloseProposals.
/**
* Close an unclosed start tag
*/
protected void addTagCloseProposals(ContentAssistRequest contentAssistRequest) {
IDOMNode node = (IDOMNode) contentAssistRequest.getParent();
if (node.getNodeType() == Node.ELEMENT_NODE) {
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
String proposedInfo = (elementDecl != null) ? getAdditionalInfo(null, elementDecl) : null;
int contentType = (elementDecl != null) ? elementDecl.getContentType() : CMElementDeclaration.ANY;
// if it's XML and content doesn't HAVE to be element, add "/>"
// proposal.
boolean endWithSlashBracket = (getXML(node) && (contentType != CMElementDeclaration.ELEMENT));
Image image = CMImageUtil.getImage(elementDecl);
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
}
// is the start tag ended properly?
if ((contentAssistRequest.getDocumentRegion() == node.getFirstStructuredDocumentRegion()) && !(node.getFirstStructuredDocumentRegion()).isEnded()) {
setErrorMessage(null);
// tell, we assume it's not.
if ((elementDecl != null) && (elementDecl.getContentType() == CMElementDeclaration.EMPTY)) {
// prompt with a self-closing end character if needed
CustomCompletionProposal proposal = new CustomCompletionProposal(getContentGenerator().getStartTagClose(node, elementDecl), contentAssistRequest.getReplacementBeginPosition(), // contentAssistRequest.getReplacementLength(),
0, getContentGenerator().getStartTagClose(node, elementDecl).length(), image, NLS.bind(XMLUIMessages.Close_with___, (new Object[] { getContentGenerator().getStartTagClose(node, elementDecl) })), null, proposedInfo, XMLRelevanceConstants.R_CLOSE_TAG);
contentAssistRequest.addProposal(proposal);
} else {
// prompt with a close for the start tag
CustomCompletionProposal proposal = new // $NON-NLS-1$
CustomCompletionProposal(// $NON-NLS-1$
">", contentAssistRequest.getReplacementBeginPosition(), // $NON-NLS-1$
0, // $NON-NLS-1$
1, // $NON-NLS-1$
image, // $NON-NLS-1$
NLS.bind(XMLUIMessages.Close_with__, (new Object[] { " '>'" })), null, proposedInfo, XMLRelevanceConstants.R_CLOSE_TAG);
contentAssistRequest.addProposal(proposal);
// if one is not present
if (node.getEndStructuredDocumentRegion() == null) {
// make sure tag name is actually what it thinks it
// is...(eg. <%@ vs. <jsp:directive)
IStructuredDocumentRegion sdr = contentAssistRequest.getDocumentRegion();
// $NON-NLS-1$
String openingTagText = (sdr != null) ? sdr.getFullText() : "";
if ((openingTagText != null) && (openingTagText.indexOf(node.getNodeName()) != -1)) {
proposal = new // $NON-NLS-2$//$NON-NLS-1$
CustomCompletionProposal(// $NON-NLS-2$//$NON-NLS-1$
"></" + node.getNodeName() + ">", contentAssistRequest.getReplacementBeginPosition(), // contentAssistRequest.getReplacementLength(),
0, 1, image, NLS.bind(XMLUIMessages.Close_with____, (new Object[] { node.getNodeName() })), null, proposedInfo, XMLRelevanceConstants.R_CLOSE_TAG);
contentAssistRequest.addProposal(proposal);
}
}
// ending tag
if (endWithSlashBracket) {
proposal = new // $NON-NLS-1$
CustomCompletionProposal(// $NON-NLS-1$
"/>", contentAssistRequest.getReplacementBeginPosition(), // $NON-NLS-1$
0, // $NON-NLS-1$
2, // $NON-NLS-1$
image, // $NON-NLS-1$
NLS.bind(XMLUIMessages.Close_with__, (new Object[] { " \"/>\"" })), null, proposedInfo, // +1
XMLRelevanceConstants.R_CLOSE_TAG + 1);
// to
// bring
// to
// top
// of
// list
contentAssistRequest.addProposal(proposal);
}
}
} else if ((contentAssistRequest.getDocumentRegion() == node.getLastStructuredDocumentRegion()) && !node.getLastStructuredDocumentRegion().isEnded()) {
setErrorMessage(null);
// prompt with a closing end character for the end tag
CustomCompletionProposal proposal = new // $NON-NLS-1$
CustomCompletionProposal(// $NON-NLS-1$
">", contentAssistRequest.getReplacementBeginPosition(), // $NON-NLS-1$
0, // $NON-NLS-1$
1, // $NON-NLS-1$
image, // $NON-NLS-1$
NLS.bind(XMLUIMessages.Close_with__, (new Object[] { " '>'" })), null, proposedInfo, XMLRelevanceConstants.R_CLOSE_TAG);
contentAssistRequest.addProposal(proposal);
}
} else if (node.getNodeType() == Node.DOCUMENT_NODE) {
setErrorMessage(UNKNOWN_CONTEXT);
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class BaseNodeActionManager method contributeAddChildActions.
protected void contributeAddChildActions(IMenuManager menu, Node node, int ic, int vc) {
int nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE && canContributeChildActions(node)) {
// 'Add Child...' and 'Add Attribute...' actions
//
Element element = (Element) node;
IMenuManager addAttributeMenu = new MyMenuManager(XMLUIMessages._UI_MENU_ADD_ATTRIBUTE);
IMenuManager addChildMenu = new MyMenuManager(XMLUIMessages._UI_MENU_ADD_CHILD);
menu.add(addAttributeMenu);
menu.add(addChildMenu);
CMElementDeclaration ed = modelQuery.getCMElementDeclaration(element);
if (ed != null) {
// add insert attribute actions
//
List modelQueryActionList = new ArrayList();
modelQuery.getInsertActions(element, ed, -1, ModelQuery.INCLUDE_ATTRIBUTES, vc, modelQueryActionList);
addActionHelper(addAttributeMenu, modelQueryActionList);
// add insert child node actions
//
modelQueryActionList = new ArrayList();
modelQuery.getInsertActions(element, ed, -1, ic, vc, modelQueryActionList);
addActionHelper(addChildMenu, modelQueryActionList);
}
// add PI and COMMENT
contributePIAndCommentActions(addChildMenu, element, ed, -1);
// add PCDATA, CDATA_SECTION
contributeTextNodeActions(addChildMenu, element, ed, -1);
// add NEW ELEMENT
contributeUnconstrainedAddElementAction(addChildMenu, element, ed, -1);
// add ATTRIBUTE
contributeUnconstrainedAttributeActions(addAttributeMenu, element, ed);
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration in project webtools.sourceediting by eclipse.
the class BaseNodeActionManager method contributeAddSiblingActions.
protected void contributeAddSiblingActions(IMenuManager menu, Node node, int ic, int vc) {
IMenuManager addBeforeMenu = new MyMenuManager(XMLUIMessages._UI_MENU_ADD_BEFORE);
IMenuManager addAfterMenu = new MyMenuManager(XMLUIMessages._UI_MENU_ADD_AFTER);
menu.add(addBeforeMenu);
menu.add(addAfterMenu);
Node parentNode = node.getParentNode();
if (parentNode != null) {
int index = getIndex(parentNode, node);
if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
Element parentElement = (Element) parentNode;
CMElementDeclaration parentED = modelQuery.getCMElementDeclaration(parentElement);
if (parentED != null) {
// 'Add Before...' and 'Add After...' actions
//
List modelQueryActionList = new ArrayList();
modelQuery.getInsertActions(parentElement, parentED, index, ic, vc, modelQueryActionList);
addActionHelper(addBeforeMenu, modelQueryActionList);
modelQueryActionList = new ArrayList();
modelQuery.getInsertActions(parentElement, parentED, index + 1, ic, vc, modelQueryActionList);
addActionHelper(addAfterMenu, modelQueryActionList);
}
// add COMMENT and PI before and after
contributePIAndCommentActions(addBeforeMenu, parentElement, parentED, index);
contributePIAndCommentActions(addAfterMenu, parentElement, parentED, index + 1);
// add PCDATA, CDATA_SECTION before and after
contributeTextNodeActions(addBeforeMenu, parentElement, parentED, index);
contributeTextNodeActions(addAfterMenu, parentElement, parentED, index + 1);
// add NEW ELEMENT before and after
contributeUnconstrainedAddElementAction(addBeforeMenu, parentElement, parentED, index);
contributeUnconstrainedAddElementAction(addAfterMenu, parentElement, parentED, index + 1);
} else if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
Document document = (Document) parentNode;
CMDocument cmDocument = modelQuery.getCorrespondingCMDocument(parentNode);
if (cmDocument != null) {
// add possible root element insertions
//
List modelQueryActionList = new ArrayList();
modelQuery.getInsertActions(document, cmDocument, index, ic, vc, modelQueryActionList);
addActionHelper(addAfterMenu, modelQueryActionList);
modelQueryActionList = new ArrayList();
modelQuery.getInsertActions(document, cmDocument, index + 1, ic, vc, modelQueryActionList);
addActionHelper(addAfterMenu, modelQueryActionList);
}
// add COMMENT and PI before and after
contributePIAndCommentActions(addBeforeMenu, document, index);
contributePIAndCommentActions(addAfterMenu, document, index + 1);
// add ELEMENT before and after
contributeUnconstrainedAddElementAction(addBeforeMenu, document, index);
contributeUnconstrainedAddElementAction(addAfterMenu, document, index + 1);
}
}
}
Aggregations