Bug 160771 - Custom Code Templates and Code Completion
Custom Code Templates and Code Completion
Product: editor
Classification: Unclassified
Component: Completion & Templates
All All
: P2 with 2 votes (vote)
: 7.4
Assigned To: Dusan Balek
: 163428 166441 212176 (view as bug list)
Depends on:
Blocks: 232287
  Show dependency treegraph
Reported: 2009-03-20 21:38 UTC by tog
Modified: 2013-07-04 21:33 UTC (History)
5 users (show)

See Also:

Proposed SPI change (3.36 KB, patch)
2013-04-29 09:38 UTC, Dusan Balek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description tog 2009-03-20 21:38:24 UTC
Dear NetBeans Team!

My background:
I seriously would like to switch from Eclipse to NB as I am interested in building upon the NB Platform. Currently I am 
working with MyEclipse. As ever when someone using Eclipse comes accross NB he will claim that coding in the NB editor 
is painful. One of these pains IMHO is this issue for which I could not find any solution on the web. Please please 
please address this as soon as possible :-)

The issue:

NB code templates at /Tools/Options/Editor/Code Templates gives use predefined templates and the option for creating 
custom ones. The issue arises when using custom templates in the editor. Please reproduce as follows:

1. Create a new java class
2. Add a public static void main(String[] args){} method
3. Within the method body type "for" and invoke the code completion "Ctrl+Space"

What we get are proposals which take the predefined "forx"-templates into account. Great so far so good. Now reproduce 
as follows:

1. Open /Tools/Options/Editor/Code Templates
2. Set Language to Java
3. Click New
4. Type "mytemplate" as abbreviation
5. Click OK
6. Type "This is my template text" in the Expanded Text tab
7. Click OK at the bottom of the window
8. Back in the editor delete the typed "for" from the method body
9. Type "mytemplate" and invoke the code completion "Ctrl+Space"

What we get is "No suggestions". Sigh :-( 

What I expected was the same behaviour as for the built-in templates. For me it also does not make much sens to have a 
hugh stack of custom code templates and have to remember each and every abbreviation to be able to expand it with TAB 
or whatever. Because please reporduce as follows:

1. Delete the typed "mytemplate" from the method body
2. Retype "mytemplate" and press "TAB"

What we get is "This is my template text". This shows us that the IDE is perfectly aware of the custom code templates 
but for some reason the code completion is not.

I file this as an ENHANCEMENT but actually consider it a DEFECT from an Eclipse point of view. I chose P2 as there is a 
workaround but I consider it difficult and impractical to learn all my custom abbreviations off by heart or use a cheat 
sheet to get along with the expansion key scenario.

It would be very great if you addressed this as soon as possible and make code completion also aware of custom code 

Looking forward to seeing this fixed :-)

Yours sincerly Thomas
(A guy who would really love to use NB)
Comment 1 Vitezslav Stejskal 2009-03-23 17:19:46 UTC
Thanks for your feedback!
Comment 2 Vitezslav Stejskal 2009-04-24 12:45:42 UTC
*** Issue 163428 has been marked as a duplicate of this issue. ***
Comment 3 Vitezslav Stejskal 2009-06-03 08:44:18 UTC
*** Issue 166441 has been marked as a duplicate of this issue. ***
Comment 4 jurib 2012-10-11 10:54:39 UTC
I want to switch from Eclipse to NetBeans as well, but I find it particularly difficult to learn that custom code templates require another key(combination) than code completion. Even if this seems negligible, it is quite a cognitive effort I have to take every time thinking on which function I have to use now, where before I just pressed the same keys for completion as well as templates. 

My suggestion is to be able to enhance the completion feature by including the defined code templates, giving a list of options when different possibilities are available. This would ease using (and switching to) NetBeans for Eclipse users to a really great extent.
Comment 5 Dusan Balek 2013-04-29 09:36:56 UTC
CodeTemplateCompletionProvider shows only those code templates in the code completion list that have their contexts specified. The contexts are unique identifiers used by the infrastructure (CodeTemplateFilters) to filter out code templates that are not suitable for the editor context, where a user types. The actual identifiers are defined by each particular language (mime type) and can be different for different languages. The language defines contexts for its constructs such as loops, methods, classes, if-else blocks, etc. and than tags each code template available for that language with a context, where it is meaningful to apply the template.

To implement the customizer that allows for uses to specify the contexts for their custom code templates we need the infrastructure (CodeTemplateFilters) to specify all the possible context values valid for the given language (supported by the particular CodeTemplateFilter). To that purpose CodeTemplateFilter.ContextBasedFactory interface with the getSupportedContexts() method is proposed as an extension to the existing SPI.
Comment 6 Dusan Balek 2013-04-29 09:38:27 UTC
Created attachment 133919 [details]
Proposed SPI change
Comment 7 Dusan Balek 2013-05-03 06:35:21 UTC
Fixed in jet-main.

Comment 8 Quality Engineering 2013-05-05 02:29:55 UTC
Integrated into 'main-golden', will be available in build *201305042300* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/86f9686c1afc
User: Dusan Balek <dbalek@netbeans.org>
Log: Issue #160771: Custom Code Templates and Code Completion - fixed.
Comment 9 Dusan Balek 2013-06-20 16:09:46 UTC
*** Bug 212176 has been marked as a duplicate of this bug. ***
Comment 10 athompson 2013-07-04 21:33:58 UTC
Very nice! The next logical step is to allow code templates with the same name so long as the contexts don't overlap. Created issue #232287.

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo