Bug 63838 - [misc] highlight control statement when break/continue selected [mark occurrences]
Summary: [misc] highlight control statement when break/continue selected [mark occurre...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 3.2 M3   Edit
Assignee: Dani Megert CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-05-25 09:19 EDT by Adam Kiezun CLA
Modified: 2005-11-01 06:56 EST (History)
3 users (show)

See Also:


Attachments
patch (6.46 KB, patch)
2005-08-03 22:39 EDT, Adam Kiezun CLA
no flags Details | Diff
patch against jdt ui (3.1) (6.57 KB, patch)
2005-08-10 00:35 EDT, Adam Kiezun CLA
no flags Details | Diff
tests - patch against jdt.text.tests (11.54 KB, patch)
2005-08-10 00:41 EDT, Adam Kiezun CLA
no flags Details | Diff
Shows the current result (3.71 KB, image/png)
2005-08-19 09:58 EDT, Dani Megert CLA
no flags Details
patch - next cut (8.44 KB, patch)
2005-08-25 00:54 EDT, Adam Kiezun CLA
no flags Details | Diff
tests - patch against jdt.text.tests (10.40 KB, patch)
2005-08-25 00:54 EDT, Adam Kiezun CLA
no flags Details | Diff
jdt ui patch (13.96 KB, patch)
2005-10-05 10:45 EDT, Adam Kiezun CLA
no flags Details | Diff
and the tests (11.30 KB, patch)
2005-10-05 10:46 EDT, Adam Kiezun CLA
no flags Details | Diff
full version (20.35 KB, patch)
2005-10-06 11:48 EDT, Adam Kiezun CLA
no flags Details | Diff
the tests (13.85 KB, patch)
2005-10-06 11:48 EDT, Adam Kiezun CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Adam Kiezun CLA 2004-05-25 09:19:34 EDT
3.0m9

it's pretty confusing sometimes what a 'continue' or 'break' refers to. it would
be cool to have the control statement highlighted when i select the 'continue'
or 'break'. it'd also take labels into account.
Comment 1 Dirk Baeumer CLA 2004-05-25 13:40:36 EDT
Not for 3.0.
Comment 2 Adam Kiezun CLA 2004-05-25 13:48:28 EDT
sure. just came to my head and i entered before i'd forget.
Comment 3 Ilja Preuss CLA 2004-08-11 07:39:38 EDT
Sounds like a good idea!
Comment 4 Dirk Baeumer CLA 2004-09-16 08:38:01 EDT
Daniel, are you taking care of this ?
Comment 5 Dani Megert CLA 2004-11-23 03:07:30 EST
see also bug 79238.
Comment 6 Adam Kiezun CLA 2005-08-02 16:34:51 EDT
any chance for this? :) It'd come really handy sometimes.
Comment 7 Dani Megert CLA 2005-08-03 11:16:31 EDT
Hey Adam, what about sending in a patch ;-)
Comment 8 Adam Kiezun CLA 2005-08-03 11:20:16 EDT
can look at it but not before september 9th (ICSE deadline) :)
Comment 9 Adam Kiezun CLA 2005-08-03 22:19:51 EDT
labels will be a problem. see bug 33739
Comment 10 Adam Kiezun CLA 2005-08-03 22:39:07 EDT
Created attachment 25654 [details]
patch

here's the patch. Currently, all *Finders select whole AST nodes so this one
does too. It looks a little strange sometimes. I did not add a preference for
it. I can, if you decide to incorporate this. (we might want to recode *Finders
to return just source positions instead of ASTNodes to allow finer grain
marking - in this case you really just want to select the 'for/do/while/switch'
keyword or the label, rather than the whole statement)
Comment 11 Adam Kiezun CLA 2005-08-03 22:40:05 EDT
patch is against jdt.ui 3.1
Comment 12 Adam Kiezun CLA 2005-08-05 09:09:10 EDT
Dani, do you have test cases for these things? If not, I can write an initial
suite for this guy and the others.
Comment 13 Dani Megert CLA 2005-08-05 09:14:40 EDT
yep, see org.eclipse.jdt.text.tests/.../MarkOccurrenceTest
Comment 14 Adam Kiezun CLA 2005-08-05 09:33:25 EDT
ok. but not to mess up your world and to make it more modular I'll create a
separate test class just for the BreakContinueTargetFinder, ok?
It'd be independent of the editor - just eating testing that for a given ast and
selection you get the right thing selected.
Comment 15 Dani Megert CLA 2005-08-05 09:38:16 EDT
Not messing up is always fine ;-)
Comment 16 Adam Kiezun CLA 2005-08-10 00:35:59 EDT
Created attachment 25947 [details]
patch against jdt ui (3.1)

