Custom code suggestions with functionSignatures.json does not preserve char type with "choices={}"

3 次查看(过去 30 天)
I am trying to implement code suggestions with the functionSignatures.json file. I have certain functions which require character input arguments. For one such function in the JSON file I have written:
{
"name": "model",
"kind": "required",
"type": ["char", "choices={'f','n','u'}"],
"purpose": "type of model"
},
However, in the Editor the autocomplete drop down sugggestion offers and then Tab/Enter inserts a string scalar (either "f", "n" or "u") instead of a character.
Is this a bug or am I missing something?
  1 个评论
Markus Leuthold
Markus Leuthold 2022-8-2
if you start you completion sequence with char, Matlab continues completing with char. For example assum the following functionSignature.json
{
"name": "model",
"kind": "namevalue",
"type": ["char", "choices={'f','n','u'}"],
"purpose": "type of model"
},
After you type
fcn('
both the key 'model' and the values 'f','n','u' will be completed in char.
But you're right, the default is string, which is sometimes cumbersome. I normally make sure all my functions accpet both string and char

请先登录,再进行评论。

回答(1 个)

Matt Cooper
Matt Cooper 2022-6-17
This is not a bug, it is the expected behavior (assuming I understand your question). To make sure I understand your question, you want to assign to 'model' a character array like:
model = char('f','n','u');
and you want to be able to autocomplete this selection? If so, then you have misunderstood how the json file works.
The line:
"type": ["char", "choices={'f','n','u'}"],
is designed to show the user the three available options ('f', 'n', or 'u') that the value of the variable 'model' is allowed to take. If you had three different models, one named 'f', one named 'n', and one named 'u', then you would have the correct setup.
To get what you want, you could change it to:
"type": ["char", "choices={'fnu','abc','xyz'}"],
where I made up two other options 'abc' and 'xyz'. Then in your code, parse them out like:
modelselection = [model(1); model(2); model(3)];
or however you want.
If you don't care about autocomplete, then just remove the part with "choices" and parse out the input however you want. However, this is actually a neat trick to getting autocomplete options for multiple parameters all at once. Normally you would need a separate variable for whatever 'f', 'n', and 'u' represent, but this way you get all three at once, albeit in a cryptic manner that nobody else will understand unless they are familiar with the options.
There is another option you might look at to pass in a structure but it can be tricky to figure out (each field of the structure you pass in is treated as a namevalue pair but must have a corresponding addParameter definition in the input parser scheme which is quite rigid but leads to more compact input notation).
  2 个评论
Ashley Redman
Ashley Redman 2022-6-22
编辑:Ashley Redman 2022-6-22
You have misunderstood my question, sorry if my use of array caused uncecessary confusion? I understand how the JSON file works (I think); however, the autosuggest is not working as I expected it to
The JSON example provides the following autocomplete. I don't want a string suggestion ("f", "n", or "u"), but a character ('f', 'n', or 'u')
Matt Cooper
Matt Cooper 2022-6-22
In that case I suggest including 'string' as a valid type in your validateattributes or properties block at the top of your function and then just convert to char. I confirmed the same behavior with my functions but I never noticed this because I must reflexively type the single quote character whenever I want the list of choices (which are displayed as chars if that's how they're defined in the json), but as you note, if you press the tab key without first typing the single quote, you will get a list of strings. So, just accept either strings or chars (no need to edit the json, just the input parser validation block if you are using that) and then convert to char.

请先登录,再进行评论。

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by