netbeans icon indicating copy to clipboard operation
netbeans copied to clipboard

Add try-with-resources abbrv 'tryr' to Java code templates

Open jkeresman01 opened this issue 7 months ago • 4 comments

Add try-with-resources abbreviation tryr to Java code templates

This pull request introduces a new code template abbreviation tryr for Java source files in NetBeans. It expands to a try-with-resources block with the cursor positioned directly in the resource declaration.

Usage

Typing tryr and pressing Tab expands to:

try (|) {
    
} catch (Exception e) {
    e.printStackTrace();
}

jkeresman01 avatar May 18 '25 14:05 jkeresman01

Thanks!

I'll simplify the snippet to focus solely on the try (...) block and remove the catch section.
I'll also update the commit author to include my full name as required.

Typing tryr and pressing Tab will now expand to:

try (|) {
    
}

Regarding the use of ${cursor} in templates:
it appears that only the first occurrence is respected — any additional ones are ignored. So the second ${cursor} in the earlier version was unnecessary.

Let me know if any further changes are needed, or if this minimal tryr version looks good as-is.

jkeresman01 avatar May 19 '25 21:05 jkeresman01

You can have multiple inputs for code templates. Have a look at the the lkp java template:

${Type} ${obj newVarName default="obj"} = ${lkp instanceof="org.openide.util.Lookup" default="Lookup.getDefault()"}.lookup(${Type}.class);
       ${cursor}

After entering lkp and triggering expand (by default Tab) you get:

grafik

Notice, that Type is marked. Now enter the desired type and notice, that not only the input location is updates, but also the lookup value:

grafik

Now press tab, and see that the mark switches to the name of the variable (in the screenshot I already changed the name, but the marker is still present):

grafik

The third tab will bring you to the final possible modification point, the lookup base:

grafik

matthiasblaesing avatar May 21 '25 19:05 matthiasblaesing

Thanks again for the feedback

The resource expression often varies — it might involve a different constructed type than the declared one, chained method calls, or multiple resources.

For example:

try (InputStream in = new FileInputStream("file.txt")) { ... }
try (Statement stmt = connection.createStatement();
     ResultSet rs = stmt.executeQuery(...)) { ... }
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, UTF_8))) { ... }

Not sure how to support these diverse cases,

I’ve defined the template like this:

<codetemplate abbreviation="tryr" contexts="BLOCK,CASE,LABELED_STATEMENT,DO_WHILE_LOOP,ENHANCED_FOR_LOOP,FOR_LOOP,IF,WHILE_LOOP,LAMBDA_EXPRESSION" descriptionId="CT_tryr">
    <code><![CDATA[try (${Type default="AutoCloseable"} ${var newVarName default="res"} = ${rhs default="new SomeResource()"}) {
    ${selection}${cursor}
}]]></code>
</codetemplate>

That said, I’m happy to revisit the defaults or structure if there’s a more useful or consistent pattern — open to further suggestions!

Also if we were to reduce the snippet to its minimal form:

try (|) {
    
}

More complex resource declarations could still be composed by combining multiple abbreviations (e.g. tryr + newo), depending on user preferences.

try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, UTF_8))) { ... }

Anyhow let me know you thoughts.

jkeresman01 avatar May 22 '25 00:05 jkeresman01

we can match the right hand side type and add ordering:

try (${TYPE rhsType default="AutoCloseable" editable=false} ${RES newVarName default="res" ordering=2} = ${RHS rhs instanceof="AutoCloseable" default="resource" ordering=1}) {
    ${selection}${cursor}
}

It will now start on the right side and NB will offer completion items which implement AutoCloseable.

Other templates like fore can compute the type on the left hand side if the user changes something on the right, but I am not completely sure how both are linked via the template tbh.

mbien avatar May 22 '25 02:05 mbien