Skip to content

Commit

Permalink
Fix for #862: filter accessor generation proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 27, 2019
1 parent 79a8f03 commit 3f98f5e
Showing 1 changed file with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -16,18 +16,20 @@
package org.codehaus.groovy.eclipse.codeassist.processors;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.codehaus.groovy.eclipse.codeassist.GroovyContentAssist;
import org.codehaus.groovy.eclipse.codeassist.ProposalUtils;
import org.codehaus.groovy.eclipse.codeassist.relevance.Relevance;
import org.codehaus.groovy.eclipse.codeassist.requestor.ContentAssistContext;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.codeassist.impl.AssistOptions;
import org.eclipse.jdt.internal.core.SearchableEnvironment;
import org.eclipse.jdt.internal.corext.codemanipulation.GetterSetterUtil;
import org.eclipse.jdt.internal.ui.text.java.GetterSetterCompletionProposal;
Expand All @@ -42,34 +44,36 @@ public GetSetMethodCompletionProcessor(ContentAssistContext context, JavaContent

@Override
public List<ICompletionProposal> generateProposals(IProgressMonitor monitor) {
List<ICompletionProposal> proposals = new LinkedList<>();
List<ICompletionProposal> proposals = new ArrayList<>();

ContentAssistContext context = getContext();
IType enclosingType = context.getEnclosingType();
if (enclosingType != null) {
AssistOptions options = new AssistOptions(getJavaContext().getProject().getOptions(true));
int length = context.completionExpression.length(), offset = context.completionLocation - length;
try {
for (IField field : enclosingType.getFields()) {
proposals.addAll(createProposal(field, context));
if (field.getSourceRange().getLength() > 0 && ProposalUtils.matches(context.completionExpression,
MetaClassHelper.capitalize(field.getElementName()), options.camelCaseMatch, options.substringMatch)) {

IMethod getter = GetterSetterUtil.getGetter(field);
if (getter == null || !getter.exists()) {
proposals.add(new GetterSetterCompletionProposal(field, offset, length, true, Relevance.HIGH.getRelevance()));
}

if (!Flags.isFinal(field.getFlags())) {
IMethod setter = GetterSetterUtil.getSetter(field);
if (setter == null || !setter.exists()) {
proposals.add(new GetterSetterCompletionProposal(field, offset, length, false, Relevance.HIGH.getRelevance()));
}
}
}
}
} catch (JavaModelException e) {
GroovyContentAssist.logError("Exception looking for proposal providers in " + context.unit.getElementName(), e);
}
}
return proposals;
}

private List<ICompletionProposal> createProposal(IField field, ContentAssistContext context) throws JavaModelException {
List<ICompletionProposal> proposals = new ArrayList<>(2);
int relevance = Relevance.HIGH.getRelevance();
IMethod getter = GetterSetterUtil.getGetter(field);
if (getter == null || !getter.exists()) {
proposals.add(new GetterSetterCompletionProposal(field, context.completionLocation -
context.completionExpression.length(), context.completionExpression.length(), true, relevance));
}
IMethod setter = GetterSetterUtil.getSetter(field);
if ((field.getFlags() & Flags.AccFinal) == 0 && (setter == null || !setter.exists())) {
proposals.add(new GetterSetterCompletionProposal(field, context.completionLocation -
context.completionExpression.length(), context.completionExpression.length(), false, relevance));
}
return proposals;
}
}

0 comments on commit 3f98f5e

Please sign in to comment.