|
Lines 12-25
Link Here
|
| 12 |
*******************************************************************************/ |
12 |
*******************************************************************************/ |
| 13 |
package org.eclipse.wst.xml.ui.internal.contentassist; |
13 |
package org.eclipse.wst.xml.ui.internal.contentassist; |
| 14 |
|
14 |
|
|
|
15 |
import java.util.ArrayList; |
| 16 |
import java.util.Collections; |
| 17 |
import java.util.Comparator; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.eclipse.jface.text.IDocument; |
| 15 |
import org.eclipse.jface.text.IRegion; |
21 |
import org.eclipse.jface.text.IRegion; |
|
|
22 |
import org.eclipse.jface.text.ITextSelection; |
| 16 |
import org.eclipse.jface.text.ITextViewer; |
23 |
import org.eclipse.jface.text.ITextViewer; |
|
|
24 |
import org.eclipse.jface.text.Region; |
| 17 |
import org.eclipse.jface.text.contentassist.ICompletionProposal; |
25 |
import org.eclipse.jface.text.contentassist.ICompletionProposal; |
| 18 |
import org.eclipse.jface.text.templates.ContextTypeRegistry; |
26 |
import org.eclipse.jface.text.templates.ContextTypeRegistry; |
| 19 |
import org.eclipse.jface.text.templates.Template; |
27 |
import org.eclipse.jface.text.templates.Template; |
| 20 |
import org.eclipse.jface.text.templates.TemplateCompletionProcessor; |
28 |
import org.eclipse.jface.text.templates.TemplateCompletionProcessor; |
| 21 |
import org.eclipse.jface.text.templates.TemplateContext; |
29 |
import org.eclipse.jface.text.templates.TemplateContext; |
| 22 |
import org.eclipse.jface.text.templates.TemplateContextType; |
30 |
import org.eclipse.jface.text.templates.TemplateContextType; |
|
|
31 |
import org.eclipse.jface.text.templates.TemplateException; |
| 32 |
import org.eclipse.jface.text.templates.TemplateProposal; |
| 23 |
import org.eclipse.jface.text.templates.persistence.TemplateStore; |
33 |
import org.eclipse.jface.text.templates.persistence.TemplateStore; |
| 24 |
import org.eclipse.swt.graphics.Image; |
34 |
import org.eclipse.swt.graphics.Image; |
| 25 |
import org.eclipse.wst.xml.ui.internal.XMLUIPlugin; |
35 |
import org.eclipse.wst.xml.ui.internal.XMLUIPlugin; |
|
Lines 35-42
Link Here
|
| 35 |
* templates. |
45 |
* templates. |
| 36 |
*/ |
46 |
*/ |
| 37 |
class XMLTemplateCompletionProcessor extends TemplateCompletionProcessor { |
47 |
class XMLTemplateCompletionProcessor extends TemplateCompletionProcessor { |
|
|
48 |
private static final class ProposalComparator implements Comparator { |
| 49 |
public int compare(Object o1, Object o2) { |
| 50 |
return ((TemplateProposal) o2).getRelevance() - ((TemplateProposal) o1).getRelevance(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
private static final Comparator fgProposalComparator = new ProposalComparator(); |
| 38 |
private String fContextTypeId = null; |
55 |
private String fContextTypeId = null; |
| 39 |
|
56 |
|
|
|
57 |
/* |
| 58 |
* Copied from super class except instead of calling createContext(viewer, |
| 59 |
* region) call createContext(viewer, region, offset) instead |
| 60 |
*/ |
| 61 |
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { |
| 62 |
|
| 63 |
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection(); |
| 64 |
|
| 65 |
// adjust offset to end of normalized selection |
| 66 |
if (selection.getOffset() == offset) |
| 67 |
offset = selection.getOffset() + selection.getLength(); |
| 68 |
|
| 69 |
String prefix = extractPrefix(viewer, offset); |
| 70 |
Region region = new Region(offset - prefix.length(), prefix.length()); |
| 71 |
TemplateContext context = createContext(viewer, region, offset); |
| 72 |
if (context == null) |
| 73 |
return new ICompletionProposal[0]; |
| 74 |
|
| 75 |
context.setVariable("selection", selection.getText()); // name of the |
| 76 |
// selection |
| 77 |
// variables |
| 78 |
// {line, |
| 79 |
// word}_selection |
| 80 |
// //$NON-NLS-1$ |
| 81 |
|
| 82 |
Template[] templates = getTemplates(context.getContextType().getId()); |
| 83 |
|
| 84 |
List matches = new ArrayList(); |
| 85 |
for (int i = 0; i < templates.length; i++) { |
| 86 |
Template template = templates[i]; |
| 87 |
try { |
| 88 |
context.getContextType().validate(template.getPattern()); |
| 89 |
} |
| 90 |
catch (TemplateException e) { |
| 91 |
continue; |
| 92 |
} |
| 93 |
if (template.matches(prefix, context.getContextType().getId())) |
| 94 |
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix))); |
| 95 |
} |
| 96 |
|
| 97 |
Collections.sort(matches, fgProposalComparator); |
| 98 |
|
| 99 |
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Creates a concrete template context for the given region in the |
| 104 |
* document. This involves finding out which context type is valid at the |
| 105 |
* given location, and then creating a context of this type. The default |
| 106 |
* implementation returns a <code>SmartReplaceTemplateContext</code> for |
| 107 |
* the context type at the given location. This takes the offset at which |
| 108 |
* content assist was invoked into consideration. |
| 109 |
* |
| 110 |
* @param viewer |
| 111 |
* the viewer for which the context is created |
| 112 |
* @param region |
| 113 |
* the region into <code>document</code> for which the |
| 114 |
* context is created |
| 115 |
* @param offset |
| 116 |
* the original offset where content assist was invoked |
| 117 |
* @return a template context that can handle template insertion at the |
| 118 |
* given location, or <code>null</code> |
| 119 |
*/ |
| 120 |
private TemplateContext createContext(ITextViewer viewer, IRegion region, int offset) { |
| 121 |
// pretty much same code as super.createContext except create SmartReplaceTemplateContext |
| 122 |
TemplateContextType contextType = getContextType(viewer, region); |
| 123 |
if (contextType != null) { |
| 124 |
IDocument document = viewer.getDocument(); |
| 125 |
return new ReplaceNameTemplateContext(contextType, document, region.getOffset(), region.getLength(), offset); |
| 126 |
} |
| 127 |
return null; |
| 128 |
} |
| 129 |
|
| 40 |
protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) { |
130 |
protected ICompletionProposal createProposal(Template template, TemplateContext context, IRegion region, int relevance) { |
| 41 |
return new CustomTemplateProposal(template, context, region, getImage(template), relevance); |
131 |
return new CustomTemplateProposal(template, context, region, getImage(template), relevance); |
| 42 |
} |
132 |
} |