lostintangent / codeswing

VS Code extension for building web applications ("swings") using a interactive and editor-integrated coding environment
https://aka.ms/codeswing
MIT License
977 stars 45 forks source link

Intellisense of imported libraries #25

Closed jasonwilliams closed 8 months ago

jasonwilliams commented 3 years ago

Hey great extension. This is a good way to try out/play with new libraries.

This may be a long shot but worth asking. Is it technically possible to get completions for libraries added in the "script.js" pane? I can imagine this isn't easy but would be awesome if doable.

So "jQuery" for example. Its added in my "scripts" array. Can definitions be pull down on the fly and used by VSCode?

Automatic Type Acquisition

We can rely on VSCodes Automatic Type Acquisition it sounds like we can force this in a jsconfig.json file and VSCode does the rest. Just auto generate the file and place it in the same directory as the script.js. Below ive done it manually.

Before: image

Then in the same folder I add jsconfig.json

{
  "typeAcquisition": {
    "include": ["jquery"]
  }
}

After: image

That works well! The only downside here is we can't force a specific version. I think VSCode just grabs the latest.

Automatic type acquisition will always download the declaration file for the latest version of a library. If you're using a different version, there may be mismatches between what's shown in Intellisense and what's actually available at runtime https://ticehurst.com/jsdocs/articles/types/ata.html

skypack

I just found that Skypack supports typescript declarations in its CDN. It sends the path in its header response. More info here: https://docs.skypack.dev/skypack-cdn/api-reference/lookup-urls#typescript-declarations

jQuery: https://cdn.skypack.dev/jquery jQuery Type: https://cdn.skypack.dev/-/jquery@v3.5.1-GJsJJ2VEWnBDZuot9fRf/dist=es2020,mode=types/index.d.ts

I'm not sure how easy it is to traverse those types and download them

It would also be nice to point directly to type declarations but I don't think that's possible right now. https://github.com/microsoft/TypeScript/issues/28985 might be a blocker

@FredKSchott can Skypack/Snowpack be utilised here in anyway?

Other ideas: Auto generating something like this https://dev.to/patarapolw/cdn-and-typescript-support-also-javascript-typing-for-ide-1bko, adding a package.json, then

There's also https://github.com/tokilabs/autotypes

lostintangent commented 3 years ago

Yeah this would be a huge improvement to the experience 😄 As you suggested, I think I'd need support from TypeScript in order to make this work. That said, I need to investigate whether I could create a VS Code/TypeScript extension point, and "inject" typings into the language server myself. I'm not sure if that's possible, but it's worth checking out 😄

jasonwilliams commented 3 years ago

@lostintangent did you see the updated edit I made to my post? I managed to get it working without typescript, sadly it will always choose the latest version for typings though. I think if you specify a version in package.json it may use the typings version for that

lostintangent commented 3 years ago

Yeah I saw that. But I'm curious if we can remove the need for the manual jsconfig.json file by implementing a TypeScript server plugin that dynamically does the same thing.

jasonwilliams commented 3 years ago

I set a version in package.json and it looks like it didn't affect the typing version. So i think the jsconfig.json option would only work for latest. Shame.

I've never implemented a typescript server plugin so im not sure what they're capable of doing.

jasonwilliams commented 3 years ago

https://stackoverflow.com/questions/39698336/inject-implicit-ambient-typings-via-visual-studio-code-extension might help

Darsh2987 commented 3 years ago

Would love to see this.

jasonwilliams commented 3 years ago

As an attempt to move forward, here is an option:

I've been doing the above manually and it seems to work well. it's a quick win which gets you 90% of the way there. I couldn't see it being possible with the typescript plugin route.

Skypack have type declarations but you would need some sort of parser/crawler to bring them down. Typescript does not automatically fetch types from url-based imports, only Deno does this currently.

lostintangent commented 8 months ago

This will be fixed in the next extension release

jasonwilliams commented 8 months ago

Good to see you’re back on this @lostintangent How did you fix it?