Bug 93897 - [compiler] Tolerate white space into non-NLS markers
Summary: [compiler] Tolerate white space into non-NLS markers
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 215466 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-05-06 08:01 EDT by Maxime Daniel CLA
Modified: 2018-02-05 11:32 EST (History)
7 users (show)

See Also:


Attachments
Possible solution (2.46 KB, patch)
2008-01-16 10:00 EST, Les Jones CLA
no flags Details | Diff
Simple patch to alter the behavior of the non-externalized string warning detection (1.07 KB, patch)
2008-01-16 13:12 EST, Les Jones CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Maxime Daniel CLA 2005-05-06 08:01:22 EDT
The following code extract raises a 'Non-externalized string' warning:
  String nonNLS = "non NLS"; // $NON-NLS-1$
whereas this one does not (as expected):
  String nonNLS = "non NLS"; //$NON-NLS-1$
I would suggest that the first case be recognized as equivalent to the second 
one.
To enlarge the case, the following may also be considered:
OK:
  String nonNLS = "non NLS" + "tail"; //$NON-NLS-1$ //$NON-NLS-2$
currently KO:
  String nonNLS = "non NLS" + "tail"; //$NON-NLS-1$ $NON-NLS-2$
  String nonNLS = "non NLS" + "tail"; //any_word$NON-NLS-1$ //$NON-NLS-2$
Comment 1 Olivier Thomann CLA 2006-10-06 14:28:02 EDT
I disagree with this.
NLS markers should remain simply and we should not tolerate whitespaces in them in order to improve detection.
Comment 2 Maxime Daniel CLA 2006-10-10 01:21:40 EDT
I would contend that simplicity does not have the same meaning here for the implementor and for the user...
Comment 3 Les Jones CLA 2008-01-16 09:06:08 EST
Has the view of the JDT team changed on this over the past year? Is this something you would consider fixing/enhancing?
Comment 4 Brian Bauman CLA 2008-01-16 09:30:14 EST
*** Bug 215466 has been marked as a duplicate of this bug. ***
Comment 5 Philipe Mulet CLA 2008-01-16 09:33:25 EST
I am not sure how much overhead this would create, considering we are already able to detect 2nd tag in:

//$NON-NLS-1$ //$NON-NLS-2$

(i.e. we still look forward...)
Comment 6 Olivier Thomann CLA 2008-01-16 09:37:36 EST
Right we search for //$NON-NLS-, we could replace this and search for $NON-NLS-.
In term of speed, this should be evaluated.
Howewer we would need to adjust all the tooling for removing and adding new tags.
These tags are not meant to be manually edited, quickfixes are available to add or remove them.
Comment 7 Philipe Mulet CLA 2008-01-16 09:43:13 EST
Martin - can you comment on quickfix impact if we did recognize more flavours of NON-NLS tags ? (not just the ones immediately following the '//')
Comment 8 Les Jones CLA 2008-01-16 10:00:29 EST
Created attachment 87049 [details]
Possible solution

If it helps with the assessment, I've attached a possible solution utilising regular expressions; although I appreciate you may prefer to stick with the prefix/suffix mechanism, depending on whether you perceive regexps to be too slow. Either way, this will at least allow you to run some tests.
Comment 9 Dani Megert CLA 2008-01-16 10:20:45 EST
>I would contend that simplicity does not have the same meaning here for the
>implementor and for the user...
I wouldn't say that having a space is simpler. We have quick fixes and the NLS tool to add/remove them. I'd says for those who still want to type that stuff in manually it's easy enough. I agree with Olivier to not change the current behavior.

What would be the preferred format?
//$NON-NLS-1$ //$NON-NLS-2$
// $NON-NLS-1$ // $NON-NLS-2$
//$NON-NLS-1$ $NON-NLS-2$
// $NON-NLS-1$ $NON-NLS-2$

Would we have to add a preference to set the preferred format and change all our tools (nls wizard, quick fix, nls hover) to respect that?

Also there's another issue with this proposal: clients who e.g. have this:
    some code; // this adds he $NON-NLS-1$ tag
    other code; // this adds he $NON-NLS-2$ tag
will get compiler warnings on their code.
Comment 10 Les Jones CLA 2008-01-16 13:12:11 EST
Created attachment 87070 [details]
Simple patch to alter the behavior of the non-externalized string warning detection

I think what is being requested is to support not a different style as such, but multiple styles by being a little more relaxed in the $NON-NLS-n$ detection - i.e. not to replace "//$NON-NLS-1$" with "// $NON-NLS-1$" but to allow both.

I appreciate that my earlier patch didn't deal with the detection of the tags as part of the build (i.e. when the "Non-externalised strings" compiler warning/error is set), but I'm not sure I've fully grasped the scenarios you're concerned about.

For example, if the build was fixed to not display the warning/error in cases where "// $NON-NLS-1$" was used, the quick fix wouldn't be available. When the quick fix is available, it'll still put "//$NON-NLS-1$" which shouldn't be an issue. 

Are there other quick fixes that I've missed? I've attached another (very simplistic, perhaps naive) patch that seems to work for the limited scenario's that I've tested. Let me know if it's too simplistic and causes problems elsewhere.

As regards the NLS tool, the patch that I'd supplied previously does deal with the issues of replacement and removal; certainly for the tests I've run. I'd be interested to understand scenarios where it's not performing as required.

