Example usage for org.eclipse.jdt.core NamingConventions suggestVariableNames

List of usage examples for org.eclipse.jdt.core NamingConventions suggestVariableNames

Introduction

In this page you can find the example usage for org.eclipse.jdt.core NamingConventions suggestVariableNames.

Prototype

public static String[] suggestVariableNames(int variableKind, int baseNameKind, String baseName,
        IJavaProject javaProject, int dim, String[] excluded, boolean evaluateDefault) 

Source Link

Document

Suggests names for a variable.

Usage

From source file:org.codehaus.groovy.eclipse.codeassist.processors.NewFieldCompletionProcessor.java

License:Apache License

public List<ICompletionProposal> generateProposals(IProgressMonitor monitor) {
    ContentAssistContext context = getContext();
    List<String> unimplementedFieldNames = getAllSuggestedFieldNames(context);
    List<ICompletionProposal> proposals = new LinkedList<ICompletionProposal>();
    IType enclosingType = context.getEnclosingType();
    if (enclosingType != null) {
        for (String fieldName : unimplementedFieldNames) {
            proposals.add(createProposal(fieldName, context, enclosingType));
        }/*from   ww w .  ja  va  2 s  . c  o  m*/
        // next check to see if we are at a partially completed field
        // declaration,
        // ie- is the type specified, but the name is not (or is only
        // partially specified)?
        NameAndLocation nameAndLocation = findCompletionTypeName(context.unit, context.completionLocation);
        if (nameAndLocation != null) {
            String typeName = nameAndLocation.toTypeName();
            if (typeName.equals("def")) {
                typeName = "value";
            }
            String[] suggestedNames = NamingConventions.suggestVariableNames(
                    NamingConventions.VK_INSTANCE_FIELD, InternalNamingConventions.BK_SIMPLE_TYPE_NAME,
                    typeName, context.unit.getJavaProject(), nameAndLocation.dims(), null, true);
            if (suggestedNames != null) {
                for (String suggestedName : suggestedNames) {
                    if (suggestedName.startsWith(context.completionExpression)) {
                        proposals.add(createProposal(suggestedName, nameAndLocation.name, context,
                                enclosingType, false, true, nameAndLocation.location,
                                context.completionLocation - nameAndLocation.location));
                    }
                }
            }
        }
    }

    return proposals;
}

From source file:org.codehaus.groovy.eclipse.refactoring.core.convert.AssignStatementToNewLocalRefactoring.java

License:Apache License

private TextEdit createEdit(IDocument doc, Expression expression) {
    TextEdit edit = new MultiTextEdit();

    String candidate;//from ww w. ja  va  2  s. c  o  m
    if (expression instanceof ConstantExpression) {
        candidate = ((ConstantExpression) expression).getText();
    } else if (expression instanceof VariableExpression) {
        candidate = ((VariableExpression) expression).getName();
    } else if (expression instanceof ClassExpression) {
        candidate = ((ClassExpression) expression).getType().getNameWithoutPackage();
    } else if (expression instanceof MethodCallExpression) {
        candidate = ((MethodCallExpression) expression).getMethodAsString();
    } else if (expression instanceof StaticMethodCallExpression) {
        candidate = ((StaticMethodCallExpression) expression).getMethod();
    } else if (expression instanceof MapExpression) {
        candidate = "map";
    } else if (expression instanceof ListExpression) {
        candidate = "list";
    } else {
        candidate = "temp";
    }

    Set<Variable> vars = ASTTools.getVariablesInScope(unit.getModuleNode(), expression);
    String[] variableNames = new String[vars.size()];
    int i = 0;
    for (Variable v : vars) {
        variableNames[i] = v.getName();
        i++;
    }

    String[] names = NamingConventions.suggestVariableNames(NamingConventions.VK_LOCAL,
            NamingConventions.BK_NAME, candidate, null, 0, variableNames, true);

    edit.addChild(new InsertEdit(expression.getStart(), "def " + names[0] + " = "));
    // add 4 for "def ".
    newOffset = expression.getStart() + 4;
    newLength = names[0].length();

    return edit;
}

From source file:org.codehaus.groovy.eclipse.refactoring.core.extract.ExtractGroovyConstantRefactoring.java

License:Apache License

@Override
public String[] guessConstantNames() {
    String text = getBaseNameFromExpression(getCu().getJavaProject(), getSelectedFragment(),
            NamingConventions.VK_STATIC_FINAL_FIELD);
    try {//from  www . jav a2  s.  co  m
        Integer.parseInt(text);
        text = "_" + text;
    } catch (NumberFormatException e) {
        // ignore
    }
    return NamingConventions.suggestVariableNames(NamingConventions.VK_STATIC_FINAL_FIELD,
            NamingConventions.BK_NAME, text, getCu().getJavaProject(),
            getContainingClassNode().isArray() ? 1 : 0, null, true);
}

From source file:org.codehaus.groovy.eclipse.refactoring.core.extract.ExtractGroovyLocalRefactoring.java

License:Apache License

public String[] guessLocalNames() {
    String text = getBaseNameFromExpression(getSelectedFragment());
    return NamingConventions.suggestVariableNames(NamingConventions.VK_LOCAL, NamingConventions.BK_NAME, text,
            unit.getJavaProject(), 0, null, true);
}

From source file:org.eclipse.xtext.common.types.xtext.ui.JdtVariableCompletions.java

License:Open Source License

public String[] getVariableProposals(String simpleTypeName, boolean isPlural, EObject ctx, VariableType varType,
        Set<String> excludedNames) {
    if (!org.eclipse.xtext.util.Strings.isEmpty(simpleTypeName)) {
        IJavaProject javaProject = null;
        if (ctx != null && ctx.eResource() != null && ctx.eResource().getResourceSet() != null)
            javaProject = javaProjectProvider.getJavaProject(ctx.eResource().getResourceSet());
        return NamingConventions.suggestVariableNames(getVariableKind(varType), NamingConventions.BK_TYPE_NAME,
                simpleTypeName, javaProject, isPlural ? 1 : 0,
                excludedNames.toArray(new String[excludedNames.size()]), false);
    }/*from  w  w  w.j  ava2s  . c  o m*/
    return new String[0];
}