2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.php.editor.completion;
43
import java.util.ArrayList;
44
import java.util.Iterator;
45
import java.util.List;
48
import javax.swing.ImageIcon;
50
import javax.swing.text.Document;
51
import org.netbeans.api.gsf.Element;
52
import org.netbeans.api.gsf.ElementKind;
53
import org.netbeans.api.gsf.HtmlFormatter;
54
import org.netbeans.api.gsf.Modifier;
55
import org.netbeans.modules.php.editor.TokenUtils;
56
import org.netbeans.modules.php.model.FormalParameter;
57
import org.netbeans.modules.php.model.FunctionDeclaration;
58
import org.netbeans.modules.php.model.PhpModel;
59
import org.netbeans.modules.php.model.SourceElement;
63
* This is function completion proposal based on user defined PHP function.
65
* @author Victor G. Vasilyev
68
class UserDefinedMethodItem extends CompletionItem {
71
private static final String COMMA = ", "; // NOI18N
73
private static final String RIGHT_PARENS = ")"; // NOI18N
75
private static final String LEFT_PARENS = "("; // NOI18N
77
UserDefinedMethodItem( FunctionDeclaration func, int caretOffset ,
78
HtmlFormatter formatter )
80
super(caretOffset , formatter );
86
* @see org.netbeans.api.gsf.CompletionProposal#getIcon()
88
public ImageIcon getIcon() {
89
// TODO An Icon for User Defined Method is required.
94
* @see org.netbeans.api.gsf.CompletionProposal#getInsertParams()
97
public List<String> getInsertParams() {
98
List<String> params = new ArrayList<String>();
99
List<FormalParameter> fpl = getFormalParameterList();
100
for(FormalParameter fp: fpl) {
101
StringBuilder sb = new StringBuilder();
102
sb.append(fp.getName());
103
params.add(sb.toString());
109
* @see org.netbeans.api.gsf.CompletionProposal#getInsertPrefix()
111
public String getInsertPrefix() {
112
return myFunction.getName();
116
* @see org.netbeans.api.gsf.CompletionProposal#getKind()
118
public ElementKind getKind() {
119
return ElementKind.METHOD;
123
* @see org.netbeans.api.gsf.CompletionProposal#getLhsHtml()
125
public String getLhsHtml() {
126
HtmlFormatter formatter = getFormatter();
128
formatter.name(getKind(), true);
129
formatter.appendText(myFunction.getName());
130
formatter.name(getKind(), false);
131
formatter.appendText(LEFT_PARENS);
132
List<FormalParameter> fpl = getFormalParameterList();
133
Iterator<FormalParameter> it = fpl.iterator();
134
while (it.hasNext()) {
135
FormalParameter fp = it.next();
136
formatter.parameters(true);
137
formatter.appendText(fp.getText());
138
formatter.parameters(false);
140
formatter.appendText(COMMA);
143
formatter.appendText(RIGHT_PARENS);
144
return formatter.getText();
148
* @see org.netbeans.api.gsf.CompletionProposal#getModifiers()
150
public Set<Modifier> getModifiers() {
151
// TODO Auto-generated method stub
156
* @see org.netbeans.api.gsf.CompletionProposal#getName()
158
public String getName() {
159
return myFunction.getName();
163
* @see org.netbeans.api.gsf.CompletionProposal#getRhsHtml()
165
public String getRhsHtml() {
166
// TODO If it is possible then a text explained the return type
167
// should be returned.
172
* @see org.netbeans.api.gsf.CompletionProposal#isSmart()
174
public boolean isSmart() {
179
private List<FormalParameter> getFormalParameterList() {
180
PhpModel model = myFunction.getModel();
184
return myFunction.getParamaterList().getParameters();
192
* @see org.netbeans.api.gsf.CompletionProposal#getElement()
195
public Element getElement() {
196
return new DocumentableElement() {
198
public String getIn() {
199
// TODO Auto-generated method stub
203
public ElementKind getKind() {
204
return ElementKind.METHOD;
207
public Set<Modifier> getModifiers() {
208
// TODO Auto-generated method stub
212
public String getName() {
213
return myFunction.getName();
216
public String getDocumentation() {
217
return getDocumentationText(getDocCommentText());
223
protected String getDocCommentText() {
224
int offset = myFunction.getOffset();
225
return TokenUtils.getDocComentText(getDocument(), offset);
228
protected String getDocumentationText(String docCommentText) {
229
StringBuilder sb = new StringBuilder();
230
// TODO: a name of the file where the method is defined
231
// TODO: a link that opens the file in the editor
233
sb.append(getName());
235
if(docCommentText!=null) {
236
sb.append(getNormalizedText(docCommentText));
240
sb.append("Description");
244
sb.append(getName());
246
sb.append(LEFT_PARENS);
247
List<FormalParameter> fpl = getFormalParameterList();
248
Iterator<FormalParameter> it = fpl.iterator();
249
while (it.hasNext()) {
250
FormalParameter fp = it.next();
251
sb.append(fp.getText());
256
sb.append(RIGHT_PARENS);
258
sb.append("<DIV CLASS='warning'><P ></P >");
259
sb.append("<TABLE CLASS='warning' BORDER='1' WIDTH='100%'>");
260
sb.append("<TR ><TD ALIGN='CENTER'><B >Warning</B ></TD ></TR >");
261
sb.append("<TR ><TD ALIGN='LEFT'><P >");
262
sb.append("This function is currently not documented");
263
sb.append("."); // sb.append("; only the argument list is available.");
264
sb.append("</P ></TD ></TR >");
265
sb.append("</TABLE >");
266
sb.append("</DIV >");
268
return sb.toString();
271
public static String getNormalizedText(String docCommentText) {
272
if(docCommentText==null) {
275
String text = docCommentText;
277
int endIndex = text.length();
278
if(text.startsWith(BLOCK_COMMENT_START)) {
279
beginIndex = BLOCK_COMMENT_START.length();
281
if(text.endsWith(BLOCK_COMMENT_END)) {
282
endIndex = text.length() - BLOCK_COMMENT_END.length();
284
text = text.substring(beginIndex, endIndex).trim();
285
text = text.replaceAll("[ \t\n\f\r]*\\*", "\n");
289
public static final String BLOCK_COMMENT_START = "/*";
290
public static final String BLOCK_COMMENT_END = "*/";
292
private Document getDocument() {
293
return myFunction.getModel().getDocument();
296
private boolean isCaretInside(int caretOffset, SourceElement e) {
297
return e.getOffset() <= caretOffset && e.getEndOffset() >= caretOffset;
300
private FunctionDeclaration myFunction;