1 package net.sourceforge.phpdt.internal.ui.text.template;
3 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
4 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
5 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
6 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
7 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9 import org.eclipse.jface.text.BadLocationException;
10 import org.eclipse.jface.text.IDocument;
11 import org.eclipse.jface.text.IRegion;
12 import org.eclipse.jface.text.ITextViewer;
13 import org.eclipse.jface.text.templates.TemplateContext;
14 import org.eclipse.swt.graphics.Image;
17 * A PHP identifier proposal.
19 public class IdentifierProposal extends AbstractProposal {
20 private final TemplateContext fContext;
22 private final Image fImage_fun;
24 private final Image fImage_var;
26 private final IRegion fRegion;
28 private final String fTemplate;
30 public IdentifierProposal(String template, TemplateContext context,
31 IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
36 fImage_fun = image_fun;
37 fImage_var = image_var;
41 public void apply(IDocument document) {
43 int start = fRegion.getOffset();
44 int end = fRegion.getOffset() + fRegion.getLength();
45 document.replace(start, end - start, fTemplate);
46 // translate positions
47 LinkedPositionManager manager = new LinkedPositionManager(document);
49 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
50 editor.setFinalCaretOffset(fTemplate.length() + start);
52 fSelectedRegion = editor.getSelectedRegion();
53 } catch (BadLocationException e) {
54 PHPeclipsePlugin.log(e);
59 public String getAdditionalProposalInfo() {
60 return textToHTML(fTemplate);
63 public String getDisplayString() {
65 + TemplateMessages.getString("TemplateProposal.delimiter")
66 + fTemplate; // $NON-NLS-1$
69 public Image getImage() {
70 if (fTemplate.charAt(0) == '$') {
76 public int getRelevance() {
77 if (fContext instanceof JavaContext) {
78 JavaContext context = (JavaContext) fContext;
79 switch (context.getCharacterBeforeStart()) {
80 // high relevance after whitespace