package org.fife.rsta.ac.js.ast.jsType;

import java.util.ArrayList;
import java.util.Iterator;

import org.fife.rsta.ac.js.Logger;
import org.fife.rsta.ac.js.SourceCompletionProvider;
import org.fife.rsta.ac.js.ast.type.TypeDeclaration;
import org.fife.rsta.ac.js.completion.JSCompletion;
import org.fife.ui.autocomplete.FunctionCompletion;


public class JSR223Type extends JavaScriptType {

	public JSR223Type(TypeDeclaration type) {
		super(type);
	}


	/**
	 * @param completionLookup
	 * @return JSCompletion using lookup name
	 * @see JSCompletion
	 */
	protected JSCompletion _getCompletion(String completionLookup,
			SourceCompletionProvider provider) {
		JSCompletion completion = (JSCompletion) typeCompletions
				.get(completionLookup);
		if (completion != null) {
			return completion;
		}
		// else
		if (completionLookup.indexOf('(') != -1) {
			// must be a function, so compare function strings
			// get a list of best fit methods
			Logger.log("Completion Lookup : " + completionLookup);
			JavaScriptFunctionType javaScriptFunctionType = JavaScriptFunctionType
					.parseFunction(completionLookup, provider);

			JSCompletion[] matches = getPotentialLookupList(javaScriptFunctionType
					.getName());

			// iterate through types and check best fit parameters
			int bestFitIndex = -1;
			int bestFitWeight = -1;
			for (int i = 0; i < matches.length; i++) {
				Logger.log("Potential match : " + matches[i].getLookupName());
				JavaScriptFunctionType matchFunctionType = JavaScriptFunctionType
						.parseFunction(matches[i].getLookupName());
				Logger.log("Matching against completion: " + completionLookup);
				int weight = matchFunctionType.compare(javaScriptFunctionType,
						provider);
				Logger.log("Weight: " + weight);
				if (weight < JavaScriptFunctionType.CONVERSION_NONE
						&& (weight < bestFitWeight || bestFitIndex == -1)) {
					bestFitIndex = i;
					bestFitWeight = weight;
				}
			}
			if (bestFitIndex > -1) {
				Logger
						.log("BEST FIT: "
								+ matches[bestFitIndex].getLookupName());
				return matches[bestFitIndex];
			}
		}

		return null;
	}


	// get a list of all potential method matches
	private JSCompletion[] getPotentialLookupList(String name) {
		ArrayList completionMatches = new ArrayList();

		for (Iterator i = typeCompletions.keySet().iterator(); i.hasNext();) {
			String key = (String) i.next();
			if (key.startsWith(name)) {
				JSCompletion completion = (JSCompletion) typeCompletions
						.get(key);
				if (completion instanceof FunctionCompletion) {
					completionMatches.add(completion);
				}
			}
		}

		return (JSCompletion[]) completionMatches
				.toArray(new JSCompletion[completionMatches.size()]);
	}

}

@@ -92,16 +92,39 @@
 erator;%0D
+%0Aimport java.util.Map;%0D
 %0A%0D%0Aimpor
@@ -1398,16 +1398,73 @@
  = -1;%0D%0A
+%09%09%09Logger.log(%22Potential matches : %22 + matches.length);%0D%0A
 %09%09%09for (
@@ -2260,14 +2260,77 @@
 %0D%0A%0D%0A
-%0D%0A
+%09private JSCompletion%5B%5D getPotentialLookupList(String name)%0D%0A%09%7B%0D%0A%09
 %09//
- 
 get 
@@ -2358,46 +2358,309 @@
 al m
-ethod matches%0D%0A%09private JSCompletion%5B%5D
+atches, including extended%0D%0A%09%09ArrayList completionMatches = new ArrayList();%0D%0A%09%09getPotentialLookupList(name, completionMatches, this);%0D%0A%09%09return (JSCompletion%5B%5D) completionMatches.toArray(new JSCompletion%5BcompletionMatches.size()%5D);%0D%0A%09%7D%0D%0A%0D%0A%09// get a list of all potential method matches%0D%0A%09private void
  get
@@ -2690,23 +2690,18 @@
 ing name
-) %7B%0D%0A%09%09
+, 
 ArrayLis
@@ -2719,37 +2719,95 @@
 nMatches
- = new ArrayList()
+, JavaScriptType type) %7B%0D%0A%09%09%0D%0A%09%09Map typeCompletions = type.typeCompletions
 ;%0D%0A
+%09%09
 %0D%0A%09%09for 
@@ -3138,112 +3138,308 @@
 %09%7D%0D%0A
+%09%09
 %0D%0A%09%09
-return (JSCompletion%5B%5D) completionMatches%0D%0A%09%09%09%09.toArray(new JSCompletion%5BcompletionMatches.size()%5D);
+//loop through extended and add it's methods too recursively%0D%0A%09%09for(Iterator extended = type.getExtendedClasses().iterator(); extended.hasNext();) %7B%0D%0A%09%09%09JavaScriptType extendedType = (JavaScriptType) extended.next();%0D%0A%09%09%09getPotentialLookupList(name, completionMatches, extendedType);%0D%0A%09%09%7D%0D%0A%0D%0A%09%09
 %0D%0A%09%7D

Fix bug for looking for potential function matches within JavaScript types. Was not checking extended types/functions