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 TemplateBuffer fTemplateBuffer;
27 private String fOldText;
29 private final IRegion fRegion;
31 // private IRegion fSelectedRegion; // initialized by apply()
32 private final String fTemplate;
34 public IdentifierProposal(String template, TemplateContext context,
35 IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
41 fImage_fun = image_fun;
42 fImage_var = image_var;
46 public void apply(IDocument document) {
48 int start = fRegion.getOffset();
49 int end = fRegion.getOffset() + fRegion.getLength();
50 document.replace(start, end - start, fTemplate);
51 // translate positions
52 LinkedPositionManager manager = new LinkedPositionManager(document);
54 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
55 editor.setFinalCaretOffset(fTemplate.length() + start);
56 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) +
59 fSelectedRegion = editor.getSelectedRegion();
60 } catch (BadLocationException e) {
61 PHPeclipsePlugin.log(e);
66 public String getAdditionalProposalInfo() {
67 return textToHTML(fTemplate); // fTemplateBuffer.getString());
70 public String getDisplayString() {
72 + TemplateMessages.getString("TemplateProposal.delimiter")
73 + fTemplate; // $NON-NLS-1$
76 public Image getImage() {
77 if (fTemplate.charAt(0) == '$') {
83 public int getRelevance() {
84 if (fContext instanceof JavaContext) {
85 JavaContext context = (JavaContext) fContext;
86 switch (context.getCharacterBeforeStart()) {
87 // high relevance after whitespace