fix #774 infinite loop in net.sourceforge.phpeclipse.builder.IdentifierIndexManager...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / BuiltInProposal.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text.template;
6
7 import net.sourceforge.phpdt.internal.corext.template.TemplateMessages;
8 import net.sourceforge.phpdt.internal.corext.template.php.JavaContext;
9 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
10 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
11 import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
12 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
13 import net.sourceforge.phpeclipse.phpeditor.php.PHPFunction;
14
15 import org.eclipse.jface.text.BadLocationException;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.text.IRegion;
18 import org.eclipse.jface.text.ITextViewer;
19 import org.eclipse.jface.text.contentassist.ContextInformation;
20 import org.eclipse.jface.text.contentassist.IContextInformation;
21 import org.eclipse.jface.text.templates.TemplateContext;
22 import org.eclipse.swt.graphics.Image;
23
24 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionManager;
25 // import net.sourceforge.phpdt.internal.ui.text.link.LinkedPositionUI;
26 // import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
27
28 /**
29  * A PHP identifier proposal.
30  */
31 public class BuiltInProposal extends AbstractProposal {
32         private final TemplateContext fContext;
33
34         private final PHPFunction fFunction;
35
36         private final IRegion fRegion;
37
38         private final String fBuiltinFunctionName;
39
40         /**
41          * Creates a template proposal with a template and its context.
42          * 
43          * @param template
44          *            the template
45          * @param context
46          *            the context in which the template was requested.
47          * @param image
48          *            the icon of the proposal.
49          */
50         public BuiltInProposal(String functionName, PHPFunction function,
51                         TemplateContext context, IRegion region, ITextViewer viewer) {
52                 super(viewer);
53                 fBuiltinFunctionName = functionName;
54                 fFunction = function;
55                 fContext = context;
56                 // fViewer = viewer;
57                 fRegion = region;
58         }
59
60         /*
61          * @see ICompletionProposal#apply(IDocument)
62          */
63         public void apply(IDocument document) {
64                 try {
65                         // if (fTemplateBuffer == null)
66                         // fTemplateBuffer= fContext.evaluate(fTemplate);
67
68                         int start = fRegion.getOffset();
69                         int end = fRegion.getOffset() + fRegion.getLength();
70
71                         // insert template string
72                         // String templateString = fTemplate; //
73                         // fTemplateBuffer.getString();
74                         document.replace(start, end - start, fBuiltinFunctionName + "()");
75
76                         // translate positions
77                         LinkedPositionManager manager = new LinkedPositionManager(document);
78                         // TemplatePosition[] variables= fTemplateBuffer.getVariables();
79                         // for (int i= 0; i != variables.length; i++) {
80                         // TemplatePosition variable= variables[i];
81                         //
82                         // if (variable.isResolved())
83                         // continue;
84                         //
85                         // int[] offsets= variable.getOffsets();
86                         // int length= variable.getLength();
87                         //
88                         // for (int j= 0; j != offsets.length; j++)
89                         // manager.addPosition(offsets[j] + start, length);
90                         // }
91
92                         LinkedPositionUI editor = new LinkedPositionUI(fViewer, manager);
93                         editor.setFinalCaretOffset(fBuiltinFunctionName.length() + start
94                                         + 1);
95                         // editor.setFinalCaretOffset(getCaretOffset(fTemplateBuffer) +
96                         // start);
97                         editor.enter();
98
99                         fSelectedRegion = editor.getSelectedRegion();
100
101                 } catch (BadLocationException e) {
102                         PHPeclipsePlugin.log(e);
103                         openErrorDialog(e);
104
105                 }
106                 // catch (CoreException e) {
107                 // handleException(e);
108                 // }
109         }
110
111         public String getAdditionalProposalInfo() {
112                 return fFunction.getHoverText();
113         }
114
115         public IContextInformation getContextInformation() {
116                 if (fContextInfo == null) {
117                         String contextInfoString = fFunction.getHoverText();
118                         if (contextInfoString != null && contextInfoString.length() > 0) {
119                                 // extract the parameter context information for the function:
120                                 int i0 = contextInfoString.indexOf('(');
121                                 int newline = contextInfoString.indexOf('\n');
122                                 if (i0 >= 0 && (i0 < newline || newline < 0)) {
123                                         int i1 = contextInfoString.indexOf(')', i0 + 1);
124                                         if (i1 > 0) {
125                                                 fContextInfo = new ContextInformation(null,
126                                                                 contextInfoString.substring(i0 + 1, i1));
127                                         } else {
128                                                 fContextInfo = new ContextInformation(null,
129                                                                 contextInfoString);
130                                         }
131                                 } else {
132                                         fContextInfo = new ContextInformation(null,
133                                                         contextInfoString);
134                                 }
135                         }
136                 }
137                 return fContextInfo;
138         }
139
140         /*
141          * @see ICompletionProposal#getDisplayString()
142          */
143         public String getDisplayString() {
144                 return fBuiltinFunctionName
145                                 + TemplateMessages.getString("TemplateProposal.delimiter") + fFunction.getUsage(); // $NON-NLS-1$
146                 // //$NON-NLS-1$
147         }
148
149         /*
150          * @see ICompletionProposal#getImage()
151          */
152         public Image getImage() {
153                 return PHPUiImages.get(PHPUiImages.IMG_BUILTIN);
154         }
155
156         /*
157          * @see IJavaCompletionProposal#getRelevance()
158          */
159         public int getRelevance() {
160
161                 if (fContext instanceof JavaContext) {
162                         JavaContext context = (JavaContext) fContext;
163                         switch (context.getCharacterBeforeStart()) {
164                         // high relevance after whitespace
165                         case ' ':
166                         case '\r':
167                         case '\n':
168                         case '\t':
169                                 return 50;
170
171                         default:
172                                 return 0;
173                         }
174                 } else {
175                         return 50;
176                 }
177         }
178
179 }