patch corrected to handle EnhancedForStatement
Comment 17 Adam Kiezun CLA 2005-08-10 00:41:50 EDT
Created attachment 25948 [details]
tests - patch against jdt.text.tests

here're the tests - they just check that when break/continue is marked in the
editor than the right thing gets selected.

The other finders in jdt ui have some tests too but they're tied to the editor.
The finding function is actually independent and this suite check only the
functionality.
Comment 18 Dani Megert CLA 2005-08-19 09:57:50 EDT
Reviewed the patch:

It currently highlights the full code which is really ugly (see attached
picture). It should only highlight the top-level 'switch', 'for' or 'while'
statement.

Like with all other occurrence markings it must be configurable and hence the
following things added:
- preference constant for this occurrence marking
- preference UI (see MarkOccurrencesConfigurationBlock)
- add code to the Java editor to react to the preference change
Comment 19 Dani Megert CLA 2005-08-19 09:58:35 EDT
Created attachment 26294 [details]
Shows the current result
Comment 20 Adam Kiezun CLA 2005-08-19 10:18:48 EDT
re comment 18: I know. See comment 10.
In order to change that, all other Finders would have to be uniformly modified -
currently everyone return an ASTNode but there's none to return if you want to
highlight the 'for' or 'while' or 'switch' keyword.

Please advise here - what you you want to do? Change all Finders to return array
of source ranges (sounds best to me) or make this one Finder special and treat
him specially in the UI?

BTW I know about all the other additional little things :). Just wanted the
funtionality approved before I spend time on productization.

Comment 21 Dani Megert CLA 2005-08-19 10:56:00 EDT
There's a trick how select the non-existing nodes. Here's an example:

	SimpleName name= fAST.newSimpleName("xxxxx"); //$NON-NLS-1$
	name.setSourceRange(node.getStartPosition(), 5);
	fResult.add(name);

After thinking about it again I think it is even better to select the next
statement that will be executed i.e. for 'continue' select the statement's
expression and for 'break' select the closing brace of the statement.
Comment 22 Adam Kiezun CLA 2005-08-19 11:42:56 EDT
ok, i can to 'the trick' (pretty nasty but ok with me)

ok, how about we select the enclosing construct's keyword (like 'for'/'while')
for continue and the closing brace for break

"The statement's expression" varies depending what it is:
 - for for-loops : it'd be the incement statement (if one exists - otherwise the
first statement in the block (block *must* be there because we selected
'continue' and that must live somewhere)
 - for foreach: the first statement in the block
 - for 'while' - the entry condition
 - for 'do' - the exit condition

