001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.php.editor.completion;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046: import java.util.Set;
047:
048: import javax.swing.ImageIcon;
049:
050: import javax.swing.text.Document;
051: import org.netbeans.modules.gsf.api.Element;
052: import org.netbeans.modules.gsf.api.ElementKind;
053: import org.netbeans.modules.gsf.api.HtmlFormatter;
054: import org.netbeans.modules.gsf.api.Modifier;
055: import org.netbeans.modules.php.editor.TokenUtils;
056: import org.netbeans.modules.php.model.FormalParameter;
057: import org.netbeans.modules.php.model.FunctionDeclaration;
058: import org.netbeans.modules.php.model.PhpModel;
059: import org.netbeans.modules.php.model.SourceElement;
060:
061: /**
062: * This is function completion proposal based on user defined PHP function.
063: *
064: * @author Victor G. Vasilyev
065: *
066: */
067: class UserDefinedMethodItem extends CompletionItem {
068:
069: private static final String COMMA = ", "; // NOI18N
070: private static final String RIGHT_PARENS = ")"; // NOI18N
071: private static final String LEFT_PARENS = "("; // NOI18N
072:
073: UserDefinedMethodItem(FunctionDeclaration func, int caretOffset,
074: HtmlFormatter formatter) {
075: super (caretOffset, formatter);
076: myFunction = func;
077: }
078:
079: /* (non-Javadoc)
080: * @see org.netbeans.modules.gsf.api.CompletionProposal#getIcon()
081: */
082: public ImageIcon getIcon() {
083: // TODO An Icon for User Defined Method is required.
084: return null;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.netbeans.modules.gsf.api.CompletionProposal#getInsertParams()
089: */
090: @Override
091: public List<String> getInsertParams() {
092: List<String> params = new ArrayList<String>();
093: List<FormalParameter> fpl = getFormalParameterList();
094: for (FormalParameter fp : fpl) {
095: StringBuilder sb = new StringBuilder();
096: sb.append(fp.getName());
097: params.add(sb.toString());
098: }
099: return params;
100: }
101:
102: /* (non-Javadoc)
103: * @see org.netbeans.modules.gsf.api.CompletionProposal#getInsertPrefix()
104: */
105: public String getInsertPrefix() {
106: return myFunction.getName();
107: }
108:
109: /* (non-Javadoc)
110: * @see org.netbeans.modules.gsf.api.CompletionProposal#getKind()
111: */
112: public ElementKind getKind() {
113: return ElementKind.METHOD;
114: }
115:
116: /* (non-Javadoc)
117: * @see org.netbeans.modules.gsf.api.CompletionProposal#getLhsHtml()
118: */
119: public String getLhsHtml() {
120: HtmlFormatter formatter = getFormatter();
121: formatter.reset();
122: formatter.name(getKind(), true);
123: formatter.appendText(myFunction.getName());
124: formatter.name(getKind(), false);
125: formatter.appendText(LEFT_PARENS);
126: List<FormalParameter> fpl = getFormalParameterList();
127: Iterator<FormalParameter> it = fpl.iterator();
128: while (it.hasNext()) {
129: FormalParameter fp = it.next();
130: formatter.parameters(true);
131: formatter.appendText(fp.getText());
132: formatter.parameters(false);
133: if (it.hasNext()) {
134: formatter.appendText(COMMA);
135: }
136: }
137: formatter.appendText(RIGHT_PARENS);
138: return formatter.getText();
139: }
140:
141: /* (non-Javadoc)
142: * @see org.netbeans.modules.gsf.api.CompletionProposal#getModifiers()
143: */
144: public Set<Modifier> getModifiers() {
145: // TODO Auto-generated method stub
146: return null;
147: }
148:
149: /* (non-Javadoc)
150: * @see org.netbeans.modules.gsf.api.CompletionProposal#getName()
151: */
152: public String getName() {
153: return myFunction.getName();
154: }
155:
156: /* (non-Javadoc)
157: * @see org.netbeans.modules.gsf.api.CompletionProposal#getRhsHtml()
158: */
159: public String getRhsHtml() {
160: // TODO If it is possible then a text explained the return type
161: // should be returned.
162: return null;
163: }
164:
165: /* (non-Javadoc)
166: * @see org.netbeans.modules.gsf.api.CompletionProposal#isSmart()
167: */
168: public boolean isSmart() {
169: return true;
170: }
171:
172: @Override
173: public String getCustomInsertTemplate() {
174: List<String> params = getInsertParams();
175: String[] delimiters = getParamListDelimiters();
176: assert delimiters.length == 2;
177: int paramCount = params.size();
178:
179: StringBuilder sb = new StringBuilder();
180: sb.append(getInsertPrefix());
181: sb.append(delimiters[0]);
182: int id = 1;
183: for (int i = 0; i < paramCount; i++) {
184: String paramDesc = params.get(i);
185: sb.append("${"); //NOI18N
186: sb.append("php-cc-"); // NOI18N
187: sb.append(Integer.toString(id++));
188: sb.append(" default=\""); // NOI18N
189: sb.append(paramDesc);
190: sb.append("\""); // NOI18N
191: sb.append("}"); //NOI18N
192: if (i < paramCount - 1) {
193: sb.append(", "); //NOI18N
194: }
195: }
196: sb.append(delimiters[1]);
197: sb.append("${cursor}"); // NOI18N
198: return sb.toString();
199: }
200:
201: private List<FormalParameter> getFormalParameterList() {
202: PhpModel model = myFunction.getModel();
203: model.sync();
204: model.readLock();
205: try {
206: return myFunction.getParamaterList().getParameters();
207: } finally {
208: model.readUnlock();
209: }
210:
211: }
212:
213: /* (non-Javadoc)
214: * @see org.netbeans.modules.gsf.api.CompletionProposal#getElement()
215: */
216: @Override
217: public Element getElement() {
218: return new DocumentableElement() {
219:
220: public String getIn() {
221: // TODO Auto-generated method stub
222: return null;
223: }
224:
225: public ElementKind getKind() {
226: return ElementKind.METHOD;
227: }
228:
229: public Set<Modifier> getModifiers() {
230: // TODO Auto-generated method stub
231: return null;
232: }
233:
234: public String getName() {
235: return myFunction.getName();
236: }
237:
238: public String getDocumentation() {
239: return getDocumentationText(getDocCommentText());
240: }
241: };
242: }
243:
244: protected String getDocCommentText() {
245: int offset = myFunction.getOffset();
246: return TokenUtils.getDocComentText(getDocument(), offset);
247: }
248:
249: protected String getDocumentationText(String docCommentText) {
250: StringBuilder sb = new StringBuilder();
251: // TODO: a name of the file where the method is defined
252: // TODO: a link that opens the file in the editor
253: sb.append("<h1>");
254: sb.append(getName());
255: sb.append("</h1>");
256: if (docCommentText != null) {
257: sb.append(getNormalizedText(docCommentText));
258: } else {
259: sb.append("<h2>");
260: sb.append("Description");
261: sb.append("</h2>");
262: // TODO return type
263: sb.append("<b>");
264: sb.append(getName());
265: sb.append("</b>");
266: sb.append(LEFT_PARENS);
267: List<FormalParameter> fpl = getFormalParameterList();
268: Iterator<FormalParameter> it = fpl.iterator();
269: while (it.hasNext()) {
270: FormalParameter fp = it.next();
271: sb.append(fp.getText());
272: if (it.hasNext()) {
273: sb.append(COMMA);
274: }
275: }
276: sb.append(RIGHT_PARENS);
277:
278: sb.append("<DIV CLASS='warning'><P ></P >");
279: sb
280: .append("<TABLE CLASS='warning' BORDER='1' WIDTH='100%'>");
281: sb
282: .append("<TR ><TD ALIGN='CENTER'><B >Warning</B ></TD ></TR >");
283: sb.append("<TR ><TD ALIGN='LEFT'><P >");
284: sb.append("This function is currently not documented");
285: sb.append("."); // sb.append("; only the argument list is available.");
286: sb.append("</P ></TD ></TR >");
287: sb.append("</TABLE >");
288: sb.append("</DIV >");
289: }
290: return sb.toString();
291: }
292:
293: public static String getNormalizedText(String docCommentText) {
294: if (docCommentText == null) {
295: return null;
296: }
297: String text = docCommentText;
298: int beginIndex = 0;
299: int endIndex = text.length();
300: if (text.startsWith(BLOCK_COMMENT_START)) {
301: beginIndex = BLOCK_COMMENT_START.length();
302: }
303: if (text.endsWith(BLOCK_COMMENT_END)) {
304: endIndex = text.length() - BLOCK_COMMENT_END.length();
305: }
306: text = text.substring(beginIndex, endIndex).trim();
307: text = text.replaceAll("[ \t\n\f\r]*\\*", "\n");
308: return text;
309: }
310:
311: public static final String BLOCK_COMMENT_START = "/*";
312: public static final String BLOCK_COMMENT_END = "*/";
313:
314: private Document getDocument() {
315: return myFunction.getModel().getDocument();
316: }
317:
318: private boolean isCaretInside(int caretOffset, SourceElement e) {
319: return e.getOffset() <= caretOffset
320: && e.getEndOffset() >= caretOffset;
321: }
322:
323: private FunctionDeclaration myFunction;
324: }
|