Hi Tomas,
  thanks for your insights.

I am creating the ClasspathInfo object with a maven project root:
            final ClassIndex classIndex =
ClasspathInfo.create(mvnPrj.getProjectDirectory()).getClassIndex();
where mvnPrj is a org.netbeans.modules.maven.NbMavenProjectImpl casted from
a org.netbeans.api.project.Project passed to the constructor of a class
annotated with @ProjectServiceProvider for maven project types.

I then query packages with:
        Set<String> packageNames = classIndex.getPackageNames(filter, true,
EnumSet.allOf(ClassIndex.SearchScope.class));
where filter is a user typed text string, and later in the code for classes
in a given package with:
            Set<ElementHandle<TypeElement>> types =
classIndex.getDeclaredTypes(typeFilter,
ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX, Collections.singleton(new
SinglePackageScope(packageFilter)));
where typeFilter and packageFilter are substrings of user typed string.

To have more context the plugin project is on GitHub at
https://github.com/AlexFalappa/nb-springboot , the snippets above are
from here:
https://github.com/AlexFalappa/nb-springboot/blob/5a5867946a0c0d89a243b91559d163f43f0015dc/src/main/java/com/github/alexfalappa/nbspringboot/projects/service/impl/SpringBootServiceImpl.java#L252-L257
and here:
https://github.com/AlexFalappa/nb-springboot/blob/5a5867946a0c0d89a243b91559d163f43f0015dc/src/main/java/com/github/alexfalappa/nbspringboot/projects/service/impl/LoggerNameHintProvider.java#L57-L79

It seems I am doing everything right, yet I noticed getting back results
from all the maven projects currently opened in the IDE. I will double
check later at home.

Your explanation is already valuable however.

Regards,
Alex

Il giorno gio 14 nov 2019 alle ore 16:10 Tomas Zezula <
[email protected]> ha scritto:

> Hi,
> it depends on how you created the ClasspathInfo.
> If the ClasspathInfo was created by `ClasspathInfo.create(FileObject)` or
> `ClasspathInfo.create(File)`
> the returned types (packages) are the types (packages) from the project
> where the `FileObject` comes from and
> projects the project depends on.
> If you need only the types (packages) from the project itself you can use
> SearchScope.SOURCE like:
>
> ci.getDeclaredTypes(“Foo”, NameKind.SIMPLE_NAME,
> Collections.singleton(SearchScopeType.SOURCE))
>
> When you need to search in a specific set of roots you can always create a
> custom ClasspathInfo to search just in given roots
> using `ClasspathInfo.create(ClassPath, ClassPath, ClassPath)`.
> The classpath for required roots can be constructed using
> `ClassPathSupport.createClassPath`.
>
> — Tomas
>
> > On 11 Nov 2019, at 14:27, Alessandro <[email protected]> wrote:
> >
> > Hi all,
> >  In a plugin I am writing I would like to complete a user text looking
> for
> > package an class names of the maven java project the edited file belongs
> > to. I am using the ClassIndex obtained via ClasspathInfo but it helps me
> > complete package and class names coming from all opened projects.
> >
> > Is there a way to limit the class index search to a single project?
> >
> > Thank you in advance.
> >
> > Regards,
> > Alex
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
> For further information about the NetBeans mailing lists, visit:
> https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
>
>
>
>

Reply via email to