use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class JSPPropertyCompletionProposalComputer method addAttributeValueProposals.
/**
* @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// only add attribute value proposals for specific elements
if (node.getNodeName().equals(JSP11Namespace.ElementName.SETPROPERTY) || node.getNodeName().equals(JSP11Namespace.ElementName.GETPROPERTY)) {
// Find the attribute name for which this position should have a value
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
ITextRegionList openRegions = open.getRegions();
int i = openRegions.indexOf(contentAssistRequest.getRegion());
if (i < 0)
return;
// get the attribute in question (first attr name to the left of the cursor)
ITextRegion attrNameRegion = null;
String attributeName = null;
while (i >= 0) {
attrNameRegion = openRegions.get(i--);
if (attrNameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
break;
}
if (attrNameRegion != null)
attributeName = open.getText(attrNameRegion);
// determine get or set
ITextRegion tagNameRegion = null;
boolean isGetProperty = true;
for (int j = 0; j < openRegions.size(); j++) {
tagNameRegion = openRegions.get(j);
if (tagNameRegion.getType() == DOMRegionContext.XML_TAG_NAME && open.getText(tagNameRegion).trim().equals("jsp:setProperty")) {
// $NON-NLS-1$
isGetProperty = false;
break;
}
}
String currentValue = null;
if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
currentValue = contentAssistRequest.getText();
else
// $NON-NLS-1$
currentValue = "";
String matchString = null;
// fixups since the matchString computations don't care if there are quotes around the value
if (currentValue.length() > StringUtils.strip(currentValue).length() && // $NON-NLS-1$ //$NON-NLS-2$
(currentValue.startsWith("\"") || currentValue.startsWith("'")) && contentAssistRequest.getMatchString().length() > 0) {
matchString = currentValue.substring(1, contentAssistRequest.getMatchString().length());
} else {
matchString = currentValue.substring(0, contentAssistRequest.getMatchString().length());
}
// for now we ignore complicated values such as jsp embedded in an attribute
boolean existingComplicatedValue = contentAssistRequest.getRegion() != null && contentAssistRequest.getRegion() instanceof ITextRegionContainer;
if (existingComplicatedValue) {
contentAssistRequest.getProposals().clear();
contentAssistRequest.getMacros().clear();
} else {
if (attributeName.equals(JSP11Namespace.ATTR_NAME_NAME)) {
addBeanNameProposals(contentAssistRequest, node, matchString);
} else if (attributeName.equals(JSP11Namespace.ATTR_NAME_PROPERTY)) {
addBeanPropertyProposals(contentAssistRequest, node, isGetProperty, matchString);
}
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class JSPTaglibCompletionProposalComputer method addAttributeValueProposals.
/**
* @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
IPath basePath = getBasePath(contentAssistRequest);
if (basePath != null) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// only add attribute value proposals for specific elements
if (node.getNodeName().equals(JSP11Namespace.ElementName.DIRECTIVE_TAGLIB)) {
// Find the attribute name for which this position should have a value
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
ITextRegionList openRegions = open.getRegions();
int i = openRegions.indexOf(contentAssistRequest.getRegion());
if (i < 0)
return;
ITextRegion nameRegion = null;
while (i >= 0) {
nameRegion = openRegions.get(i--);
if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
break;
}
String attributeName = null;
if (nameRegion != null)
attributeName = open.getText(nameRegion);
String currentValue = null;
if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
currentValue = contentAssistRequest.getText();
else
// $NON-NLS-1$
currentValue = "";
String matchString = null;
// fixups
int start = contentAssistRequest.getReplacementBeginPosition();
int length = contentAssistRequest.getReplacementLength();
if (currentValue.length() > StringUtils.strip(currentValue).length() && // $NON-NLS-1$ //$NON-NLS-2$
(currentValue.startsWith("\"") || currentValue.startsWith("'")) && contentAssistRequest.getMatchString().length() > 0) {
matchString = currentValue.substring(1, contentAssistRequest.getMatchString().length());
} else {
matchString = currentValue.substring(0, contentAssistRequest.getMatchString().length());
}
boolean existingComplicatedValue = contentAssistRequest.getRegion() != null && contentAssistRequest.getRegion() instanceof ITextRegionContainer;
if (existingComplicatedValue) {
contentAssistRequest.getProposals().clear();
contentAssistRequest.getMacros().clear();
} else {
String lowerCaseMatch = matchString.toLowerCase(Locale.US);
if (attributeName.equals(JSP11Namespace.ATTR_NAME_URI)) {
ITaglibRecord[] availableTaglibRecords = TaglibIndex.getAvailableTaglibRecords(basePath);
/*
* a simple enough way to remove duplicates (resolution at
* runtime would be nondeterministic anyway)
*/
Map uriToRecords = new HashMap();
for (int taglibRecordNumber = 0; taglibRecordNumber < availableTaglibRecords.length; taglibRecordNumber++) {
ITaglibRecord taglibRecord = availableTaglibRecords[taglibRecordNumber];
ITaglibDescriptor descriptor = taglibRecord.getDescriptor();
String uri = null;
switch(taglibRecord.getRecordType()) {
case ITaglibRecord.URL:
uri = descriptor.getURI();
uriToRecords.put(uri, taglibRecord);
break;
case ITaglibRecord.JAR:
{
IPath location = ((IJarRecord) taglibRecord).getLocation();
IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(location);
IPath localContextRoot = FacetModuleCoreSupport.computeWebContentRootPath(basePath);
for (int fileNumber = 0; fileNumber < files.length; fileNumber++) {
if (localContextRoot.isPrefixOf(files[fileNumber].getFullPath())) {
uri = IPath.SEPARATOR + files[fileNumber].getFullPath().removeFirstSegments(localContextRoot.segmentCount()).toString();
uriToRecords.put(uri, taglibRecord);
} else {
uri = FacetModuleCoreSupport.getRuntimePath(files[fileNumber].getFullPath()).toString();
uriToRecords.put(uri, taglibRecord);
}
}
break;
}
case ITaglibRecord.TLD:
{
uri = descriptor.getURI();
if (uri == null || uri.trim().length() == 0) {
IPath path = ((ITLDRecord) taglibRecord).getPath();
IPath localContextRoot = FacetModuleCoreSupport.computeWebContentRootPath(basePath);
if (localContextRoot.isPrefixOf(path)) {
uri = IPath.SEPARATOR + path.removeFirstSegments(localContextRoot.segmentCount()).toString();
} else {
uri = FacetModuleCoreSupport.getRuntimePath(path).toString();
}
}
uriToRecords.put(uri, taglibRecord);
break;
}
}
}
/*
* use the records and their descriptors to construct
* proposals
*/
Object[] uris = uriToRecords.keySet().toArray();
for (int uriNumber = 0; uriNumber < uris.length; uriNumber++) {
String uri = uris[uriNumber].toString();
ITaglibRecord taglibRecord = (ITaglibRecord) uriToRecords.get(uri);
ITaglibDescriptor descriptor = (taglibRecord).getDescriptor();
if (uri != null && uri.length() > 0 && (matchString.length() == 0 || uri.toLowerCase(Locale.US).startsWith(lowerCaseMatch))) {
String url = getSmallImageURL(taglibRecord);
ImageDescriptor imageDescriptor = JSPUIPlugin.getInstance().getImageRegistry().getDescriptor(url);
if (imageDescriptor == null && url != null) {
URL imageURL;
try {
imageURL = new URL(url);
imageDescriptor = ImageDescriptor.createFromURL(imageURL);
JSPUIPlugin.getInstance().getImageRegistry().put(url, imageDescriptor);
} catch (MalformedURLException e) {
Logger.logException(e);
}
}
String additionalInfo = // $NON-NLS-1$
descriptor.getDisplayName() + "<br/>" + descriptor.getDescription() + "<br/>" + // $NON-NLS-1$
descriptor.getTlibVersion();
Image image = null;
try {
image = JSPUIPlugin.getInstance().getImageRegistry().get(url);
} catch (Exception e) {
Logger.logException(e);
}
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
}
CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
"\"" + uri + "\"", // $NON-NLS-1$ //$NON-NLS-2$
start, // $NON-NLS-1$ //$NON-NLS-2$
length, // $NON-NLS-1$ //$NON-NLS-2$
uri.length() + 2, image, uri, null, additionalInfo, IRelevanceConstants.R_NONE);
contentAssistRequest.addProposal(proposal);
}
}
} else if (attributeName.equals(JSP20Namespace.ATTR_NAME_TAGDIR)) {
ITaglibRecord[] availableTaglibRecords = TaglibIndex.getAvailableTaglibRecords(basePath);
/*
* a simple enough way to remove duplicates (resolution at
* runtime would be nondeterministic anyway)
*/
Map uriToRecords = new HashMap();
IPath localContextRoot = FacetModuleCoreSupport.computeWebContentRootPath(basePath);
for (int taglibRecordNumber = 0; taglibRecordNumber < availableTaglibRecords.length; taglibRecordNumber++) {
ITaglibRecord taglibRecord = availableTaglibRecords[taglibRecordNumber];
String uri = null;
if (taglibRecord.getRecordType() == ITaglibRecord.TAGDIR) {
IPath path = ((ITagDirRecord) taglibRecord).getPath();
if (localContextRoot.isPrefixOf(path)) {
uri = IPath.SEPARATOR + path.removeFirstSegments(localContextRoot.segmentCount()).toString();
uriToRecords.put(uri, taglibRecord);
}
}
}
/*
* use the records and their descriptors to construct
* proposals
*/
Object[] uris = uriToRecords.keySet().toArray();
for (int uriNumber = 0; uriNumber < uris.length; uriNumber++) {
String uri = uris[uriNumber].toString();
ITaglibRecord taglibRecord = (ITaglibRecord) uriToRecords.get(uri);
ITaglibDescriptor descriptor = (taglibRecord).getDescriptor();
if (uri != null && uri.length() > 0 && (matchString.length() == 0 || uri.toLowerCase(Locale.US).startsWith(lowerCaseMatch))) {
String url = getSmallImageURL(taglibRecord);
ImageDescriptor imageDescriptor = null;
if (url != null) {
imageDescriptor = JSPUIPlugin.getInstance().getImageRegistry().getDescriptor(url);
}
if (imageDescriptor == null && url != null) {
URL imageURL;
try {
imageURL = new URL(url);
imageDescriptor = ImageDescriptor.createFromURL(imageURL);
JSPUIPlugin.getInstance().getImageRegistry().put(url, imageDescriptor);
} catch (MalformedURLException e) {
Logger.logException(e);
}
}
// $NON-NLS-1$
String additionalInfo = descriptor.getDescription() + "<br/>" + descriptor.getTlibVersion();
Image image = null;
try {
image = JSPUIPlugin.getInstance().getImageRegistry().get(url);
} catch (Exception e) {
Logger.logException(e);
}
if (image == null) {
image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
}
CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
"\"" + uri + "\"", // $NON-NLS-1$ //$NON-NLS-2$
start, // $NON-NLS-1$ //$NON-NLS-2$
length, // $NON-NLS-1$ //$NON-NLS-2$
uri.length() + 2, // $NON-NLS-1$ //$NON-NLS-2$
image, // $NON-NLS-1$ //$NON-NLS-2$
uri, null, additionalInfo, IRelevanceConstants.R_NONE);
contentAssistRequest.addProposal(proposal);
}
}
} else if (attributeName.equals(JSP11Namespace.ATTR_NAME_PREFIX)) {
Node uriAttr = node.getAttributes().getNamedItem(JSP11Namespace.ATTR_NAME_URI);
String uri = null;
if (uriAttr != null) {
uri = uriAttr.getNodeValue();
ITaglibRecord[] availableTaglibRecords = TaglibIndex.getAvailableTaglibRecords(basePath);
Map prefixMap = new HashMap();
for (int taglibrecordNumber = 0; taglibrecordNumber < availableTaglibRecords.length; taglibrecordNumber++) {
ITaglibDescriptor descriptor = availableTaglibRecords[taglibrecordNumber].getDescriptor();
if (isTaglibForURI(uri, basePath, availableTaglibRecords[taglibrecordNumber])) {
String shortName = descriptor.getShortName().trim();
if (shortName.length() > 0) {
boolean valid = true;
for (int character = 0; character < shortName.length(); character++) {
valid = valid && !Character.isWhitespace(shortName.charAt(character));
}
if (valid) {
prefixMap.put(shortName, descriptor);
}
}
}
}
Object[] prefixes = prefixMap.keySet().toArray();
for (int j = 0; j < prefixes.length; j++) {
String prefix = (String) prefixes[j];
ITaglibDescriptor descriptor = (ITaglibDescriptor) prefixMap.get(prefix);
Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
"\"" + prefix + "\"", // $NON-NLS-1$ //$NON-NLS-2$
start, // $NON-NLS-1$ //$NON-NLS-2$
length, // $NON-NLS-1$ //$NON-NLS-2$
prefix.length() + 2, // $NON-NLS-1$ //$NON-NLS-2$
image, prefix, null, descriptor.getDescription(), IRelevanceConstants.R_NONE);
contentAssistRequest.addProposal(proposal);
}
} else {
Node dirAttr = node.getAttributes().getNamedItem(JSP20Namespace.ATTR_NAME_TAGDIR);
if (dirAttr != null) {
String dir = dirAttr.getNodeValue();
if (dir != null) {
ITaglibRecord record = TaglibIndex.resolve(basePath.toString(), dir, false);
if (record != null) {
ITaglibDescriptor descriptor = record.getDescriptor();
if (descriptor != null) {
String shortName = descriptor.getShortName();
Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
"\"" + shortName + "\"", // $NON-NLS-1$ //$NON-NLS-2$
start, // $NON-NLS-1$ //$NON-NLS-2$
length, // $NON-NLS-1$ //$NON-NLS-2$
shortName.length() + 2, image, shortName, null, descriptor.getDescription(), IRelevanceConstants.R_NONE);
contentAssistRequest.addProposal(proposal);
} else {
if (dir.startsWith("/WEB-INF/")) {
// $NON-NLS-1$
dir = dir.substring(9);
}
// $NON-NLS-1$ //$NON-NLS-2$
String prefix = StringUtils.replace(dir, "/", "-");
Image image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
CustomCompletionProposal proposal = new CustomCompletionProposal(// $NON-NLS-1$ //$NON-NLS-2$
"\"" + prefix + "\"", // $NON-NLS-1$ //$NON-NLS-2$
start, // $NON-NLS-1$ //$NON-NLS-2$
length, // $NON-NLS-1$ //$NON-NLS-2$
prefix.length() + 2, image, prefix, null, null, IRelevanceConstants.R_NONE);
contentAssistRequest.addProposal(proposal);
}
}
}
}
}
}
}
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class JSPUseBeanCompletionProposalComputer method addAttributeValueProposals.
/**
* @see org.eclipse.wst.xml.ui.internal.contentassist.DefaultXMLCompletionProposalComputer#addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest, org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext)
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// only add attribute value proposals for specific elements
if (node.getNodeName().equals(JSP11Namespace.ElementName.USEBEAN)) {
// Find the attribute name for which this position should have a value
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
ITextRegionList openRegions = open.getRegions();
int i = openRegions.indexOf(contentAssistRequest.getRegion());
if (i < 0)
return;
ITextRegion nameRegion = null;
while (i >= 0) {
nameRegion = openRegions.get(i--);
if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
break;
}
String attributeName = null;
if (nameRegion != null)
attributeName = open.getText(nameRegion);
String currentValue = null;
if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
currentValue = contentAssistRequest.getText();
else
// $NON-NLS-1$
currentValue = "";
String matchString = null;
// fixups
int start = contentAssistRequest.getReplacementBeginPosition();
int length = contentAssistRequest.getReplacementLength();
if (// $NON-NLS-1$ //$NON-NLS-2$
currentValue.length() > StringUtils.strip(currentValue).length() && (currentValue.startsWith("\"") || currentValue.startsWith("'")) && contentAssistRequest.getMatchString().length() > 0) {
matchString = currentValue.substring(1, contentAssistRequest.getMatchString().length());
start++;
length = matchString.length();
} else
matchString = currentValue.substring(0, contentAssistRequest.getMatchString().length());
boolean existingComplicatedValue = contentAssistRequest.getRegion() != null && contentAssistRequest.getRegion() instanceof ITextRegionContainer;
if (existingComplicatedValue) {
contentAssistRequest.getProposals().clear();
contentAssistRequest.getMacros().clear();
} else {
if (attributeName.equals(JSP11Namespace.ATTR_NAME_CLASS)) {
// class is the concrete implementation class
IResource resource = JSPContentAssistHelper.getResource(contentAssistRequest);
ICompletionProposal[] classProposals = JavaTypeFinder.getClassProposals(resource, start, length);
if (classProposals != null) {
for (int j = 0; j < classProposals.length; j++) {
JavaTypeCompletionProposal proposal = (JavaTypeCompletionProposal) classProposals[j];
if (matchString.length() == 0 || proposal.getQualifiedName().toLowerCase().startsWith(matchString.toLowerCase()) || proposal.getShortName().toLowerCase().startsWith(matchString.toLowerCase()))
contentAssistRequest.addProposal(proposal);
}
}
} else if (attributeName.equals(JSP11Namespace.ATTR_NAME_TYPE)) {
// type is the more general type for the bean
// which means it may be an interface
IResource resource = JSPContentAssistHelper.getResource(contentAssistRequest);
ICompletionProposal[] typeProposals = JavaTypeFinder.getTypeProposals(resource, start, length);
if (typeProposals != null) {
for (int j = 0; j < typeProposals.length; j++) {
JavaTypeCompletionProposal proposal = (JavaTypeCompletionProposal) typeProposals[j];
if (matchString.length() == 0 || proposal.getQualifiedName().toLowerCase().startsWith(matchString.toLowerCase()) || proposal.getShortName().toLowerCase().startsWith(matchString.toLowerCase()))
contentAssistRequest.addProposal(proposal);
}
}
} else if (attributeName.equals(JSP11Namespace.ATTR_NAME_BEAN_NAME)) {
IResource resource = JSPContentAssistHelper.getResource(contentAssistRequest);
ICompletionProposal[] beanNameProposals = JavaTypeFinder.getBeanProposals(resource, start, length);
if (beanNameProposals != null) {
for (int j = 0; j < beanNameProposals.length; j++) {
if (beanNameProposals[j] instanceof CustomCompletionProposal) {
JavaTypeCompletionProposal proposal = (JavaTypeCompletionProposal) beanNameProposals[j];
if (matchString.length() == 0 || proposal.getDisplayString().toLowerCase().startsWith(matchString.toLowerCase()))
contentAssistRequest.addProposal(proposal);
} else if (beanNameProposals[j] instanceof JavaTypeCompletionProposal) {
JavaTypeCompletionProposal proposal = (JavaTypeCompletionProposal) beanNameProposals[j];
if (matchString.length() == 0 || proposal.getQualifiedName().toLowerCase().startsWith(matchString.toLowerCase()) || proposal.getShortName().toLowerCase().startsWith(matchString.toLowerCase()))
contentAssistRequest.addProposal(proposal);
}
}
}
}
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class JSPContentAssistProcessor method addAttributeValueProposals.
/**
* add proposals for tags in attribute values
*/
protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
addTemplates(contentAssistRequest, TemplateContextTypeIdsJSP.ATTRIBUTE_VALUE);
IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
// add JSP extra proposals from JSPBeanInfoContentAssistProcessor
// JSPPropertyContentAssistProcessor
// 2.1
// get results from JSPUseBean and JSPProperty here
// (look up processor in a map based on node name)
JSPDummyContentAssistProcessor extraProcessor = (JSPDummyContentAssistProcessor) fNameToProcessorMap.get(node.getNodeName());
if (extraProcessor != null && contentAssistRequest != null) {
extraProcessor.addAttributeValueProposals(contentAssistRequest);
}
ModelQuery mq = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
if (mq != null) {
CMDocument doc = mq.getCorrespondingCMDocument(node);
// this shouldn't have to have the prefix coded in
if (// $NON-NLS-1$
doc instanceof JSPCMDocument || doc instanceof CMNodeWrapper || node.getNodeName().startsWith("jsp:"))
return;
}
// Find the attribute name for which this position should have a value
IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
ITextRegionList openRegions = open.getRegions();
int i = openRegions.indexOf(contentAssistRequest.getRegion());
if (i < 0)
return;
ITextRegion nameRegion = null;
while (i >= 0) {
nameRegion = openRegions.get(i--);
if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME)
break;
}
// on an empty value, add all the JSP and taglib tags
CMElementDeclaration elementDecl = getCMElementDeclaration(node);
if (nameRegion != null && elementDecl != null) {
String attributeName = open.getText(nameRegion);
if (attributeName != null) {
String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
if (currentValue == null || currentValue.length() == 0) {
// $NON-NLS-1$
List additionalElements = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
for (i = 0; i < additionalElements.size(); i++) {
Object additionalElement = additionalElements.get(i);
if (additionalElement instanceof CMElementDeclaration) {
CMElementDeclaration ed = (CMElementDeclaration) additionalElement;
String tagname = getContentGenerator().getRequiredName(node, ed);
// $NON-NLS-1$
StringBuffer contents = new StringBuffer("\"");
getContentGenerator().generateTag(node, ed, contents);
// $NON-NLS-1$
contents.append('"');
CustomCompletionProposal proposal = new CustomCompletionProposal(contents.toString(), contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), contents.length(), JSPEditorPluginImageHelper.getInstance().getImage(JSPEditorPluginImages.IMG_OBJ_TAG_GENERIC), tagname, null, null, XMLRelevanceConstants.R_JSP_ATTRIBUTE_VALUE);
contentAssistRequest.addProposal(proposal);
}
}
}
}
} else if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
try {
// Create a new model for Content Assist to operate on. This
// will simulate
// a full Document and then adjust the offset numbers in the
// list of results.
IStructuredModel internalModel = null;
IModelManager mmanager = StructuredModelManager.getModelManager();
internalModel = mmanager.createUnManagedStructuredModelFor(ContentTypeIdForJSP.ContentTypeID_JSP);
IDOMNode xmlNode = null;
IDOMModel xmlOuterModel = null;
if (contentAssistRequest.getNode() instanceof IDOMNode) {
xmlNode = (IDOMNode) contentAssistRequest.getNode();
xmlOuterModel = xmlNode.getModel();
internalModel.setResolver(xmlOuterModel.getResolver());
internalModel.setBaseLocation(xmlOuterModel.getBaseLocation());
}
String contents = StringUtils.strip(contentAssistRequest.getText());
if (xmlNode != null && contents != null) {
int additionalShifts = 0;
// Be sure that custom tags from taglibs also show up
// by
// adding taglib declarations to the internal model.
TLDCMDocumentManager mgr = TaglibController.getTLDCMDocumentManager(xmlOuterModel.getStructuredDocument());
if (mgr != null) {
List trackers = mgr.getCMDocumentTrackers(contentAssistRequest.getReplacementBeginPosition());
if (trackers != null) {
for (i = 0; i < trackers.size(); i++) {
CMDocumentTracker tracker = (CMDocumentTracker) trackers.get(i);
String declaration = tracker.getStructuredDocumentRegion().getText();
if (declaration != null) {
contents = declaration + contents;
additionalShifts += declaration.length();
}
}
}
}
// Also copy any jsp:useBean tags so that
// jsp:[gs]etProperty will function
Document doc = null;
if (contentAssistRequest.getNode().getNodeType() == Node.DOCUMENT_NODE)
doc = (Document) node;
else
doc = node.getOwnerDocument();
NodeList useBeans = doc.getElementsByTagName(JSP12Namespace.ElementName.USEBEAN);
for (int k = 0; k < useBeans.getLength(); k++) {
IDOMNode useBean = (IDOMNode) useBeans.item(k);
if (useBean.getStartOffset() < contentAssistRequest.getReplacementBeginPosition()) {
// $NON-NLS-1$
StringBuffer useBeanText = new StringBuffer("<jsp:useBean");
for (int j = 0; j < useBean.getAttributes().getLength(); j++) {
Attr attr = (Attr) useBean.getAttributes().item(j);
useBeanText.append(' ');
useBeanText.append(attr.getName());
// $NON-NLS-1$
useBeanText.append("=\"");
useBeanText.append(attr.getValue());
useBeanText.append('"');
}
// $NON-NLS-1$
useBeanText.append("/>");
additionalShifts += useBeanText.length();
contents = useBeanText.toString() + contents;
}
}
internalModel.getStructuredDocument().set(contents);
int internalOffset = 0;
boolean quoted = false;
// if quoted, use position inside and shift by one
if (contentAssistRequest.getMatchString().length() > 0 && (contentAssistRequest.getMatchString().charAt(0) == '\'' || contentAssistRequest.getMatchString().charAt(0) == '"')) {
internalOffset = contentAssistRequest.getMatchString().length() - 1 + additionalShifts;
quoted = true;
} else // if unquoted, use position inside
if (contentAssistRequest.getMatchString().length() > 0 && contentAssistRequest.getMatchString().charAt(0) == '<')
internalOffset = contentAssistRequest.getMatchString().length() + additionalShifts;
else
internalOffset = contentAssistRequest.getReplacementBeginPosition() - contentAssistRequest.getStartOffset() + additionalShifts;
depthCount++;
IndexedRegion internalNode = null;
int tmpOffset = internalOffset;
while (internalNode == null && tmpOffset >= 0) internalNode = internalModel.getIndexedRegion(tmpOffset--);
if (internalModel.getFactoryRegistry() != null) {
// set up the internal model
if (internalModel.getFactoryRegistry().getFactoryFor(PageDirectiveAdapter.class) == null) {
internalModel.getFactoryRegistry().addFactory(new PageDirectiveAdapterFactory());
}
PageDirectiveAdapter outerEmbeddedTypeAdapter = (PageDirectiveAdapter) xmlOuterModel.getDocument().getAdapterFor(PageDirectiveAdapter.class);
PageDirectiveAdapter internalEmbeddedTypeAdapter = (PageDirectiveAdapter) ((INodeNotifier) ((Node) internalNode).getOwnerDocument()).getAdapterFor(PageDirectiveAdapter.class);
internalEmbeddedTypeAdapter.setEmbeddedType(outerEmbeddedTypeAdapter.getEmbeddedType());
}
AdapterFactoryRegistry adapterRegistry = JSPUIPlugin.getDefault().getAdapterFactoryRegistry();
Iterator adapterList = adapterRegistry.getAdapterFactories();
// of content
while (adapterList.hasNext()) {
try {
AdapterFactoryProvider provider = (AdapterFactoryProvider) adapterList.next();
if (provider.isFor(internalModel.getModelHandler())) {
provider.addAdapterFactories(internalModel);
}
} catch (Exception e) {
Logger.logException(e);
}
}
/**
* the internal adapter does all the real work of using
* the JSP content model to form proposals
*/
ICompletionProposal[] results = null;
depthCount--;
if (results != null) {
for (i = 0; i < results.length; i++) {
contentAssistRequest.addProposal(new CustomCompletionProposal(((CustomCompletionProposal) results[i]).getReplacementString(), ((CustomCompletionProposal) results[i]).getReplacementOffset() - additionalShifts + contentAssistRequest.getStartOffset() + (quoted ? 1 : 0), ((CustomCompletionProposal) results[i]).getReplacementLength(), ((CustomCompletionProposal) results[i]).getCursorPosition(), results[i].getImage(), results[i].getDisplayString(), ((CustomCompletionProposal) results[i]).getContextInformation(), ((CustomCompletionProposal) results[i]).getAdditionalProposalInfo(), (results[i] instanceof IRelevanceCompletionProposal) ? ((IRelevanceCompletionProposal) results[i]).getRelevance() : IRelevanceConstants.R_NONE));
}
}
}
} catch (Exception e) {
// $NON-NLS-1$
Logger.logException("Error in embedded JSP Content Assist", e);
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class JSPContentAssistProcessor method computeCompletionProposals.
/**
* This method is acting as a "catch all" for pulling together content
* assist proposals from different Processors when document partitioning
* alone couldn't determine definitively what content assist should show
* up at that particular position in the document
*
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(ITextViewer,
* int)
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentPosition) {
fTemplateContexts.clear();
IStructuredDocumentRegion sdRegion = ContentAssistUtils.getStructuredDocumentRegion(viewer, documentPosition);
fViewer = viewer;
ICompletionProposal[] jspResults = EMPTY_PROPOSAL_SET;
ICompletionProposal[] embeddedResults = EMPTY_PROPOSAL_SET;
// check the actual partition type
String partitionType = getPartitionType(viewer, documentPosition);
IStructuredDocument structuredDocument = (IStructuredDocument) viewer.getDocument();
IStructuredDocumentRegion fn = structuredDocument.getRegionAtCharacterOffset(documentPosition);
// check for xml-jsp tags...
if (partitionType == IJSPPartitions.JSP_DIRECTIVE && fn != null) {
IStructuredDocumentRegion possibleXMLJSP = ((fn.getType() == DOMRegionContext.XML_CONTENT) && fn.getPrevious() != null) ? fn.getPrevious() : fn;
ITextRegionList regions = possibleXMLJSP.getRegions();
if (regions.size() > 1) {
// check bounds cases
ITextRegion xmlOpenOrClose = regions.get(0);
if (xmlOpenOrClose.getType() == DOMRegionContext.XML_TAG_OPEN && documentPosition == possibleXMLJSP.getStartOffset()) {
// do regular jsp content assist
} else if (xmlOpenOrClose.getType() == DOMRegionContext.XML_END_TAG_OPEN && documentPosition > possibleXMLJSP.getStartOffset()) {
// do regular jsp content assist
} else {
// possible xml-jsp
ITextRegion nameRegion = regions.get(1);
String name = possibleXMLJSP.getText(nameRegion);
if (name.equals("jsp:scriptlet") || name.equals("jsp:expression") || name.equals("jsp:declaration")) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
}
}
}
// check for XML-JSP in a <script> region
if (partitionType == IJSPPartitions.JSP_CONTENT_JAVASCRIPT || partitionType == IHTMLPartitions.SCRIPT) {
// fn should be block text
IStructuredDocumentRegion decodedSDRegion = decodeScriptBlock(fn.getFullText());
// decodedSDRegion.getEndOffset()));
if (decodedSDRegion != null) {
IStructuredDocumentRegion sdr = decodedSDRegion;
while (sdr != null) {
// sdr.getEndOffset()));
if (sdr.getType() == DOMJSPRegionContexts.JSP_CONTENT) {
if (documentPosition >= fn.getStartOffset() + sdr.getStartOffset() && documentPosition <= fn.getStartOffset() + sdr.getEndOffset()) {
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
} else if (sdr.getType() == DOMRegionContext.XML_TAG_NAME) {
if (documentPosition > fn.getStartOffset() + sdr.getStartOffset() && documentPosition < fn.getStartOffset() + sdr.getEndOffset()) {
return EMPTY_PROPOSAL_SET;
} else if (documentPosition == fn.getStartOffset() + sdr.getEndOffset() && sdr.getNext() != null && sdr.getNext().getType() == DOMJSPRegionContexts.JSP_CONTENT) {
// <jsp:scriptlet>| blah </jsp:scriptlet>
return getJSPJavaCompletionProposals(viewer, documentPosition);
} else if (documentPosition == fn.getStartOffset() + sdr.getStartOffset() && sdr.getPrevious() != null && sdr.getPrevious().getType() == DOMRegionContext.XML_TAG_NAME) {
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
}
sdr = sdr.getNext();
}
}
}
// check special JSP delimiter cases
if (fn != null && partitionType == IJSPPartitions.JSP_CONTENT_DELIMITER) {
IStructuredDocumentRegion fnDelim = fn;
// if it's a nested JSP region, need to get the correct
// StructuredDocumentRegion
// not sure why this check was there...
// if (fnDelim.getType() == XMLRegionContext.BLOCK_TEXT) {
Iterator blockRegions = fnDelim.getRegions().iterator();
ITextRegion temp = null;
ITextRegionContainer trc;
while (blockRegions.hasNext()) {
temp = (ITextRegion) blockRegions.next();
// we hit a nested
if (temp instanceof ITextRegionContainer) {
trc = (ITextRegionContainer) temp;
// it's in this region
if (documentPosition >= trc.getStartOffset() && documentPosition < trc.getEndOffset()) {
Iterator nestedJSPRegions = trc.getRegions().iterator();
while (nestedJSPRegions.hasNext()) {
temp = (ITextRegion) nestedJSPRegions.next();
if (XMLContentAssistUtilities.isJSPOpenDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
// adapter
if (documentPosition > 0) {
partitionType = getPartitionType(viewer, documentPosition - 1);
break;
}
} else if (XMLContentAssistUtilities.isJSPCloseDelimiter(temp.getType()) && documentPosition == trc.getStartOffset(temp)) {
// JSP content assist
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
}
}
}
// }
}
// take care of XML-JSP delimter cases
if (XMLContentAssistUtilities.isXMLJSPDelimiter(fnDelim)) {
// since it's a delimiter, we know it's a ITextRegionContainer
ITextRegion firstRegion = fnDelim.getRegions().get(0);
if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN)) {
// |<jsp:scriptlet> </jsp:scriptlet>
// (pa) commented out so that we get regular behavior JSP
// macros etc...
// return getHTMLCompletionProposals(viewer,
// documentPosition);
} else if (fnDelim.getStartOffset() == documentPosition && (firstRegion.getType() == DOMRegionContext.XML_END_TAG_OPEN)) {
// adapter get the proposals
if (documentPosition > 0) {
String checkType = getPartitionType(viewer, documentPosition - 1);
if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
// javascript...)
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
}
} else if ((firstRegion.getType() == DOMRegionContext.XML_TAG_OPEN) && documentPosition >= fnDelim.getEndOffset()) {
// anything else inbetween
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
} else if (XMLContentAssistUtilities.isJSPDelimiter(fnDelim)) {
// the delimiter <%, <%=, <%!, ...
if (XMLContentAssistUtilities.isJSPCloseDelimiter(fnDelim)) {
if (documentPosition == fnDelim.getStartOffset()) {
// JAVASCRIPT adapter get the proposals
if (documentPosition > 0) {
String checkType = getPartitionType(viewer, documentPosition - 1);
if (checkType != IJSPPartitions.JSP_CONTENT_JAVASCRIPT) {
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
partitionType = IJSPPartitions.JSP_CONTENT_JAVASCRIPT;
}
}
} else if (XMLContentAssistUtilities.isJSPOpenDelimiter(fnDelim)) {
// use embedded HTML results
if (documentPosition == fnDelim.getStartOffset()) {
embeddedResults = getHTMLCompletionProposals(viewer, documentPosition);
} else if (documentPosition == fnDelim.getEndOffset()) {
// it's at the EOF <%|
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
}
}
}
// <!-- <% |%> -->
if (fn != null && (fn.getType() == DOMRegionContext.XML_CDATA_TEXT || fn.getType() == DOMRegionContext.XML_COMMENT_TEXT)) {
if (fn instanceof ITextRegionContainer) {
Object[] cdataRegions = fn.getRegions().toArray();
ITextRegion r = null;
ITextRegion jspRegion = null;
for (int i = 0; i < cdataRegions.length; i++) {
r = (ITextRegion) cdataRegions[i];
if (r instanceof ITextRegionContainer) {
// CDATA embedded container, or comment container
Object[] jspRegions = ((ITextRegionContainer) r).getRegions().toArray();
for (int j = 0; j < jspRegions.length; j++) {
jspRegion = (ITextRegion) jspRegions[j];
if (jspRegion.getType() == DOMJSPRegionContexts.JSP_CLOSE) {
if (sdRegion.getStartOffset(jspRegion) == documentPosition)
return getJSPJavaCompletionProposals(viewer, documentPosition);
}
}
}
}
}
}
// check if it's in an attribute value, if so, don't add CDATA
// proposal
ITextRegion attrContainer = (fn != null) ? fn.getRegionAtCharacterOffset(documentPosition) : null;
if (attrContainer != null && attrContainer instanceof ITextRegionContainer) {
if (attrContainer.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
// test location of the cursor
// return null if it's in the middle of an open/close
// delimeter
Iterator attrRegions = ((ITextRegionContainer) attrContainer).getRegions().iterator();
ITextRegion testRegion = null;
while (attrRegions.hasNext()) {
testRegion = (ITextRegion) attrRegions.next();
// need to check for other valid attribute regions
if (XMLContentAssistUtilities.isJSPOpenDelimiter(testRegion.getType())) {
if (!(((ITextRegionContainer) attrContainer).getEndOffset(testRegion) <= documentPosition))
return EMPTY_PROPOSAL_SET;
} else if (XMLContentAssistUtilities.isJSPCloseDelimiter(testRegion.getType())) {
if (!(((ITextRegionContainer) attrContainer).getStartOffset(testRegion) >= documentPosition))
return EMPTY_PROPOSAL_SET;
}
}
// TODO: handle non-Java code such as nested tags
if (testRegion.getType().equals(DOMJSPRegionContexts.JSP_CONTENT))
return getJSPJavaCompletionProposals(viewer, documentPosition);
return EMPTY_PROPOSAL_SET;
}
}
IContentAssistProcessor p = (IContentAssistProcessor) fPartitionToProcessorMap.get(partitionType);
if (p != null) {
embeddedResults = p.computeCompletionProposals(viewer, documentPosition);
// get bean methods, objects, and constants if there are any...
if (partitionType == IJSPPartitions.JSP_CONTENT_JAVASCRIPT || partitionType == IHTMLPartitions.SCRIPT) {
ICompletionProposal[] beanResults = getJSPJavaBeanProposals(viewer, documentPosition);
if (beanResults != null && beanResults.length > 0) {
ICompletionProposal[] added = new ICompletionProposal[beanResults.length + embeddedResults.length];
System.arraycopy(beanResults, 0, added, 0, beanResults.length);
System.arraycopy(embeddedResults, 0, added, beanResults.length, embeddedResults.length);
embeddedResults = added;
}
}
} else {
// the partition type is probably not mapped
}
// HTML content assist give JSP tags in between empty script tags
if (!((getJSContentAssistProcessor() != null && getJSContentAssistProcessor().getClass().isInstance(p)) || p instanceof CSSContentAssistProcessor)) {
fTemplateContexts.clear();
jspResults = super.computeCompletionProposals(viewer, documentPosition);
}
// merge the embedded results
if (useEmbeddedResults && embeddedResults != null && embeddedResults.length > 0) {
jspResults = merge(jspResults, embeddedResults);
}
if (jspResults == null)
jspResults = EMPTY_PROPOSAL_SET;
setErrorMessage(jspResults.length == 0 ? UNKNOWN_CONTEXT : null);
// check for |<%-- --%> first position of jsp comment
if (partitionType == IJSPPartitions.JSP_COMMENT) {
if (sdRegion.getStartOffset() == documentPosition) {
ICompletionProposal[] htmlResults = getHTMLCompletionProposals(viewer, documentPosition);
jspResults = merge(jspResults, htmlResults);
}
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=86656
if (partitionType == IJSPPartitions.JSP_DIRECTIVE) {
ICompletionProposal[] importProposals = getImportProposals(viewer, documentPosition);
if (importProposals.length > 0)
jspResults = merge(jspResults, importProposals);
}
return jspResults;
}
Aggregations