Select python facet for a module

Answered

Hi,

I would like to automatically add a python facet to a module from my plugin. I can do this through the UI but I am unable to do so through the plugin. I am aware of FacetManager class and its addFacet but how do I get the facet to add in the first place? Essentially, I am looking for a way to get all the facets available in the project (the facets shown in the image below).

 

1
4 comments

Hi,

Please try using FacetTypeRegistry.getInstance().getFacetTypes().

1

Thanks Karol, this is what I was looking for! Just to confirm, is this the correct way of using this API:

var pythonFacetType = FacetTypeRegistry.getInstance()
                                       .findFacetType("Python");
var facetModel = FacetManager.getInstance(module)
                             .createModifiableModel();

if (pythonFacetType != null && facetModel.getFacetByType(pythonFacetType.getId()) == null) {
    var pythonFacet = pythonFacetType.createFacet(
        module,
        "My Python Facet",
        pythonFacetType.createDefaultConfiguration(),
        null
                                                 );

    facetModel.addFacet(pythonFacet);
    Runnable r = facetModel::commit;
    executeRunnable(r);
}

Also, 

  1. Is it possible to configure which Python SDK is to be used by this facet from the plugin itself?
  2. I noticed that on adding this facet, python interpreter is added to the module but not the corresponding python interpreter library. I can add the library separately but is there something missing in my code that is causing this because when you add python facet from the IDE UI, the library is added automatically.

Thanks,

Aman

0

Hi,

You can use PythonFacetType.getInstance(), so you don’t need to hardcode the “Python” ID.

To set SDK, try pythonFacet.setSdk(<required SDK>), see:
https://github.com/JetBrains/intellij-community/blob/master/python/pluginJava/com/jetbrains/python/facet/PythonFacetType.java#L44-L47

1

Perfect! Thanks for your help.

0

Please sign in to leave a comment.