use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode 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.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method computeTagOpenProposals.
protected ContentAssistRequest computeTagOpenProposals(int documentPosition, String matchString, ITextRegion completionRegion, IDOMNode nodeAtOffset, IDOMNode node) {
ContentAssistRequest contentAssistRequest = null;
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
if (sdRegion != nodeAtOffset.getFirstStructuredDocumentRegion() || sdRegion.getPrevious() != null && sdRegion.getPrevious().getLastRegion().getType() == DOMRegionContext.XML_TAG_OPEN) {
// completing the *first* XML_TAG_OPEN in "<<tagname"
IDOMNode actualNode = (IDOMNode) node.getModel().getIndexedRegion(sdRegion.getStartOffset(completionRegion));
if (actualNode != null) {
if (sdRegion.getFirstRegion().getType() == DOMRegionContext.XML_END_TAG_OPEN) {
contentAssistRequest = newContentAssistRequest(actualNode, actualNode, sdRegion, completionRegion, documentPosition, 0, matchString);
if (actualNode.hasChildNodes())
addTagNameProposals(contentAssistRequest, getElementPositionForModelQuery(actualNode.getLastChild()));
else
addTagNameProposals(contentAssistRequest, 0);
} else {
contentAssistRequest = newContentAssistRequest(actualNode, actualNode.getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString);
addTagNameProposals(contentAssistRequest, getElementPositionForModelQuery(actualNode));
}
// (pa) 220850
addEndTagProposals(contentAssistRequest);
}
} else {
if (documentPosition == sdRegion.getStartOffset(completionRegion)) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
// at the start of an existing tag, right before the '<'
contentAssistRequest = newContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString);
addTagInsertionProposals(contentAssistRequest, getElementPositionForModelQuery(nodeAtOffset));
addEndTagProposals(contentAssistRequest);
} else if (node.getNodeType() == Node.DOCUMENT_NODE) {
// at the opening of the VERY first tag with a '<'
contentAssistRequest = newContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, completionRegion, sdRegion.getStartOffset(completionRegion), completionRegion.getTextLength(), matchString);
addStartDocumentProposals(contentAssistRequest);
}
} else {
// within the white space
ITextRegion name = getNameRegion(node.getStartStructuredDocumentRegion());
// {
if ((name != null) && ((sdRegion.getStartOffset(name) <= documentPosition) && (sdRegion.getEndOffset(name) >= documentPosition)) && (sdRegion.getLastRegion().getType() == DOMRegionContext.XML_TAG_CLOSE || sdRegion.getLastRegion().getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE)) {
// replace the existing name
contentAssistRequest = newContentAssistRequest(node, node.getParentNode(), sdRegion, completionRegion, sdRegion.getStartOffset(name), name.getTextLength(), matchString);
} else {
// insert a valid new name, or possibly an end tag
contentAssistRequest = newContentAssistRequest(nodeAtOffset, ((Node) nodeAtOffset).getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString);
addEndTagProposals(contentAssistRequest);
contentAssistRequest.setReplacementBeginPosition(documentPosition);
contentAssistRequest.setReplacementLength(0);
}
addTagNameProposals(contentAssistRequest, getElementPositionForModelQuery(nodeAtOffset));
}
}
return contentAssistRequest;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class AbstractContentAssistProcessor method addEndTagProposals.
/**
* Prompt for end tags to a non-empty Node that hasn't ended Handles these
* cases: <br>
* <tagOpen>| <br>
* <tagOpen>< |<br>
* <tagOpen></ |
*
* @param contentAssistRequest
*/
protected void addEndTagProposals(ContentAssistRequest contentAssistRequest) {
IDOMNode node = (IDOMNode) contentAssistRequest.getParent();
if (isCommentNode(node)) {
// loop and find non comment node parent
while ((node != null) && isCommentNode(node)) {
node = (IDOMNode) node.getParentNode();
}
}
// node is already closed
if (node.isClosed()) {
// loop and find non comment unclose node parent
while ((node != null) && node.isClosed()) {
node = (IDOMNode) node.getParentNode();
}
}
// there were no unclosed tags
if (node == null) {
return;
}
// data to create a CustomCompletionProposal
// $NON-NLS-1$
String replaceText = node.getNodeName() + ">";
int replaceBegin = contentAssistRequest.getReplacementBeginPosition();
int replaceLength = contentAssistRequest.getReplacementLength();
int cursorOffset = node.getNodeName().length() + 1;
// $NON-NLS-1$
String displayString = "";
// $NON-NLS-1$
String proposedInfo = "";
Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_TAG_GENERIC);
setErrorMessage(null);
boolean addProposal = false;
if (node.getNodeType() == Node.ELEMENT_NODE) {
// ////////////////////////////////////////////////////////////////////////////////////
IStructuredDocument sDoc = (IStructuredDocument) fTextViewer.getDocument();
IStructuredDocumentRegion xmlEndTagOpen = sDoc.getRegionAtCharacterOffset(contentAssistRequest.getReplacementBeginPosition());
// skip backward to "<", "</", or the (unclosed) start tag, null
// if not found
// $NON-NLS-1$
String type = "";
while ((xmlEndTagOpen != null) && ((type = xmlEndTagOpen.getType()) != DOMRegionContext.XML_END_TAG_OPEN) && (type != DOMRegionContext.XML_TAG_CLOSE) && !needsEndTag(xmlEndTagOpen) && (type != DOMRegionContext.XML_TAG_OPEN)) {
xmlEndTagOpen = xmlEndTagOpen.getPrevious();
}
if (xmlEndTagOpen == null) {
return;
}
node = (IDOMNode) node.getModel().getIndexedRegion(xmlEndTagOpen.getStartOffset());
node = (IDOMNode) node.getParentNode();
if (isStartTag(xmlEndTagOpen)) {
// |
if (needsEndTag(xmlEndTagOpen)) {
String tagName = getTagName(xmlEndTagOpen);
xmlEndTagOpen.getTextEndOffset();
replaceLength = 0;
// $NON-NLS-1$ //$NON-NLS-2$ $NON-NLS-2$
replaceText = "</" + tagName + ">";
// replaceText = "</" + node.getNodeName() + ">";
// //$NON-NLS-1$ $NON-NLS-2$
cursorOffset = tagName.length() + 3;
displayString = NLS.bind(XMLUIMessages.End_with__, (new Object[] { tagName }));
addProposal = true;
}
} else if (type == DOMRegionContext.XML_END_TAG_OPEN) {
// this is the case for: <tag> </ |
// possibly <tag> </ |<anotherTag>
// should only be replacing white space...
replaceLength = (replaceBegin > xmlEndTagOpen.getTextEndOffset()) ? replaceBegin - xmlEndTagOpen.getTextEndOffset() : 0;
// $NON-NLS-1$
replaceText = node.getNodeName() + ">";
cursorOffset = replaceText.length();
replaceBegin = xmlEndTagOpen.getTextEndOffset();
displayString = NLS.bind(XMLUIMessages.End_with_, (new Object[] { node.getNodeName() }));
addProposal = true;
} else if (type == DOMRegionContext.XML_TAG_OPEN) {
// this is the case for: <tag> < |
// $NON-NLS-1$ //$NON-NLS-2$ $NON-NLS-2$
replaceText = "/" + node.getNodeName() + ">";
cursorOffset = replaceText.length();
// replaceText = "/" + node.getNodeName() + ">"; //$NON-NLS-1$
// $NON-NLS-2$
// should only be replacing white space...
replaceLength = (replaceBegin > xmlEndTagOpen.getTextEndOffset()) ? replaceBegin - xmlEndTagOpen.getTextEndOffset() : 0;
replaceBegin = xmlEndTagOpen.getTextEndOffset();
// $NON-NLS-1$
displayString = NLS.bind(XMLUIMessages.End_with_, (new Object[] { "/" + node.getNodeName() }));
addProposal = true;
}
} else // getNodeValue() is null, put in a null check
if ((node.getNodeValue() != null) && (node.getNodeValue().indexOf("</") != -1)) {
// $NON-NLS-1$
// the case where "</" is started, but the nodes comes in as a
// text node (instead of element)
// like this: <tag> </|
Node parent = node.getParentNode();
if ((parent != null) && (parent.getNodeType() != Node.DOCUMENT_NODE)) {
// $NON-NLS-1$
replaceText = parent.getNodeName() + ">";
cursorOffset = replaceText.length();
displayString = NLS.bind(XMLUIMessages.End_with__, (new Object[] { parent.getNodeName() }));
setErrorMessage(null);
addProposal = true;
}
} else // ////////////////////////////////////////////////////////////////////////////////////
if (node.getNodeType() == Node.DOCUMENT_NODE) {
setErrorMessage(UNKNOWN_CONTEXT);
}
if (addProposal == true) {
CustomCompletionProposal proposal = new CustomCompletionProposal(replaceText, replaceBegin, replaceLength, cursorOffset, image, displayString, null, proposedInfo, XMLRelevanceConstants.R_END_TAG);
contentAssistRequest.addProposal(proposal);
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class RenameInFileQuickAssistProposal method apply.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer,
* char, int, int)
*/
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDocument document = viewer.getDocument();
Point originalRange = viewer.getSelectedRange();
LinkedPositionGroup group = new LinkedPositionGroup();
try {
IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
ITextRegion region = (startStructuredDocumentRegion == null) ? null : startStructuredDocumentRegion.getRegionAtCharacterOffset(offset);
if (region != null) {
group.addPosition(new LinkedPosition(document, startStructuredDocumentRegion.getStartOffset() + region.getStart(), region.getTextLength(), 0));
if ((region.getType() == DOMRegionContext.XML_TAG_NAME) && (node.getEndStructuredDocumentRegion() != null)) {
region = node.getEndStructuredDocumentRegion().getRegions().get(1);
if (region != null) {
group.addPosition(new LinkedPosition(document, node.getEndStructuredDocumentRegion().getStartOffset() + region.getStart(), region.getTextLength(), 1));
}
}
} else {
IStructuredDocumentRegion endStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
region = (endStructuredDocumentRegion == null) ? null : endStructuredDocumentRegion.getRegionAtCharacterOffset(offset);
if (region != null) {
if ((region.getType() == DOMRegionContext.XML_TAG_NAME) && (node.getStartStructuredDocumentRegion() != null)) {
ITextRegion startTagNameRegion = node.getStartStructuredDocumentRegion().getRegions().get(1);
if (region != null) {
group.addPosition(new LinkedPosition(document, node.getStartStructuredDocumentRegion().getStartOffset() + startTagNameRegion.getStart(), startTagNameRegion.getTextLength(), 0));
group.addPosition(new LinkedPosition(document, endStructuredDocumentRegion.getStartOffset() + region.getStart(), region.getTextLength(), 1));
}
} else {
group.addPosition(new LinkedPosition(document, endStructuredDocumentRegion.getStartOffset() + region.getStart(), region.getTextLength(), 0));
}
}
}
// which disables redraw in ITextViewer. Workaround for now.
if (viewer instanceof ITextViewerExtension)
((ITextViewerExtension) viewer).setRedraw(true);
LinkedModeModel linkedModeModel = new LinkedModeModel();
linkedModeModel.addGroup(group);
linkedModeModel.forceInstall();
LinkedModeUI ui = new EditorLinkedModeUI(linkedModeModel, viewer);
ui.setExitPosition(viewer, offset, 0, LinkedPositionGroup.NO_STOP);
ui.enter();
/*
* Keep the cursor at the original offset and restore the original
* selection instead of the entire first matching region
*/
viewer.setSelectedRange(originalRange.x, originalRange.y);
fSelectedRegion = ui.getSelectedRegion();
} catch (BadLocationException e) {
// log for now, unless find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.
the class XMLQuickAssistProcessor method getInsertRequiredAttrs.
private void getInsertRequiredAttrs(List proposals, ISourceViewer viewer, int offset) {
IDOMNode node = (IDOMNode) getNodeAt(viewer, offset);
if ((node != null) && (node.getNodeType() == Node.ELEMENT_NODE)) {
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
if ((startStructuredDocumentRegion != null) && startStructuredDocumentRegion.containsOffset(offset)) {
IDOMNode cursorNode = (IDOMNode) getNodeAt(viewer, offset);
List requiredAttrs = getRequiredAttrs(cursorNode);
if (requiredAttrs.size() > 0) {
NamedNodeMap currentAttrs = node.getAttributes();
List insertAttrs = new ArrayList();
if (currentAttrs.getLength() == 0) {
insertAttrs.addAll(requiredAttrs);
} else {
for (int i = 0; i < requiredAttrs.size(); i++) {
String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
boolean found = false;
for (int j = 0; j < currentAttrs.getLength(); j++) {
String currentAttrName = currentAttrs.item(j).getNodeName();
if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
found = true;
break;
}
}
if (!found) {
insertAttrs.add(requiredAttrs.get(i));
}
}
}
if (insertAttrs.size() > 0) {
proposals.add(new InsertRequiredAttrsQuickAssistProposal(insertAttrs));
}
}
}
}
}
Aggregations