This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 155516 - Smarter code completion based on surrounding context
Summary: Smarter code completion based on surrounding context
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Editor (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Dusan Balek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-12-16 02:22 UTC by _ wadechandler
Modified: 2010-01-02 12:46 UTC (History)
1 user (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description _ wadechandler 2008-12-16 02:22:22 UTC
It is hard to fit it into a sentence necessarily, but the editor needs to be smarter about how it guesses what to show
the user for code completion. One example, and maybe I can come up with more over time, let's define some variable using
simple alphabetical names:

int x = 0;
int y = 0;
int z = 0;
String a = 0;
String b = 0;
String c = 0;
String d = 0;

This is what happens in this context:

if(f.length()>0){
  String g = |^code completion here...a is the first shown
}

if(y==10){
  int i = |^...x is the first shown in the list.
}

Now, a better guess might be that because I'm using y in the if statement, or scope, that I most likely have the if
testing that variable for a reason, and that reason is I want to use it. Same for Strings. The most code I use on
Strings is with a helper class with methods to test if a String is empty or null or length or other word, so something like:

if(StringUtils.nempty(a)){
  someBean.setSomeProperty(a);
}

Anyways, I'm pretty sure if we look harder we can find other context related issues which might help make code
completion a little smarter for scope and variables.
Comment 1 _ wadechandler 2008-12-16 02:23:02 UTC
This can really be an umbrella issue I guess.
Comment 2 _ wadechandler 2008-12-16 02:28:19 UTC
Here is another one:

String a = "";
String b = "";
String c = "";
String d = "";
String e = "";
String f = "";
String g = "";
String h = "";
        
if(f.length()>0){
  String a2 = "";
  String a3 = "";
  String h2 = "";
  String x = |^...here I would most likely expect to see inner scope before a or any others.
}

So, you see where the completion ... is. There I would expect to see a2, a3, h2 in completion before anything in the
outer scope. Most likely I'm creating local variables to do some work and use them. Instead I get variables from the
other scopes first and ordered alphabetically.
Comment 3 _ gsporar 2008-12-24 02:41:38 UTC
Hi Wade.  I just want to make sure I understand the requirements here.  On this code fragment from your first example:

if(f.length()>0){
  String g = |^code completion here...a is the first shown
}

You would prefer that f be listed first since f is used in the if statement?  (BTW, your code in the first example won't
compile, should be String a = ""; etc. :-) And you should declare f before referencing it in the if statement.)

I agree that the editor should be smarter, but we also have to deal with the legacy of other tools that have set user
expectations.  In other words, to some extent maybe follow the path of "least surprise."

FWIW, toward that end, I tried your examples using IntelliJ IDEA v8.0.1.  In all three cases it did *not* pay any
attention to scope.  In other words, it does not work the way you are suggesting.  Which is not to say that your
proposal is not valid - we do want to be better than the competition, but again, we have to think about "least surprise"
for the largest number of users.  I'll try Eclipse tomorrow and then I'll add another comment.
Comment 4 _ gsporar 2008-12-24 16:58:27 UTC
I tested with Eclipse v3.4.1 (Ganymede) and it also displays the available variables in alphabetical order, without
preference for the locally scoped variables.  So from what I can tell, NetBeans v6.5, Eclipse v3.4.1, and IntelliJ IDEA
v8.0.1 all work the same here.  Which is not to say that they could not be improved! :-)  But again, we need to think
about "least surprise."
Comment 5 _ wadechandler 2008-12-24 21:44:25 UTC
Yes, some how I forgot to copy f when I copied and pasted some example code I was drumming up; either way, the base
meaning isn't too skewed by it :-D 

I have been thinking of the issue for some time, and my current project where I'm using a lot of strings was just too
much to ignore; I was actually using .NET when I decided to create this issue. The code involves pulling a lot of data
from the database, and checking the result to see if I need to add an XML element or not, and this while using tools
which take a schema and create an object model and write it to XML for me, so whether or not to add an object to the
model as the schema may or may not allow the values to be blank or null (<element/> or nil respectively). 

Too, I run into this often whether working with XML or some other type data and whether String or numbers. We do this
now for methods. Define a class:

public class ScopeTest{

    String a = null;
    String b = null;
    String c = null;

