Named result variables

Robert Field robert.field at oracle.com
Tue Nov 3 07:04:29 UTC 2015


Ville, thanks for the ESC then Enter suggestion, it works!

Jan, this is great!

A sign of cool new stuff — lots of suggestions …

Seems it tries to work with any input, including complete garbage.  I’d suggest it only work with valid expressions.  Perhaps beep/flash if it isn’t.

Nit: There is is code for type names to trim out package names that are imported: SnippetMaps.fullClassNameAndPackageToClass(), which is used by Eval.typeOfExpression() which might be exactly what you need for the previous paragraph (returns null if not a valid expression).  

Currently 

new ArrayList<String>()
//alt-enter

becomes:

java.util.ArrayList<java.lang.String> s = new ArrayList<String>()

where it would become:

ArrayList<String> s = new ArrayList<String>()

-Robert





> On Nov 2, 2015, at 3:57 PM, Ville Misaki <ville at misaki.fi> wrote:
> 
> Seems to work great on mac also, just have to press ESC and enter.
> 
> It doesn't seem to take the context that much into account, e.g. if I already have a variable declaration on the row, it still adds the type and equal sign (without variable name) in the beginning -- and will crash if accidentally doing twice in a row.
> 
> E.g.
> 
>   -> "text"
>   // alt-enter
>   -> java.lang.String  = "text"
>   // fill in variable manually
>   -> java.lang.String var = "text"
>   // alt-enter
>   -> java.lang.String  = java.lang.String var = "text"
>   // alt-enter
>   -> java.lang.String [ERROR] Exception while running registered action
>   java.lang.reflect.InvocationTargetException
>           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>           at java.lang.reflect.Method.invoke(Method.java:520)
>           at jdk.internal.jline.console.ConsoleReader.readLine(ConsoleReader.java:2490)
>           at jdk.internal.jline.console.ConsoleReader.readLine(ConsoleReader.java:2277)
>           at jdk.internal.jshell.tool.ConsoleIOContext.readLine(ConsoleIOContext.java:154)
>           at jdk.internal.jshell.tool.JShellTool.run(JShellTool.java:447)
>           at jdk.internal.jshell.tool.JShellTool.start(JShellTool.java:266)
>           at jdk.internal.jshell.tool.JShellTool.start(JShellTool.java:245)
>           at jdk.internal.jshell.tool.JShellTool.main(JShellTool.java:235)
>   Caused by: java.lang.NullPointerException
>           at jdk.jshell.SourceCodeAnalysisImpl.analyzeType(SourceCodeAnalysisImpl.java:1024)
>           at jdk.internal.jshell.tool.ConsoleIOContext.computeType(ConsoleIOContext.java:297)
>           at jdk.internal.jshell.tool.ConsoleIOContext.lambda$new$5(ConsoleIOContext.java:147)
>           ... 11 more
> 
> 
> Cheers,
> 
> Ville
> 
> 
> 
> 
> On Tue, Nov 3, 2015 at 4:28 AM, Robert Field <robert.field at oracle.com <mailto:robert.field at oracle.com>> wrote:
> That was fast!  :-)
> 
> I can’t get it to work on mac. Tried 3+4 then option-enter, command-enter, shift-option-enter, ctrl-option-enter, or ctrl-command-option-enter, and a few others.
> 
> Will try on Linux…
> 
> On merge I get this:
> 
> $ patch -p1 < ~/work/complete-variable-prototype.diff
> patching file src/jdk.jshell/share/classes/jdk/internal/jshell/tool/ConsoleIOContext.java
> patching file src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysis.java
> patching file src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java
> Hunk #1 succeeded at 997 (offset -187 lines).
> 
> Could that be my problem?
> 
> -Robert
> 
> > On Nov 2, 2015, at 7:07 AM, Jan Lahoda <jan.lahoda at oracle.com <mailto:jan.lahoda at oracle.com>> wrote:
> >
> > Hi Robert,
> >
> > Attached is a crude prototype implementing the proposal - the shortcut to active the function is Alt-Enter.
> >
> > (Many things, like using simple names whenever possible, could be improved in the prototype, but hopefully good enough for experimenting.)
> >
> > Jan
> >
> > On 30.10.2015 07:48, Robert Field wrote:
> >> Keep in mind I'm open to any solution.
> >>
> >> Let me frame my idea as a strawman so it is easier to shoot arrows at :-)
> >>
> >> Think: source code transformations that IDEs sometimes do.
> >> So, in this case say, ...
> >> You type in a complex expression expr. Then you press ctrl-V. The tool
> >> generates the type typedecl and an equals sign and sticks the cursor
> >> between them so you can type the variable name:
> >>
> >>       typedecl. |   = expr:
> >>
> >> Where | represents the cursor position.
> >>
> >> Jan can tell me if this is even something jline can do.
> >>
> >> The idea is, like tab, you type characters which don't correspond to
> >> valid Java, but what you see is valid.
> >>
> >> Of course what character is typed to initiate this is arbitrary.
> >>
> >> Ideas...
> >>
> >> -Robert
> >>
> >>
> >>
> >> On October 29, 2015 11:17:24 PM Robert Field <robert.field at oracle.com <mailto:robert.field at oracle.com>>
> >> wrote:
> >>
> >>> Thanks Cay  putting this out for discussion, would love to come up
> >>> with some nice solution for this.  I would like to avoid something
> >>> that straddles the snippet and command worlds which are now distinct.
> >>>
> >>> I don't think I expressed my idea very clearly -- mostly because it
> >>> isn't at all clear to me ;-)
> >>> What I was thinking was something vaguely tab completion like, in that
> >>> you wind up looking at a well formed snippet not all of which you
> >>> explicitly typed. But not literally a tab nor completion.
> >>>
> >>> Thanks,
> >>> Robert
> >>>
> >>>
> >>> On October 29, 2015 6:50:46 PM Cay Horstmann <cay at horstmann.com <mailto:cay at horstmann.com>> wrote:
> >>>
> >>>> When I teach, I like to use jshell for quick demos. I might predefine
> >>>>
> >>>> <T> Set<T> setOf(T... values) { return new
> >>>> TreeSet<>(Arrays.asList(values)); }
> >>>>
> >>>> and then construct two sets:
> >>>>
> >>>> Set<Integer> set1 = setOf(1, 2, 3);
> >>>> Set<Integer> set2 = setOf(2, 3, 5);
> >>>> set1.removeAll(set2)
> >>>>
> >>>> Except, that's a lot of typing, so I don't do that. I do
> >>>>
> >>>> setOf(1, 2, 3);
> >>>> setOf(2, 3, 5);
> >>>> $1.removeAll($2)
> >>>>
> >>>> Note the absence of types. The $n are automatically typed.
> >>>>
> >>>> But I lose track of those $n prettty soon. It would be nice if i could
> >>>> name them.
> >>>>
> >>>> I brought this up at the Java One presentation and suggested
> >>>>
> >>>> /var set1 $1
> >>>>
> >>>> or
> >>>>
> >>>> /var set2
> >>>>
> >>>> to name the last $n.
> >>>>
> >>>> Robert thought that was a bit too much like inventing a new langauge
> >>>> feature and suggested that one might be able to solve this with tab
> >>>> completion instead.
> >>>>
> >>>> I thought about this a bit and couldn't come up with a compelling way.
> >>>> Right now, when one hits Tab on a blank line, one is offered a
> >>>> collection of well over 400 possible completions (types, packages,
> >>>> classes, $ variables). Using that to get to something like Set<Integer>
> >>>> or TreeMap<String, TreeSet<Integer>> isn't satisfactory.
> >>>>
> >>>> Does anyone else think that an easy way of naming results is worthwhile?
> >>>> Any ideas on what the user interface should be?
> >>>>
> >>>> Cheers,
> >>>>
> >>>> Cay
> >>>>
> >>>>
> >>>> --
> >>>>
> >>>> Cay S. Horstmann | http://horstmann.com <http://horstmann.com/> | mailto:cay at horstmann.com <mailto:cay at horstmann.com>
> > <complete-variable-prototype.txt>
> 
> 



More information about the kulla-dev mailing list