context sensitive code completion, code completion for function parameters
authorMartin Fousek <marfous@netbeans.org>
Mon, 21 Jun 2010 15:26:29 +0200
changeset 1633947d9371b40d6
parent 16333 f8e78293b568
child 16340 2cce557ebbaa
context sensitive code completion, code completion for function parameters
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/CodeCompletionUtils.java
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionItem.java
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionProvider.java
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionQuery.java
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionEntries.java
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionEntryMetadata.java
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionParamMetadata.java
php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/SmartyCodeCompletionOffer.java
     1.1 --- a/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/CodeCompletionUtils.java	Wed Jun 16 14:44:01 2010 +0200
     1.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/CodeCompletionUtils.java	Mon Jun 21 15:26:29 2010 +0200
     1.3 @@ -45,6 +45,7 @@
     1.4  import org.netbeans.api.lexer.TokenSequence;
     1.5  import org.netbeans.modules.editor.NbEditorUtilities;
     1.6  import org.netbeans.modules.php.smarty.SmartyFramework;
     1.7 +import org.netbeans.modules.php.smarty.editor.completion.entries.SmartyCodeCompletionOffer;
     1.8  import org.netbeans.modules.php.smarty.editor.lexer.TplTopTokenId;
     1.9  import org.netbeans.modules.php.smarty.editor.utlis.LexerUtils;
    1.10  import org.openide.util.Exceptions;
    1.11 @@ -56,6 +57,7 @@
    1.12  public class CodeCompletionUtils {
    1.13  
    1.14      private static final int COMPLETION_MAX_FILTER_LENGHT = 20;
    1.15 +    private static final int SCANNING_MAX_FILTER_LENGHT = 100;
    1.16  
    1.17      public static String getTextPrefix(Document doc, int offset) {
    1.18          int readLength = (COMPLETION_MAX_FILTER_LENGHT > offset) ? offset : COMPLETION_MAX_FILTER_LENGHT;
    1.19 @@ -101,4 +103,44 @@
    1.20          return true;
    1.21      }
    1.22  
    1.23 +    static boolean inVariableModifiers(Document doc, int caretOffset) {
    1.24 +        TokenHierarchy tokenHierarchy = TokenHierarchy.get(doc);
    1.25 +        TokenSequence tokenSequence = tokenHierarchy.tokenSequence();
    1.26 +
    1.27 +        tokenSequence.move(caretOffset);
    1.28 +        tokenSequence.movePrevious(); tokenSequence.moveNext();
    1.29 +        while (!tokenSequence.isEmpty()) {
    1.30 +            if (tokenSequence.token().id() == TplTopTokenId.T_SMARTY_OPEN_DELIMITER) {
    1.31 +                return false;
    1.32 +            } else if (tokenSequence.token().id() == TplTopTokenId.T_SMARTY) {
    1.33 +                if (tokenSequence.token().text().toString().contains("|"))
    1.34 +                    return true;
    1.35 +            }
    1.36 +            tokenSequence.movePrevious();
    1.37 +        }
    1.38 +        return false;
    1.39 +    }
    1.40 +
    1.41 +    static String afterSmartyCommand(Document doc, int offset) {
    1.42 +        int readLength = (SCANNING_MAX_FILTER_LENGHT > offset) ? offset : SCANNING_MAX_FILTER_LENGHT;
    1.43 +        try {
    1.44 +            return getLastKeyword(doc.getText(offset - readLength, readLength), SmartyFramework.getOpenDelimiter(NbEditorUtilities.getFileObject(doc)));
    1.45 +        } catch (BadLocationException ex) {
    1.46 +            Exceptions.printStackTrace(ex);
    1.47 +        }
    1.48 +        return "";
    1.49 +    }
    1.50 +
    1.51 +    public static String getLastKeyword(String area, String openDelimiter) {
    1.52 +        int delimiterPosition = area.lastIndexOf(openDelimiter);
    1.53 +        String searchingContent = (delimiterPosition > -1) ? area.substring(delimiterPosition + openDelimiter.length()) : area;
    1.54 +        String[] keywords = searchingContent.split(" ");
    1.55 +        String lastKeyword = "";
    1.56 +        for (String string : keywords) {
    1.57 +            if (SmartyCodeCompletionOffer.getFunctionParameters().get(string) != null) {
    1.58 +                lastKeyword = string;
    1.59 +            }
    1.60 +        }
    1.61 +        return lastKeyword;
    1.62 +    }
    1.63  }
     2.1 --- a/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionItem.java	Wed Jun 16 14:44:01 2010 +0200
     2.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionItem.java	Mon Jun 21 15:26:29 2010 +0200
     2.3 @@ -74,6 +74,11 @@
     2.4          this.text = text;
     2.5      }
     2.6  
     2.7 +    protected TplCompletionItem(String text, String help) {
     2.8 +        this.text = text;
     2.9 +        this.help = help;
    2.10 +    }
    2.11 +
    2.12      protected TplCompletionItem(String text, String help, String helpUrl) {
    2.13          this(text);
    2.14          this.help = help;
    2.15 @@ -306,6 +311,26 @@
    2.16  
    2.17      }
    2.18  
    2.19 +    public static class FunctionParametersCompletionItem extends TplCompletionItem {
    2.20 +
    2.21 +        protected static final String CUSTOM_FUNC_COLOR = "D6822D";
    2.22 +
    2.23 +        public FunctionParametersCompletionItem(String value, String help) {
    2.24 +            super(value, help);
    2.25 +        }
    2.26 +
    2.27 +        @Override
    2.28 +        protected String getLeftHtmlText() {
    2.29 +            return "<font color=#" + CUSTOM_FUNC_COLOR + ">" + getItemText() + "</font>"; //NOI18N
    2.30 +        }
    2.31 +
    2.32 +        @Override
    2.33 +        public int getSortPriority() {
    2.34 +            return 18;
    2.35 +        }
    2.36 +
    2.37 +    }
    2.38 +
    2.39      public static class VariableModifiersCompletionItem extends TplCompletionItem {
    2.40  
    2.41          protected static final String ATTR_NAME_COLOR = hexColorCode(Color.blue.darker());
     3.1 --- a/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionProvider.java	Wed Jun 16 14:44:01 2010 +0200
     3.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionProvider.java	Mon Jun 21 15:26:29 2010 +0200
     3.3 @@ -40,10 +40,13 @@
     3.4   */
     3.5  package org.netbeans.modules.php.smarty.editor.completion;
     3.6  
     3.7 +import java.lang.String;
     3.8  import java.util.Collection;
     3.9  import java.util.Collections;
    3.10  import java.net.URL;
    3.11 +import java.util.ArrayList;
    3.12  import javax.swing.Action;
    3.13 +import javax.swing.text.BadLocationException;
    3.14  import javax.swing.text.Document;
    3.15  import javax.swing.text.JTextComponent;
    3.16  import org.netbeans.api.lexer.TokenHierarchy;
    3.17 @@ -80,15 +83,13 @@
    3.18          AsyncCompletionTask task = null;
    3.19          if ((queryType & COMPLETION_QUERY_TYPE & COMPLETION_ALL_QUERY_TYPE) != 0) {
    3.20              task = new AsyncCompletionTask(new Query(), component);
    3.21 -        } else if (queryType == DOCUMENTATION_QUERY_TYPE) {
    3.22 -            task = new AsyncCompletionTask(new DocQuery(null), component);
    3.23          }
    3.24          return task;
    3.25      }
    3.26  
    3.27      private static class Query extends AbstractQuery {
    3.28  
    3.29 -        private volatile Collection<? extends CompletionItem> items = Collections.<CompletionItem>emptyList();
    3.30 +        private volatile Collection<? extends CompletionItem> items = Collections.emptyList();
    3.31          private JTextComponent component;
    3.32  
    3.33          @Override
    3.34 @@ -100,10 +101,23 @@
    3.35          protected void doQuery(CompletionResultSet resultSet, Document doc, int caretOffset) {
    3.36              try {
    3.37                  TplCompletionQuery.CompletionResult result = new TplCompletionQuery(doc, caretOffset).query();
    3.38 -                if (result != null && CodeCompletionUtils.insideSmartyCode(doc, caretOffset)) {
    3.39 -                    items = result.getItems();
    3.40 -                } else {
    3.41 -                    items = Collections.emptyList();
    3.42 +                String command = ""; boolean inSmarty = false;
    3.43 +                if (CodeCompletionUtils.insideSmartyCode(doc, caretOffset)) {
    3.44 +                    if (CodeCompletionUtils.inVariableModifiers(doc, caretOffset)) {
    3.45 +                        items = result.getVariableModifiers();
    3.46 +                        inSmarty = true;
    3.47 +                    }
    3.48 +                    if (!(command = CodeCompletionUtils.afterSmartyCommand(doc, caretOffset)).equals("")) {
    3.49 +                        items = new ArrayList<CompletionItem>(result.getParamsForCommand(command));
    3.50 +                        inSmarty = true;
    3.51 +                    }
    3.52 +                    if (!inSmarty) {
    3.53 +                        if (result == null){
    3.54 +                            items = Collections.emptyList();
    3.55 +                        } else {
    3.56 +                            items = result.getFunctions();
    3.57 +                        }
    3.58 +                    }
    3.59                  }
    3.60                  resultSet.addAllItems(items);
    3.61  
    3.62 @@ -114,6 +128,14 @@
    3.63  
    3.64          @Override
    3.65          protected boolean canFilter(JTextComponent component) {
    3.66 +            try {
    3.67 +                if (component.getText(component.getCaretPosition() - 1, 1).toString().equals("|")) {
    3.68 +                    return false;
    3.69 +                }
    3.70 +            } catch (BadLocationException ex) {
    3.71 +                return false;
    3.72 +            }
    3.73 +
    3.74              String prefix = CodeCompletionUtils.getTextPrefix(component.getDocument(), component.getCaretPosition());
    3.75  
    3.76              //check the items
    3.77 @@ -142,7 +164,6 @@
    3.78              }
    3.79              resultSet.finish();
    3.80          }
    3.81 -
    3.82      }
    3.83  
    3.84      public static class DocQuery extends AbstractQuery {
    3.85 @@ -161,8 +182,8 @@
    3.86                      //based on the explicit documentation opening request
    3.87                      //(not ivoked by selecting a completion item in the list)
    3.88                      TplCompletionQuery.CompletionResult result = new TplCompletionQuery(doc, caretOffset).query();
    3.89 -                    if (result != null && result.getItems().size() > 0) {
    3.90 -                        item = result.getItems().iterator().next();
    3.91 +                    if (result != null && result.getFunctions().size() > 0) {
    3.92 +                        item = result.getFunctions().iterator().next();
    3.93                      }
    3.94                  } catch (ParseException ex) {
    3.95                      Exceptions.printStackTrace(ex);
    3.96 @@ -174,7 +195,6 @@
    3.97              }
    3.98          }
    3.99      }
   3.100 -
   3.101      private static abstract class AbstractQuery extends AsyncCompletionQuery {
   3.102  
   3.103          @Override
     4.1 --- a/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionQuery.java	Wed Jun 16 14:44:01 2010 +0200
     4.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/TplCompletionQuery.java	Mon Jun 21 15:26:29 2010 +0200
     4.3 @@ -86,19 +86,37 @@
     4.4      }
     4.5  
     4.6      private CompletionResult query(ResultIterator resultIterator) {
     4.7 -        return new CompletionResult(SmartyCodeCompletionOffer.getCCData());
     4.8 +        return new CompletionResult(SmartyCodeCompletionOffer.getFunctions(), 
     4.9 +                SmartyCodeCompletionOffer.getVariableModifiers(),
    4.10 +                SmartyCodeCompletionOffer.getFunctionParameters());
    4.11      }
    4.12  
    4.13      public static class CompletionResult {
    4.14  
    4.15 -        private Collection<? extends CompletionItem> items;
    4.16 +        private Collection<? extends CompletionItem> functions;
    4.17 +        private Collection<? extends CompletionItem> variableModifiers;
    4.18 +        private HashMap<String, Collection<? extends CompletionItem>> functionParams;
    4.19  
    4.20 -        CompletionResult(Collection<? extends CompletionItem> items) {
    4.21 -            this.items = items;
    4.22 +        CompletionResult(Collection<? extends CompletionItem> functions, Collection<? extends CompletionItem>
    4.23 +                variableModifiers, HashMap<String, Collection<? extends CompletionItem>> functionParams) {
    4.24 +            this.functions = functions;
    4.25 +            this.variableModifiers = variableModifiers;
    4.26 +            this.functionParams = functionParams;
    4.27          }
    4.28  
    4.29 -        public Collection<? extends CompletionItem> getItems() {
    4.30 -            return items;
    4.31 +        public Collection<? extends CompletionItem> getFunctions() {
    4.32 +            return functions;
    4.33          }
    4.34 +
    4.35 +        public Collection<? extends CompletionItem> getVariableModifiers() {
    4.36 +            return variableModifiers;
    4.37 +        }
    4.38 +
    4.39 +        public Collection<? extends CompletionItem> getParamsForCommand(String command) {
    4.40 +            return functionParams.get(command);
    4.41 +        }
    4.42 +
    4.43 +
    4.44 +
    4.45      }
    4.46  }
     5.1 --- a/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionEntries.java	Wed Jun 16 14:44:01 2010 +0200
     5.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionEntries.java	Mon Jun 21 15:26:29 2010 +0200
     5.3 @@ -42,9 +42,11 @@
     5.4  import java.io.InputStream;
     5.5  import java.util.ArrayList;
     5.6  import java.util.Collection;
     5.7 +import java.util.Collections;
     5.8  import javax.xml.parsers.DocumentBuilder;
     5.9  import javax.xml.parsers.DocumentBuilderFactory;
    5.10  import javax.xml.parsers.ParserConfigurationException;
    5.11 +import org.netbeans.modules.php.smarty.editor.completion.entries.CodeCompletionParamMetadata;
    5.12  import org.w3c.dom.Document;
    5.13  import org.w3c.dom.Element;
    5.14  import org.w3c.dom.Node;
    5.15 @@ -57,6 +59,7 @@
    5.16   */
    5.17  public class CodeCompletionEntries {
    5.18  
    5.19 +
    5.20      public CodeCompletionEntries() {
    5.21      }
    5.22  
    5.23 @@ -76,20 +79,23 @@
    5.24                  String desc = elem.getElementsByTagName("description").item(0).getTextContent();
    5.25                  String url = elem.getElementsByTagName("url").item(0).getTextContent();
    5.26                  String help = "";
    5.27 +                Collection<CodeCompletionParamMetadata> params = Collections.<CodeCompletionParamMetadata>emptyList();
    5.28                  NodeList attributes = elem.getElementsByTagName("attributes");
    5.29                  if (completionType.equals("built-in-functions") || completionType.equals("custom-functions")) {
    5.30 -                    help = generateHelpForBuiltInFunctions(desc, attributes);
    5.31 +                    help = generateHelpForFunctions(desc, attributes);
    5.32 +                    params = getParametersForFunction(attributes);
    5.33                  } else {
    5.34                      help = generateHelpForVariableModifiers(desc, attributes);
    5.35 +                    params = null;
    5.36                  }
    5.37 -                ccEntries.add(new CodeCompletionEntryMetadata(elem.getAttribute("name"), help, url));
    5.38 +                ccEntries.add(new CodeCompletionEntryMetadata(elem.getAttribute("name"), help, url, params));
    5.39              }
    5.40          }
    5.41  
    5.42          return ccEntries;
    5.43      }
    5.44  
    5.45 -    private static String generateHelpForBuiltInFunctions(String desc, NodeList attributesRoot) {
    5.46 +    private static String generateHelpForFunctions(String desc, NodeList attributesRoot) {
    5.47          Element parent = (Element) (attributesRoot.item(0));
    5.48          if (parent != null) {
    5.49              String help = desc + "<br><br><table border=1>"
    5.50 @@ -139,4 +145,36 @@
    5.51          help += "<td>" + attributeParams.getElementsByTagName("description").item(0).getTextContent() + "</td>";
    5.52          return help;
    5.53      }
    5.54 +
    5.55 +    private static Collection<CodeCompletionParamMetadata> getParametersForFunction(NodeList attributesRoot) {
    5.56 +        Element parent = (Element) (attributesRoot.item(0));
    5.57 +        if (parent != null) {
    5.58 +            Collection<CodeCompletionParamMetadata> params = new ArrayList<CodeCompletionParamMetadata>();
    5.59 +            NodeList attributes = parent.getChildNodes();
    5.60 +            for (int i = 0; i < attributes.getLength(); i++) {
    5.61 +                if (attributes.item(i).getNodeType() == Node.ELEMENT_NODE) {
    5.62 +                    Element attribute = (Element) attributes.item(i);
    5.63 +                    String name = attribute.getAttribute("name");
    5.64 +                    String help = generateHelpFunctionParameters(attribute);
    5.65 +                    CodeCompletionParamMetadata ccpm = new CodeCompletionParamMetadata(name, help);
    5.66 +                    assert (ccpm != null);
    5.67 +                    params.add(ccpm);
    5.68 +
    5.69 +                }
    5.70 +            }
    5.71 +            return params;
    5.72 +        } else {
    5.73 +            return null;
    5.74 +        }
    5.75 +    }
    5.76 +
    5.77 +    private static String generateHelpFunctionParameters(Element attribute) {
    5.78 +        String help = attribute.getElementsByTagName("description").item(0).getTextContent() + "<br><br><table border=1>";
    5.79 +        help += "<tr><td style=\"font-weight:bold\">Type</td><td>" + attribute.getElementsByTagName("type").item(0).getTextContent() + "</td></tr>";
    5.80 +        help += "<tr><td style=\"font-weight:bold\">Required</td><td>" + attribute.getElementsByTagName("required").item(0).getTextContent() + "</td></tr>";
    5.81 +        help += "<tr><td style=\"font-weight:bold\">Default</td><td>" + attribute.getElementsByTagName("default").item(0).getTextContent() + "</td></tr>";
    5.82 +        help += "</table>";
    5.83 +        return help;
    5.84 +    }
    5.85  }
    5.86 +
     6.1 --- a/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionEntryMetadata.java	Wed Jun 16 14:44:01 2010 +0200
     6.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionEntryMetadata.java	Mon Jun 21 15:26:29 2010 +0200
     6.3 @@ -36,8 +36,13 @@
     6.4   *
     6.5   * Portions Copyrighted 2010 Sun Microsystems, Inc.
     6.6   */
     6.7 +package org.netbeans.modules.php.smarty.editor.completion.entries;
     6.8  
     6.9 -package org.netbeans.modules.php.smarty.editor.completion.entries;
    6.10 +import java.util.ArrayList;
    6.11 +import java.util.Collection;
    6.12 +import java.util.Collections;
    6.13 +import org.netbeans.modules.php.smarty.editor.completion.TplCompletionItem.FunctionParametersCompletionItem;
    6.14 +import org.netbeans.spi.editor.completion.CompletionItem;
    6.15  
    6.16  /**
    6.17   *
    6.18 @@ -48,11 +53,20 @@
    6.19      private String keyword;
    6.20      private String help;
    6.21      private String helpUrl;
    6.22 +    private Collection<CompletionItem> params = new ArrayList<CompletionItem>();
    6.23  
    6.24 -    public CodeCompletionEntryMetadata(String keyword, String help, String helpUrl) {
    6.25 +    public CodeCompletionEntryMetadata(String keyword, String help, String helpUrl,
    6.26 +            Collection<CodeCompletionParamMetadata> params) {
    6.27          this.keyword = keyword;
    6.28          this.help = help;
    6.29          this.helpUrl = helpUrl;
    6.30 +        if (params != null) {
    6.31 +            for (CodeCompletionParamMetadata codeCompletionParamMetadata : params) {
    6.32 +                this.params.add(new FunctionParametersCompletionItem(
    6.33 +                        codeCompletionParamMetadata.getKeyword(),
    6.34 +                        codeCompletionParamMetadata.getHelp()));
    6.35 +            }
    6.36 +        }
    6.37      }
    6.38  
    6.39      public String getKeyword() {
    6.40 @@ -66,4 +80,8 @@
    6.41      public String getHelpUrl() {
    6.42          return helpUrl;
    6.43      }
    6.44 +
    6.45 +    public Collection<? extends CompletionItem> getParameters() {
    6.46 +        return params;
    6.47 +    }
    6.48  }
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/CodeCompletionParamMetadata.java	Mon Jun 21 15:26:29 2010 +0200
     7.3 @@ -0,0 +1,67 @@
     7.4 +/*
     7.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     7.6 + *
     7.7 + * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
     7.8 + *
     7.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    7.10 + * Other names may be trademarks of their respective owners.
    7.11 + *
    7.12 + * The contents of this file are subject to the terms of either the GNU
    7.13 + * General Public License Version 2 only ("GPL") or the Common
    7.14 + * Development and Distribution License("CDDL") (collectively, the
    7.15 + * "License"). You may not use this file except in compliance with the
    7.16 + * License. You can obtain a copy of the License at
    7.17 + * http://www.netbeans.org/cddl-gplv2.html
    7.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    7.19 + * specific language governing permissions and limitations under the
    7.20 + * License.  When distributing the software, include this License Header
    7.21 + * Notice in each file and include the License file at
    7.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    7.23 + * particular file as subject to the "Classpath" exception as provided
    7.24 + * by Oracle in the GPL Version 2 section of the License file that
    7.25 + * accompanied this code. If applicable, add the following below the
    7.26 + * License Header, with the fields enclosed by brackets [] replaced by
    7.27 + * your own identifying information:
    7.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    7.29 + *
    7.30 + * If you wish your version of this file to be governed by only the CDDL
    7.31 + * or only the GPL Version 2, indicate your decision by adding
    7.32 + * "[Contributor] elects to include this software in this distribution
    7.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    7.34 + * single choice of license, a recipient has the option to distribute
    7.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    7.36 + * to extend the choice of license to its licensees as provided above.
    7.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    7.38 + * Version 2 license, then the option applies only if the new code is
    7.39 + * made subject to such option by the copyright holder.
    7.40 + *
    7.41 + * Contributor(s):
    7.42 + *
    7.43 + * Portions Copyrighted 2010 Sun Microsystems, Inc.
    7.44 + */
    7.45 +
    7.46 +package org.netbeans.modules.php.smarty.editor.completion.entries;
    7.47 +
    7.48 +/**
    7.49 + *
    7.50 + * @author Martin Fousek
    7.51 + */
    7.52 +public class CodeCompletionParamMetadata {
    7.53 +
    7.54 +    protected String keyword;
    7.55 +    protected String help;
    7.56 +
    7.57 +    public CodeCompletionParamMetadata(String keyword, String help) {
    7.58 +        this.keyword = keyword;
    7.59 +        this.help = help;
    7.60 +    }
    7.61 +
    7.62 +    public String getKeyword() {
    7.63 +        return keyword;
    7.64 +    }
    7.65 +
    7.66 +    public String getHelp() {
    7.67 +        return help;
    7.68 +    }
    7.69 +
    7.70 +}
     8.1 --- a/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/SmartyCodeCompletionOffer.java	Wed Jun 16 14:44:01 2010 +0200
     8.2 +++ b/php.smarty/src/org/netbeans/modules/php/smarty/editor/completion/entries/SmartyCodeCompletionOffer.java	Mon Jun 21 15:26:29 2010 +0200
     8.3 @@ -41,10 +41,13 @@
     8.4  import java.io.InputStream;
     8.5  import java.util.ArrayList;
     8.6  import java.util.Collection;
     8.7 +import java.util.HashMap;
     8.8  import org.netbeans.modules.php.smarty.editor.completion.TplCompletionItem;
     8.9  import org.netbeans.modules.php.smarty.editor.completion.TplCompletionItem.BuiltInFunctionsCompletionItem;
    8.10  import org.netbeans.modules.php.smarty.editor.completion.TplCompletionItem.CustomFunctionsCompletionItem;
    8.11 +import org.netbeans.modules.php.smarty.editor.completion.TplCompletionItem.FunctionParametersCompletionItem;
    8.12  import org.netbeans.modules.php.smarty.editor.completion.TplCompletionItem.VariableModifiersCompletionItem;
    8.13 +import org.netbeans.spi.editor.completion.CompletionItem;
    8.14  import org.openide.util.Exceptions;
    8.15  
    8.16  /**
    8.17 @@ -53,35 +56,50 @@
    8.18   */
    8.19  public class SmartyCodeCompletionOffer {
    8.20  
    8.21 -    private final static Collection<TplCompletionItem> completionItems = new ArrayList<TplCompletionItem>();
    8.22 -    private final static String[] completionTypes = {"built-in-functions", "variable-modifiers", "custom-functions"};
    8.23 +    private final static Collection<TplCompletionItem> completionItemsFunctions = new ArrayList<TplCompletionItem>();
    8.24 +    private final static Collection<TplCompletionItem> completionItemsModifiers = new ArrayList<TplCompletionItem>();
    8.25 +    private final static HashMap<String, Collection<? extends CompletionItem>> completionItemsFunctionParams = new HashMap<String, Collection<? extends CompletionItem>>();
    8.26  
    8.27      static {
    8.28 -        loadCCData();
    8.29 +        loadFunctions(new String[]{"built-in-functions", "custom-functions"});
    8.30 +        loadModifiers("variable-modifiers");
    8.31      }
    8.32  
    8.33 -    public static Collection<TplCompletionItem> getCCData() {
    8.34 -        return completionItems;
    8.35 +    public static Collection<TplCompletionItem> getFunctions() {
    8.36 +        return completionItemsFunctions;
    8.37      }
    8.38  
    8.39 -    private static void loadCCData() {
    8.40 -        for (String completionType : completionTypes) {
    8.41 +    public static Collection<TplCompletionItem> getVariableModifiers() {
    8.42 +        return completionItemsModifiers;
    8.43 +    }
    8.44 +
    8.45 +    public static HashMap<String, Collection<? extends CompletionItem>> getFunctionParameters() {
    8.46 +        return completionItemsFunctionParams;
    8.47 +    }
    8.48 +
    8.49 +    private static void loadFunctions(String[] types) {
    8.50 +        for (String completionType : types) {
    8.51              Collection<CodeCompletionEntryMetadata> ccList = parseCCData(completionType);
    8.52 -            if (completionType.equals("built-in-functions") ) {
    8.53 +            if (completionType.equals("built-in-functions")) {
    8.54                  for (CodeCompletionEntryMetadata entryMetadata : ccList) {
    8.55 -                    completionItems.add(new BuiltInFunctionsCompletionItem(entryMetadata.getKeyword(), entryMetadata.getHelp(), entryMetadata.getHelpUrl()));
    8.56 +                    completionItemsFunctions.add(new BuiltInFunctionsCompletionItem(entryMetadata.getKeyword(), entryMetadata.getHelp(), entryMetadata.getHelpUrl()));
    8.57 +                    completionItemsFunctionParams.put(entryMetadata.getKeyword(), entryMetadata.getParameters());
    8.58                  }
    8.59 +            } else if (completionType.equals("custom-functions")) {
    8.60 +                for (CodeCompletionEntryMetadata entryMetadata : ccList) {
    8.61 +                    completionItemsFunctions.add(new CustomFunctionsCompletionItem(entryMetadata.getKeyword(), entryMetadata.getHelp(), entryMetadata.getHelpUrl()));
    8.62 +                    completionItemsFunctionParams.put(entryMetadata.getKeyword(), entryMetadata.getParameters());
    8.63 +                }
    8.64 +
    8.65              }
    8.66 -            else if (completionType.equals("custom-functions")) {
    8.67 -                for (CodeCompletionEntryMetadata entryMetadata : ccList) {
    8.68 -                    completionItems.add(new CustomFunctionsCompletionItem(entryMetadata.getKeyword(), entryMetadata.getHelp(), entryMetadata.getHelpUrl()));
    8.69 -                }
    8.70 -            }
    8.71 -            else if (completionType.equals("variable-modifiers")) {
    8.72 -                for (CodeCompletionEntryMetadata entryMetadata : ccList) {
    8.73 -                    completionItems.add(new VariableModifiersCompletionItem(entryMetadata.getKeyword(), entryMetadata.getHelp(), entryMetadata.getHelpUrl()));
    8.74 -                }
    8.75 -            }
    8.76 +        }
    8.77 +    }
    8.78 +
    8.79 +    private static void loadModifiers(String functionsFile) {
    8.80 +        Collection<CodeCompletionEntryMetadata> ccList = parseCCData(functionsFile);
    8.81 +
    8.82 +        for (CodeCompletionEntryMetadata entryMetadata : ccList) {
    8.83 +            completionItemsModifiers.add(new VariableModifiersCompletionItem(entryMetadata.getKeyword(), entryMetadata.getHelp(), entryMetadata.getHelpUrl()));
    8.84          }
    8.85      }
    8.86  
    8.87 @@ -91,8 +109,10 @@
    8.88  
    8.89          try {
    8.90              Collection<CodeCompletionEntryMetadata> ccData = CodeCompletionEntries.readAllCodeCompletionEntriesFromXML(inputStream, filePath);
    8.91 -            ccList.addAll(ccData);
    8.92 -
    8.93 +            for (CodeCompletionEntryMetadata codeCompletionEntryMetadata : ccData) {
    8.94 +                ccList.add(codeCompletionEntryMetadata);
    8.95 +            }
    8.96 +//            ccList.addAll(ccData);
    8.97          } catch (Exception ex) {
    8.98              Exceptions.printStackTrace(ex);
    8.99          }