mapstruct / mapstruct-idea

An IntelliJ IDEA plugin for working with MapStruct
Other
142 stars 38 forks source link

Support for completion and Go To Definition within Mapping#qualifiedByName - for shared configurations #136

Closed philipp-paland closed 1 year ago

philipp-paland commented 1 year ago

I love the feature described in #107 - it is extremely helpful. I would like it very much if it also worked in shared configuration classes as described here:

https://mapstruct.org/documentation/stable/reference/html/#shared-configurations

thunderhook commented 1 year ago

This is already supported and covered with the following test (unwrapOptional is referenced in a used mapper from a shared configuration): https://github.com/thunderhook/mapstruct-idea/blob/b55c1e12fad45b90abb88fa3b0740b3c3a75b4ba/src/test/java/org/mapstruct/intellij/completion/MappingQualifiedByNameCompletionTestCase.java#L54-L54

If it doesn't work for you, would you mind making a minimal reproducible example?

Oh... I see - we identify named methods from methods of referenced mappers (uses = ...) of a shared configuration. But we ignore the methods inside the shared configuration itself, like:


@MapperConfig(uses = OptionalMapper.class)
public class MapperConfigUsingOptionalMapper {

    @Named("thisIsCurrentlyNotCovered")
    public int doSomething(long value) {
        return 0;
    }

}

I will take care of it, thanks for pointing this out. If your use case is something else, feel free to provide your code.

philipp-paland commented 1 year ago

In my case it looks like this:

@Mapper
public interface MyOptionalMapper {

  @Named("stringLength")
  default int stringLength(String str) {
    return str.length();
  }
}
@MapperConfig(uses = MyOptionalMapper.class)
public interface MyMapperConfig {

  @Mapping(target = "targetInt", source = "sourceString", qualifiedByName = "stringLength")
  TargetBean doSomething(SourceBean value);
}
@Mapper(config = MyMapperConfig.class)
public interface MyMapperUsingConfig {

  @InheritConfiguration
  TargetBean doSomething(SourceBean value);
}

I can't navigate to "qualifiedByName = "stringLength"" in the IDE.

I have mapstruct 1.5.4, Intellij 2023.1, MapStruct Support 1.5.0

thunderhook commented 1 year ago

Thank you for the reproducal. The PR #137 handles references inside a @MapperConfig now.

Oh... I see - we identify named methods from methods of referenced mappers (uses = ...) of a shared configuration. But we ignore the methods inside the shared configuration itself, like:

@MapperConfig(uses = OptionalMapper.class)
public class MapperConfigUsingOptionalMapper {

    @Named("thisIsCurrentlyNotCovered")
    public int doSomething(long value) {
        return 0;
    }

}

@Named methods inside a shared configuration itself are not supported by MapStruct itself, therefore I did not handle this case. I don't know if this is by design or a not-yet implemented feature (or bug).

filiphr commented 1 year ago

Thanks for working on this @thunderhook.

@Named methods inside a shared configuration itself are not supported by MapStruct itself, therefore I did not handle this case. I don't know if this is by design or a not-yet implemented feature (or bug).

This is by design. Methods in a @MapperConfig are only template methods, we are not using them to invoke any code.

philipp-paland commented 1 year ago

It works, thank you :)