-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[INFRA] Customize code suggestions & completions by adding a functionSignatures file #42
Conversation
The file is used to provide Matlab with information required for code suggestions and completions. Only the required arguments for layout, query and validate were annotated.
Thank you @mslw, it looks great. It is not something I have been using so far but that's a welcome addition. You are right this is a MATLAB-specific thing and is unlikely to work with Octave any time soon. The only "issue" is to make sure that the signatures remain in sync with the definition of the functions if and when they evolve. |
Yes this looks interesting. As long as the Octave functionality is not impaired I am in favor of any "Plus" for matlab. In terms of workflowto keep things up to date: using a pull-request template with a reminder to update the function signature could be an "easy" and gentle reminder. More involved would be to have something on the CI side that flags functions with no definitions. We are going to need some documentation I think... |
Thanks for the kind words and sorry for the long silence - I went on holiday soon after submitting the PR. I didn't initially do Regarding further changes - for me having the path autocomplete was the biggest convenience (and the reason why I added the json to my local copy of bids-matlab...). I think having all the entities for the One additional thing I was thinking about was adding a test, but the best I can come up is merely checking whether the So to answer the question about merging - I will try to annotate |
Subj, Sess & Run are described as numeric. It appears that "Subj" and "Run" could be ["numeric", "scalar"], meaning (numeric & scalar), but it only affects what variables would be suggested by tab completion so perhaps less is more here.
In the 2nd parameter to bids.query, listing char & string together did not do what I thought. From the docs: "For the inner lists, MATLAB uses a logical AND of the values. For the outer list, MATLAB uses a logical OR of the values."
Finally taking some time to have a look at this. @mslw I am starting to play around with this so bear with me: but I suspect that other people unfamiliar with this feature might benefit from some pointers. Starting with this in the command line, Tab autocompletes works fine.
But I am unclear on how to make this work with the other functions. Can you give an example? |
Maybe don't over-engineer things on
Well I think that something like this should be part of our
I would say that making sure we have a valid JSON is already good. 😉 If we had tons of them we would use some other validator but that will do for now.
Oh that would be nice indeed!
Yes that is a good point. But it seems that you already read the JSON with |
automatic reformating of JSON with prettier
Regarding the usage examples
Please bear in mind that I work in matlab GUI, so by "autocompletion" I mean both pop-ups with suggestions and actual filling-in of things I started typing, as shown below: With bids.layout (argument with type: folder) the actual deal is that even without fullfile (for which matlab already knows it should expect a path), Tab-autocompletion of a path will work if bids.layout is annotated, i.e. :
That being said, real life benefit isn't that big - I may benefit from my path being autocompleted when I'm doing some interactive work, but in most of my scripts I would build a variable storing the path before I get to calling something like For bids.query, two things happen. For the first argument, workspace variables of type struct will be suggested (marginally useful). For the second argument, a popup will show all the available options (specified as "choices" in JSON), i. e. "data", "metadata", etc (much more useful). Also, if I start typing one of those, it would tab-autocomplete. That is:
As a side note, same thing might also happen for the subsequent key-value arguments if they were also listed in the JSON, but I think that would be too much - for me having the "mode switches" (2nd argument) was the most useful thing. For others, bids.validate is same as layout; bids.report will also try to autocomplete path in the 1st argument if you type a string, and for subsequent argument will try suggesting variables which are numeric if you hit tab before typing anything. For me these behaviours are not very useful, and the two functions are annotated mostly for completeness. I guess part of the question is whether these need to be documented - perhaps for the developers, as you suggest, but from the user perspective the presence of annotation mostly mimics the behaviour of built-in functions. |
Thank you for taking time to look closer at the proposed additions. Here are my comments to the remaining points you raised. Json formatting bids.report
Agreed. I managed to come up with some minimal annotations and I think I am happy with where we're at right now. As mentioned earlier, I see less utility in annotations for report compared to layout & query. If anything, they should provide slight assistance when calling the functions in matlab, not cause headaches for development. Keeping annotations up to date
I can only agree - and perhaps just saying "when adding top-level functions or modifying their inputs, make sure that they have valid entries in functionSignatures.json (insert link to mathworks docs)" would be enough. I guess it is just conceptualization for now, or should I be adding it as part of this PR? Tests
but if the aim was to safeguard against new unannotated functions, it won't help, as they would have to be manually included in the test. So I am not sure if it would help or not (and didn't commit). |
hey @mslw Thanks for that explanation!! This is SO much clearer than the one on MathWorks !! 😉 I think the key element I was missing was this: "popup with struct variables already defined in the workspace appears". Will test on my computer as soon it is done crunching numbers(*): but this is looking good to me. I will open side issues to keep track of updating the "contributing.md" to mention the signatures. No need to add this to this PR. (*) An unfortunate keystroke had me run a |
A month later I finally have the time to check this out... So sorry. 😢 OK I am not sure why but most of my tab completion just throw an error: Unknown kind value "ordered" This was solved by removing any mention of Could this be a Matlab version issue? Testing on 2017a at the moment. |
Huh, the error sounds strange. Thanks for taking a closer look - this is becoming more complex than I expected. I'm on 2019b right now but I will check on 2017a in the coming days. Theorycrafting below. It might indeed be related to Matlab version. From what I could find on the web, it seems that the mechanism with functionSignatures.json was introduced somewhere around 2016b (initially undocumented). Perhaps the specs for the json file evolved between 2016 and now? The current docs say that schema version can be specified (and 1.0.0 is assumed by default), which implies that other versions may have existed. As a sidenote, this means versions prior to 2016 would simply not make use of the file. In case you beat me to it, there are two quick things I would try in R2017a:
|
Sorry about that. :-(
I think we are OK with that. as long as stuff don't break compatibility of the core functionality with older Matlab or Octave I think it can be fine to have things like this. :-) |
That did not change anything.
That seems to "work". Changed all the |
If anything, it's on Matlab. I think it's for the better that you found issues at this stage.
So it would seem that ordered wasn't added until mid-2017 at least. Interesting... I think positional is perfectly fine in the context of bids.report given the definitions are: Ordered - Argument is optional, and its location is relative to the required and preceding optional arguments in the signature object. I am changing to positional to avoid causing trouble in Matlab releases from 2016/17. |
Change argument kind from ordered to positional in the signature for report. Prevents errors on Matlab R2017a (probably 2016 as well, kind=ordered likely introduced later).
Awesome thanks! This is good to merge for me. Will open a side issue to make sure to remember to add "keeping the function signatures up to date" part of our "release protocol". |
As this is more "infrastructure" than purely code related I will merge now rather than having the "approval" period of our decision making process describes (also I think that this has been long on the back burner for way too long!!!) 😉 |
@all-contributors please add @mslw for infra, ideas, code |
I've put up a pull request to add @mslw! 🎉 |
I would like to propose a minor Quality of Life improvement (at least for Matlab users - I don't think Octave uses the same mechanism) by providing hints for tab-autocompletion. This can be done by adding a functionSignatures file (docs), which affects the behavior of Matlab's Command Window, Editor and Live Editor.
The file included in the pull request causes Matlab to:
bids.query(BIDS, <tab>)
shows the suggestions)I am not sure if you will consider having autocompletion hints a useful feature, so the provided annotations are rather minimal (i.e just a few arguments, and none for bids.report), although that is already quite handy for my own use.
Possible additions could include eg. adding all possible keys for key-value pairs in bids.query (although - is there a limited set?) and annotating bids.report. I would be happy to do more work if you think an expansion would be needed.