julia-vscode icon indicating copy to clipboard operation
julia-vscode copied to clipboard

Support DocStringExtensions

Open tk3369 opened this issue 5 years ago • 18 comments

Ideally the DocStringExtensions variables are expanded automatically and markdown format is respected. The Julia REPL can handle it properly.

image

tk3369 avatar Jun 29 '20 23:06 tk3369

Note that it's not working ONLY within the same package that I'm developing. If DocStringExtensions is used in a third package, and I'm just using that package, then the doc is displayed properly.

tk3369 avatar Jun 29 '20 23:06 tk3369

My gut feeling is that we probably only want to support this once these features are moved into Base...

davidanthoff avatar Jun 29 '20 23:06 davidanthoff

In some sense it already is in Base though. The docsystem has an (undocumented) API to add a custom hook to process docstrings (that's how DocStringExtensions works). The "user" (e.g. julia-vscode) should fetch the processed docstrings.

But I guess it already does that, since it actually works for "third packages". Just that for the docstrings that are updated in VS Code the custom hook doesn't get run on the new docstring or something along those lines?

mortenpi avatar Jul 02 '20 03:07 mortenpi

Yeah, for third party packages it works because we load the package and then use the normal doc extraction feature.

But for code that the user edits live, we extract the doc string simply by parsing the code file, with no additional processing. We could probably support some pre-defined things if they are official enough, but I don't see a reasonable design that would run custom hooks in that scenario...

davidanthoff avatar Jul 02 '20 03:07 davidanthoff

Do the updated docstrings get passed on to the docsystem? If yes, could you not extract the docstrings also in this instance? Or.. do you actually never run the edited code? (Pardon my ignorance -- I haven't actually ever used julia-vscode)

mortenpi avatar Jul 02 '20 06:07 mortenpi

We have both a runtime process (in which the user can run code, either via the REPL or inline execution, or with Revise) and a server process that never comes into contact with user code. The latter parses user code and extracts docs from the CST, so no, those never come into contact with the docsystem.

pfitzseb avatar Jul 02 '20 07:07 pfitzseb

I understand. What would be a sensible implementation if DocStringExtensions never gets into Base? I am imagining that code like the callme function above could be evaluated in a made-up module, just for extracting the interpolated doc string?

module TempModule1245

using DocStringExtensions

"""
$(TYPEDSIGNATURES)
Just call me!
"""
function callme()
    # intentionally blank
end

end

tk3369 avatar Jul 03 '20 02:07 tk3369

We generally never execute user code in the language server process (because it could crash it). So if we wanted to run something, it would have to be in a different process, like what we do with the symbol server process. That would be a lot of overhead...

The alternative would be to teach the LS about particular syntax, like the TYPEDSIGNATURES for example, presumably there aren't that many and one could just reimplement them in the LS, and not execute any code? But I think the likelihood of us adding that to the LS would really be higher if it was in base and officially part of the doc system...

davidanthoff avatar Jul 03 '20 04:07 davidanthoff

I don't think I'm suggesting executing user code though. Knowing that there's a doc string above a function definition, we can just evaluate the doc string with an empty function with the same name. Same thing for structs.

Come to think of it - for a MVP (minimum viable product), I would actually be fine if the string is parsed and shown as markdown rather than a regular string. The \n's are more annoying as majority of my doc strings have multiple lines, sometimes with bullets, etc. and they're not formatted properly.

tk3369 avatar Jul 03 '20 05:07 tk3369

we can just evaluate the doc string

As soon as we do that, we are executing user code, though...

davidanthoff avatar Jul 03 '20 18:07 davidanthoff

I can see your point :-) but I was just thinking about what can go wrong. The eval's are done using DocStringExtensions and it does generate the text from the function's signature. Could that really crash the process?

tk3369 avatar Jul 03 '20 21:07 tk3369

Users can presumably run arbitrary code inside $(), it is just standard string interpolation, right?

davidanthoff avatar Jul 03 '20 22:07 davidanthoff

It is. Would it be safe if we make it resolve to only DocStringExtensions variables and disallow arbitrary expressions?

tk3369 avatar Jul 03 '20 22:07 tk3369

I think some sort of whitelist approach would probably be safest: only allow a few hand-selected things like $(TYPEDSIGNATURES). But no matter how, we would have to include special casing for DocStringExtensions.jl in the language server code base, and we try quite hard to not do that in general...

davidanthoff avatar Jul 04 '20 00:07 davidanthoff

How about we just settle with the MVP that I described above then i.e. just make sure that it's parsed as a markdown string rather than a regular string. That honestly feels more like a bug.

I think this approach would be reasonable even if DocStringExtensions never gets into Base.

tk3369 avatar Jul 04 '20 00:07 tk3369

I can also work on this if you can just point me to the right direction 😄 I'm quite confused about where to find the code among all the vscode repos...

tk3369 avatar Jul 04 '20 00:07 tk3369

Yes, totally agreed, we should definitely treat it as markdown! I have no idea where to change that, @ZacLN is the king of all of this stuff :)

davidanthoff avatar Jul 04 '20 02:07 davidanthoff

Ping on this. Would be really nice to have :)

MilesCranmer avatar Jun 01 '24 21:06 MilesCranmer