This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 252949

Summary: Make the "variableFromPreviousAssignment " code templates hints work for Java
Product: editor Reporter: taringamberini
Component: Completion & TemplatesAssignee: Dusan Balek <dbalek>
Status: NEW ---    
Severity: normal CC: anicetpremier, markiewb, taringamberini
Priority: P3    
Version: 8.0.2   
Hardware: PC   
OS: Windows 7   
Issue Type: ENHANCEMENT Exception Reporter:

Description taringamberini 2015-06-12 10:45:05 UTC
REQUEST

1.
Rename the code template hints from the current name:

    variableFromPreviousAssignment 

to the new name:

    variableFromPreviousAssignmentName

2.
Make the "variableFromPreviousAssignmentName" hints:

    ${param_name variableFromPreviousAssignmentName} 	The parameter value is the closest previously assigned variable. Usually used with instanceof and default.

work for Java.



DESCRIPTION

As documented at http://wiki.netbeans.org/Java_EditorUsersGuide#How_to_use_Code_Templates
the "variableFrom..." hints:

    ${param_name variableFromPreviousAssignment } 	The parameter value is the closest previously assigned variable. Usually used with instanceof and default.
    ${param_name variableFromNextAssignmentName } 	The parameter value is the name of the closest variable assigned after the code template. Usually used with default.
    ${param_name variableFromNextAssignmentType } 	The parameter value is the type of the closest variable assigned after the code template. Usually used with default.

don't work for Java.

It seems that the variableFromPreviousAssignment is similar to the variableFromNextAssignmentName so it would be better to rename variableFromPreviousAssignment as described above at point 1 of this request. The renaming might be emphasize the lack of the variableFromPreviousAssignmentType.

The point 2 of this request would be a *very useful* enhancement, if implemented for Java Language (in PHP these hints already works), because there are lots of code templates which may be written based on the name of a previous assigned variable.



EXAMPLE: CHECK FOR NULL code template

1.
Go to Tools -> Options -> Editor -> Code Templates

2.
Select "Java" in the Language drop down menu

3.
Create a new code template with:

* abbreviation: ifn

* description: if (param_name == null) { ${cursor} }

* expanded text:

    if (${param_name variableFromPreviousAssignmentName editable=false} == null) {
      ${cursor}
    } 

4.
Then inside a block of code, which allow inserting an "if", the IDE should look for the name of the variable present on the left side of the first assignment instruction which precedes the cursor position "|": see following cases for details.



CASE 1 (just after assignment)

Given the code:

    {
        ...(some instructions)...
        billingAddress = user.getBillingAddress();
        |
        ...(some instructions)...
    }

typing ifn + [TAB] the IDE should generate:

    {
        ...(some instructions)...
        billingAddress = user.getBillingAddress();
        if (billingAddress == null) {
            |
        }
        ...(some instructions)...
    }



CASE 2 (just after assignment with variable declaration)

Given the code:

    {
        ...(some instructions)...
        Address billingAddress = user.getBillingAddress();
        |
        ...(some instructions)...
    }

typing ifn + [TAB] the IDE should generate (again):

    {
        ...(some instructions)...
        Address billingAddress = user.getBillingAddress();
        if (billingAddress == null) {
            |
        }
        ...(some instructions)...
    }



CASE 3 (far from assignment)

Given the code:

    {
        ...(some instructions)...
        billingAddress = user.getBillingAddress();
        ...(some instructions not any of which is an assignment)...
        |
        ...(some instructions)...
    }

typing ifn + [TAB] the IDE should generate:

    {
        ...(some instructions)...
        billingAddress = user.getBillingAddress();
        ...(some instructions not any of which is an assignment)...
        if (billingAddress == null) {
            |
        }
        ...(some instructions)...
    }



CASE 4 (far from assignment with variable declaration)

Given the code:

    {
        ...(some instructions)...
        Address billingAddress = user.getBillingAddress();
        ...(some instructions not any of which is an assignment)...
        |
        ...(some instructions)...
    }

typing ifn + [TAB] the IDE should generate:

    {
        ...(some instructions)...
        Address billingAddress = user.getBillingAddress();
        ...(some instructions not any of which is an assignment)...
        if (billingAddress == null) {
            |
        }
        ...(some instructions)...
    }

Thanks,
Tarin