When working on Google Apps Script projects locally with CLASP, it can be handy to have text completion for the Apps Script classes and methods.
I’m currently shopping for a new IDE or text editor (the developer’s existential crisis) and have decided to pick up Sublime Text again after a 9-year hiatus after a mediocre three-week trial of Neovim.
After a fresh install of Sublime Text 4 here is what I needed to do:
Requirements
- Sublime Text 4 … I mean… c’mon.
- NPM to install CLASP and the text completion package.
- CLASP – The command line Apps Script tool for local development.
- Git (possibly) – If the TypeScript Sublime package is missing, you must install it via Git.
Setup
Google gracefully created a text completion package that uses Definitely Typed a TypeScript repository for type definitions that can be used with both TypeScript and vanilla JavaScript.
Install the TypeScript Package for Sublime Text
Check for the TypeScript Package
First, we check if the TypeScript package has been added to Sublime Text.
Select ctrl shift P
(cmd ⌘ should replace ctrl for Apple) to get the Command Pallete and start typing: Package Control: List Packages
and select it.
If the TypeScript package is present, move on to adding the Apps Script type package.
Add the TypeScript Package
Head back to the Command Pallete ( ctrl shift P
) and this time start typing Package Control: Install Package
.
This will load the Sublime package. Search for ‘TypeScript’. The name must be exact. If it is in the package list (It wasn’t for me) install it and move on to adding the Apps Script type package installation.
If the package is not on the list we will need to use git to clone the package from the Windows GitHub repo for the TypeScrpt-Sublime-Plugin.
Linux
1 2 |
cd ~/.config/sublime-text-3 git clone --depth 1 https://github.com/Microsoft/TypeScript-Sublime-Plugin.git TypeScript |
Windows
1 2 |
cd "%APPDATA%\Sublime Text 3\Packages" git clone --depth 1 https://github.com/Microsoft/TypeScript-Sublime-Plugin.git TypeScript |
Mac
1 2 |
cd ~/"Library/Application Support/Sublime Text 3/Packages" git clone --depth 1 https://github.com/Microsoft/TypeScript-Sublime-Plugin.git TypeScript |
You may need to close and reopen Sublime Text to see the effects.
Install the Google Apps Script Type Package
In your terminal or shell, run the following:
npm install --save @types/google-apps-script
This will install the autocomplete types for all of the Google Apps Script Classes and Methods.
Testing the Google Apps Script text completion in Sublime Text
Go ahead and open Sublime Text and create a JavaSript *.js
file or TypeScirpt *.ts
file.
Navigating Autocomplete
Start typing a Google Apps Script class like SpreadsheetApp
. You should see it appear in the list.
You can use the up-down (↑↓) arrow keys to navigate the list and enter
to select the method or class.
Tooltips – Method argument instructions
Information for each method should appear as you enter the braces.
Hovering over the method or class will reveal similar information.
Alt ,
will open the tooltip when the cursor is within the parentheses of the method.
Look at Multiple Arguments or tooltips
When there are multiple argument types, then you can click on the number to select from the list of types.
Or you can use the keyboard shortcuts:
Alt ,
– to open the tooltip if it is not available
Alt ↑
or Alt ↓
– to navigate to the next tooltip for the method.
Rename a Variable, Class or Function
Ctrl T Ctrl M
– With your cursor over a variable, class, method or function you can rename it and all occurrences of it.
Formatting
You can also use the TypeScript plugin to format your document.
Ctrl T Ctrl F
Formats the whole document
Ctrl T Ctrl F
Formats the selection
Ctrl ;
Formats the line
You can learn more shortcuts from the TypeScript Plugin for Sublime Text docs.
Getting *.gs files to display autocomplete
Sometimes we use the gs
file ending for Google Apps Script files. To get these to autocomplete we need to pair them to either JavaScript or TypeScrit.
In Sublime Text first create any .gs
file and ensure you are open in that file.
Then, go to View -> Syntax -> Open all with current extension as … .
Select either JavaScript or TypeScript.
Now, when you edit the file, the Apps Script autocomplete will work as expected.
Conclusion
I’m not sure if I am going to stick with Sublime Text, but it is like slipping on a comfy pair of shoes after such a long break from it, so who knows?
I’d love to hear in the comments if you have used Sublime Text recently or are even using it for your own Apps Script projects. What do you love and not so love about it?
Oh, by the way, I recently built a little CLI tool to help update multiple Google Apps Script projects from a single source project. If this interests you, you can check it out here:
CLASPALL – Update Multiple Apps Script Projects From a Single Source
If you have found the tutorial helpful, why not shout me a coffee ☕? I'd really appreciate it.
~Yagi