Improved CodeCompletion with information from the project.index file.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / template / DeclarationEngine.java
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/template/DeclarationEngine.java
new file mode 100644 (file)
index 0000000..56e309e
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * (c) Copyright IBM Corp. 2000, 2001.
+ * All Rights Reserved.
+ */
+package net.sourceforge.phpdt.internal.ui.text.template;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.SortedMap;
+
+import net.sourceforge.phpdt.internal.corext.template.ContextType;
+import net.sourceforge.phpdt.internal.corext.template.php.CompilationUnitContextType;
+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 org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.Region;
+import org.eclipse.swt.graphics.Point;
+//import org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager;
+
+public class DeclarationEngine {
+
+  /** 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 DeclarationEngine(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();
+  }
+
+  /**
+   * 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, SortedMap map)
+  //,ICompilationUnit compilationUnit)
+  //hrows JavaModelException
+  {
+    IDocument document = viewer.getDocument();
+
+    if (!(fContextType instanceof CompilationUnitContextType))
+      return;
+
+    Point selection = viewer.getSelectedRange();
+    // remember selected text
+    String selectedText = null;
+    if (selection.y != 0) {
+      try {
+        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();
+               String prefix = context.getKey();
+    IRegion region = new Region(start, end - start);
+
+    String identifier = null;
+    
+    SortedMap subMap = map.subMap(prefix,prefix+'\255');
+    Iterator iter = subMap.keySet().iterator();
+
+    while (iter.hasNext()) {
+      identifier = (String) iter.next();
+      if (context.canEvaluate(identifier)) {
+        fProposals.add(
+          new DeclarationProposal(
+            identifier,
+            context,
+            region,
+            viewer,
+            PHPUiImages.get(PHPUiImages.IMG_FUN),
+            PHPUiImages.get(PHPUiImages.IMG_VAR)));
+      }
+    }
+
+  }
+
+}