<commit_before>package com.redhat.ceylon.eclipse.code.complete;

import static com.redhat.ceylon.eclipse.code.complete.CodeCompletions.getDescriptionFor;
import static com.redhat.ceylon.eclipse.code.complete.CodeCompletions.getTextFor;
import static com.redhat.ceylon.eclipse.code.hover.DocumentationHover.getDocumentationFor;
import static com.redhat.ceylon.eclipse.util.Indents.getDefaultLineDelimiter;
import static com.redhat.ceylon.eclipse.util.Indents.getIndent;

import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.swt.graphics.Point;

import com.redhat.ceylon.model.typechecker.model.Declaration;
import com.redhat.ceylon.model.typechecker.model.DeclarationWithProximity;
import com.redhat.ceylon.model.typechecker.model.Type;
import com.redhat.ceylon.model.typechecker.model.TypedDeclaration;
import com.redhat.ceylon.model.typechecker.model.Unit;
import com.redhat.ceylon.model.typechecker.model.Value;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.eclipse.code.parse.CeylonParseController;
import com.redhat.ceylon.eclipse.ui.CeylonResources;

class ControlStructureCompletionProposal extends CompletionProposal {
    
    static void addForProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (d instanceof Value) {
            TypedDeclaration td = (TypedDeclaration) d;
            if (td.getType()!=null && 
                    d.getUnit().isIterableType(td.getType())) {
                String elemName;
                String name = d.getName();
                if (name.length()==1) {
                    elemName = "element";
                }
                else if (name.endsWith("s")) {
                    elemName = name.substring(0, name.length()-1);
                }
                else {
                    elemName = name.substring(0, 1);
                }
                Unit unit = cpc.getLastCompilationUnit().getUnit();
                result.add(new ControlStructureCompletionProposal(offset, prefix, 
                        "for (" + elemName + " in " + getDescriptionFor(d, unit) + ")", 
                        "for (" + elemName + " in " + getTextFor(d, unit) + ") {}",
                        d, cpc));
            }
        }
    }

    static void addIfExistsProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isOptionalType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "if (exists " + getDescriptionFor(d, unit) + ")", 
                            "if (exists " + getTextFor(d, unit) + ") {}", 
                            d, cpc));
                }
            }
        }
    }

    static void addAssertExistsProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isOptionalType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "assert (exists " + getDescriptionFor(d, unit) + ")", 
                            "assert (exists " + getTextFor(d, unit) + ");", 
                            d, cpc));
                }
            }
        }
    }

    static void addIfNonemptyProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isPossiblyEmptyType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "if (nonempty " + getDescriptionFor(d, unit) + ")", 
                            "if (nonempty " + getTextFor(d, unit) + ") {}", 
                            d, cpc));
                }
            }
        }
    }
    
    static void addAssertNonemptyProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isPossiblyEmptyType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "assert (nonempty " + getDescriptionFor(d, unit) + ")", 
                            "assert (nonempty " + getTextFor(d, unit) + ");", 
                            d, cpc));
                }
            }
        }
    }
    
    static void addTryProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        v.getType().getDeclaration()
                            .inherits(d.getUnit().getObtainableDeclaration()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "try (" + getDescriptionFor(d, unit) + ")", 
                            "try (" + getTextFor(d, unit) + ") {}", 
                            d, cpc));
                }
            }
        }
    }
    
    static void addSwitchProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d, Node node, 
            IDocument doc) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        v.getType().getCaseTypes()!=null && 
                        !v.isVariable()) {
                    StringBuilder body = new StringBuilder();
                    String indent = getIndent(node, doc);
                    Unit unit = node.getUnit();
                    for (Type pt: v.getType().getCaseTypes()) {
                        body.append(indent).append("case (");
                        if (pt.getDeclaration().isAnonymous()) {
                            body.append(pt.getDeclaration().getName(unit));
                        }
                        else {
                            body.append("is ")
                                .append(pt.asSourceCodeString(unit));
                        }
                        body.append(") {}")
                            .append(getDefaultLineDelimiter(doc));
                    }
                    body.append(indent);
                    Unit u = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "switch (" + getDescriptionFor(d, u) + ")", 
                            "switch (" + getTextFor(d, u) + ")" + 
                                    getDefaultLineDelimiter(doc) + body, 
                            d, cpc));
                }
            }
        }
    }
    
    private final CeylonParseController cpc;
    private final Declaration declaration;
    
    private ControlStructureCompletionProposal(int offset, String prefix, 
            String desc, String text, Declaration dec, 
            CeylonParseController cpc) {
        super(offset, prefix, CeylonResources.MINOR_CHANGE, 
                desc, text);
        this.cpc = cpc;
        this.declaration = dec;
    }

    public String getAdditionalProposalInfo() {
        return getAdditionalProposalInfo(null);
    }

    public String getAdditionalProposalInfo(IProgressMonitor monitor) {
        return getDocumentationFor(cpc, declaration, monitor);
    }
    
    @Override
    public Point getSelection(IDocument document) {
        int loc = text.indexOf('}');
        if (loc<0) {
            loc = text.indexOf(';')+1;
        }
        return new Point(offset + loc - prefix.length(), 0);
    }
    
}<commit_msg>correctly handle value constructors and Java enums<commit_after>package com.redhat.ceylon.eclipse.code.complete;

