|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
* |
| 11 |
*******************************************************************************/ |
| 12 |
package org.eclipse.jpt.jpa.ui.internal; |
| 13 |
|
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Arrays; |
| 16 |
import java.util.Collections; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
import java.util.Properties; |
| 20 |
import java.util.Vector; |
| 21 |
|
| 22 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 23 |
import org.eclipse.jface.text.BadLocationException; |
| 24 |
import org.eclipse.jface.text.ITextViewer; |
| 25 |
import org.eclipse.jface.text.contentassist.ICompletionProposal; |
| 26 |
import org.eclipse.wst.sse.core.StructuredModelManager; |
| 27 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
| 28 |
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
| 29 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument; |
| 30 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
| 31 |
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion; |
| 32 |
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer; |
| 33 |
import org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext; |
| 34 |
import org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer; |
| 35 |
import org.eclipse.wst.sse.ui.internal.contentassist.ContentAssistUtils; |
| 36 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
| 37 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel; |
| 38 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode; |
| 39 |
import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext; |
| 40 |
import org.eclipse.wst.xml.ui.internal.Logger; |
| 41 |
import org.eclipse.wst.xml.ui.internal.XMLUIMessages; |
| 42 |
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest; |
| 43 |
import org.w3c.dom.Document; |
| 44 |
import org.w3c.dom.Node; |
| 45 |
import org.w3c.dom.NodeList; |
| 46 |
|
| 47 |
/** |
| 48 |
* <p>Implements the framework for making proposals in XML type documents. Deals with |
| 49 |
* all the nastiness needed to figure out where in an XML type document the content |
| 50 |
* assist was invoked and then calls one of many abstract methods depending on the |
| 51 |
* area the content assist was invoked. In this way implementers only have to worry about |
| 52 |
* implementing what to do in each situation while not having to deal with figuring out |
| 53 |
* which situation the content assist was invoked in.</p> |
| 54 |
* |
| 55 |
* @base org.eclipse.wst.xml.ui.internal.contentassist.AbstractContentAssistProcessor |
| 56 |
* @see DefaultJpaXmlCompletionProposalComputer |
| 57 |
* |
| 58 |
* The code of this class was copied from AbstractXMLCompletionProposalComputer and modified according to |
| 59 |
* the needs of the XML content assist of Dali. This class can be removed after bug 371939 is addressed. |
| 60 |
*/ |
| 61 |
|
| 62 |
public abstract class AbstractJpaXmlCompletionProposalComputer implements ICompletionProposalComputer { |
| 63 |
|
| 64 |
private String fErrorMessage; |
| 65 |
private ITextViewer fTextViewer; |
| 66 |
|
| 67 |
public AbstractJpaXmlCompletionProposalComputer() { |
| 68 |
fErrorMessage = null; |
| 69 |
fTextViewer = null; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* <p>Return a list of proposed code completions based on the specified |
| 74 |
* location within the document that corresponds to the current cursor |
| 75 |
* position within the text-editor control.</p> |
| 76 |
* |
| 77 |
* @see org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor) |
| 78 |
*/ |
| 79 |
public List<ICompletionProposal> computeCompletionProposals( |
| 80 |
CompletionProposalInvocationContext context, |
| 81 |
IProgressMonitor monitor) { |
| 82 |
|
| 83 |
ITextViewer textViewer = context.getViewer(); |
| 84 |
int documentPosition = context.getInvocationOffset(); |
| 85 |
|
| 86 |
setErrorMessage(null); |
| 87 |
|
| 88 |
fTextViewer = textViewer; |
| 89 |
|
| 90 |
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(textViewer, documentPosition); |
| 91 |
|
| 92 |
Node node = (Node) treeNode; |
| 93 |
while ((node != null) && (node.getNodeType() == Node.TEXT_NODE) && (node.getParentNode() != null)) { |
| 94 |
node = node.getParentNode(); |
| 95 |
} |
| 96 |
IDOMNode xmlnode = (IDOMNode) node; |
| 97 |
|
| 98 |
ContentAssistRequest contentAssistRequest = null; |
| 99 |
|
| 100 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 101 |
ITextRegion completionRegion = getCompletionRegion(documentPosition, node); |
| 102 |
|
| 103 |
String matchString = null; |
| 104 |
try { |
| 105 |
matchString = getMatchString(sdRegion, completionRegion, documentPosition); |
| 106 |
} catch (BadLocationException e) { |
| 107 |
e.printStackTrace(); |
| 108 |
} |
| 109 |
|
| 110 |
// Handle empty Documents |
| 111 |
if (completionRegion == null) { |
| 112 |
if (((treeNode == null) || (((Node) treeNode).getNodeType() == Node.DOCUMENT_NODE)) && (completionRegion == null) && |
| 113 |
((xmlnode == null) || (xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) { |
| 114 |
|
| 115 |
IStructuredModel sModel = null; |
| 116 |
try { |
| 117 |
if(textViewer.getDocument() instanceof IStructuredDocument) { |
| 118 |
sModel = StructuredModelManager.getModelManager().getModelForRead((IStructuredDocument)textViewer.getDocument()); |
| 119 |
} |
| 120 |
if (sModel != null) { |
| 121 |
IDOMDocument docNode = ((IDOMModel) sModel).getDocument(); |
| 122 |
contentAssistRequest = new ContentAssistRequest(docNode, docNode, sdRegion, completionRegion, documentPosition, 0, null); |
| 123 |
addEmptyDocumentProposals(contentAssistRequest, context); |
| 124 |
} |
| 125 |
} |
| 126 |
finally { |
| 127 |
if (sModel != null) { |
| 128 |
sModel.releaseFromRead(); |
| 129 |
} |
| 130 |
} |
| 131 |
if (contentAssistRequest == null) { |
| 132 |
Logger.logException(new IllegalStateException("problem getting model")); //$NON-NLS-1$ |
| 133 |
return new ArrayList<ICompletionProposal>(0); |
| 134 |
} |
| 135 |
|
| 136 |
ICompletionProposal[] props = contentAssistRequest.getCompletionProposals(); |
| 137 |
return (props != null) ? Arrays.asList(props) : new ArrayList<ICompletionProposal>(0); |
| 138 |
} |
| 139 |
// MASSIVE ERROR CONDITION |
| 140 |
Logger.logException(new IllegalStateException("completion region was null")); //$NON-NLS-1$ |
| 141 |
setErrorMessage(XMLUIMessages.SEVERE_internal_error_occu_UI_); |
| 142 |
contentAssistRequest = new ContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, ""); //$NON-NLS-1$ |
| 143 |
ICompletionProposal[] props = contentAssistRequest.getCompletionProposals(); |
| 144 |
return (props != null) ? Arrays.asList(props) : new ArrayList<ICompletionProposal>(0); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
// catch documents where no region can be determined |
| 149 |
if ((xmlnode.getNodeType() == Node.DOCUMENT_NODE) && ((completionRegion == null) || |
| 150 |
(xmlnode.getChildNodes() == null) || (xmlnode.getChildNodes().getLength() == 0))) { |
| 151 |
|
| 152 |
contentAssistRequest = computeStartDocumentProposals(matchString, completionRegion, |
| 153 |
(IDOMNode) treeNode,xmlnode, context); |
| 154 |
ICompletionProposal[] props = contentAssistRequest.getCompletionProposals(); |
| 155 |
return (props != null) ? Arrays.asList(props) : new ArrayList<ICompletionProposal>(0); |
| 156 |
} |
| 157 |
|
| 158 |
// compute normal proposals |
| 159 |
contentAssistRequest = computeCompletionProposals(matchString, completionRegion, (IDOMNode) treeNode, xmlnode, context); |
| 160 |
if (contentAssistRequest == null) { |
| 161 |
contentAssistRequest = new ContentAssistRequest((Node) treeNode, node.getParentNode(), sdRegion, completionRegion, documentPosition, 0, ""); //$NON-NLS-1$ |
| 162 |
setErrorMessage(XMLUIMessages.Content_Assist_not_availab_UI_); |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
* Only set this error message if nothing else was already set |
| 167 |
**/ |
| 168 |
if (contentAssistRequest.getProposals().size() == 0 && getErrorMessage() == null) { |
| 169 |
setErrorMessage(XMLUIMessages.Content_Assist_not_availab_UI_); |
| 170 |
} |
| 171 |
|
| 172 |
ICompletionProposal[] props = contentAssistRequest.getCompletionProposals(); |
| 173 |
return (props != null) ? Arrays.asList(props) : new ArrayList<ICompletionProposal>(0); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* <p>Returns information about possible contexts based on the specified |
| 178 |
* location within the document that corresponds to the current cursor |
| 179 |
* position within the text viewer.</p> |
| 180 |
* |
| 181 |
* @see org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer#computeContextInformation(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor) |
| 182 |
*/ |
| 183 |
public List<ICompletionProposal> computeContextInformation( |
| 184 |
CompletionProposalInvocationContext context, |
| 185 |
IProgressMonitor monitor) { |
| 186 |
|
| 187 |
//no default context info |
| 188 |
return Collections.emptyList(); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* @see org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer#getErrorMessage() |
| 193 |
*/ |
| 194 |
public String getErrorMessage() { |
| 195 |
return fErrorMessage; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Add proposals for attribute names |
| 200 |
* |
| 201 |
* @param contentAssistRequest |
| 202 |
* @param context |
| 203 |
*/ |
| 204 |
protected abstract void addAttributeNameProposals(ContentAssistRequest contentAssistRequest, |
| 205 |
CompletionProposalInvocationContext context); |
| 206 |
|
| 207 |
/** |
| 208 |
* Add proposals for attribute values |
| 209 |
* |
| 210 |
* @param contentAssistRequest |
| 211 |
* @param context |
| 212 |
*/ |
| 213 |
protected abstract void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, |
| 214 |
CompletionProposalInvocationContext context); |
| 215 |
|
| 216 |
/** |
| 217 |
* Add comment proposals |
| 218 |
* |
| 219 |
* @param contentAssistRequest |
| 220 |
* @param context |
| 221 |
*/ |
| 222 |
protected abstract void addCommentProposal(ContentAssistRequest contentAssistRequest, |
| 223 |
CompletionProposalInvocationContext context); |
| 224 |
|
| 225 |
/** |
| 226 |
* Add the proposals for a completely empty document |
| 227 |
* |
| 228 |
* @param contentAssistRequest |
| 229 |
* @param context |
| 230 |
*/ |
| 231 |
protected abstract void addEmptyDocumentProposals(ContentAssistRequest contentAssistRequest, |
| 232 |
CompletionProposalInvocationContext context); |
| 233 |
|
| 234 |
/** |
| 235 |
* Add the proposals for the name in an end tag |
| 236 |
* |
| 237 |
* @param contentAssistRequest |
| 238 |
* @param context |
| 239 |
*/ |
| 240 |
protected abstract void addEndTagNameProposals(ContentAssistRequest contentAssistRequest, |
| 241 |
CompletionProposalInvocationContext context); |
| 242 |
|
| 243 |
/** |
| 244 |
* Prompt for end tags to a non-empty Node that hasn't ended Handles these |
| 245 |
* cases: <br> |
| 246 |
* <tagOpen>| <br> |
| 247 |
* <tagOpen>< |<br> |
| 248 |
* <tagOpen></ | |
| 249 |
* |
| 250 |
* @param contentAssistRequest |
| 251 |
* @param context |
| 252 |
*/ |
| 253 |
protected abstract void addEndTagProposals(ContentAssistRequest contentAssistRequest, |
| 254 |
CompletionProposalInvocationContext context); |
| 255 |
|
| 256 |
/** |
| 257 |
* Add entity proposals |
| 258 |
* |
| 259 |
* @param contentAssistRequest |
| 260 |
* @param completionRegion |
| 261 |
* @param treeNode |
| 262 |
* @param context |
| 263 |
*/ |
| 264 |
protected abstract void addEntityProposals(ContentAssistRequest contentAssistRequest, |
| 265 |
ITextRegion completionRegion, IDOMNode treeNode, CompletionProposalInvocationContext context); |
| 266 |
|
| 267 |
/** |
| 268 |
* add entity proposals |
| 269 |
* |
| 270 |
* @param proposals |
| 271 |
* @param map |
| 272 |
* @param key |
| 273 |
* @param nodeOffset |
| 274 |
* @param sdRegion |
| 275 |
* @param completionRegion |
| 276 |
* @param context |
| 277 |
*/ |
| 278 |
protected abstract void addEntityProposals(Vector<ICompletionProposal> proposals, Properties map, String key, |
| 279 |
int nodeOffset, IStructuredDocumentRegion sdRegion, ITextRegion completionRegion, |
| 280 |
CompletionProposalInvocationContext context); |
| 281 |
|
| 282 |
/** |
| 283 |
* Add PCData proposals |
| 284 |
* |
| 285 |
* @param nodeName |
| 286 |
* @param contentAssistRequest |
| 287 |
* @param context |
| 288 |
*/ |
| 289 |
protected abstract void addPCDATAProposal(String nodeName, ContentAssistRequest contentAssistRequest, |
| 290 |
CompletionProposalInvocationContext context); |
| 291 |
|
| 292 |
/** |
| 293 |
* Add start document proposals |
| 294 |
* |
| 295 |
* @param contentAssistRequest |
| 296 |
* @param context |
| 297 |
*/ |
| 298 |
protected abstract void addStartDocumentProposals(ContentAssistRequest contentAssistRequest, |
| 299 |
CompletionProposalInvocationContext context); |
| 300 |
|
| 301 |
/** |
| 302 |
* Close an unclosed start tag |
| 303 |
* |
| 304 |
* @param contentAssistRequest |
| 305 |
* @param context |
| 306 |
*/ |
| 307 |
protected abstract void addTagCloseProposals(ContentAssistRequest contentAssistRequest, |
| 308 |
CompletionProposalInvocationContext context); |
| 309 |
|
| 310 |
/** |
| 311 |
* Add tag insertion proposals |
| 312 |
* |
| 313 |
* @param contentAssistRequest |
| 314 |
* @param childPosition |
| 315 |
* @param context |
| 316 |
*/ |
| 317 |
protected abstract void addTagInsertionProposals(ContentAssistRequest contentAssistRequest, |
| 318 |
int childPosition, CompletionProposalInvocationContext context); |
| 319 |
|
| 320 |
/** |
| 321 |
* Add tag name proposals |
| 322 |
* |
| 323 |
* @param contentAssistRequest |
| 324 |
* @param childPosition |
| 325 |
* @param context |
| 326 |
*/ |
| 327 |
protected abstract void addTagNameProposals(ContentAssistRequest contentAssistRequest, |
| 328 |
int childPosition, CompletionProposalInvocationContext context); |
| 329 |
|
| 330 |
/** |
| 331 |
* @param errorMessage the reason why computeProposals was not able to find any |
| 332 |
* completions. |
| 333 |
*/ |
| 334 |
protected void setErrorMessage(String errorMessage) { |
| 335 |
fErrorMessage = errorMessage; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* <p>This does all the magic of figuring out where in the XML type document |
| 340 |
* the content assist was invoked and then calling the corresponding method |
| 341 |
* to add the correct proposals</p> |
| 342 |
* |
| 343 |
* <p><b>NOTE: </b>if overriding be sure to make super call back to this method otherwise |
| 344 |
* you will loose all of the proposals generated by this method</p> |
| 345 |
* |
| 346 |
* @param matchString |
| 347 |
* @param completionRegion |
| 348 |
* @param treeNode |
| 349 |
* @param xmlnode |
| 350 |
* @param context |
| 351 |
* |
| 352 |
* @return {@link ContentAssistRequest} that now has all the proposals in it |
| 353 |
*/ |
| 354 |
protected ContentAssistRequest computeCompletionProposals(String matchString, ITextRegion completionRegion, |
| 355 |
IDOMNode treeNode, IDOMNode xmlnode, CompletionProposalInvocationContext context) { |
| 356 |
int documentPosition = context.getInvocationOffset(); |
| 357 |
|
| 358 |
ContentAssistRequest contentAssistRequest = null; |
| 359 |
String regionType = completionRegion.getType(); |
| 360 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 361 |
|
| 362 |
// Handle the most common and best supported cases |
| 363 |
if ((xmlnode.getNodeType() == Node.ELEMENT_NODE) || (xmlnode.getNodeType() == Node.DOCUMENT_NODE)) { |
| 364 |
if (regionType == DOMRegionContext.XML_TAG_OPEN) { |
| 365 |
contentAssistRequest = computeTagOpenProposals(matchString, completionRegion, treeNode, xmlnode, context); |
| 366 |
} |
| 367 |
else if (regionType == DOMRegionContext.XML_TAG_NAME) { |
| 368 |
contentAssistRequest = computeTagNameProposals(matchString, completionRegion, |
| 369 |
treeNode, xmlnode, context); |
| 370 |
} |
| 371 |
else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) { |
| 372 |
contentAssistRequest = computeAttributeProposals(matchString, completionRegion, treeNode, xmlnode, context); |
| 373 |
} |
| 374 |
else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) { |
| 375 |
contentAssistRequest = computeEqualsProposals(matchString, completionRegion, |
| 376 |
treeNode, xmlnode, context); |
| 377 |
} |
| 378 |
else if ((regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) && (documentPosition == sdRegion.getTextEndOffset()) && |
| 379 |
(sdRegion.getText(completionRegion).endsWith("\"") || sdRegion.getText(completionRegion).endsWith("\'"))) { //$NON-NLS-1$ //$NON-NLS-2$ |
| 380 |
// this is for when the cursor is at the end of the closing quote for an attribute.. |
| 381 |
IDOMNode actualNode = (IDOMNode) xmlnode.getModel().getIndexedRegion(sdRegion.getStartOffset(completionRegion)); |
| 382 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 383 |
addTagCloseProposals(contentAssistRequest, context); |
| 384 |
} |
| 385 |
else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) { |
| 386 |
contentAssistRequest = computeAttributeValueProposals(matchString, completionRegion, treeNode, xmlnode, context); |
| 387 |
} |
| 388 |
else if ((regionType == DOMRegionContext.XML_TAG_CLOSE) || (regionType == DOMRegionContext.XML_EMPTY_TAG_CLOSE)) { |
| 389 |
|
| 390 |
contentAssistRequest = computeTagCloseProposals(matchString, completionRegion, treeNode, xmlnode, context); |
| 391 |
} |
| 392 |
else if (regionType == DOMRegionContext.XML_END_TAG_OPEN) { |
| 393 |
contentAssistRequest = computeEndTagOpenProposals(matchString, completionRegion, |
| 394 |
treeNode, xmlnode, context); |
| 395 |
} |
| 396 |
else if ((regionType == DOMRegionContext.XML_CONTENT) || (regionType == DOMRegionContext.XML_CHAR_REFERENCE) || |
| 397 |
(regionType == DOMRegionContext.XML_ENTITY_REFERENCE) || (regionType == DOMRegionContext.XML_PE_REFERENCE)) { |
| 398 |
|
| 399 |
contentAssistRequest = computeContentProposals(matchString, completionRegion, |
| 400 |
treeNode, xmlnode, context); |
| 401 |
} |
| 402 |
|
| 403 |
// These ITextRegion types begin DOM Elements as well and although |
| 404 |
// internally harder to assist, text insertions directly before them can be made |
| 405 |
else if ((documentPosition == sdRegion.getStartOffset(completionRegion)) && |
| 406 |
((regionType == DOMRegionContext.XML_DECLARATION_OPEN) || |
| 407 |
(regionType == DOMRegionContext.XML_PI_OPEN) || |
| 408 |
(regionType == DOMRegionContext.XML_COMMENT_OPEN) || |
| 409 |
(regionType == DOMRegionContext.XML_CDATA_OPEN))) { |
| 410 |
|
| 411 |
contentAssistRequest = new ContentAssistRequest(treeNode, xmlnode.getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString); |
| 412 |
addTagInsertionProposals(contentAssistRequest, getElementPosition(treeNode), context); |
| 413 |
addStartDocumentProposals(contentAssistRequest, context); |
| 414 |
} |
| 415 |
} |
| 416 |
// Not a Document or Element |
| 417 |
else if (isCloseRegion(completionRegion)) { |
| 418 |
contentAssistRequest = new ContentAssistRequest(treeNode, xmlnode.getParentNode(), sdRegion, completionRegion, sdRegion.getStartOffset(completionRegion) + completionRegion.getLength(), 0, matchString); |
| 419 |
addStartDocumentProposals(contentAssistRequest, context); |
| 420 |
if (documentPosition >= sdRegion.getTextEndOffset(completionRegion)) { |
| 421 |
addTagInsertionProposals(contentAssistRequest, getElementPosition(treeNode) + 1, context); |
| 422 |
} |
| 423 |
} |
| 424 |
else if ((documentPosition == sdRegion.getStartOffset(completionRegion)) && |
| 425 |
((regionType == DOMRegionContext.XML_DECLARATION_OPEN) || |
| 426 |
(regionType == DOMRegionContext.XML_PI_OPEN) || |
| 427 |
(regionType == DOMRegionContext.XML_COMMENT_OPEN) || |
| 428 |
(regionType == DOMRegionContext.XML_CDATA_OPEN))) { |
| 429 |
|
| 430 |
contentAssistRequest = new ContentAssistRequest(treeNode, xmlnode.getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString); |
| 431 |
addTagInsertionProposals(contentAssistRequest, getElementPosition(treeNode), context); |
| 432 |
addStartDocumentProposals(contentAssistRequest, context); |
| 433 |
} |
| 434 |
return contentAssistRequest; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* <p>Similar to {@link #computeCompletionProposals(CompletionProposalInvocationContext, IProgressMonitor)} only specificly for |
| 439 |
* attribute proposals</p> |
| 440 |
* |
| 441 |
* <p>Implementers should not override this method, it is made available to implementers so that if they override |
| 442 |
* {@link #computeCompletionProposals(String, ITextRegion, IDOMNode, IDOMNode, CompletionProposalInvocationContext)} |
| 443 |
* they can call this method if needed</p> |
| 444 |
* |
| 445 |
* @param matchString |
| 446 |
* @param completionRegion |
| 447 |
* @param nodeAtOffset |
| 448 |
* @param node |
| 449 |
* @param context |
| 450 |
* @return |
| 451 |
*/ |
| 452 |
protected final ContentAssistRequest computeAttributeProposals(String matchString, ITextRegion completionRegion, |
| 453 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 454 |
|
| 455 |
int documentPosition = context.getInvocationOffset(); |
| 456 |
ITextViewer viewer = context.getViewer(); |
| 457 |
ContentAssistRequest contentAssistRequest = null; |
| 458 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 459 |
// if the attribute name is selected, replace it instead of creating a new attribute |
| 460 |
if (documentPosition <= sdRegion.getStartOffset(completionRegion) && (viewer != null && viewer.getSelectedRange().y != (sdRegion.getEndOffset(completionRegion) - sdRegion.getStartOffset(completionRegion)))) { |
| 461 |
// setup to insert new attributes |
| 462 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 463 |
} |
| 464 |
else { |
| 465 |
// Setup to replace an existing attribute name |
| 466 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, sdRegion.getStartOffset(completionRegion), completionRegion.getTextLength(), matchString); |
| 467 |
} |
| 468 |
addAttributeNameProposals(contentAssistRequest, context); |
| 469 |
contentAssistRequest.setReplacementBeginPosition(documentPosition); |
| 470 |
contentAssistRequest.setReplacementLength(0); |
| 471 |
if ((node.getFirstStructuredDocumentRegion() != null) && (!node.getFirstStructuredDocumentRegion().isEnded())) { |
| 472 |
addTagCloseProposals(contentAssistRequest, context); |
| 473 |
} |
| 474 |
return contentAssistRequest; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* <p>this is the position the cursor should be in after the proposal is |
| 479 |
* applied</p> |
| 480 |
* |
| 481 |
* @param proposedText |
| 482 |
* @return the position the cursor should be in after the proposal is |
| 483 |
* applied |
| 484 |
*/ |
| 485 |
protected static int getCursorPositionForProposedText(String proposedText) { |
| 486 |
int cursorAdjustment; |
| 487 |
cursorAdjustment = proposedText.indexOf("\"\"") + 1; //$NON-NLS-1$ |
| 488 |
// otherwise, after the first tag |
| 489 |
if (cursorAdjustment == 0) { |
| 490 |
cursorAdjustment = proposedText.indexOf('>') + 1; |
| 491 |
} |
| 492 |
if (cursorAdjustment == 0) { |
| 493 |
cursorAdjustment = proposedText.length() + 1; |
| 494 |
} |
| 495 |
|
| 496 |
return cursorAdjustment; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* <p>helpful utility method for determining if one string starts with another one. |
| 501 |
* This is case insensitive. If either are null then result is <code>true</code></p> |
| 502 |
* |
| 503 |
* @param aString the string to check to see if it starts with the given prefix |
| 504 |
* @param prefix check that the given string starts with this prefix |
| 505 |
* |
| 506 |
* @return <code>true</code> if the given string starts with the given prefix, |
| 507 |
* <code>false</code> otherwise |
| 508 |
*/ |
| 509 |
protected static boolean beginsWith(String aString, String prefix) { |
| 510 |
if ((aString == null) || (prefix == null)) { |
| 511 |
return true; |
| 512 |
} |
| 513 |
return aString.toLowerCase().startsWith(prefix.toLowerCase()); |
| 514 |
} |
| 515 |
|
| 516 |
private ContentAssistRequest computeAttributeValueProposals(String matchString, ITextRegion completionRegion, |
| 517 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 518 |
|
| 519 |
int documentPosition = context.getInvocationOffset(); |
| 520 |
|
| 521 |
ContentAssistRequest contentAssistRequest = null; |
| 522 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 523 |
if ((documentPosition > sdRegion.getStartOffset(completionRegion) + completionRegion.getTextLength()) && (sdRegion.getStartOffset(completionRegion) + completionRegion.getTextLength() != sdRegion.getStartOffset(completionRegion) + completionRegion.getLength())) { |
| 524 |
// setup to add a new attribute at the documentPosition |
| 525 |
IDOMNode actualNode = (IDOMNode) node.getModel().getIndexedRegion(sdRegion.getStartOffset(completionRegion)); |
| 526 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 527 |
addAttributeNameProposals(contentAssistRequest, context); |
| 528 |
if ((actualNode.getFirstStructuredDocumentRegion() != null) && !actualNode.getFirstStructuredDocumentRegion().isEnded()) { |
| 529 |
addTagCloseProposals(contentAssistRequest, context); |
| 530 |
} |
| 531 |
} |
| 532 |
else { |
| 533 |
// setup to replace the existing value |
| 534 |
if (!nodeAtOffset.getFirstStructuredDocumentRegion().isEnded() && (documentPosition < sdRegion.getStartOffset(completionRegion))) { |
| 535 |
// if the IStructuredDocumentRegion isn't closed and the |
| 536 |
// cursor is in front of the value, add |
| 537 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 538 |
addAttributeNameProposals(contentAssistRequest, context); |
| 539 |
} |
| 540 |
else { |
| 541 |
int replaceLength = completionRegion.getTextLength(); |
| 542 |
|
| 543 |
//if container region, be sure replace length is only the attribute value region not the entire container |
| 544 |
if(completionRegion instanceof ITextRegionContainer){ |
| 545 |
ITextRegion openRegion = ((ITextRegionContainer) completionRegion).getFirstRegion(); |
| 546 |
ITextRegion closeRegion = ((ITextRegionContainer) completionRegion).getLastRegion(); |
| 547 |
|
| 548 |
/* |
| 549 |
* check to see if the container is opened the same way its closed. |
| 550 |
* Such as: |
| 551 |
* <img src=' ' |
| 552 |
* But not: |
| 553 |
* <img src=' |
| 554 |
* |
| 555 |
* </body> |
| 556 |
* </html> |
| 557 |
* In the latter case we only want to replace the opening text of the container |
| 558 |
* Admittedly crude test, but effective. |
| 559 |
*/ |
| 560 |
if(openRegion.getType() != closeRegion.getType()) { |
| 561 |
replaceLength = openRegion.getTextLength(); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, |
| 566 |
completionRegion, sdRegion.getStartOffset(completionRegion), |
| 567 |
replaceLength, matchString); |
| 568 |
|
| 569 |
addAttributeValueProposals(contentAssistRequest, context); |
| 570 |
} |
| 571 |
} |
| 572 |
return contentAssistRequest; |
| 573 |
} |
| 574 |
|
| 575 |
private ContentAssistRequest computeContentProposals(String matchString, ITextRegion completionRegion, |
| 576 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 577 |
|
| 578 |
int documentPosition = context.getInvocationOffset(); |
| 579 |
ContentAssistRequest contentAssistRequest = null; |
| 580 |
|
| 581 |
// setup to add children at the content node's position |
| 582 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, getStructuredDocumentRegion(documentPosition), completionRegion, documentPosition, 0, matchString); |
| 583 |
if ((node != null) && (node.getNodeType() == Node.DOCUMENT_NODE) && (((Document) node).getDoctype() == null)) { |
| 584 |
addStartDocumentProposals(contentAssistRequest, context); |
| 585 |
} |
| 586 |
addTagInsertionProposals(contentAssistRequest, getElementPosition(nodeAtOffset), context); |
| 587 |
if (node.getNodeType() != Node.DOCUMENT_NODE) { |
| 588 |
addEndTagProposals(contentAssistRequest, context); |
| 589 |
} |
| 590 |
// entities? |
| 591 |
addEntityProposals(contentAssistRequest, completionRegion, node, context); |
| 592 |
return contentAssistRequest; |
| 593 |
} |
| 594 |
|
| 595 |
private ContentAssistRequest computeEndTagOpenProposals(String matchString, ITextRegion completionRegion, |
| 596 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 597 |
|
| 598 |
int documentPosition = context.getInvocationOffset(); |
| 599 |
ContentAssistRequest contentAssistRequest = null; |
| 600 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 601 |
int completionRegionStart = sdRegion.getStartOffset(completionRegion); |
| 602 |
int completionRegionLength = completionRegion.getLength(); |
| 603 |
IStructuredDocumentRegion sdRegionAtCompletionOffset = node.getStructuredDocument().getRegionAtCharacterOffset(completionRegionStart + completionRegionLength); |
| 604 |
ITextRegion regionAtEndOfCompletion = sdRegionAtCompletionOffset.getRegionAtCharacterOffset(completionRegionStart + completionRegionLength); |
| 605 |
|
| 606 |
if ((documentPosition != completionRegionStart) && (regionAtEndOfCompletion != null) && (regionAtEndOfCompletion.getType() == DOMRegionContext.XML_TAG_NAME)) { |
| 607 |
ITextRegion nameRegion = regionAtEndOfCompletion; |
| 608 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, nodeAtOffset.getParentNode(), sdRegion, completionRegion, sdRegion.getStartOffset(nameRegion), nameRegion.getTextLength(), matchString); |
| 609 |
} |
| 610 |
else { |
| 611 |
if (nodeAtOffset.getFirstStructuredDocumentRegion() == sdRegion) { |
| 612 |
// abnormal case, this unmatched end tag will be a sibling |
| 613 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, nodeAtOffset.getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString); |
| 614 |
} |
| 615 |
else { |
| 616 |
// normal case, this end tag is the parent |
| 617 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, nodeAtOffset, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 618 |
} |
| 619 |
} |
| 620 |
// if (documentPosition >= sdRegion.getStartOffset(completionRegion) + |
| 621 |
// completionRegion.getTextLength()) |
| 622 |
addEndTagProposals(contentAssistRequest, context); |
| 623 |
// else |
| 624 |
if (completionRegionStart == documentPosition) { |
| 625 |
// positioned at start of end tag |
| 626 |
addTagInsertionProposals(contentAssistRequest, node.getChildNodes().getLength(), context); |
| 627 |
} |
| 628 |
return contentAssistRequest; |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
private ContentAssistRequest computeEqualsProposals(String matchString, ITextRegion completionRegion, |
| 633 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 634 |
|
| 635 |
int documentPosition = context.getInvocationOffset(); |
| 636 |
ContentAssistRequest contentAssistRequest = null; |
| 637 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 638 |
ITextRegion valueRegion = node.getStartStructuredDocumentRegion().getRegionAtCharacterOffset(sdRegion.getStartOffset(completionRegion) + completionRegion.getLength()); |
| 639 |
if ((valueRegion != null) && (valueRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) && (sdRegion.getStartOffset(valueRegion) <= documentPosition)) { |
| 640 |
// replace the adjacent attribute value |
| 641 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, valueRegion, sdRegion.getStartOffset(valueRegion), valueRegion.getTextLength(), matchString); |
| 642 |
} |
| 643 |
else { |
| 644 |
// append an attribute value after the '=' |
| 645 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 646 |
} |
| 647 |
addAttributeValueProposals(contentAssistRequest, context); |
| 648 |
return contentAssistRequest; |
| 649 |
} |
| 650 |
|
| 651 |
private ContentAssistRequest computeStartDocumentProposals(String matchString, ITextRegion completionRegion, |
| 652 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 653 |
int documentPosition = context.getInvocationOffset(); |
| 654 |
|
| 655 |
// setup for a non-empty document, but one that hasn't been formally started |
| 656 |
ContentAssistRequest contentAssistRequest = null; |
| 657 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, |
| 658 |
getStructuredDocumentRegion(documentPosition), completionRegion, |
| 659 |
documentPosition, 0, matchString); |
| 660 |
addStartDocumentProposals(contentAssistRequest, context); |
| 661 |
return contentAssistRequest; |
| 662 |
} |
| 663 |
|
| 664 |
private ContentAssistRequest computeTagCloseProposals(String matchString, ITextRegion completionRegion, |
| 665 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 666 |
|
| 667 |
int documentPosition = context.getInvocationOffset(); |
| 668 |
ContentAssistRequest contentAssistRequest = null; |
| 669 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 670 |
|
| 671 |
if ((node.getNodeType() == Node.DOCUMENT_NODE) || (documentPosition >= sdRegion.getEndOffset())) { |
| 672 |
// this is a content request as the documentPosition is |
| 673 |
// AFTER the end of the closing region |
| 674 |
if ((node == nodeAtOffset) && (node.getParentNode() != null)) { |
| 675 |
node = (IDOMNode) node.getParentNode(); |
| 676 |
} |
| 677 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 678 |
addTagInsertionProposals(contentAssistRequest, getElementPosition(nodeAtOffset), context); |
| 679 |
if ((node.getNodeType() != Node.DOCUMENT_NODE) && (node.getEndStructuredDocumentRegion() == null)) { |
| 680 |
addEndTagProposals(contentAssistRequest, context); |
| 681 |
} |
| 682 |
} |
| 683 |
else { |
| 684 |
// at the start of the tag's close or within it |
| 685 |
ITextRegion closeRegion = sdRegion.getLastRegion(); |
| 686 |
boolean insideTag = !sdRegion.isEnded() || (documentPosition <= sdRegion.getStartOffset(closeRegion)); |
| 687 |
if (insideTag) { |
| 688 |
// this is a request for completions within a tag |
| 689 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 690 |
if ((node.getNodeType() != Node.DOCUMENT_NODE) && (node.getEndStructuredDocumentRegion() != null)) { |
| 691 |
addTagCloseProposals(contentAssistRequest, context); |
| 692 |
} |
| 693 |
if (sdRegion == nodeAtOffset.getFirstStructuredDocumentRegion()) { |
| 694 |
contentAssistRequest.setReplacementBeginPosition(documentPosition); |
| 695 |
contentAssistRequest.setReplacementLength(0); |
| 696 |
addAttributeNameProposals(contentAssistRequest, context); |
| 697 |
} |
| 698 |
} |
| 699 |
} |
| 700 |
return contentAssistRequest; |
| 701 |
} |
| 702 |
|
| 703 |
private ContentAssistRequest computeTagNameProposals(String matchString, ITextRegion completionRegion, |
| 704 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 705 |
|
| 706 |
int documentPosition = context.getInvocationOffset(); |
| 707 |
ContentAssistRequest contentAssistRequest = null; |
| 708 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 709 |
|
| 710 |
if (sdRegion != nodeAtOffset.getFirstStructuredDocumentRegion()) { |
| 711 |
// completing the *first* tag in "<tagname1 |<tagname2" |
| 712 |
IDOMNode actualNode = (IDOMNode) node.getModel().getIndexedRegion(sdRegion.getStartOffset(completionRegion)); |
| 713 |
if (actualNode != null) { |
| 714 |
if (actualNode.getFirstStructuredDocumentRegion() == sdRegion) { |
| 715 |
// start tag |
| 716 |
if (documentPosition > sdRegion.getStartOffset(completionRegion) + completionRegion.getLength()) { |
| 717 |
// it's attributes |
| 718 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode, sdRegion, completionRegion, documentPosition - matchString.length(), matchString.length(), matchString); |
| 719 |
if (node.getStructuredDocument().getRegionAtCharacterOffset(sdRegion.getStartOffset(completionRegion) - 1).getRegionAtCharacterOffset(sdRegion.getStartOffset(completionRegion) - 1).getType() == DOMRegionContext.XML_TAG_OPEN) { |
| 720 |
addAttributeNameProposals(contentAssistRequest, context); |
| 721 |
} |
| 722 |
addTagCloseProposals(contentAssistRequest, context); |
| 723 |
} |
| 724 |
else { |
| 725 |
// it's name |
| 726 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode.getParentNode(), |
| 727 |
sdRegion, completionRegion, documentPosition - matchString.length(), |
| 728 |
matchString.length(), matchString); |
| 729 |
addTagNameProposals(contentAssistRequest, getElementPosition(actualNode), |
| 730 |
context); |
| 731 |
} |
| 732 |
} |
| 733 |
else { |
| 734 |
if (documentPosition >= sdRegion.getStartOffset(completionRegion) + completionRegion.getLength()) { |
| 735 |
// insert name |
| 736 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode.getParentNode(), |
| 737 |
sdRegion, completionRegion, documentPosition, 0, matchString); |
| 738 |
} |
| 739 |
else { |
| 740 |
// replace name |
| 741 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode.getParentNode(), |
| 742 |
sdRegion, completionRegion, sdRegion.getStartOffset(completionRegion), |
| 743 |
completionRegion.getTextLength(), matchString); |
| 744 |
} |
| 745 |
addEndTagNameProposals(contentAssistRequest, context); |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
else { |
| 750 |
if (documentPosition > sdRegion.getStartOffset(completionRegion) + completionRegion.getTextLength()) { |
| 751 |
// unclosed tag with only a name; |
| 752 |
//should prompt for attributes and a close instead |
| 753 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node, sdRegion, completionRegion, |
| 754 |
documentPosition - matchString.length(), matchString.length(), matchString); |
| 755 |
addAttributeNameProposals(contentAssistRequest, context); |
| 756 |
addTagCloseProposals(contentAssistRequest, context); |
| 757 |
} |
| 758 |
else { |
| 759 |
if (sdRegion.getRegions().get(0).getType() != DOMRegionContext.XML_END_TAG_OPEN) { |
| 760 |
int replaceLength = documentPosition - sdRegion.getStartOffset(completionRegion); |
| 761 |
contentAssistRequest = new ContentAssistRequest(node, node.getParentNode(), sdRegion, |
| 762 |
completionRegion, sdRegion.getStartOffset(completionRegion), replaceLength, matchString); |
| 763 |
addTagNameProposals(contentAssistRequest, getElementPosition(nodeAtOffset), context); |
| 764 |
} |
| 765 |
else { |
| 766 |
IDOMNode actualNode = (IDOMNode) node.getModel().getIndexedRegion(documentPosition); |
| 767 |
if (actualNode != null) { |
| 768 |
if (documentPosition >= sdRegion.getStartOffset(completionRegion) + completionRegion.getTextLength()) { |
| 769 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode.getParentNode(), |
| 770 |
sdRegion, completionRegion, documentPosition, 0, matchString); |
| 771 |
} |
| 772 |
else { |
| 773 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode.getParentNode(), |
| 774 |
sdRegion, completionRegion, sdRegion.getStartOffset(completionRegion), |
| 775 |
completionRegion.getTextLength(), matchString); |
| 776 |
} |
| 777 |
addEndTagNameProposals(contentAssistRequest, context); |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
return contentAssistRequest; |
| 783 |
} |
| 784 |
|
| 785 |
private ContentAssistRequest computeTagOpenProposals(String matchString, ITextRegion completionRegion, |
| 786 |
IDOMNode nodeAtOffset, IDOMNode node, CompletionProposalInvocationContext context) { |
| 787 |
|
| 788 |
int documentPosition = context.getInvocationOffset(); |
| 789 |
ContentAssistRequest contentAssistRequest = null; |
| 790 |
IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition); |
| 791 |
if (sdRegion != nodeAtOffset.getFirstStructuredDocumentRegion() || sdRegion.getPrevious() != null && sdRegion.getPrevious().getLastRegion().getType() == DOMRegionContext.XML_TAG_OPEN) { |
| 792 |
// completing the *first* XML_TAG_OPEN in "<<tagname" |
| 793 |
IDOMNode actualNode = (IDOMNode) node.getModel().getIndexedRegion(sdRegion.getStartOffset(completionRegion)); |
| 794 |
if (actualNode != null) { |
| 795 |
if(sdRegion.getFirstRegion().getType() == DOMRegionContext.XML_END_TAG_OPEN) { |
| 796 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode, sdRegion, completionRegion, documentPosition, 0, matchString); |
| 797 |
if(actualNode.hasChildNodes()) |
| 798 |
addTagNameProposals(contentAssistRequest, |
| 799 |
getElementPosition(actualNode.getLastChild()), context); |
| 800 |
else |
| 801 |
addTagNameProposals(contentAssistRequest, 0, context); |
| 802 |
} |
| 803 |
else { |
| 804 |
contentAssistRequest = new ContentAssistRequest(actualNode, actualNode.getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString); |
| 805 |
addTagNameProposals(contentAssistRequest, getElementPosition(actualNode), context); |
| 806 |
} |
| 807 |
addEndTagProposals(contentAssistRequest, context); // (pa) 220850 |
| 808 |
} |
| 809 |
} |
| 810 |
else { |
| 811 |
if (documentPosition == sdRegion.getStartOffset(completionRegion)) { |
| 812 |
if (node.getNodeType() == Node.ELEMENT_NODE) { |
| 813 |
// at the start of an existing tag, right before the '<' |
| 814 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, |
| 815 |
completionRegion, documentPosition, 0, matchString); |
| 816 |
addTagInsertionProposals(contentAssistRequest, getElementPosition(nodeAtOffset), context); |
| 817 |
addEndTagProposals(contentAssistRequest, context); |
| 818 |
} |
| 819 |
else if (node.getNodeType() == Node.DOCUMENT_NODE) { |
| 820 |
// at the opening of the VERY first tag with a '<' |
| 821 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, |
| 822 |
completionRegion, sdRegion.getStartOffset(completionRegion), completionRegion.getTextLength(), matchString); |
| 823 |
addStartDocumentProposals(contentAssistRequest, context); |
| 824 |
} |
| 825 |
} |
| 826 |
else { |
| 827 |
// within the white space |
| 828 |
ITextRegion name = getNameRegion(node.getStartStructuredDocumentRegion()); |
| 829 |
if ((name != null) && |
| 830 |
((sdRegion.getStartOffset(name) <= documentPosition) && (sdRegion.getEndOffset(name) >= documentPosition)) && |
| 831 |
(sdRegion.getLastRegion().getType() == DOMRegionContext.XML_TAG_CLOSE || sdRegion.getLastRegion().getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE)){ |
| 832 |
|
| 833 |
// replace the existing name |
| 834 |
contentAssistRequest = new ContentAssistRequest(node, node.getParentNode(), sdRegion, completionRegion, sdRegion.getStartOffset(name), name.getTextLength(), matchString); |
| 835 |
} |
| 836 |
else { |
| 837 |
// insert a valid new name, or possibly an end tag |
| 838 |
contentAssistRequest = new ContentAssistRequest(nodeAtOffset, ((Node) nodeAtOffset).getParentNode(), sdRegion, completionRegion, documentPosition, 0, matchString); |
| 839 |
addEndTagProposals(contentAssistRequest, context); |
| 840 |
contentAssistRequest.setReplacementBeginPosition(documentPosition); |
| 841 |
contentAssistRequest.setReplacementLength(0); |
| 842 |
} |
| 843 |
addTagNameProposals(contentAssistRequest, getElementPosition(nodeAtOffset), context); |
| 844 |
} |
| 845 |
} |
| 846 |
return contentAssistRequest; |
| 847 |
} |
| 848 |
|
| 849 |
private ITextRegion getCompletionRegion(int offset, IStructuredDocumentRegion sdRegion) { |
| 850 |
ITextRegion region = sdRegion.getRegionAtCharacterOffset(offset); |
| 851 |
if (region == null) { |
| 852 |
return null; |
| 853 |
} |
| 854 |
|
| 855 |
if (sdRegion.getStartOffset(region) == offset) { |
| 856 |
// The offset is at the beginning of the region |
| 857 |
if ((sdRegion.getStartOffset(region) == sdRegion.getStartOffset()) && (sdRegion.getPrevious() != null) && (!sdRegion.getPrevious().isEnded())) { |
| 858 |
// Is the region also the start of the node? If so, the |
| 859 |
// previous IStructuredDocumentRegion is |
| 860 |
// where to look for a useful region. |
| 861 |
region = sdRegion.getPrevious().getRegionAtCharacterOffset(offset - 1); |
| 862 |
} |
| 863 |
else { |
| 864 |
// Is there no separating whitespace from the previous region? |
| 865 |
// If not, |
| 866 |
// then that region is the important one |
| 867 |
ITextRegion previousRegion = sdRegion.getRegionAtCharacterOffset(offset - 1); |
| 868 |
if ((previousRegion != null) && (previousRegion != region) && (previousRegion.getTextLength() == previousRegion.getLength())) { |
| 869 |
region = previousRegion; |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
else { |
| 874 |
// The offset is NOT at the beginning of the region |
| 875 |
if (offset > sdRegion.getStartOffset(region) + region.getTextLength()) { |
| 876 |
// Is the offset within the whitespace after the text in this |
| 877 |
// region? |
| 878 |
// If so, use the next region |
| 879 |
ITextRegion nextRegion = sdRegion.getRegionAtCharacterOffset(sdRegion.getStartOffset(region) + region.getLength()); |
| 880 |
if (nextRegion != null) { |
| 881 |
region = nextRegion; |
| 882 |
} |
| 883 |
} |
| 884 |
else { |
| 885 |
// Is the offset within the important text for this region? |
| 886 |
// If so, then we've already got the right one. |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
// valid WHITE_SPACE region handler (#179924) |
| 891 |
if ((region != null) && (region.getType() == DOMRegionContext.WHITE_SPACE)) { |
| 892 |
ITextRegion previousRegion = sdRegion.getRegionAtCharacterOffset(sdRegion.getStartOffset(region) - 1); |
| 893 |
if (previousRegion != null) { |
| 894 |
region = previousRegion; |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
return region; |
| 899 |
} |
| 900 |
|
| 901 |
/** |
| 902 |
* Return the region whose content's require completion. This is something |
| 903 |
* of a misnomer as sometimes the user wants to be prompted for contents |
| 904 |
* of a non-existant ITextRegion, such as for enumerated attribute values |
| 905 |
* following an '=' sign. |
| 906 |
*/ |
| 907 |
private ITextRegion getCompletionRegion(int documentPosition, Node domnode) { |
| 908 |
if (domnode == null) { |
| 909 |
return null; |
| 910 |
} |
| 911 |
|
| 912 |
ITextRegion region = null; |
| 913 |
int offset = documentPosition; |
| 914 |
IStructuredDocumentRegion flatNode = null; |
| 915 |
IDOMNode node = (IDOMNode) domnode; |
| 916 |
|
| 917 |
if (node.getNodeType() == Node.DOCUMENT_NODE) { |
| 918 |
if (node.getStructuredDocument().getLength() == 0) { |
| 919 |
return null; |
| 920 |
} |
| 921 |
ITextRegion result = node.getStructuredDocument().getRegionAtCharacterOffset(offset).getRegionAtCharacterOffset(offset); |
| 922 |
while (result == null) { |
| 923 |
offset--; |
| 924 |
result = node.getStructuredDocument().getRegionAtCharacterOffset(offset).getRegionAtCharacterOffset(offset); |
| 925 |
} |
| 926 |
return result; |
| 927 |
} |
| 928 |
|
| 929 |
IStructuredDocumentRegion startTag = node.getStartStructuredDocumentRegion(); |
| 930 |
IStructuredDocumentRegion endTag = node.getEndStructuredDocumentRegion(); |
| 931 |
|
| 932 |
// Determine if the offset is within the start |
| 933 |
// IStructuredDocumentRegion, end IStructuredDocumentRegion, or |
| 934 |
// somewhere within the Node's XML content. |
| 935 |
if ((startTag != null) && (startTag.getStartOffset() <= offset) && (offset < startTag.getStartOffset() + startTag.getLength())) { |
| 936 |
flatNode = startTag; |
| 937 |
} |
| 938 |
else if ((endTag != null) && (endTag.getStartOffset() <= offset) && (offset < endTag.getStartOffset() + endTag.getLength())) { |
| 939 |
flatNode = endTag; |
| 940 |
} |
| 941 |
|
| 942 |
if (flatNode != null) { |
| 943 |
// the offset is definitely within the start or end tag, continue |
| 944 |
// on and find the region |
| 945 |
region = getCompletionRegion(offset, flatNode); |
| 946 |
} |
| 947 |
else { |
| 948 |
// the docPosition is neither within the start nor the end, so it |
| 949 |
// must be content |
| 950 |
flatNode = node.getStructuredDocument().getRegionAtCharacterOffset(offset); |
| 951 |
// (pa) ITextRegion refactor |
| 952 |
// if (flatNode.contains(documentPosition)) { |
| 953 |
if ((flatNode.getStartOffset() <= documentPosition) && (flatNode.getEndOffset() >= documentPosition)) { |
| 954 |
// we're interesting in completing/extending the previous |
| 955 |
// IStructuredDocumentRegion if the current |
| 956 |
// IStructuredDocumentRegion isn't plain content or if it's |
| 957 |
// preceded by an orphan '<' |
| 958 |
if ((offset == flatNode.getStartOffset()) && |
| 959 |
(flatNode.getPrevious() != null) && |
| 960 |
(((flatNode.getRegionAtCharacterOffset(documentPosition) != null) && |
| 961 |
(flatNode.getRegionAtCharacterOffset(documentPosition).getType() != DOMRegionContext.XML_CONTENT)) || |
| 962 |
(flatNode.getPrevious().getLastRegion().getType() == DOMRegionContext.XML_TAG_OPEN) || |
| 963 |
(flatNode.getPrevious().getLastRegion().getType() == DOMRegionContext.XML_END_TAG_OPEN))) { |
| 964 |
|
| 965 |
// Is the region also the start of the node? If so, the |
| 966 |
// previous IStructuredDocumentRegion is |
| 967 |
// where to look for a useful region. |
| 968 |
region = flatNode.getPrevious().getLastRegion(); |
| 969 |
} |
| 970 |
else if (flatNode.getEndOffset() == documentPosition) { |
| 971 |
region = flatNode.getLastRegion(); |
| 972 |
} |
| 973 |
else { |
| 974 |
region = flatNode.getFirstRegion(); |
| 975 |
} |
| 976 |
} |
| 977 |
else { |
| 978 |
// catch end of document positions where the docPosition isn't |
| 979 |
// in a IStructuredDocumentRegion |
| 980 |
region = flatNode.getLastRegion(); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
return region; |
| 985 |
} |
| 986 |
|
| 987 |
private int getElementPosition(Node child) { |
| 988 |
Node parent = child.getParentNode(); |
| 989 |
if (parent == null) { |
| 990 |
return 0; |
| 991 |
} |
| 992 |
|
| 993 |
NodeList children = parent.getChildNodes(); |
| 994 |
if (children == null) { |
| 995 |
return 0; |
| 996 |
} |
| 997 |
int count = 0; |
| 998 |
|
| 999 |
for (int i = 0; i < children.getLength(); i++) { |
| 1000 |
if (children.item(i) == child) { |
| 1001 |
return count; |
| 1002 |
} |
| 1003 |
else { |
| 1004 |
// if (children.item(i).getNodeType() == Node.ELEMENT_NODE) |
| 1005 |
count++; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
return 0; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/** |
| 1012 |
* the returned string will be used as the filter to show the proposed values |
| 1013 |
* @throws BadLocationException |
| 1014 |
*/ |
| 1015 |
protected String getMatchString(IStructuredDocumentRegion parent, ITextRegion aRegion, int offset) throws BadLocationException { |
| 1016 |
if ((aRegion == null) || isCloseRegion(aRegion)) { |
| 1017 |
return ""; //$NON-NLS-1$ |
| 1018 |
} |
| 1019 |
String matchString = null; |
| 1020 |
String regionType = aRegion.getType(); |
| 1021 |
if ((regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) || (regionType == DOMRegionContext.XML_TAG_OPEN) || (offset > parent.getStartOffset(aRegion) + aRegion.getTextLength())) { |
| 1022 |
matchString = ""; //$NON-NLS-1$ |
| 1023 |
} |
| 1024 |
else if (regionType == DOMRegionContext.XML_CONTENT || regionType == DOMRegionContext.XML_END_TAG_OPEN) { |
| 1025 |
int begin = parent.getPrevious().getStartOffset(); |
| 1026 |
int length = offset-parent.getPrevious().getStart(); |
| 1027 |
matchString = parent.getParentDocument().get(begin, length); |
| 1028 |
|
| 1029 |
// parent.getParentDocument().get(begin, length) could return |
| 1030 |
// something likes <schema> or <schema>a or <schema> a |
| 1031 |
// get the substring as the match string |
| 1032 |
if ((matchString.length() > 0) && (beginsWith(matchString, "<"))) { |
| 1033 |
if (matchString.contains(">")) { |
| 1034 |
matchString = parent.getParentDocument().get(begin + matchString.indexOf('>') + 1, length-matchString.indexOf('>')-1); |
| 1035 |
} else { |
| 1036 |
matchString =""; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
// chop off any leading whitespace(s) from the match string |
| 1041 |
while ((matchString.length() > 0) && (Character.isWhitespace(matchString.charAt(0)))) { |
| 1042 |
matchString = matchString.substring(1); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
else { |
| 1046 |
if ((parent.getText(aRegion).length() > 0) && (parent.getStartOffset(aRegion) < offset)) { |
| 1047 |
matchString = parent.getText(aRegion).substring(0, offset - parent.getStartOffset(aRegion)); |
| 1048 |
} else { |
| 1049 |
matchString = ""; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
return matchString; |
| 1053 |
} |
| 1054 |
|
| 1055 |
@SuppressWarnings("rawtypes") |
| 1056 |
private ITextRegion getNameRegion(IStructuredDocumentRegion flatNode) { |
| 1057 |
if (flatNode == null) { |
| 1058 |
return null; |
| 1059 |
} |
| 1060 |
Iterator regionList = flatNode.getRegions().iterator(); |
| 1061 |
while (regionList.hasNext()) { |
| 1062 |
ITextRegion region = (ITextRegion) regionList.next(); |
| 1063 |
if (isNameRegion(region)) { |
| 1064 |
return region; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
return null; |
| 1068 |
} |
| 1069 |
|
| 1070 |
private boolean isCloseRegion(ITextRegion region) { |
| 1071 |
String type = region.getType(); |
| 1072 |
return ((type == DOMRegionContext.XML_PI_CLOSE) || |
| 1073 |
(type == DOMRegionContext.XML_TAG_CLOSE) || |
| 1074 |
(type == DOMRegionContext.XML_EMPTY_TAG_CLOSE) || |
| 1075 |
(type == DOMRegionContext.XML_CDATA_CLOSE) || |
| 1076 |
(type == DOMRegionContext.XML_COMMENT_CLOSE) || |
| 1077 |
(type == DOMRegionContext.XML_ATTLIST_DECL_CLOSE) || |
| 1078 |
(type == DOMRegionContext.XML_ELEMENT_DECL_CLOSE) || |
| 1079 |
(type == DOMRegionContext.XML_DOCTYPE_DECLARATION_CLOSE) || |
| 1080 |
(type == DOMRegionContext.XML_DECLARATION_CLOSE)); |
| 1081 |
} |
| 1082 |
|
| 1083 |
private boolean isNameRegion(ITextRegion region) { |
| 1084 |
String type = region.getType(); |
| 1085 |
return ((type == DOMRegionContext.XML_TAG_NAME) || |
| 1086 |
(type == DOMRegionContext.XML_ELEMENT_DECL_NAME) || |
| 1087 |
(type == DOMRegionContext.XML_DOCTYPE_NAME) || |
| 1088 |
(type == DOMRegionContext.XML_ATTLIST_DECL_NAME)); |
| 1089 |
} |
| 1090 |
|
| 1091 |
/** |
| 1092 |
* StructuredTextViewer must be set before using this. |
| 1093 |
*/ |
| 1094 |
private IStructuredDocumentRegion getStructuredDocumentRegion(int pos) { |
| 1095 |
return ContentAssistUtils.getStructuredDocumentRegion(fTextViewer, pos); |
| 1096 |
} |
| 1097 |
} |