<commit_before>package de.peeeq.eclipsewurstplugin.editor.autocomplete;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ContextInformation;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.graphics.Image;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import de.peeeq.eclipsewurstplugin.WurstConstants;
import de.peeeq.eclipsewurstplugin.editor.WurstEditor;
import de.peeeq.eclipsewurstplugin.editor.outline.Icons;
import de.peeeq.wurstscript.ast.AstElement;
import de.peeeq.wurstscript.ast.CompilationUnit;
import de.peeeq.wurstscript.ast.ExprMemberVar;
import de.peeeq.wurstscript.ast.ExtensionFuncDef;
import de.peeeq.wurstscript.ast.FunctionDefinition;
import de.peeeq.wurstscript.ast.NameDef;
import de.peeeq.wurstscript.ast.WParameter;
import de.peeeq.wurstscript.ast.WScope;
import de.peeeq.wurstscript.types.PscriptType;
import de.peeeq.wurstscript.types.PscriptTypeNamedScope;
import de.peeeq.wurstscript.utils.Utils;

public class WurstCompletionProcessor implements IContentAssistProcessor {

	private final WurstEditor editor;
	private int offset;
	private String alreadyEntered;
	private String errorMessage;
	private IContextInformationValidator validator;

	public WurstCompletionProcessor(WurstEditor editor) {
		this.editor = editor;
		this.validator = new WurstContextInformationValidator();
	}
	
	
	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		return new char[] {'.'};
	}
	
	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		this.offset = offset;
		CompilationUnit cu = editor.reconcile();
		if (Utils.isEmptyCU(cu)) {
			errorMessage = "Could not parse file.";
			System.out.println("cu is empty ...");
			return null;
		}
		
		
		alreadyEntered = getAlreadyEnteredText(viewer, offset);
		System.out.println("already entered = " + alreadyEntered);
		
		List<ICompletionProposal> completions = Lists.newArrayList();
		
		
		
		AstElement elem =  Utils.getAstElementAtPos(cu, offset);
		System.out.println("get completions at " + Utils.printElement(elem));
		
		if (elem instanceof ExprMemberVar) {
			ExprMemberVar e = (ExprMemberVar) elem;
			PscriptType leftType = e.getLeft().attrTyp();
			if (leftType instanceof PscriptTypeNamedScope) {
				PscriptTypeNamedScope ns = (PscriptTypeNamedScope) leftType;
				Multimap<String, NameDef> visibleNames = ns.getDef().attrVisibleNamesPrivate();
				completionsAddVisibleNames(alreadyEntered, completions, visibleNames);
			}
			
			// add member vars
			WScope scope = elem.attrNearestScope();
			while (scope != null) {
				Multimap<String, NameDef> visibleNames = scope.attrVisibleNamesPrivate();
				
				completionsAddVisibleExtensionFunctions(alreadyEntered, completions, visibleNames, leftType);
				scope = scope.attrNextScope();
			}
		} else {
			WScope scope = elem.attrNearestScope();
			while (scope != null) {
				Multimap<String, NameDef> visibleNames = scope.attrVisibleNamesPrivate();
				completionsAddVisibleNames(alreadyEntered, completions, visibleNames);
				scope = scope.attrNextScope();
			}
		}
		if (completions.size() > 0) {
			return completions.toArray(new ICompletionProposal[completions.size()]);
		}
		errorMessage = null;
		return null;
	}

	
	private String getAlreadyEnteredText(ITextViewer viewer, int offset) {
		try {<commit_msg>fixed autocomplete for real numbers<commit_after>package de.peeeq.eclipsewurstplugin.editor.autocomplete;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map.Entry;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.CompletionProposal;
import org.eclipse.jface.text.contentassist.ContextInformation;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.contentassist.IContextInformationValidator;
import org.eclipse.swt.graphics.Image;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import de.peeeq.eclipsewurstplugin.WurstConstants;
import de.peeeq.eclipsewurstplugin.editor.WurstEditor;
import de.peeeq.eclipsewurstplugin.editor.outline.Icons;
import de.peeeq.wurstscript.ast.AstElement;
import de.peeeq.wurstscript.ast.CompilationUnit;
import de.peeeq.wurstscript.ast.ExprMemberVar;
import de.peeeq.wurstscript.ast.ExprRealVal;
import de.peeeq.wurstscript.ast.ExtensionFuncDef;
import de.peeeq.wurstscript.ast.FunctionDefinition;
import de.peeeq.wurstscript.ast.NameDef;
import de.peeeq.wurstscript.ast.WParameter;
import de.peeeq.wurstscript.ast.WScope;
import de.peeeq.wurstscript.types.PscriptType;
import de.peeeq.wurstscript.types.PscriptTypeNamedScope;
import de.peeeq.wurstscript.utils.Utils;

public class WurstCompletionProcessor implements IContentAssistProcessor {

	private final WurstEditor editor;
	private int offset;
	private String alreadyEntered;
	private String errorMessage;
	private IContextInformationValidator validator;

	public WurstCompletionProcessor(WurstEditor editor) {
		this.editor = editor;
		this.validator = new WurstContextInformationValidator();
	}
	
	
	@Override
	public char[] getCompletionProposalAutoActivationCharacters() {
		return new char[] {'.'};
	}
	
	@Override
	public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
		this.offset = offset;
		CompilationUnit cu = editor.reconcile();
		if (Utils.isEmptyCU(cu)) {
			errorMessage = "Could not parse file.";
			System.out.println("cu is empty ...");
			return null;
		}
		
		
		alreadyEntered = getAlreadyEnteredText(viewer, offset);
		System.out.println("already entered = " + alreadyEntered);
		
		List<ICompletionProposal> completions = Lists.newArrayList();
		
		
		
		AstElement elem =  Utils.getAstElementAtPos(cu, offset);
		System.out.println("get completions at " + Utils.printElement(elem));
		
		if (elem instanceof ExprMemberVar) {
			ExprMemberVar e = (ExprMemberVar) elem;
			PscriptType leftType = e.getLeft().attrTyp();
			if (leftType instanceof PscriptTypeNamedScope) {
				PscriptTypeNamedScope ns = (PscriptTypeNamedScope) leftType;
				Multimap<String, NameDef> visibleNames = ns.getDef().attrVisibleNamesPrivate();
				completionsAddVisibleNames(alreadyEntered, completions, visibleNames);
			}
			
			// add member vars
			WScope scope = elem.attrNearestScope();
			while (scope != null) {
				Multimap<String, NameDef> visibleNames = scope.attrVisibleNamesPrivate();
				
				completionsAddVisibleExtensionFunctions(alreadyEntered, completions, visibleNames, leftType);
				scope = scope.attrNextScope();
			}
		} else if (elem instanceof ExprRealVal) {
			// show now hints for reals
			return null;
		} else {
			WScope scope = elem.attrNearestScope();
			while (scope != null) {
				Multimap<String, NameDef> visibleNames = scope.attrVisibleNamesPrivate();
				completionsAddVisibleNames(alreadyEntered, completions, visibleNames);
				scope = scope.attrNextScope();
			}
		}
		if (completions.size() > 0) {
			return completions.toArray(new ICompletionProposal[completions.size()]);
		}
		errorMessage = null;
		return null;
	}

	
	private String getAlreadyEnteredText(ITextViewer viewer, int offset) {
		try {
{'fraction_non_alphanumeric': 0.10069965017491254, 'fraction_numerical': 0.0002498750624687656, 'mean_word_length': 5.373407643312102, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '264953', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2502, 'n_words': 452}