import static com.redhat.ceylon.eclipse.code.complete.CodeCompletions.getDescriptionFor;
import static com.redhat.ceylon.eclipse.code.complete.CodeCompletions.getTextFor;
import static com.redhat.ceylon.eclipse.code.hover.DocumentationHover.getDocumentationFor;
import static com.redhat.ceylon.eclipse.util.Indents.getDefaultLineDelimiter;
import static com.redhat.ceylon.eclipse.util.Indents.getIndent;

import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.swt.graphics.Point;

import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.eclipse.code.parse.CeylonParseController;
import com.redhat.ceylon.eclipse.ui.CeylonResources;
import com.redhat.ceylon.model.typechecker.model.Declaration;
import com.redhat.ceylon.model.typechecker.model.DeclarationWithProximity;
import com.redhat.ceylon.model.typechecker.model.Type;
import com.redhat.ceylon.model.typechecker.model.TypeDeclaration;
import com.redhat.ceylon.model.typechecker.model.TypedDeclaration;
import com.redhat.ceylon.model.typechecker.model.Unit;
import com.redhat.ceylon.model.typechecker.model.Value;

class ControlStructureCompletionProposal extends CompletionProposal {
    
    static void addForProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (d instanceof Value) {
            TypedDeclaration td = (TypedDeclaration) d;
            if (td.getType()!=null && 
                    d.getUnit().isIterableType(td.getType())) {
                String elemName;
                String name = d.getName();
                if (name.length()==1) {
                    elemName = "element";
                }
                else if (name.endsWith("s")) {
                    elemName = name.substring(0, name.length()-1);
                }
                else {
                    elemName = name.substring(0, 1);
                }
                Unit unit = cpc.getLastCompilationUnit().getUnit();
                result.add(new ControlStructureCompletionProposal(offset, prefix, 
                        "for (" + elemName + " in " + getDescriptionFor(d, unit) + ")", 
                        "for (" + elemName + " in " + getTextFor(d, unit) + ") {}",
                        d, cpc));
            }
        }
    }

    static void addIfExistsProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isOptionalType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "if (exists " + getDescriptionFor(d, unit) + ")", 
                            "if (exists " + getTextFor(d, unit) + ") {}", 
                            d, cpc));
                }
            }
        }
    }

    static void addAssertExistsProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isOptionalType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "assert (exists " + getDescriptionFor(d, unit) + ")", 
                            "assert (exists " + getTextFor(d, unit) + ");", 
                            d, cpc));
                }
            }
        }
    }

    static void addIfNonemptyProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isPossiblyEmptyType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "if (nonempty " + getDescriptionFor(d, unit) + ")", 
                            "if (nonempty " + getTextFor(d, unit) + ") {}", 
                            d, cpc));
                }
            }
        }
    }
    
    static void addAssertNonemptyProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        d.getUnit().isPossiblyEmptyType(v.getType()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "assert (nonempty " + getDescriptionFor(d, unit) + ")", 
                            "assert (nonempty " + getTextFor(d, unit) + ");", 
                            d, cpc));
                }
            }
        }
    }
    
    static void addTryProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                if (v.getType()!=null &&
                        v.getType().getDeclaration()
                            .inherits(d.getUnit().getObtainableDeclaration()) && 
                        !v.isVariable()) {
                    Unit unit = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "try (" + getDescriptionFor(d, unit) + ")", 
                            "try (" + getTextFor(d, unit) + ") {}", 
                            d, cpc));
                }
            }
        }
    }
    
    static void addSwitchProposal(int offset, String prefix, 
            CeylonParseController cpc, List<ICompletionProposal> result, 
            DeclarationWithProximity dwp, Declaration d, Node node, 
            IDocument doc) {
        if (!dwp.isUnimported()) {
            if (d instanceof Value) {
                TypedDeclaration v = (TypedDeclaration) d;
                Type type = v.getType();
                if (type!=null &&
                        type.getCaseTypes()!=null && 
                        !v.isVariable()) {
                    StringBuilder body = new StringBuilder();
                    String indent = getIndent(node, doc);
                    Unit unit = node.getUnit();
                    for (Type pt: type.getCaseTypes()) {
                        body.append(indent).append("case (");
                        TypeDeclaration ctd = pt.getDeclaration();
                        if (ctd.isAnonymous()) {
                            if (!ctd.isToplevel()) {
                                TypeDeclaration td = type.getDeclaration();
                                body.append(td.getName(unit)).append('.');
                            }
                            body.append(ctd.getName(unit));
                        }
                        else {
                            body.append("is ")
                                .append(pt.asSourceCodeString(unit));
                        }
                        body.append(") {}")
                            .append(getDefaultLineDelimiter(doc));
                    }
                    body.append(indent);
                    Unit u = cpc.getLastCompilationUnit().getUnit();
                    result.add(new ControlStructureCompletionProposal(offset, prefix, 
                            "switch (" + getDescriptionFor(d, u) + ")", 
                            "switch (" + getTextFor(d, u) + ")" + 
                                    getDefaultLineDelimiter(doc) + body, 
                            d, cpc));
                }
            }
        }
    }
    
    private final CeylonParseController cpc;
    private final Declaration declaration;
    
    private ControlStructureCompletionProposal(int offset, String prefix, 
            String desc, String text, Declaration dec, 
            CeylonParseController cpc) {
        super(offset, prefix, CeylonResources.MINOR_CHANGE, 
                desc, text);
        this.cpc = cpc;
        this.declaration = dec;
    }

    public String getAdditionalProposalInfo() {
        return getAdditionalProposalInfo(null);
    }

    public String getAdditionalProposalInfo(IProgressMonitor monitor) {
        return getDocumentationFor(cpc, declaration, monitor);
    }
    
    @Override
    public Point getSelection(IDocument document) {
        int loc = text.indexOf('}');
        if (loc<0) {
            loc = text.indexOf(';')+1;
        }
        return new Point(offset + loc - prefix.length(), 0);
    }
    
}
{'fraction_non_alphanumeric': 0.10424242424242425, 'fraction_numerical': 0.0008080808080808081, 'mean_word_length': 1.590736621745388, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '837000', 'n_tokens_mistral': 5249, 'n_tokens_neox': 5006, 'n_words': 948}