use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addEntityProposals.
protected void addEntityProposals(Vector proposals, Properties map, String key, int nodeOffset, IStructuredDocumentRegion sdRegion, ITextRegion completionRegion) {
if (map == null) {
return;
}
// $NON-NLS-1$
String entityName = "";
// $NON-NLS-1$
String entityValue = "";
Image entityIcon = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENTITY_REFERENCE);
// $NON-NLS-1$
String replacementText = "";
// $NON-NLS-1$
String displayString = "";
Enumeration keys = map.keys();
while ((keys != null) && keys.hasMoreElements()) {
entityName = (String) keys.nextElement();
entityValue = map.getProperty(entityName);
// filter based on partial entity string...
if (// $NON-NLS-1$
entityName.toLowerCase().startsWith(key.toLowerCase()) || key.trim().equals("")) {
// figure out selection...if text is selected, add it to
// selection length
int selectionLength = nodeOffset;
if (fTextViewer != null) {
selectionLength += fTextViewer.getSelectedRange().y;
}
// create a new proposal for entity string...
// $NON-NLS-1$ //$NON-NLS-2$
replacementText = "&" + entityName + ";";
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
displayString = "&" + entityName + "; (" + entityValue + ")";
ICompletionProposal cp = new CustomCompletionProposal(replacementText, sdRegion.getStartOffset(completionRegion), selectionLength, replacementText.length(), entityIcon, displayString, null, null, XMLRelevanceConstants.R_ENTITY);
if (cp != null) {
proposals.add(cp);
}
}
}
}
use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal 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.sse.ui.internal.contentassist.CustomCompletionProposal 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.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method addDocTypeProposal.
/**
* <p>Adds a generic doc type proposal</p>
*
* @param contentAssistRequest
* @param context
*/
private void addDocTypeProposal(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
// if a DocumentElement exists, use that for the root Element name
// $NON-NLS-1$
String rootname = "unspecified";
if (contentAssistRequest.getNode().getOwnerDocument().getDocumentElement() != null) {
rootname = contentAssistRequest.getNode().getOwnerDocument().getDocumentElement().getNodeName();
}
// $NON-NLS-1$ //$NON-NLS-2$
String proposedText = "<!DOCTYPE " + rootname + " PUBLIC \"//UNKNOWN/\" \"unknown.dtd\">";
ICompletionProposal proposal = new CustomCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), 10, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DOCTYPE), // $NON-NLS-1$
"<!DOCTYPE ... >", null, null, XMLRelevanceConstants.R_DOCTYPE);
// TODO provide special documentation on doc type
contentAssistRequest.addProposal(proposal);
}
use of org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal in project webtools.sourceediting by eclipse.
the class AbstractXMLModelQueryCompletionProposalComputer method addTagInsertionProposals.
protected void addTagInsertionProposals(ContentAssistRequest contentAssistRequest, int childPosition, CompletionProposalInvocationContext context) {
List cmnodes = null;
Node parent = contentAssistRequest.getParent();
String error = null;
// only valid if it's XML (check added 2/17/2004)
if ((parent != null) && (parent.getNodeType() == Node.DOCUMENT_NODE) && ((IDOMDocument) parent).isXMLType() && !isCursorAfterXMLPI(contentAssistRequest)) {
return;
}
// only want proposals if cursor is after doctype...
if (!isCursorAfterDoctype(contentAssistRequest)) {
return;
}
// have a content model (so can't propose any children..)
if ((parent != null) && (parent instanceof IDOMNode) && isCommentNode((IDOMNode) parent)) {
// loop and find non comment node?
while ((parent != null) && isCommentNode((IDOMNode) parent)) {
parent = parent.getParentNode();
}
}
if (parent.getNodeType() == Node.ELEMENT_NODE) {
CMElementDeclaration parentDecl = getCMElementDeclaration(parent);
if (parentDecl != null) {
// XSD-specific ability - no filtering
CMDataType childType = parentDecl.getDataType();
if (childType != null) {
String[] childStrings = childType.getEnumeratedValues();
String defaultValue = childType.getImpliedValue();
if (childStrings != null || defaultValue != null) {
// the content string is the sole valid child...so replace the rest
int begin = contentAssistRequest.getReplacementBeginPosition();
int length = contentAssistRequest.getReplacementLength();
if (parent instanceof IDOMNode) {
if (((IDOMNode) parent).getLastStructuredDocumentRegion() != ((IDOMNode) parent).getFirstStructuredDocumentRegion()) {
begin = ((IDOMNode) parent).getFirstStructuredDocumentRegion().getEndOffset();
length = ((IDOMNode) parent).getLastStructuredDocumentRegion().getStartOffset() - begin;
}
}
String proposedInfo = getAdditionalInfo(parentDecl, childType);
for (int i = 0; i < childStrings.length; i++) {
if (!childStrings[i].equals(defaultValue)) {
CustomCompletionProposal textProposal = new MarkupCompletionProposal(childStrings[i], begin, length, childStrings[i].length(), XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), childStrings[i], null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
contentAssistRequest.addProposal(textProposal);
}
}
if (defaultValue != null) {
CustomCompletionProposal textProposal = new MarkupCompletionProposal(defaultValue, begin, length, defaultValue.length(), XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DEFAULT), defaultValue, null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
contentAssistRequest.addProposal(textProposal);
}
}
}
}
if ((parentDecl != null) && (parentDecl.getContentType() == CMElementDeclaration.PCDATA)) {
addPCDATAProposal(parentDecl.getNodeName(), contentAssistRequest, context);
} else {
// retrieve the list of all possible children within this parent context
cmnodes = getAvailableChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
// retrieve the list of the possible children within this
// parent context and at this index
List strictCMNodeSuggestions = null;
if (XMLUIPreferenceNames.SUGGESTION_STRATEGY_VALUE_STRICT.equals(XMLUIPlugin.getInstance().getPreferenceStore().getString(XMLUIPreferenceNames.SUGGESTION_STRATEGY))) {
strictCMNodeSuggestions = getValidChildElementDeclarations((Element) parent, childPosition, ModelQueryAction.INSERT);
}
Iterator nodeIterator = cmnodes.iterator();
if (!nodeIterator.hasNext()) {
if (getCMElementDeclaration(parent) != null) {
error = NLS.bind(XMLUIMessages._Has_no_available_child, (new Object[] { parent.getNodeName() }));
} else {
error = NLS.bind(XMLUIMessages.Element__is_unknown, (new Object[] { parent.getNodeName() }));
}
}
String matchString = contentAssistRequest.getMatchString();
// 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);
}
while (nodeIterator.hasNext()) {
Object o = nodeIterator.next();
if (o instanceof CMElementDeclaration) {
CMElementDeclaration elementDecl = (CMElementDeclaration) o;
// only add proposals for the child element's that
// begin with the matchstring
String tagname = getRequiredName(parent, elementDecl);
boolean isStrictCMNodeSuggestion = strictCMNodeSuggestions != null ? strictCMNodeSuggestions.contains(elementDecl) : false;
// get the proposal image
Image image = CMImageUtil.getImage(elementDecl);
if (image == null) {
if (strictCMNodeSuggestions != null) {
image = isStrictCMNodeSuggestion ? this.getEmphasizedTagImage() : this.getDeemphasizedTagImage();
} else {
image = this.getGenericTagImage();
}
}
if (beginsWith(tagname, matchString)) {
String proposedText = getRequiredText(parent, elementDecl);
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=89811
// place cursor in first empty quotes
int markupAdjustment = getCursorPositionForProposedText(proposedText);
String proposedInfo = getAdditionalInfo(parentDecl, elementDecl);
int relevance = isStrictCMNodeSuggestion ? XMLRelevanceConstants.R_STRICTLY_VALID_TAG_INSERTION : XMLRelevanceConstants.R_TAG_INSERTION;
CustomCompletionProposal proposal = new MarkupCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), markupAdjustment, image, tagname, null, proposedInfo, relevance);
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, (new Object[] { parent.getNodeName(), contentAssistRequest.getMatchString() })));
} else {
setErrorMessage(NLS.bind(XMLUIMessages.__Has_no_known_child, (new Object[] { parent.getNodeName() })));
}
}
}
} else if (parent.getNodeType() == Node.DOCUMENT_NODE) {
// Can only prompt with elements if the cursor position is past
// the XML processing
// instruction and DOCTYPE declaration
boolean xmlpiFound = false;
boolean doctypeFound = false;
int minimumOffset = -1;
for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
// $NON-NLS-1$
boolean xmlpi = ((child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) && child.getNodeName().equals("xml"));
boolean doctype = child.getNodeType() == Node.DOCUMENT_TYPE_NODE;
if (xmlpi || (doctype && (minimumOffset < 0))) {
minimumOffset = ((IDOMNode) child).getFirstStructuredDocumentRegion().getStartOffset() + ((IDOMNode) child).getFirstStructuredDocumentRegion().getTextLength();
}
xmlpiFound = xmlpiFound || xmlpi;
doctypeFound = doctypeFound || doctype;
}
if (contentAssistRequest.getReplacementBeginPosition() >= minimumOffset) {
List childDecls = getAvailableRootChildren((Document) parent, childPosition);
for (int i = 0; i < childDecls.size(); i++) {
CMElementDeclaration ed = (CMElementDeclaration) childDecls.get(i);
if (ed != null) {
Image image = CMImageUtil.getImage(ed);
if (image == null) {
image = this.getGenericTagImage();
}
String proposedText = getRequiredText(parent, ed);
String tagname = getRequiredName(parent, ed);
// account for the < and >
int markupAdjustment = getContentGenerator().getMinimalStartTagLength(parent, ed);
String proposedInfo = getAdditionalInfo(null, ed);
CustomCompletionProposal proposal = new MarkupCompletionProposal(proposedText, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), markupAdjustment, image, tagname, null, proposedInfo, XMLRelevanceConstants.R_TAG_INSERTION);
contentAssistRequest.addProposal(proposal);
}
}
}
}
}
Aggregations