    public void test(){
        String aa = null;
        String bb = null;
        String cc = null;
        
        cc = |^ aa will be the first in the list
    }

}

Legacy doesn't take care of my wrists :-D They hurt. If tools would get passed "work like XYZ or it is no good" then one
of them is going to get it right versus them all being wrong; kind of like JavaFX syntax looks very different from Java
for its purpose, or the programmer who uses domain specific languages versus trying to make a base language do all the
work (more repetitive tasks); whether they are all wrong or merely do something correctly just not as well as they might
be able to do is subjective.

I use a lot of different tools. I use .NET and Visual Studio, JDeveloper, and other domain specific tools such as Toad
or some other database tools with DB specifics built them and many others along with NB. Even in the cases where they
are all the same they don't always get it right, and they all make my wrists hurt as I have to type a lot; I'm sure many
and most including you are in the same boat. The better completion works the less we have to type, and not just the less
we have to type, but the more productive we are. 

The main point here isn't to keep on doing the same thing over and over again, but instead to move beyond legacy
concerns and look at it from a fresh perspective of making it better and not just the same. It's like fuel. We can
assume since we have been using our current fossil fuel and alcohol model and infrastructure for the past hundred years
and all the automotive and other fuel related industries are using those things that it is the best, or we can study and
move onto some alternatives which are more renewable and possibly even easier to engineer on site throughout space. I'm
voting on the latter, and I'm not in the crowd that we need to make those things more expensive to force the issue as
that misses the point of affordability and hurts more than it helps; the essential part is either there is or is not a
better way.

I don't find any tools go above and beyond the mark here to be more ergonomic. This is just one step in a giant issue of
ergonomics which computer systems need to surpass. Truly, considering we have been using keyboards and mice since the PC
became popular it is time to find some better methods. Regardless of our available input methods, we should find ways to
better the experience in our given domains; a source code editor in this case.

I get what you are saying about "least surprise", but that doesn't move tooling forward, but instead holds it back for
fear of upsetting the current or some other fear. Would we introduce new methods of building, agriculture, etc to a lost
society if we found they had a life expectancy of 30-40 years because their way of life was too hard on them, or would
we let them continue to beat out their existence and leave them to their own devices? 

I'm sure some would try to justify the morality of leaving them to their own devices because our society is SO horrible
and greedy, but given the choice, with proper understanding, most, if placed in a similar circumstance, would want to at
least be given the knowledge and option to use it, so maybe the answer is to make some of these behaviors optional.
Regardless, I think it is important to move forward and to try to make it better.
Comment 6 _ gsporar 2008-12-24 23:48:08 UTC
A slight variation on Wade's most recent example, to mix up the alphabetizing a bit:

public class Test {

    String x = null;
    String y = null;
    String z = null;

    public void test(){
         String ss = null;
         String tt = null;
         String uu = null;

         String vv = |
        }

}

Requesting code completion in IntelliJ IDEA v8.0.1 for "String vv =" results in ss, followed by tt and then uu.  And
then there is a huge long list of suggested classes from java.lang (AbstractMethodError, Appendable, etc.) and then x,
y, and z are listed at the very end.

Which makes me wonder if maybe this enhancement might be about adding a different type of code completion?  We have at
least three techniques today that I think of as code completion:

1. Ctrl-Space, for the full-blown from the classpath code completion
2. Tab, for expanding pre-defined code templates ("sout", etc.)
3. Insert Previous/Next Matching Word, for "hippie completion" (see
http://weblogs.java.net/blog/gsporar/archive/2008/02/hidden_hippie.html)

In reading through your use cases, it appears that you're mostly interested in quick access to variable names, *not*
methods or classes, is that right?  If that assumption is correct, then it seems that perhaps the resulting
implementation could be much *faster* at runtime if the entire classpath were not searched, and if methods and classes
were skipped, etc.

So it is *sort* *of* like Insert Previous/Next Matching Word, only matching on variable names only *and* paying
attention to scope.  And perhaps displaying the results in a list that looks more like Ctrl-Space.

While I am reluctant to suggest introducing yet another keystroke that people would have to learn, it might be better in
the long run because it could be more clearly defined and therefore would work better at runtime.  Or maybe not.  I'm
really just sort of throwin' out some ideas here.  :-)