[289498]: Add additional context types for the XSL editor templates.
https://bugs.eclipse.org/bugs/show_bug.cgi?id=289498
diff --git a/bundles/org.eclipse.wst.xsl.ui/plugin.properties b/bundles/org.eclipse.wst.xsl.ui/plugin.properties
index 2af2e20..9b7fc44 100644
--- a/bundles/org.eclipse.wst.xsl.ui/plugin.properties
+++ b/bundles/org.eclipse.wst.xsl.ui/plugin.properties
@@ -32,4 +32,6 @@
XSL_Property_validation = XSLT Validation
specification.label.0 = Override
-XSLContentAssistExtension = XSL Content Assistance
\ No newline at end of file
+XSLContentAssistExtension = XSL Content Assistance
+contextTypeXSLTag = XSL Tag
+contextTypeXSLAttr = XSL Attribute
\ No newline at end of file
diff --git a/bundles/org.eclipse.wst.xsl.ui/plugin.xml b/bundles/org.eclipse.wst.xsl.ui/plugin.xml
index ba0f525..234f132 100644
--- a/bundles/org.eclipse.wst.xsl.ui/plugin.xml
+++ b/bundles/org.eclipse.wst.xsl.ui/plugin.xml
@@ -69,6 +69,15 @@
class="org.eclipse.wst.xsl.ui.internal.templates.TemplateContextTypeXSL"
id="xsl_new" name="%contextTypeNewXSL">
</contextType>
+ <contextType
+ class="org.eclipse.wst.xsl.ui.internal.templates.TemplateContextTypeXSL"
+ id="xsl_tag" name="%contextTypeXSLTag">
+ </contextType>
+ <contextType
+ class="org.eclipse.wst.xsl.ui.internal.templates.TemplateContextTypeXSL"
+ id="xsl_attr" name="%contextTypeXSLAttr">
+ </contextType>
+
<include file="templates/file_templates.xml" />
</extension>
@@ -261,11 +270,11 @@
<extension
point="org.eclipse.wst.xsl.ui.contentAssistProcessor">
<processor
- class="org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor">
- </processor>
+ class="org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor"/>
<processor
- class="org.eclipse.wst.xsl.ui.internal.contentassist.XSLContentAssistProcessor">
- </processor>
+ class="org.eclipse.wst.xsl.ui.internal.contentassist.XSLTemplateContentAssistProcessor"/>
+ <processor
+ class="org.eclipse.wst.xsl.ui.internal.contentassist.XSLContentAssistProcessor"/>
</extension>
<extension
point="org.eclipse.wst.sse.ui.adapterFactoryDescription">
diff --git a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/XSLUIPlugin.java b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/XSLUIPlugin.java
index 086b0ad..aa26a07 100644
--- a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/XSLUIPlugin.java
+++ b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/XSLUIPlugin.java
@@ -24,6 +24,7 @@
import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.eclipse.wst.xsl.ui.internal.templates.TemplateContextTypeXSL;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -130,7 +131,9 @@
if (fContextTypeRegistry == null)
{
ContributionContextTypeRegistry registry = new ContributionContextTypeRegistry();
- registry.addContextType("xsl_new"); //$NON-NLS-1$
+ registry.addContextType(TemplateContextTypeXSL.XSL_NEW);
+ registry.addContextType(TemplateContextTypeXSL.XSL_TAG);
+ registry.addContextType(TemplateContextTypeXSL.XSL_ATTR);
fContextTypeRegistry = registry;
}
return fContextTypeRegistry;
diff --git a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLContentAssistProcessor.java b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLContentAssistProcessor.java
index 1e92231..d63ec97 100644
--- a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLContentAssistProcessor.java
+++ b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLContentAssistProcessor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2008 Standards for Technology in Automotive Retail and others.
+ * Copyright (c) 2008, 2010 Standards for Technology in Automotive Retail and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -11,7 +11,7 @@
* the DOM Document for the current editor
* David Carver - STAR - bug 240170 - refactored code to help with narrowing of
* results and easier maintenance.
- *
+ * David Carver (Intalio) - bug 289498 - Added additional context types.
*******************************************************************************/
package org.eclipse.wst.xsl.ui.internal.contentassist;
@@ -25,8 +25,6 @@
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
-import org.eclipse.wst.xml.ui.internal.contentassist.AbstractContentAssistProcessor;
-import org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor;
import org.eclipse.wst.xsl.core.XSLCore;
import org.eclipse.wst.xsl.ui.internal.Messages;
import org.eclipse.wst.xsl.ui.provisional.contentassist.AbstractXSLContentAssistProcessor;
diff --git a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLTemplateCompletionProcessor.java b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLTemplateCompletionProcessor.java
new file mode 100755
index 0000000..115c116
--- /dev/null
+++ b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLTemplateCompletionProcessor.java
@@ -0,0 +1,173 @@
+/*******************************************************************************
+ * Copyright (c) 2010 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - Based on XMLTemplateCompletionatProposal
+ * David Carver (Intalio) - bug 289498 - Added additional context types.
+ *******************************************************************************/
+package org.eclipse.wst.xsl.ui.internal.contentassist;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+import org.eclipse.jface.text.templates.ContextTypeRegistry;
+import org.eclipse.jface.text.templates.Template;
+import org.eclipse.jface.text.templates.TemplateCompletionProcessor;
+import org.eclipse.jface.text.templates.TemplateContext;
+import org.eclipse.jface.text.templates.TemplateContextType;
+import org.eclipse.jface.text.templates.TemplateException;
+import org.eclipse.jface.text.templates.TemplateProposal;
+import org.eclipse.jface.text.templates.persistence.TemplateStore;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.wst.xml.ui.internal.contentassist.ReplaceNameTemplateContext;
+import org.eclipse.wst.xml.xpath.ui.internal.contentassist.CustomTemplateProposal;
+import org.eclipse.wst.xsl.ui.internal.XSLUIPlugin;
+import org.eclipse.wst.xsl.ui.internal.util.XSLPluginImageHelper;
+import org.eclipse.wst.xsl.ui.internal.util.XSLPluginImages;
+
+
+/**
+ * Completion processor for XML Templates. Most of the work is already done by
+ * the XML Content Assist processor, so by the time the
+ * XMLTemplateCompletionProcessor is asked for content assist proposals, the
+ * XML content assist processor has already set the context type for
+ * templates.
+ */
+class XSLTemplateCompletionProcessor extends TemplateCompletionProcessor {
+ private static final class ProposalComparator implements Comparator {
+ public int compare(Object o1, Object o2) {
+ return ((TemplateProposal) o2).getRelevance() - ((TemplateProposal) o1).getRelevance();
+ }
+ }
+
+ private static final Comparator fgProposalComparator = new ProposalComparator();
+ private String fContextTypeId = null;
+
+ /*
+ * Copied from super class except instead of calling createContext(viewer,
+ * region) call createContext(viewer, region, offset) instead
+ */
+ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
+
+ ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
+
+ // adjust offset to end of normalized selection
+ if (selection.getOffset() == offset) {
+ offset = selection.getOffset() + selection.getLength();
+ }
+
+ String prefix = extractPrefix(viewer, offset);
+ Region region = new Region(offset - prefix.length(), prefix.length());
+ TemplateContext context = createContext(viewer, region, offset);
+ if (context == null) {
+ return new ICompletionProposal[0];
+ }
+
+ // name of the selection variables {line, word}_selection
+ context.setVariable("selection", selection.getText()); //$NON-NLS-1$
+
+ Template[] templates = getTemplates(context.getContextType().getId());
+
+ List matches = new ArrayList();
+ for (int i = 0; i < templates.length; i++) {
+ Template template = templates[i];
+ try {
+ context.getContextType().validate(template.getPattern());
+ }
+ catch (TemplateException e) {
+ continue;
+ }
+ if (template.matches(prefix, context.getContextType().getId())) {
+ matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
+ }
+ }
+
+ Collections.sort(matches, fgProposalComparator);
+
+ return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
+ }
+
+ /**
+ * Creates a concrete template context for the given region in the
+ * document. This involves finding out which context type is valid at the
+ * given location, and then creating a context of this type. The default
+ * implementation returns a <code>SmartReplaceTemplateContext</code> for
+ * the context type at the given location. This takes the offset at which
+ * content assist was invoked into consideration.
+ *
+ * @param viewer
+ * the viewer for which the context is created
+ * @param region
+ * the region into <code>document</code> for which the
+ * context is created
+ * @param offset
+ * the original offset where content assist was invoked
+ * @return a template context that can handle template insertion at the
+ * given location, or <code>null</code>
+ */
+ private TemplateContext createContext(ITextViewer viewer, IRegion region, int offset) {
+ // pretty much same code as super.createContext except create
+ // SmartReplaceTemplateContext
+ TemplateContextType contextType = getContextType(viewer, region);
+ if (contextType != null) {
+ IDocument document = viewer.getDocument();
+ return new ReplaceNameTemplateContext(contextType, document, region.getOffset(), region.getLength(), offset);
+ }
+ return null;
+ }
+
+ protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) {
+ return new CustomTemplateProposal(template, context, region, getImage(template), relevance);
+ }
+
+ protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
+ TemplateContextType type = null;
+
+ ContextTypeRegistry registry = getTemplateContextRegistry();
+ if (registry != null) {
+ type = registry.getContextType(fContextTypeId);
+ }
+
+ return type;
+ }
+
+ protected Image getImage(Template template) {
+ // just return the same image for now
+ return XSLPluginImageHelper.getInstance().getImage(XSLPluginImages.IMG_XPATH_FUNCTION);
+ }
+
+ private ContextTypeRegistry getTemplateContextRegistry() {
+ return XSLUIPlugin.getDefault().getTemplateContextRegistry();
+ }
+
+ protected Template[] getTemplates(String contextTypeId) {
+ Template templates[] = null;
+
+ TemplateStore store = getTemplateStore();
+ if (store != null) {
+ templates = store.getTemplates(contextTypeId);
+ }
+
+ return templates;
+ }
+
+ private TemplateStore getTemplateStore() {
+ return XSLUIPlugin.getDefault().getTemplateStore();
+ }
+
+ void setContextType(String contextTypeId) {
+ fContextTypeId = contextTypeId;
+ }
+}
diff --git a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLTemplateContentAssistProcessor.java b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLTemplateContentAssistProcessor.java
new file mode 100644
index 0000000..18c8dd7
--- /dev/null
+++ b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/contentassist/XSLTemplateContentAssistProcessor.java
@@ -0,0 +1,95 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Intalio and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * David Carver (Intalio) - bug 289498 - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.wst.xsl.ui.internal.contentassist;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.contentassist.ICompletionProposal;
+import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
+import org.eclipse.jface.text.contentassist.IContextInformation;
+import org.eclipse.jface.text.contentassist.IContextInformationValidator;
+import org.eclipse.wst.xsl.ui.internal.templates.TemplateContextTypeXSL;
+import org.eclipse.wst.xsl.ui.provisional.contentassist.AbstractXSLContentAssistProcessor;
+
+public class XSLTemplateContentAssistProcessor extends
+ AbstractXSLContentAssistProcessor implements IContentAssistProcessor {
+
+ public XSLTemplateContentAssistProcessor() {
+
+ }
+
+ public ArrayList<String> getNamespaces() {
+ return null;
+ }
+
+ public String getMinimumVersion() {
+ return null;
+ }
+
+ public String getMaximumVersion() {
+ return null;
+ }
+
+ public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
+ int offset) {
+ List<ICompletionProposal> proposals = new ArrayList<ICompletionProposal>();
+ XSLTemplateCompletionProcessor templates = new XSLTemplateCompletionProcessor();
+
+ if (viewer.getDocument().getLength() == 0) {
+ templates.setContextType(TemplateContextTypeXSL.XSL_NEW);
+ ICompletionProposal[] newFileTemplates = templates.computeCompletionProposals(viewer, offset);
+ if (newFileTemplates != null && newFileTemplates.length > 0) {
+ proposals.addAll(Arrays.asList(newFileTemplates));
+ }
+ }
+
+ templates.setContextType(TemplateContextTypeXSL.XSL_TAG);
+ ICompletionProposal[] xsltagproposals = templates.computeCompletionProposals(viewer, offset);
+ if (xsltagproposals != null && xsltagproposals.length > 0) {
+ proposals.addAll(Arrays.asList(xsltagproposals));
+ }
+
+ templates.setContextType(TemplateContextTypeXSL.XSL_ATTR);
+ ICompletionProposal[] xslattrproposals = templates.computeCompletionProposals(viewer, offset);
+ if (xslattrproposals != null && xslattrproposals.length > 0) {
+ proposals.addAll(Arrays.asList(xslattrproposals));
+ }
+
+ ICompletionProposal[] allProposals = new ICompletionProposal[proposals.size()];
+ if (proposals.size() > 0) {
+ proposals.toArray(allProposals);
+ } else {
+ return null;
+ }
+ return allProposals;
+ }
+
+ public IContextInformation[] computeContextInformation(ITextViewer viewer,
+ int offset) {
+ return null;
+ }
+
+ public char[] getContextInformationAutoActivationCharacters() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public IContextInformationValidator getContextInformationValidator() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
diff --git a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/templates/TemplateContextTypeXSL.java b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/templates/TemplateContextTypeXSL.java
index 0b1d83f..08ed91f 100644
--- a/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/templates/TemplateContextTypeXSL.java
+++ b/bundles/org.eclipse.wst.xsl.ui/src/org/eclipse/wst/xsl/ui/internal/templates/TemplateContextTypeXSL.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Doug Satchwell (Chase Technology Ltd) - initial API and implementation
+ * David Carver (Intalio) - bug 289498 - Added additional context types.
*******************************************************************************/
package org.eclipse.wst.xsl.ui.internal.templates;
@@ -16,6 +17,11 @@
public class TemplateContextTypeXSL extends TemplateContextType
{
+ public static final String XSL_TAG = "xsl_tag"; //$NON-NLS-1$
+ public static final String XSL_ATTR = "xsl_attr"; //$NON-NLS-1$
+ public static final String XSL_NEW = "xsl_new"; //$NON-NLS-1$
+
+
public TemplateContextTypeXSL()
{
super();