So I think it may be confusing. But maybe not. What do you think?
Comment 23 Dani Megert CLA 2005-08-19 11:51:30 EDT
Yes, selecting the keyword is fine with me. The other solution would be only
needed if we don't want to do the hack. But since we already use that hack, e.g.
for in the MethodExitsFinder we can go for the keyword.
Comment 24 Adam Kiezun CLA 2005-08-19 11:59:54 EDT
ok, i'm busy with some other stuff but this is very simple so let's plan for m2
Comment 25 Ilja Preuss CLA 2005-08-22 03:01:24 EDT
Personally, I'd prefer for break to also highlight the keyword, not the closing 
brace. I think that's the more important information to me as the developer. 
(And in fact, there doesn't need to be a closing brace at all.)
Comment 26 Markus Keller CLA 2005-08-22 06:16:23 EDT
(In reply to comment #25)
> Personally, I'd prefer for break to also highlight the keyword, not the closing 
> brace.

So maybe both the keyword and the closing brace should be highlighted then?
Comment 27 Dani Megert CLA 2005-08-22 06:25:16 EDT
I'd like to see where the code continues after the 'break'. If there's no '}' we
could select the next statement or the whitespace in front of it.
Comment 28 Ilja Preuss CLA 2005-08-22 06:40:19 EDT
(In reply to comment #26)
> (In reply to comment #25)
> > Personally, I'd prefer for break to also highlight the keyword, not the 
closing 
> > brace.
> So maybe both the keyword and the closing brace should be highlighted then?

Or make it configurable?
Comment 29 Dani Megert CLA 2005-08-22 06:49:19 EDT
>Or make it configurable?
No additional option.
Comment 30 Adam Kiezun CLA 2005-08-25 00:54:02 EDT
Created attachment 26440 [details]
patch - next cut

next cut of patch against jdt ui (3.1)
Comment 31 Adam Kiezun CLA 2005-08-25 00:54:36 EDT
Created attachment 26441 [details]
tests - patch against jdt.text.tests

tests - next cut
Comment 32 Adam Kiezun CLA 2005-08-25 01:01:00 EDT
agreed - no new options.

I disagree with selecting "the next thing that would happen" after break.
It'd quickly get very confusing.

So here's what I did:
1. what gets selected is the first token in the target statement (for, do,
while, switch or label)
2. selection works now also inside the label names (i.e. you can select 'foo' in
'break foo' and it should work too)
3. changed tests to reflect this

Dani, please give it a whirl and let me know what you think about this behavior.
It looks pretty good to me. Once we agree on the functionality I'll do all those
productization tweaks.

For reference: here's the test case I use when playing around with settings:
class A {
	void foo(int[] i) {
		for (int j = 0; j < i.length; j++) {
			break;
		}
		label: while (i != null) {
			if (true)
				break;
			do {
				if (false) 
					continue;
				if (false) 
					continue label;
			} while(i.length>9);
			continue;
		}
	}
}
Comment 33 Adam Kiezun CLA 2005-08-25 01:05:58 EDT
oh, one more thing: in order to get the correct length of the thing to select, I
need to scan. And in order to scan I need the file contents. Currently, I use
document.get() inside the JavaEditor but have no idea if this call is cheap or
expensive. 
I did not look inside the call - if you tell me it's expensive I'll try to find
something else.
Comment 34 Dani Megert CLA 2005-09-08 06:52:10 EDT
Looks nice. Some comments:

- don't pass the document via initialize but get source from the AST using
  root.getJavaElement(), check this for ISourceReference and use getSource()
- don't set the full source to the scanner and reset it but only give it 
  directly the correct code snippet (also reduce the end offset). To be even 
  smarter you could reduce the length of the source via first child of the 
  statement
- set fIsBreak already in the initialize method to reduce the number of
  instance checks

Please also mark the ending '}' if there's any (don't mark the next statement or
whitespace). In the slightly modified example from comment 32 select the first
break. As you can see it will be really handy to see the '}', which is almost at
the bottom, highlighted.

public class BreakOccurrenceMarking {
	void foo(int[] i) {
		for (int j = 0; j < i.length; j++) {
			if (false)
				break;
		
			label: while (i != null) {
				if (true)
					break;
				do {
					if (false) 
						continue;
					if (false) 
						continue label        ;
					if (false)
						break;
				} while(i.length>9);
				continue;
			}
		}
	}
}
Comment 35 Adam Kiezun CLA 2005-09-16 10:41:58 EDT
Dani, I just got back and am swapmed with stuff. May not make it for M2
Comment 36 Dani Megert CLA 2005-09-16 11:12:40 EDT
FYI: The test candidate build is already on Monday.
Comment 37 Dani Megert CLA 2005-09-19 05:02:06 EDT
Moving to next milestone.
Comment 38 Adam Kiezun CLA 2005-10-05 10:45:34 EDT
Created attachment 27865 [details]
jdt ui patch

Dani,
here's the almost final cut. I did all (?) the productization things and
addressed your comments.
What's still missing is the highlighting of the closing brace.
Comment 39 Adam Kiezun CLA 2005-10-05 10:46:16 EDT
Created attachment 27866 [details]
and the tests
Comment 40 Adam Kiezun CLA 2005-10-05 10:47:57 EDT
aha, right. The ui too is missing :)
Comment 41 Dani Megert CLA 2005-10-06 03:50:13 EDT
I assume an updated patch with UI and highlighting of the closing brace will
appear soon ;-)
Comment 42 Adam Kiezun CLA 2005-10-06 11:48:12 EDT
Created attachment 27948 [details]
full version

here it is. check it out and let me know if I missed something.
Comment 43 Adam Kiezun CLA 2005-10-06 11:48:39 EDT
Created attachment 27949 [details]
the tests
Comment 44 Dani Megert CLA 2005-10-07 11:55:24 EDT
Released the patches with minor changes (typos, some code reorg).

Thanks Adam!
Comment 45 Tobias Widmer CLA 2005-11-01 06:56:29 EST
Verified using I20051031-2000