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