|
Lines 56-61
Link Here
|
| 56 |
import org.eclipse.ant.internal.ui.editor.model.AntTargetNode; |
56 |
import org.eclipse.ant.internal.ui.editor.model.AntTargetNode; |
| 57 |
import org.eclipse.ant.internal.ui.editor.model.AntTaskNode; |
57 |
import org.eclipse.ant.internal.ui.editor.model.AntTaskNode; |
| 58 |
import org.eclipse.ant.internal.ui.editor.outline.AntModel; |
58 |
import org.eclipse.ant.internal.ui.editor.outline.AntModel; |
|
|
59 |
import org.eclipse.ant.internal.ui.editor.templates.*; |
| 59 |
import org.eclipse.ant.internal.ui.model.AntUIImages; |
60 |
import org.eclipse.ant.internal.ui.model.AntUIImages; |
| 60 |
import org.eclipse.ant.internal.ui.model.AntUIPlugin; |
61 |
import org.eclipse.ant.internal.ui.model.AntUIPlugin; |
| 61 |
import org.eclipse.ant.internal.ui.model.IAntUIConstants; |
62 |
import org.eclipse.ant.internal.ui.model.IAntUIConstants; |
|
Lines 69-76
Link Here
|
| 69 |
import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
70 |
import org.eclipse.jface.text.contentassist.IContentAssistProcessor; |
| 70 |
import org.eclipse.jface.text.contentassist.IContextInformation; |
71 |
import org.eclipse.jface.text.contentassist.IContextInformation; |
| 71 |
import org.eclipse.jface.text.contentassist.IContextInformationValidator; |
72 |
import org.eclipse.jface.text.contentassist.IContextInformationValidator; |
|
|
73 |
import org.eclipse.jface.text.templates.ContextType; |
| 72 |
import org.eclipse.jface.text.templates.DocumentTemplateContext; |
74 |
import org.eclipse.jface.text.templates.DocumentTemplateContext; |
| 73 |
import org.eclipse.jface.text.templates.Template; |
75 |
import org.eclipse.jface.text.templates.Template; |
|
|
76 |
import org.eclipse.jface.text.templates.TemplateCompletionProcessor; |
| 74 |
import org.eclipse.jface.text.templates.TemplateContext; |
77 |
import org.eclipse.jface.text.templates.TemplateContext; |
| 75 |
import org.eclipse.jface.text.templates.TemplateProposal; |
78 |
import org.eclipse.jface.text.templates.TemplateProposal; |
| 76 |
import org.eclipse.swt.graphics.Image; |
79 |
import org.eclipse.swt.graphics.Image; |
|
Lines 85-91
Link Here
|
| 85 |
/** |
88 |
/** |
| 86 |
* The text completion processor for the Ant Editor. |
89 |
* The text completion processor for the Ant Editor. |
| 87 |
*/ |
90 |
*/ |
| 88 |
public class AntEditorCompletionProcessor implements IContentAssistProcessor { |
91 |
public class AntEditorCompletionProcessor extends TemplateCompletionProcessor implements IContentAssistProcessor { |
| 89 |
|
92 |
|
| 90 |
private Comparator proposalComparator= new Comparator() { |
93 |
private Comparator proposalComparator= new Comparator() { |
| 91 |
public int compare(Object o1, Object o2) { |
94 |
public int compare(Object o1, Object o2) { |
|
Lines 205-218
Link Here
|
| 205 |
} |
208 |
} |
| 206 |
|
209 |
|
| 207 |
/** |
210 |
/** |
| 208 |
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(ITextViewer, int) |
211 |
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(ITextViewer, int) |
| 209 |
*/ |
212 |
*/ |
| 210 |
public ICompletionProposal[] computeCompletionProposals(ITextViewer refViewer, int documentOffset) { |
213 |
public ICompletionProposal[] computeCompletionProposals( |
| 211 |
this.viewer = refViewer; |
214 |
ITextViewer refViewer, int documentOffset) { |
| 212 |
return determineProposals(); |
215 |
|
| 213 |
} |
216 |
this.viewer = refViewer; |
|
|
217 |
|
| 218 |
return mergeProposals(super.computeCompletionProposals(refViewer, |
| 219 |
documentOffset), determineProposals()); |
| 220 |
} |
| 214 |
|
221 |
|
| 215 |
/** |
222 |
/** |
|
|
223 |
* @param proposals1 |
| 224 |
* @param proposals2 |
| 225 |
* @return |
| 226 |
*/ |
| 227 |
private ICompletionProposal[] mergeProposals( |
| 228 |
ICompletionProposal[] proposals1, ICompletionProposal[] proposals2) { |
| 229 |
|
| 230 |
ICompletionProposal[] combinedProposals = new ICompletionProposal[proposals1.length |
| 231 |
+ proposals2.length]; |
| 232 |
|
| 233 |
System.arraycopy(proposals1,0,combinedProposals,0,proposals1.length); |
| 234 |
System.arraycopy(proposals2,0,combinedProposals,proposals1.length,proposals2.length); |
| 235 |
|
| 236 |
Arrays.sort(combinedProposals,proposalComparator); |
| 237 |
return combinedProposals; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 216 |
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(ITextViewer, int) |
241 |
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeContextInformation(ITextViewer, int) |
| 217 |
*/ |
242 |
*/ |
| 218 |
public IContextInformation[] computeContextInformation(ITextViewer refViewer, int documentOffset) { |
243 |
public IContextInformation[] computeContextInformation(ITextViewer refViewer, int documentOffset) { |
|
Lines 657-663
Link Here
|
| 657 |
// think of a usecase for templates other than tasks at the moment, but |
682 |
// think of a usecase for templates other than tasks at the moment, but |
| 658 |
// since users can add templates via the preferences we may need to |
683 |
// since users can add templates via the preferences we may need to |
| 659 |
// rethink this. |
684 |
// rethink this. |
| 660 |
proposals.addAll(getTemplateProposals(document, prefix)); |
685 |
// proposals.addAll(getTemplateProposals(document, prefix)); |
| 661 |
|
686 |
|
| 662 |
return (ICompletionProposal[])proposals.toArray(new ICompletionProposal[proposals.size()]); |
687 |
return (ICompletionProposal[])proposals.toArray(new ICompletionProposal[proposals.size()]); |
| 663 |
} |
688 |
} |
|
Lines 674-707
Link Here
|
| 674 |
} |
699 |
} |
| 675 |
} |
700 |
} |
| 676 |
|
701 |
|
| 677 |
/* |
|
|
| 678 |
* @return a collection of |
| 679 |
* {@link org.eclipse.jface.text.templates.TemplateProposal}s |
| 680 |
*/ |
| 681 |
protected Collection getTemplateProposals(IDocument document, String prefix) { |
| 682 |
|
| 683 |
Point selection = viewer.getSelectedRange(); |
| 684 |
IRegion selectedRegion = new Region(selection.x - (prefix.length() + 1), selection.y + prefix.length() + 1); |
| 685 |
if (selectedRegion.getOffset() == -1) { |
| 686 |
return Collections.EMPTY_LIST; |
| 687 |
} |
| 688 |
TemplateContext templateContext = new DocumentTemplateContext( |
| 689 |
AntTemplates.CONTEXT, document, selectedRegion.getOffset(), selectedRegion.getLength()); //$NON-NLS-1$ |
| 690 |
|
| 691 |
Template[] templates= AntTemplates.getAntTemplates(); |
| 692 |
List proposals = new ArrayList(templates.length); |
| 693 |
TemplateProposal templateProposal; |
| 694 |
for (int i = 0; i < templates.length; i++) { |
| 695 |
Template template = templates[i]; |
| 696 |
if (prefix.length() == 0 || template.getName().toLowerCase().startsWith(prefix)) { |
| 697 |
templateProposal = new TemplateProposal(template, templateContext, selectedRegion, AntUIImages.getImage(IAntUIConstants.IMG_TEMPLATE_PROPOSAL)); |
| 698 |
proposals.add(templateProposal); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
return proposals; |
| 703 |
} |
| 704 |
|
| 705 |
private ICompletionProposal newCompletionProposal(IDocument document, String aPrefix, String elementName) { |
702 |
private ICompletionProposal newCompletionProposal(IDocument document, String aPrefix, String elementName) { |
| 706 |
additionalProposalOffset= 0; |
703 |
additionalProposalOffset= 0; |
| 707 |
Image proposalImage = AntUIImages.getImage(IAntUIConstants.IMG_TASK_PROPOSAL); |
704 |
Image proposalImage = AntUIImages.getImage(IAntUIConstants.IMG_TASK_PROPOSAL); |
|
Lines 1314-1317
Link Here
|
| 1314 |
public void setCompletionProposalAutoActivationCharacters(char[] activationSet) { |
1311 |
public void setCompletionProposalAutoActivationCharacters(char[] activationSet) { |
| 1315 |
autoActivationChars= activationSet; |
1312 |
autoActivationChars= activationSet; |
| 1316 |
} |
1313 |
} |
|
|
1314 |
|
| 1315 |
/** |
| 1316 |
* We watch for angular brackets since those are often part of XML |
| 1317 |
* templates. |
| 1318 |
*/ |
| 1319 |
protected String extractPrefix(ITextViewer viewer, int offset) { |
| 1320 |
IDocument document= viewer.getDocument(); |
| 1321 |
int i= offset; |
| 1322 |
if (i > document.getLength()) |
| 1323 |
return ""; //$NON-NLS-1$ |
| 1324 |
|
| 1325 |
try { |
| 1326 |
while (i > 0) { |
| 1327 |
char ch= document.getChar(i - 1); |
| 1328 |
if (ch != '<' && !Character.isJavaIdentifierPart(ch)) |
| 1329 |
break; |
| 1330 |
i--; |
| 1331 |
} |
| 1332 |
|
| 1333 |
return document.get(i, offset - i); |
| 1334 |
} catch (BadLocationException e) { |
| 1335 |
return ""; //$NON-NLS-1$ |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
/** |
| 1340 |
* Cut out angular brackets for relevance sorting, since the template name |
| 1341 |
* does not contain the brackets. |
| 1342 |
*/ |
| 1343 |
protected int getRelevance(Template template, String prefix) { |
| 1344 |
if (prefix.startsWith("<")) //$NON-NLS-1$ |
| 1345 |
prefix= prefix.substring(1); |
| 1346 |
if (template.getName().startsWith(prefix)) |
| 1347 |
return 90; |
| 1348 |
return 0; |
| 1349 |
} |
| 1350 |
|
| 1351 |
/** |
| 1352 |
* Simply return all templates. |
| 1353 |
*/ |
| 1354 |
protected Template[] getTemplates(String contextTypeId) { |
| 1355 |
return AntTemplateAccess.getDefault().getTemplateStore().getTemplates(); |
| 1356 |
} |
| 1357 |
|
| 1358 |
/** |
| 1359 |
* Return the XML context type that is supported by this plugin. |
| 1360 |
*/ |
| 1361 |
protected ContextType getContextType(ITextViewer viewer, IRegion region) { |
| 1362 |
return AntTemplateAccess.getDefault().getContextTypeRegistry().getContextType(XMLContextType.XML_CONTEXT_TYPE); |
| 1363 |
} |
| 1364 |
|
| 1365 |
/* (non-Javadoc) |
| 1366 |
* @see org.eclipse.jface.text.templates.TemplateCompletionProcessor#getImage(org.eclipse.jface.text.templates.Template) |
| 1367 |
*/ |
| 1368 |
protected Image getImage(Template template) { |
| 1369 |
return AntUIImages.getImage(IAntUIConstants.IMG_TEMPLATE_PROPOSAL); |
| 1370 |
} |
| 1317 |
} |
1371 |
} |