Skip to content

Commit

Permalink
Fixes bndtools#5975. Changes in refactoring UI
Browse files Browse the repository at this point in the history
---
 Signed-off-by: Peter Kriens <[email protected]>

Signed-off-by: Peter Kriens <[email protected]>
  • Loading branch information
pkriens committed Jan 18, 2024
1 parent de54cac commit ab9d1a2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@
@Component
public class ComponentRefactorer extends BaseRefactorer implements IQuickFixProcessor {

public final RE BIND_METHOD_P = g(g("prefix", or("add", "set")), g("service", setAll));
public final RE UNBIND_METHOD_P = g(g("prefix", or("remove", "unset")), g("service", setAll));
public final RE ACTIVATE_METHOD_P = g(or("activate", "start", "init", "initialize", "onActivate",
public static final RE BIND_METHOD_P = g(g("prefix", or("add", "set")), g("service", setAll));
public static final RE UNBIND_METHOD_P = g(g("prefix", or("remove", "unset")), g("service", setAll));
public static final RE ACTIVATE_METHOD_P = g(or("activate", "start", "init", "initialize", "onActivate",
"onStart", "begin", "doActivate", "create", "setup", "ready", "load"), setAll);
public final RE DEACTIVATE_METHOD_P = g(or("deactivate", "stop", "close", "finish", "dispose",
public static final RE DEACTIVATE_METHOD_P = g(or("deactivate", "stop", "close", "finish", "dispose",
"shutdown", "onDeactivate", "onStop", "end", "doDeactivate", "release", "teardown", "cleanup"), setAll);
public final String COMPONENT_A = "org.osgi.service.component.annotations.Component";
public final String REFERENCE_A = "org.osgi.service.component.annotations.Reference";
public final String ACTIVATE_A = "org.osgi.service.component.annotations.Activate";
public final String DEACTIVATE_A = "org.osgi.service.component.annotations.Deactivate";
public final String BUNDLECONTEXT_T = "org.osgi.framework.BundleContext";
public final String SERVICEREFERENCE_T = "org.osgi.framework.ServiceReference";
public final String MAP_T = "java.util.Map";
public final Set<String> NOT_REFERENCE = Set.of(BUNDLECONTEXT_T, SERVICEREFERENCE_T, MAP_T);
public final static int BASE_LEVEL = 2000;
public static final String COMPONENT_A = "org.osgi.service.component.annotations.Component";
public static final String REFERENCE_A = "org.osgi.service.component.annotations.Reference";
public static final String ACTIVATE_A = "org.osgi.service.component.annotations.Activate";
public static final String DEACTIVATE_A = "org.osgi.service.component.annotations.Deactivate";
public static final String BUNDLECONTEXT_T = "org.osgi.framework.BundleContext";
public static final String SERVICEREFERENCE_T = "org.osgi.framework.ServiceReference";
public static final String MAP_T = "java.util.Map";
public static final Set<String> NOT_REFERENCE = Set.of(BUNDLECONTEXT_T, SERVICEREFERENCE_T, MAP_T);
public static final int BASE_LEVEL = 2000;

class MethodState extends DomainBase<MethodDeclaration> {
final String annotation;
Expand Down Expand Up @@ -227,13 +227,15 @@ public void addCompletions(ProposalBuilder builder, RefactorAssistant assistant,
root.isJavaSourceType(JavaSourceType.CLASS)
.upTo(TypeDeclaration.class)
.forEach((ass, typeDeclaration) -> {

int relevance = root.getNode()
.map(selected -> 2 - ass.getDistance(selected, typeDeclaration, 1000))
.orElse(-1);

if (ass.hasAnnotation(typeDeclaration, COMPONENT_A)) {
ComponentState cs = new ComponentState(ass.cursor(typeDeclaration), builder);

builder.build("comp-", "Remove @Component", "component", 3, cs::remove);
if (relevance >= 1)
builder.build("comp-", "Remove @Component", "component", 3, cs::remove);

root.upTo(VariableDeclarationFragment.class)
.isNotPrimitive()
Expand Down Expand Up @@ -263,8 +265,9 @@ public void addCompletions(ProposalBuilder builder, RefactorAssistant assistant,
.forEach((x, md) -> cs.proposeDeactivate(md));

} else {
builder.build("comp+", "Add @Component", "component", relevance,
() -> ass.ensureAnnotation(typeDeclaration, ass.newAnnotation(COMPONENT_A)));
if (relevance >= 1)
builder.build("comp+", "Add @Component", "component", relevance,
() -> ass.ensureAnnotation(typeDeclaration, ass.newAnnotation(COMPONENT_A)));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,21 @@ public void add(Command command) {
public void addCompletions(ProposalBuilder builder, RefactorAssistant assistant, Cursor<?> root,
IInvocationContext context) {


root = root.isJavaSourceType(JavaSourceType.CLASS);

GogoState state = root.upTo(TypeDeclaration.class)
.anyOfTheseAnnotations(ComponentRefactorer.COMPONENT_A)
.processSingletons(GogoState::new)
.stream()
.findAny()
.orElse(null);

if (state != null) {

doScope(builder, state);
root.upTo(TypeDeclaration.class, 1)
.forEach((ass, n) -> {
doScope(builder, state);
});
if (state.isPresent()) {
root.upTo(SingleVariableDeclaration.class, 4)
.parentType(MethodDeclaration.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ public RefactorAssistant(CompilationUnit unit, @Nullable
engine = Memoize.supplier(() -> new ASTEngine(unit));
source = iunit == null ? null : iunit.getSource();
this.iunit = iunit;
init();
}

/**
Expand All @@ -1039,6 +1040,7 @@ public RefactorAssistant(ICompilationUnit iunit) throws JavaModelException {
});
this.iunit = iunit;
this.source = iunit.getSource();
init();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ class Foo {
public static String this_before = """
class Foo {
Foo( int x){
super(x);
this(x);
}
}
""";
public static String this_after = """
class Foo {
final int x;
Foo( int x){
super(x);
this(x);
this.x=x;
}
}
Expand Down

0 comments on commit ab9d1a2

Please sign in to comment.