Bug 21825 - Add ability for templates to wrap existing code [templates]
Summary: Add ability for templates to wrap existing code [templates]
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 2.0   Edit
Hardware: Other other
: P3 enhancement (vote)
Target Milestone: 3.2   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: investigate
: 7213 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-07-23 15:01 EDT by Scott Rutledge CLA
Modified: 2006-01-20 12:29 EST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Scott Rutledge CLA 2002-07-23 15:01:38 EDT
There are a few refactor operations that involve 'wrapping' an existing bit of code. For example, 
making a block of code conditional. Rather than coming up with explicit refactoring actions, it 
would be possible to do these if Code Templates were expanded slightly: the ability to send 
existing text into the template, through some variable (eg. ${selection}). These templates 
would be invoked by using code-assist when a block of text is selected.

Eg.

Template: 
if
if (${condition}) {
   ${selection}
}

Code:
...
obj.callMethod(param); 
[select line, ctrl+space, 'if']
...

Selecting the given line of code and invoking code-
assist would (among other options) provide a list of wrapping templates. If the 'if' template is 
selected, the selected code is passed into the template, which is then evaluated as normal.
Comment 1 Dirk Baeumer CLA 2002-07-26 09:40:38 EDT
This is a good suggestion. We should consider this for 2.1
Comment 2 Claude Knaus CLA 2002-08-14 05:28:24 EDT
I like the idea. Will investigate if this can be easily done.
Comment 3 Claude Knaus CLA 2002-08-15 12:12:15 EDT
implemented >= 20020815

Select a range and then hit Ctrl-Space for code assist. The templates with 
${cursor} are used to wrap code. The assumption is made that ${cursor} is
usually used inside some kind of block. The main issue is that all the templates
with the ${cursor} variable are shown, since there's no longer a "completion" 
(prefix matching).
Comment 4 Claude Knaus CLA 2002-08-15 12:20:24 EDT
changed resolution to fixed
Comment 5 Scott Rutledge CLA 2002-08-15 12:29:23 EDT
Excellent! Can't wait to try it out. Thanks!
Comment 6 Scott Rutledge CLA 2002-08-22 22:53:49 EDT
Finally got a new build to try this out with. Very nice. But I do see the issue regarding the missing 
completion. Is it possible to catch key events while the codeassist popup is displayed and use 
them to perform the completion? Typing while the popup is displayed usually works in this fashion 
(allowing you to refine the prefix).
Comment 7 Claude Knaus CLA 2002-08-23 03:59:06 EDT
The problem is that it's not possible in a generic way since it conflicts with 
ordinary code assist. In case of code assist w/o templates, you want to 
overwrite the selection. After all, it's supposed to be a lightweight dialog
and not introduce a new editor behaviour. It would be possible to 
just 'remember' the old selection and use that as input, but then the user 
would be too suprised/scared that the selected text was lost.

I agree that the current solution with listing up all the templates with 
${cursor} is not elegant. I'm open to more suggestions.
Comment 8 Johan Compagner CLA 2002-08-29 10:12:55 EDT
i think remembering it is oan option. And a user knows what he is doing if he 
first selects it and then press CTRL-SPACE.

but if i do this:

str = str.subtring(2);

and then:

for[cursor] str = str.substring(2);

and i select from cursor to the end. I see all the templates with a cursor. 
If this could be different (only templates with a cursor that starts with what 
is before the cursor) then the completion list is in my eyes good enough.

but now i select for from the list in the above example and i get this:

for for(int i=xx ect)

so double time "for"

johan

P.S. isn't this bug a dublicate of 7213??

Comment 9 Claude Knaus CLA 2002-08-30 08:01:30 EDT
*** Bug 7213 has been marked as a duplicate of this bug. ***
Comment 10 Scott Rutledge CLA 2002-08-31 10:33:01 EDT
I vote for Johan's idea: use the text _before_ the selection as the completion. This at least gives 
the user a chance to narrow the options. It should still have a decent work flow. Whereas before I 
would do:

type 'if', code assist, select text, cut, paste

now it becomes:

type 'if', 
select text, code assist
Comment 11 Claude Knaus CLA 2002-09-10 11:18:02 EDT
partially implemented >= 20020910 (not in integration build)

I changed the behaviour such that keywords in front of the selections are 
respected, but the selection is not remembered (some implementation issues).
Please check if this is already useful...
Comment 12 Johan Compagner CLA 2002-09-11 08:33:30 EDT
what do you mean with: Selection is not remembered?

It works fine now just as i want (i think)

type: if[select everyting i want][CTRL-SPACE] only things that starts with if 
are there and i select one. And it does it's work.

good job
Comment 13 Claude Knaus CLA 2002-09-11 09:15:44 EDT
What I meant was that when you start typing, the selection is gone.

For example:

  fo<selection-begin>System.out.println();<selection-end>

If I want to refine 'fo' to 'for', then the selection is lost.
But if the current behaviour is sufficient, the more happy I am :)
Comment 14 Scott Rutledge CLA 2002-09-11 10:21:55 EDT
I agree, this works great as is. Thanks!
Comment 15 vin CLA 2002-09-11 12:29:48 EDT
I tried this feature.

It worked the first time. I got two options for "while" when enclosing code 
with "while"

I tried the first "while" it worked.

Subsequently I tried the second while and it gave an error dialog box about 
collision of something (I don't remember the wording).

After that I am not able to bring up wrapping help when I highlight and press 
ctl-space.

As if the feature went dead.

The code I was playing with is very simple:

public class test1 {

	public static void main(String[] args) {
		
		String p;
		
		if 
		p = "";
		p = "";		
		
	}
}

I would have liked to give a more precise feedback. However, the feature just 
doesnot work anymore for me.

I think being a new feature it may not be stable yet.


Comment 16 Scott Rutledge CLA 2002-09-11 12:44:11 EDT
This is probably related to a new regession: bug #23425
Comment 17 vin CLA 2002-09-11 12:58:19 EDT
I was hoping that when I wrap code, say, with "while", that I still should be 
allowed to populate the template parameters of while template as I normally do.

	while (enumeration.hasMoreElements()) {
		type element = (type) enumeration.nextElement();
		
	}

The ability to tab and fill the name of the variable (enumeration) and type 
above were lost.


Comment 18 Claude Knaus CLA 2002-09-12 04:06:43 EDT
will look into it
Comment 19 Claude Knaus CLA 2002-09-12 11:47:35 EDT
fixed 23425 >= 20020912
Comment 20 Andrew Utkin CLA 2003-02-13 09:59:18 EST
It seems stop working on M5. Code assist popup contains keywords (if, 
for, ... ) proposals only.
Comment 21 Andrew Utkin CLA 2003-03-27 12:30:34 EST
This nice feature is not working! (from M5 to 200303071024).
Code assist popup for selection contains keywords (if, for, ... ) proposals 
only, but not templates proposals. Is this feature disabled now?
Comment 22 Adam Kiezun CLA 2003-03-27 12:35:04 EST
works for me
what's your test case?
(note that you have to select full lines)
Comment 23 Andrew Utkin CLA 2003-03-27 12:46:15 EST
hm...
if[selection-start]System.out.println();[selection-end][cursor-here][eol]
press CTRL-SPACE
popup contains:
- if
- IftpClient - sun.net.ftp 
and so on...

selecting "if" do nothing.
there are no templates like
- if - if statement
- ifelse - if else statement
Comment 24 Adam Kiezun CLA 2003-03-27 12:50:29 EST
>(note that you have to select full lines)
Comment 25 Andrew Utkin CLA 2003-03-27 13:08:38 EST
What is "full lines"?
My selection area is from letter 'S' to end of line.
Right before i try this sample for M4 - it works fine.
Comment 26 Adam Kiezun CLA 2003-03-27 13:11:26 EST
Kai, fyi
Comment 27 Adam Kiezun CLA 2003-03-27 13:12:41 EST
Kai, fyi
Comment 28 Tom Hofmann CLA 2006-01-20 12:29:33 EST
I believe this works as expected - please reopen if you disagree.