Finally, I can imagine that you guys are quite busy, so any time you have available to assess these patches is greatly appreciated.
Comment 11 Les Jones CLA 2008-01-16 13:19:24 EST
For Info: The second patch is in addition to the first patch, not instead of.
Comment 12 Maxime Daniel CLA 2008-01-17 02:50:41 EST
(In reply to comment #9)
> >I would contend that simplicity does not have the same meaning here for the
> >implementor and for the user...
> I wouldn't say that having a space is simpler. We have quick fixes and the NLS
> tool to add/remove them. I'd says for those who still want to type that stuff
> in manually it's easy enough. I agree with Olivier to not change the current
> behavior.
> 
> What would be the preferred format?
> //$NON-NLS-1$ //$NON-NLS-2$
> // $NON-NLS-1$ // $NON-NLS-2$
> //$NON-NLS-1$ $NON-NLS-2$
> // $NON-NLS-1$ $NON-NLS-2$
> 
> Would we have to add a preference to set the preferred format and change all
> our tools (nls wizard, quick fix, nls hover) to respect that?
> 
> Also there's another issue with this proposal: clients who e.g. have this:
>     some code; // this adds he $NON-NLS-1$ tag
>     other code; // this adds he $NON-NLS-2$ tag
> will get compiler warnings on their code.
> 
Dani, like it or not, users do edit code, often using things like typing characters and cut and paste.
Having said that, our current behavior makes '//$NON-NLS-[1-9][0-9]*$' the syntax for our tags. This is counter-intuitive, at best, especially because programmers tend to see the '$.*$' part of it as a nice, symmetric expression. (In other words, the addition of the // in front of it is awkward - at best.)
May I also dare stress the fact that, while the warning reads '... it should be followed by //$NON-NLS-<n>$' and conforms to that odd syntax, the quick fix says 'Add missing $NON-NLS$ tag', failing to conform to the syntax on the one hand (missing // and missing -<n>), and reinforces the user's impression that the tag could well be '$NON-NLS-<n>$'.
Regarding the preferred formats, I believe we need to be flexible here, and avoid to over-specify this. What I'd like to see is $NON-NLS-<n>$ tags in any place of a comment on the same line erase the warning for the matching string. This would be intuitive, and the fact that the tooling adds a (any) preferred form of it when performing it on behalf of the user is far less important to me.

While we are at looking at that NON-NLS usability issue, here are further thoughts that came to me, for which I'll open separate bugs if needed:
- if you have s = "cheers"; //$NON-NLS-1$ and you insert a line break before the semi-column, you get two warnings because the tag is on the wrong line; this verges on the side of the bug;
- same remark if you cut between the two e-s; the Java editor is smart enough to
  add the needed double quotes and plus character, but fails to adjust the $NON-NLS-<n>$ tags;
- variations of the above...
- when you have several strings on the same line, a catch-all notation would help quite a lot; compare:
  s = "a" + s2 + "b"; //$NON-NLS-1$ //$NON-NLS-2
with:
  s = "a" + s2 + "b"; //$NON-NLS-*$
- s = "a"; //$NO^code assist should work;
- (more controversial: nowadays, 'tag' in a Java context would naturally raise images like @something in any programmer's mind; would be nice to consider such a pattern in this context as an addition to the $.*$ pattern).
Comment 13 Olivier Thomann CLA 2008-01-17 09:05:24 EST
If we invest any time in this, then we should change the NON-NLS tags to be used inside block comment and then they could be placed right besides the corresponding string literals.
I would not change anything unless we modify the NON-NLS support in depth.
Comment 14 Dani Megert CLA 2008-01-17 09:44:19 EST
I'm against using block comments beside each string: that would make reading the code much harder and pollute it in my opinion.
Comment 15 Les Jones CLA 2008-01-17 10:17:45 EST
I agree with Daniel about the block comment option; for me, it would produce very cluttered and ugly code.
Comment 16 Olivier Thomann CLA 2008-01-17 10:50:32 EST
Any NON-NLS tag is already ugly :-)
Comment 17 Dani Megert CLA 2008-01-17 11:19:26 EST
>Any NON-NLS tag is already ugly :-)
Yeah, we should have chosen the snowman character:
	System.out.println("\u2603"); // &#9731;
;-)
Comment 18 Martin Aeschlimann CLA 2008-01-29 07:08:20 EST
I haven't tried, but I guess this change will require us to also be adapted in the  NLS wizard, where we also have a scanner for 'NON-NLS' comments

I personally think it's better to not extend the syntax:
- the change generates ripples in the compiler, NLS tooling and quick fix
- I think it's OK to ask the user to pay attention to the detail here. The matching rule is simple.
- Typically, 'NON-NLS' tags are created by quick fix and the NLS tooling and not by typing and copy/paste
- the change can lead to new warnings/errors if users happen to use 'NON-NLS' in their comments (Unnecessary NON-NLS tags)
- the change can break other tools (unknown to us)

Looking at the IMO small benefit, I don't think the invested time and risks should be taken

- I agree that we should fix our warning messages in the compiler and quick fix to be consistent and correct (comment 12)
Comment 19 Brian de Alwis CLA 2018-02-05 11:32:37 EST
JDT now supports external formatters, some of which are unaware of the //$NON-NLS- convention.  It would be more flexible if the compiler allowed spaces, and would be consistent with how the JDT formatter handles @formatter: directives.