kylebarron / stata_kernel

A Jupyter kernel for Stata. Works with Windows, macOS, and Linux.
https://kylebarron.dev/stata_kernel/
GNU General Public License v3.0
267 stars 57 forks source link

Allow displaying local by sending just local #19

Closed kylebarron closed 5 years ago

kylebarron commented 6 years ago

If I have a local in my environment named x and set to 5, and I send

x

to Stata,

  1. If the selection sent is a single identifier
  2. Check if the selection matches a program, first with which program then by matching against program dir.
  3. Check if selection is name of a local or global. Might be able to do this without pinging Stata again by using environment state created for completions.
  4. If a local or global, run di `local'. Might also do this for r-class or e-class?

Note: Add check_env flag to run function so that I only check environment when needed.

Make sure this is an option in the configuration file

kylebarron commented 6 years ago

@mcaceresb I still want to implement something like this, although definitely behind a feature flag that is off by default. Since Atom/Hydrogen is selection based, there are tons of times I want to just run the local by itself to see its value, and hate having to type di "`macro'", like

power_param, `options' v2 _value outcome(`out') suffix(`suffix') `controls' if(if `base' & `sample1')

Ideally I'd love to be able to select just `out' and press ctrl-enter to see its value. I think this would be straightforward with the restriction that in this mode you couldn't store a program name in a macro and send just the macro to run it. -- If the first character sent to the kernel is ` and this mode is active, run di "`macro'" to Stata.

mcaceresb commented 6 years ago

While I do think the idea sounds quite useful, I don't like the way you have suggested for how to implement this, though I have a hard time explaining why.

Can I suggest this be an ofshoot of the code introspection code? If the word starts with ` or $ then show the local/global.

As a code introspection implementation this can be more general, since Atom can request it for any word. First look for global/local if the word starts like that. Otherwise look for help, and if no help is found start looking for any programs, variables, locals, matrices, etc. matching. Then list

out can refer to

local [value]
str32 variable [first value]
program [where found; e.g. directory, built-in, user-defined]
etc

Something like that. Obviously presented in a nice table or list, the above is just the contents idea. You know what I mean?

kylebarron commented 6 years ago

Well in Atom, code introspection comes up in a separate pane, and takes up part of the screen. It would be more visually convenient and appealing if just the value were displayed next to the code.

mcaceresb commented 6 years ago

Ah! Ok, this makes more sense now. I kind of assumed you could customize the introspection to be a tooltip, which is the default in qtconsole.

Random suggestion: Wouldn't it be easier to make a...macro/function/shortcut (not sure what the term is in atom) that grabs a word chunk and if it is a local then send disp `"`local'"' or is that actually more cumbersome?

mcaceresb commented 6 years ago

I think we can add an option like

%set keyword {eval|inspect}

If a single word is sent to the kernel, evaluate it or give feedback (inspect) on what object that word might refer to. It would be easy to run programs, etc. by just prepending noi if this is set to inspect and most commands wouldn't trigger it (since most commands are not a single word).

kylebarron commented 6 years ago

I kind of assumed you could customize the introspection to be a tooltip, which is the default in qtconsole. This is separate I think. I haven't tried introspection in qtconsole yet.

Random suggestion: Wouldn't it be easier to make a...macro/function/shortcut (not sure what the term is in atom) that grabs a word chunk and if it is a local then send disp "local'"' or is that actually more cumbersome?

No that would actually be really annoying because it would have to alter the text in the editor under the selection, run Hydrogen, then alter the text back.

I think we can add an option

Yes but I think it's still best to only inspect objects that we know are objects. And then try running it in Stata if it doesn't exist.

kylebarron commented 5 years ago

Closing as too difficult for what it's worth