5

The background: I'm a dyed-in-the-wool Emacs user who dabbles in a lot of languages. Recently a Famous Engineer upbraided me for continuing to use Emacs in this day and age, and I wish to put this Famous Engineer's chiding to the test. (This Famous Engineer happens to be partial to NetBeans, but we'll let that slide for the moment. ;-) ) So I'm getting myself acquainted with Eclipse, and I want to find out just how programmable it is.

How would you go about designing a command for Eclipse that does the following to a selection of code?

  • Extend the selection so that complete lines are selected
  • Untabify the code (replace tabs with 4 spaces)
  • Insert a 4-space block at the beginning of each selected line
  • Copy the result to the clipboard, so I can paste it into a Stack Overflow answer :-)

I'm not looking for code snippets per se, but pointers to what I should be doing. Do I need to write a full-out plugin for this? Should I be looking at macro facilities? What APIs will help me out, and where are they documented (if anywhere)? Are there any examples already out there of doing this kind of ad-hoc but programmatic text manipulation in Eclipse?

For this problem, I'm looking for a solution that's as lightweight (read: easy to hack up) as possible...

animuson
  • 53,861
  • 28
  • 137
  • 147
Owen S.
  • 7,665
  • 1
  • 28
  • 44
  • So, he's famous. Is he any good? Is he famous for being a crusty, opinionated old crank? Use whatever tools work best for you, and take comfort that there are plenty of other famous engineers who use Emacs, vi what have you and scoff at IDEs. – Adam Crossland Jul 09 '10 at 20:54
  • @Adam: Don't worry, I'm secure in the knowledge of what Emacs can and can't do, and I feel no shame whatsoever in using it, this person's rant notwithstanding. I'm but a humble seeker of objective Eclipse knowledge here, so I know what the other side of the fence looks like. – Owen S. Jul 09 '10 at 21:25

2 Answers2

1

Thinking about a vanilla Eclipse installation, I think the closest you could get would be creating a formatter template for SO. There may be 3rd party plugins which give a more advanced interface though.

As far as plug-ins go however, I think this one would be pretty simple. I'd guess you could look at how the line-comment (Ctrl-/) command works to see both how to create a command and how to "extend the selection so that complete lines are selected." I would imagine "untabifying" would be some very simple string manipulation, as would inserting a 4-space block. Finally, copying to the clipboard is a pretty common Java task (see this link, or look at Toolkit.getSystemClipboard(); ). As you might guess, the real work just comes in putting together the pieces.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • Thanks for the pointer to templates – there are definitely cases where those could be useful, but they won't work here. I'll be looking in the direction of plugins, and am hunting around for where Ctrl-/ is implemented. – Owen S. Jul 15 '10 at 05:57
1

First: Go to Window > Preferences > Java > Code Style > Formatter

And set my formatting style like so:

  • Tab Policy - Spaces Only
  • Tab Size - 4

Then:

  • Select code
  • Ctrl+Shift+F for fixing tabs
  • Tab for 4 additional spaces on each line
  • Ctrl+C to copy to clipboard

I am sure that, nonetheless, emacs has some M-x stackify command that ridicules all the commands above :)

Yuval Adam
  • 161,610
  • 92
  • 305
  • 395
  • Hey, that's cheating. :-) Any idea how to do it programmatically? I'm trying to get a feel for how more complicated ad-hoc text processing plugins could be written as well. – Owen S. Jul 10 '10 at 18:17
  • Re stackify: say, that's a good idea. ;-) Here's the Emacs Lisp code I'm trying to replicate: http://pastebin.com/KDiD4DaX – Owen S. Jul 10 '10 at 18:58
  • 1
    @Owen - Eclipse has no out-of-the-box scripting capabilities. You will need to write a full-fledged plug-in for that. – Yuval Adam Jul 10 '10 at 21:14