X

Geertjan's Blog

  • August 11, 2006

Never Type System.out.println Again!

Geertjan Wielenga
Product Manager
OK, so in the IDE, when you type "sout" and then press Space, you get an empty System.out.println, exactly like this:

System.out.println("");

That's nice but what I want is to be able to select a word and then have this appear below the line in which the word is found:

System.out.println("word: " + word);

Yes, this is exactly what the keyboard shortcut of the week (see yesterday's blog entry) does for System.err.println. So, using Sandip's excellent LineOperations code (LineOperations.java) as my starting point, here's the InjectSout module for NetBeans IDE 5.5 Beta 2:

org-netbeans-modules-injectsout.nbm

How it works is that you select a word in a Java file. Any word. Not necessarily a variable or a field or anything like that. It could be anything at all, because the assumption is that you're smart enough to know which words make sense for this functionality. Then you press Ctrl-F1. (By the way, if there's a conflict between shortcuts provided by different modules, I wonder what happens..?) If no word is selected, you get a message telling you to select a word. If a word is selected, it is put in a new line below the current line (with a line above and below, which, if you don't like them, you can delete as always with Ctrl-E) within a System.out.println statement as described above. The status bar also shows the line that is added. If more than one word is selected, the same thing happens, again under the assumption that you're smart enough to only select the appropriate parts of the code. The Ctrl-F1 shortcut only works in Java files, which also makes sense.

Note: After you use the shortcut, you need to save the file, otherwise you can't use the shortcut again. Don't know why this is the case, something with locking or cursors maybe. (But not a big problem, just do Ctrl-S). The code that is generated is not formatted. You need to do that yourself. (Not a problem, use Ctrl-Shift-F.) Something else is that I don't know where the shortcut is found in the Keymap, so don't know where you'd need to go to change the shortcut to something different.

So remember: if it isn't working, save the file and try again.

The module could very easily be extended for JOptionPane, for example. Or maybe a palette could be added with statements such as these for 'injection' into the code. I guess I'm not using the word 'inject' correctly, or at least not as used in the Java EE 5 specification, but on the other hand I don't mind not being Java EE 5 compliant, in this instance...

Join the discussion

Comments ( 5 )
  • Michel Graciano Friday, August 11, 2006
    Hi,
    just adding a new idea, I use the different approach for cases when I need insert some "words" inside a code snippet. For example, using your example, I use the 'Code Template Tools', by Sandip. With the follow code snippet:
    System.out.println( ${selection editable=false} )
    I select the text to be written by System.out. I define a shortcut for the 'Show Templates' action, as 'Ctrl + Alt + S', and all works fine. A tip, use _ in the template name beginning, this will be placed at list top.
    Regards
  • Ahmed Mohombe Friday, August 11, 2006
    Hi,



    IntelliJ has the concept of Live Templates.

    that does the thing you mentioned and many more. See the documentation for
    output live templates.



    These live templates are very powerfull and much more intelligent, and IMHO they would be a good addition to Netbeans too.

    Just look at the "soutv" live template from the above link, that does what your "word" does, but in a more intelligent way.



    Ahmed.
  • Robert Cooper Friday, August 11, 2006
    You may have just inflicted one of the greatest evils in the history of man.
    Seriously, how about you change System.out.println to Logger.getAnonymousLogger().finest()?
  • Geertjan Saturday, August 12, 2006
    Firstly: Ahmed, what Sandip has provided, Michel refers to it in his comment, is essentially (from how I understand it) the equivalent of IntelliJ's live templates. But I still haven't tried out Sandip's module (i.e, the Code Template Tools), but I keep meaning to do so. It's just that I prefer creating my own solution to using someone else's... :-) (Plus, you should take a look at the previous blog entry's comments, someone there discovered some IDE functionality that they say is similar to IntelliJ's live templates. By the way, thanks for the tip about the name of the template,Michel. It works! Very useful little tip.

    Secondly: Robert, Robert, Robert... "one of the greatest evils in the history of man"? Wow. :-) Granted using System.out.println isn't the best thing. Still, this is more for being able to do it than anything else. Sure, I'll change it to proper logger code. Or something different. But it's pretty cool to be able to programatically add code to the IDE's Java editor at all!

  • Robert Cooper Sunday, August 13, 2006
    Well, my jihad against S.o.p is a long running thing, especially when you start to see uncontrollable catalina.out files from webapps :P
    Overstated? Yes, but once you see a filesystem crumble under a 3 gig log file, you start to take the matter seriously :)
    It is a pretty cool feature, I guess my problem with "code I write all the time" is not so much a one-line long something, but being able to re-template refactorings: making getters and setters automatically support PropertyChangeSupport, for instance. I had not looked at the Code Template Tools. That might be closer to what I might want to do, but it still seems to lack code knowledge -- like types... for instance, being able to turn:
    this.total = total;
    into:
    int old = this.total;
    this.total = total;
    changes.firePropertyChange("total", old, total);
    would be a hughe time saver, but you still need to know the "type" of this.total when you start the change.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.