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.contentassist.ContextInformation;
14 import org.eclipse.jface.text.contentassist.IContextInformation;
15 import org.eclipse.jface.text.templates.TemplateContext;
16 import org.eclipse.swt.graphics.Image;
19 * A PHP identifier proposal.
21 public class IdentifierProposal extends AbstractProposal {
22 private final TemplateContext fContext;
24 private final Image fImage_fun;
25 private final Image fImage_var;
26 //private TemplateBuffer fTemplateBuffer;
27 private String fOldText;
28 private final IRegion fRegion;
29 // private IRegion fSelectedRegion; // initialized by apply()
30 private final String fTemplate;
32 public IdentifierProposal(String template, TemplateContext context,
33 IRegion region, ITextViewer viewer, Image image_fun, Image image_var) {
39 fImage_fun = image_fun;
40 fImage_var = image_var;
44 public void apply(IDocument document) {
46 int start = fRegion.getOffset();
47 int end = fRegion.getOffset() + fRegion.getLength();
48 document.replace(start, end - start, fTemplate);
49 // translate positions
50 LinkedPositionManager manager = new LinkedPositionManager(document);
52 LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
53 editor.setFinalCaretOffset(fTemplate.length() + start);
54 // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) + start);
56 fSelectedRegion = editor.getSelectedRegion();
57 } catch (BadLocationException e) {
58 PHPeclipsePlugin.log(e);
63 * @see ICompletionProposal#getAdditionalProposalInfo()
65 public String getAdditionalProposalInfo() {
66 return textToHTML(fTemplate); // fTemplateBuffer.getString());
69 public String getDisplayString() {
70 return fTemplate + TemplateMessages.getString("TemplateProposal.delimiter") + fTemplate; // $NON-NLS-1$
73 public Image getImage() {
74 if (fTemplate.charAt(0) == '$') {
80 public int getRelevance() {
81 if (fContext instanceof JavaContext) {
82 JavaContext context = (JavaContext) fContext;
83 switch (context.getCharacterBeforeStart()) {
84 // high relevance after whitespace