Dynamically add new properties to .properties files

I have tried to add a new property to a .properties file, but I get stuck when trying to create a new Property using the PropertyImpl contructor. It takes in a ASTNode, and from the PsiViewer I can see that it's an "Element(Properties:PROPERTY)". However, I didn't see any ASTNode's that directly correspond to a "Property" like many of the other available options.

What I have tried is (applying this in a quick fix):


If this is not the right direction to take, is there a factory for doing something along these lines? I didn't see the ability to do such using the PsiElementFactory.

5 comments
Comment actions Permalink

You probably use PropertiesElementFactory. I found this with
ctrlaltshiftn "createProperty". CtrlAltShiftN is the #1 best way
to find something in IDEA plugin API.

Mike McCullough wrote:

I have tried to add a new property to a .properties file, but I get stuck when trying to create a new Property using the PropertyImpl contructor. It takes in a ASTNode, and from the PsiViewer I can see that it's an "Element(Properties:PROPERTY)". However, I didn't see any ASTNode's that directly correspond to a "Property" like many of the other available options.

What I have tried is (applying this in a quick fix):

 public final void applyFix(Project project, ProblemDescriptor descriptor) {
>     ASTNode node = new DummyHolderElement(); // Didn't see one for a property
>     try {
>         PropertyImpl property = new PropertyImpl(node);
>         property.setName("Check");
>         property.setValue("1-2");
>         propertiesFile.addProperty(property);
>     } catch (IncorrectOperationException exception) {
>         exception.printStackTrace();
> }]]>


If this is not the right direction to take, is there a factory for doing something along these lines? I didn't see the ability to do such using the PsiElementFactory.

0
Comment actions Permalink

Hi,

I tried using PropertiesElementFactory but I couldn't find it.
Is it part of the Open API 5.0?

Thanks,
Roy

0
Comment actions Permalink

Hello Roy,

RN> I tried using PropertiesElementFactory but I couldn't find it. Is it
RN> part of the Open API 5.0?

The class is in idea.jar. In 5.0 it was not officially documented, and in
6.0 it's considered "Extended API" (API is supported, but no source code
is provided).

--
Dmitry Jemerov
Software Developer
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks for the prompt reply Dmitry,

My problem was that idea.jar wasn't in my classpath for some reason.

Is there any way to determine the location of the new property in the file?
addAfter() doesn't work here and I have to use addProperty() instead.

Thanks again,
Roy

0
Comment actions Permalink

Hello Roy,

RN> Is there any way to determine the location of the new property in
RN> the file? addAfter() doesn't work here and I have to use
RN> addProperty() instead.

Looks like it's not currently possible. Please file a JIRA request to add
this to the API.

--
Dmitry Jemerov
Software Developer
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.