Defined a limit for code completion list entries PHPeclipsePlugin.MAX_PROPOSALS
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / BuiltInEngine.java
index 5b43224..950765f 100644 (file)
@@ -11,6 +11,7 @@ import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContext
 import net.sourceforge.phpdt.internal.corext.template.php.PHPUnitContext;
 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
 import net.sourceforge.phpdt.internal.ui.text.java.IPHPCompletionProposal;
+import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
 import org.eclipse.jface.text.BadLocationException;
 import org.eclipse.jface.text.IDocument;
@@ -22,81 +23,85 @@ import org.eclipse.swt.graphics.Point;
 
 public class BuiltInEngine {
 
-       /** The context type. */
-       private ContextType fContextType;
-       /** The result proposals. */
-       private ArrayList fProposals= new ArrayList();
+  /** The context type. */
+  private ContextType fContextType;
+  /** The result proposals. */
+  private ArrayList fProposals = new ArrayList();
 
-       /**
-        * Creates the template engine for a particular context type.
-        * See <code>TemplateContext</code> for supported context types.
-        */
-       public BuiltInEngine(ContextType contextType) {
-       //      Assert.isNotNull(contextType);
-               fContextType= contextType;
-       }
+  /**
+   * Creates the template engine for a particular context type.
+   * See <code>TemplateContext</code> for supported context types.
+   */
+  public BuiltInEngine(ContextType contextType) {
+    // Assert.isNotNull(contextType);
+    fContextType = contextType;
+  }
 
-       /**
-        * Empties the collector.
-        * 
-        * @param viewer the text viewer  
-        * @param unit   the compilation unit (may be <code>null</code>)
-        */
-       public void reset() {
-               fProposals.clear();
-       }
+  /**
+   * Empties the collector.
+   * 
+   * @param viewer the text viewer  
+   * @param unit   the compilation unit (may be <code>null</code>)
+   */
+  public void reset() {
+    fProposals.clear();
+  }
 
-       /**
-        * Returns the array of matching templates.
-        */
-       public IPHPCompletionProposal[] getResults() {
-               return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
-       }
+  /**
+   * Returns the array of matching templates.
+   */
+  public IPHPCompletionProposal[] getResults() {
+    return (IPHPCompletionProposal[]) fProposals.toArray(new IPHPCompletionProposal[fProposals.size()]);
+  }
 
-       /**
-        * Inspects the context of the compilation unit around <code>completionPosition</code>
-        * and feeds the collector with proposals.
-        * @param viewer the text viewer
-        * @param completionPosition the context position in the document of the text viewer
-        * @param compilationUnit the compilation unit (may be <code>null</code>)
-        */
-       public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers)
+  /**
+   * Inspects the context of the compilation unit around <code>completionPosition</code>
+   * and feeds the collector with proposals.
+   * @param viewer the text viewer
+   * @param completionPosition the context position in the document of the text viewer
+   * @param compilationUnit the compilation unit (may be <code>null</code>)
+   */
+  public void complete(ITextViewer viewer, int completionPosition, Object[] identifiers)
   //,ICompilationUnit compilationUnit)
-       //hrows JavaModelException
-       {
-           IDocument document= viewer.getDocument();
-           
-               // prohibit recursion
-//             if (LinkedPositionManager.hasActiveManager(document))
-//                     return;
+  //hrows JavaModelException
+  {
+    IDocument document = viewer.getDocument();
 
-               if (!(fContextType instanceof CompilationUnitContextType))
-                       return;
-    Point selection= viewer.getSelectedRange();
+    // prohibit recursion
+    //         if (LinkedPositionManager.hasActiveManager(document))
+    //                 return;
+
+    if (!(fContextType instanceof CompilationUnitContextType))
+      return;
+    Point selection = viewer.getSelectedRange();
     // remember selected text
-    String selectedText= null;
+    String selectedText = null;
     if (selection.y != 0) {
       try {
-        selectedText= document.get(selection.x, selection.y);
-      } catch (BadLocationException e) {}
+        selectedText = document.get(selection.x, selection.y);
+      } catch (BadLocationException e) {
+      }
     }
-    
-    ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y);//mpilationUnit);
 
-               PHPUnitContext context= (PHPUnitContext) fContextType.createContext();
-               int start= context.getStart();
-               int end= context.getEnd();
-               IRegion region= new Region(start, end - start);
+    ((CompilationUnitContextType) fContextType).setContextParameters(document, completionPosition, selection.y); //mpilationUnit);
 
-//             Template[] templates= Templates.getInstance().getTemplates();
+    PHPUnitContext context = (PHPUnitContext) fContextType.createContext();
+    int start = context.getStart();
+    int end = context.getEnd();
+    IRegion region = new Region(start, end - start);
+
+    //         Template[] templates= Templates.getInstance().getTemplates();
     String identifier = null;
-               for (int i= 0; i != identifiers.length; i++) {
+    int maxProposals = PHPeclipsePlugin.MAX_PROPOSALS;
+    for (int i = 0; i != identifiers.length; i++) {
       identifier = (String) identifiers[i];
-                       if (context.canEvaluate(identifier)) {
-                               fProposals.add(new BuiltInProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_BUILTIN))); 
+      if (context.canEvaluate(identifier)) {
+        if (maxProposals-- < 0) {
+          return;
+        }
+        fProposals.add(new BuiltInProposal(identifier, context, region, viewer, PHPUiImages.get(PHPUiImages.IMG_BUILTIN)));
       }
     }
-       }
+  }
 
 }
-