3
$\begingroup$

Not much to add to the question. I'm not looking for function autocompletion which is described in several posts, particularly 56984.

I find myself constantly doing asc//Keys and then typing the full thing.

$\endgroup$
6
  • $\begingroup$ I'm not sure if I understand correctly, but are you looking for something like AssociationKeyPicker[asc]? $\endgroup$ Commented Oct 19 at 0:21
  • 1
    $\begingroup$ I don't know what AssociationKeyPicker is, that doesn't show up when I search for it anywhere. Is it a resource function I can't find? I'm looking for something that (e.g.) when I type asc[longkeyname] it autocompletes after just typing asc[lon kinda like symbol autocompletion. $\endgroup$ Commented Oct 19 at 0:27
  • $\begingroup$ Apologies, I misunderstood what you were looking for. AssociationKeyPicker is actually something I came up with to dynamically select keys from an Association using an interactive dropdown menu. I now realize you're asking for inline autocompletion when typing something like asc[longkeyname], similar to symbol autocompletion. Unfortunately, Mathematica doesn't natively support that kind of autocompletion for Association keys. $\endgroup$ Commented Oct 19 at 0:31
  • 1
    $\begingroup$ well, that still sounds potentially very handy! Where can I find it? $\endgroup$ Commented Oct 19 at 0:34
  • $\begingroup$ Thanks! I'm currently designing AssociationKeyPicker to share as a possible solution. I'm thinking of adding it to my response as a utility for dynamic key selection from an Association. How would you prefer it to work—would you like it to show the keys in a separate window with options, or would you prefer using a PopupMenu right within the notebook? $\endgroup$ Commented Oct 19 at 0:39

2 Answers 2

2
$\begingroup$

While this may not strictly address your concern, I’d like to present an alternative that might be useful.

Thinking of something balanced regarding your needs, I came up with a function that allows copying the keys from associations. I included an option to present the association in a Dataset. Here’s the implementation:

CurrentValue[EvaluationNotebook[], {InputAliases, "cck"}] = 
RowBox[{"ClickToCopyKey", "[", 
TemplateBox[{}, "SelectionPlaceholder", 
DisplayFunction -> (FrameBox["\"Association\""] &)], "]"}];

addCompletion = FE`Evaluate[FEPrivate`AddSpecialArgCompletion[#]] &;

addCompletion["ClickToCopyKey" -> {{None}, {"DatasetFormat"}}];

ClickToCopyKey[asc_Association,
type : (Automatic | "DatasetFormat") : Automatic] := 
Module[{assoc},
assoc = Association@KeyValueMap[Rule @@ {ClickToCopy[#1], #2} &]@asc;
Switch[type, Automatic, assoc, "DatasetFormat", Dataset[assoc]]]

The alias implementation is due to @CarlWoll and can be found at:

Input alias having a placeholder which gets selected when the alias is used

The argument completion is due to @Szabolcs, and can be found at:

Argument completions for user-defined functions

I think you can build a dynamic selection function using PopupMenu or with a DialogInput (I'm working on it), as I put in the comments, but ClickToCopyKey might be a good start if you just want to copy keys.

Please try ClickToCopyKey and comment if you find it useful for something you need.

Note: You might also want an implementation where clicking on a key retrieves its corresponding value.

$\endgroup$
2
$\begingroup$

No

(this is where I add more characters to make this acceptable as an answer)

edit: typing asc//Keys and then copying the key is a great way to do what the OP wants to do

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.