google / clasp

🔗 Command Line Apps Script Projects
https://developers.google.com/apps-script/guides/clasp
Apache License 2.0
4.57k stars 427 forks source link

IDE support? (IntelliJ, VS Code) #10

Closed deinspanjer closed 6 years ago

deinspanjer commented 6 years ago

Are any of you working with gs files inside IntelliJ by any chance? Would be really nice to have a plugin to provide support. I found references to an older plugin that has been removed ( https://github.com/MichaelSnowden/intellij-gas-plugin ) with a clone here: ( https://github.com/kehh/intellij-gas-plugin ), but haven't tried it out yet to see if it works with clasp.

grant commented 6 years ago

If you can add JS support to .GS files, then you should have support for Apps Script files (just don't use ES6 features).

grant commented 6 years ago

I haven't tried it, but I'd be interested to see the IntelliJ support with Apps Script. It would be great if you could see autocomplete bindings like the script.google.com editor. (I can try to open-source the services/bindings).

Let me know if you have used IntelliJ with Apps Script, could be a much-improved developer experience. 😄

deinspanjer commented 6 years ago

I did add .GS as a file type for Javascript, and set the language version to avoid ES6 features, but as you said, the really important thing would be a plugin that would provide autocomplete for the App Scripts APIs

grant commented 6 years ago

I'm working on typescript + Apps Script. It works really well with autocompletion, linting, etc.

Working on updating the Apps Script dts files right now. @motemen created a library that can generate them (https://github.com/motemen/dts-google-apps-script).

I'll try to update the bindings and then write a tutorial for how you can actually develop Apps Script outside of the online editor. Looking promising right now!

jondcallahan commented 6 years ago

@grant would you consider saving .gs files locally as .js and treating .js as SERVER_JS to put the onus on the cli instead of the user?

grant commented 6 years ago

@jondcallahan We could save as .js locally, that's probably a good idea. It would be a breaking change for current users, but it's probably something we should do anyways.

Feel free to send a PR for the gs/js change.

grant commented 6 years ago

We should probably leave this issue open and document how to actually use an IDE with Apps Script. (Hint: It's incredible, autocomplete galore, TypeScript + Apps Script!)

wulftone commented 6 years ago

Would love to have up-to-date (including Gmail Add-on) typescript files definitions.

jondcallahan commented 6 years ago

This is mostly up to date, save for UrlFetchApp.fetchAll https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google-apps-script

I think you mentioned writing a blog post about using TS with apps script @grant , if so how is that coming along? I'm thinking of migrating an existing project over to TS but there wasn't too much written about configuring the tooling. Would love to have a canonical guide from you/your team.

grant commented 6 years ago

Current Progress

I have a few PRs out:

I have access to a big JSON document (96,000 lines) that used for the Apps Script editor's autocompletion. If someone wants to build a d.ts generator using this doc, I could probably release it after getting permission. It looks like:

{
      "parentType": {
        "name": "SlidesApp",
        "fields": [
         ...
          {
            "name": "ColorType",
            "typeName": "SlidesApp.ColorType",
            "kind": "NORMAL",
            "description": "An enumeration of color types."
          },
          ...
          {
            "name": "VideoSourceType",
            "typeName": "SlidesApp.VideoSourceType",
            "kind": "NORMAL",
            "description": "An enumeration of the types of video source."
          }
        ],
        "methods": [
          {
            "name": "create",
            "returnTypeName": "SlidesApp.Presentation",
            "params": [
              {
                "name": "name",
                "typeName": "String",
                "description": "The name to be given to the created presentation."
              }
            ],
            "description": "Creates and opens a new \u003ccode\u003e\u003ca target='_blank' href='https://developers.google.com/apps-script/reference/slides/presentation.html'\u003ePresentation\u003c/a\u003e\u003c/code\u003e.",
            "returnDescription": "the presentation with the given name."
          },
          {
            "name": "getActivePresentation",
            "returnTypeName": "SlidesApp.Presentation",
            "description": "Returns the currently active presentation to which the script is \u003ca\n href=\"/apps-script/scripts_containers\"\u003econtainer-bound\u003c/a\u003e, or \u003ccode\u003enull\u003c/code\u003e if there is no\n active presentation. To interact with a presentation to which the script is not\n container-bound, use \u003ccode\u003e\u003ca target='_blank' href='https://developers.google.com/apps-script/reference/slides/slides-app.html#openById(String)'\u003eopenById(id)\u003c/a\u003e\u003c/code\u003e instead.\n\n \u003cpre class=\"prettyprint\"\u003e\n // Get the current presentation to which this script is bound.\n var presentation = SlidesApp.getActivePresentation();\n \u003c/pre\u003e\n\n If the presentation is already open, the same presentation instance is returned."
          },
...

Today

You can use TS + GAS today, just npm i @types/google-apps-script and use an IDE like VS Code (free unlike IntelliJ Ultimate for TS). The types just haven't been updated in 1 year so there's no SlidesApp, GmailApp etc. I'll make a bigger push after updating the bindings.

gasts

Next Steps

  1. Find a reliable way to update the d.ts types. Use motemen's scraper/generator or build a new one.
  2. Define a standard way to write TS that can compile to Apps Script (ES3), with some other tools like linting, etc. Create a blog/video.
  3. Create a doc/tutorial to teach people how to write Apps Script this way.

Help is welcome.

JeanRemiDelteil commented 6 years ago

@grant For a while now I'm using this library https://github.com/JeanRemiDelteil/gas-lib that also comes as an NPM package. It will provide AppsScript autocomplete feature.

This package is auto-generated from the kind of files you just linked as example, however it does not generate typescript for now, but javascript JsDoc. I figure it would not be to difficult to generate typescript since the JSON parser is already done and working.

I will try to dip my hands in this and clean the generator code while I'm at it, so I can publish it here.

@deinspanjer By the way, I'm currently working with webstorm on AppsScript projects. Just set the *.gs type to be Javascript in the settings, and then we use https://github.com/JeanRemiDelteil/gas-shell as a starter kit. It has the npm gas-lib package for auto-complete, a build to be able to have multiple targes, and a bunch of other nice thing to have with GAS projects.

grant commented 6 years ago

FYI: This will be documented and solved in clasp@2.0 (#70) with optional Typescript support.

It's a bit complicated to explain, but basically, you'll rename files from js to ts if you want type support locally, or want to use es6.

grant commented 6 years ago

(FYI: I've renamed this to IDE Support from IntelliJ Support) Working on TS support for IDEs like VS Code and IntelliJ/WebStorm.

JeanRemiDelteil commented 6 years ago

@grant I've invited you to access my repo for generating GAS stubs / d.ts files. Seems that this matter got stuck a little. And I would like to make it move on again !

grant commented 6 years ago

Thanks for the ping. I think the TypeScript definitions (@types/google-apps-script) should be usable today. I'm working on a guide that will be on developers.google.com. Unfortunately progress is slower there.

Are there specific things missing from using TypeScript today? If so, I can help make those changes. I think the definitions are complete.

JeanRemiDelteil commented 6 years ago

I have to make some tests, but some code are giving errors when it should not.

let dir = Maps.newDirectionFinder();
dir.setAvoid(Maps.DirectionFinder.Avoid.HIGHWAYS);

gives the error (i'm using "Jetbrains Webstorms" == "Intellij for JS"):

Argument type GoogleAppsScript.Maps.Avoid.HIGHWAYS is not assignable to parameter type string

on Maps.DirectionFinder.Avoid.HIGHWAYS

Also these typing should be up-to-date when there are changes in GAS, not every years, in fact, it should be the gas team that should generate and provide those !

If we can use those typing without issues, then I will be more than happy to switch ! That would be awesome. But I don't want project with small errors everywhere, it defeats the purpose of using typings in the first place.

grant commented 6 years ago

clasp@1.5 launches TypeScript support which allows for IDE support in editors like VSC. See the guide here:

https://github.com/google/clasp/blob/master/docs/typescript.md

The GAS team can't provide better @types/google-apps-script types, but I've been able to update them and test them recently. Please comment on definition errors in the DefinitelyTyped google-apps-script commits and tag me. Also, feel free to make your own changes/PRs to the DefinitelyTyped repo.

I'm going to close this original issue of creating IDE support. If there are issues with the new support, we can create a new GitHub issue with specific details.

logemann commented 6 years ago

still not sure if IntelliJ can work with the current state. See my latest report at https://github.com/google/clasp/issues/268

Basically IntelliJ wants to create an import like this: import GmailMessage = GoogleAppsScript.Gmail.GmailMessage;

I ve tried using: import "google-apps-script" without any success. The types wont be recognized then. My mentioned imports gets transpiled into something which doesnt work in runtime (var GmailMessage = GoogleAppsScript.Gmail.GmailMessage;)

I just installed the types via NPM as normal.

grant commented 6 years ago

I'll continue the conversation with that issue. I should mention you cannot use the import statement, but autocompletion should come automatically after installing the types.

I haven't tried IntelliJ, only VSC. I'll investigate more during the week.

maelcaldas commented 5 years ago

@grant I've started building a d.ts generator to solve our own needs, so it can generate dts files from @public annotated global functions, interfaces, classes, enums etc, like the API Extractor:

https://github.com/maelcaldas/clasp-types

It works by writing and reading a JSON model from Typedoc engine, so, its only for libraries already written in Typescript.

Some questions:

clasp --library-types

To generate d.ts for libraries and

clasp --client-types

To generate d.ts for client-side api

If JSDoc engine also supported, once classes properly annotated with @public all built in services could be generated with clasp[-types] instead of scrapping. 9li It could also support reading openapi spec in to generate connection code like this and types.

Happy to have your thoughts on that and possibly collaborate on shipping it on clasp, if that make sense.