1 of 74

Building VS Code extensions

Naman Sancheti

2 of 74

Agenda

  • Setup�
  • Anatomy of an extension�
  • Codelab - Building ‘RxJS Buddy’�
  • Publishing�
  • Extension Capabilities & Restrictions�
  • Sample extension - Theming�
  • Sample extension - Autocomplete�
  • Sample extension - Snippet�
  • Sample extension - Language Server

3 of 74

Setup

4 of 74

5 of 74

6 of 74

File structure

7 of 74

Anatomy

  • package.json as Extension Manifest - mix of Node.js + VS Code fields.�
  • Extension’s unique ID: <publisher>.<name>�
  • main - entry point�Eg - ./out/extension.js�
  • activationEvents:�onCommand, onLanguage, onDebug …��Ref:�https://code.visualstudio.com/api/references/activation-events��

8 of 74

Anatomy

  • contributes�commands, menus, languages , themes ...��Ref:https://code.visualstudio.com/api/references/contribution-points
  • engines.vscode - minimum version of VS Code API required��
  • activate - executed when registered Activation Event happens.��
  • deactivate - cleanup before deactivation happens.���

9 of 74

Extension Capabilities

  • Common:�commands, notifications, user input, system file picker …�
  • Theming:�Source code + VS Code UI�
  • Declarative Language Features:�bracket matching, auto-indentation ...�
  • Programming Language Features:�Hovers, IntelliSense, Go to Definition …�
  • Workbench Extensions:�additional options, new views ....�
  • Debugging:�integrate specific debugger or runtime …��Ref:�https://code.visualstudio.com/api/extension-capabilities/overview�����Ref:�https://code.visualstudio.com/api/extension-capabilities/overview

10 of 74

Extension Restrictions

  • No DOM access:�Cannot write an extension applying custom CSS or adding an HTML element to VS Code UI.�
  • Stability / Isolation ��Extension Host - Node.js process for loading and running extensions.��prevents impact on:�startup performance�modifying / slowing down UI�
  • Lazy loading - using activation events.

11 of 74

Codelab - Building ‘RxJS Buddy’

  • Clone repo:�https://github.com/namansancheti/rxjs-buddy.git
  • Corresponding to each step, you will find a branch named 'step-stepNumber', which contains the completed code until the previous step.��Eg: branch 'step-3' would contain the completed code from steps 1 and 2.�
  • Starter code - master�
  • Finished code - final

12 of 74

Publishing

  • VS Code Extension Marketplace��OR��VSIX format��
  • vsce - CLI tool for managing VS Code extensions�������

13 of 74

  • publisherId

14 of 74

Useful links

15 of 74

16 of 74

17 of 74

18 of 74

19 of 74

20 of 74

21 of 74

22 of 74

23 of 74

24 of 74

25 of 74

Samples

26 of 74

Samples - Theming

  • Two categories for colors:��Workbench colors - Activity bar, Side bar, Status bar...��Syntax colors - based on TextMate grammar / themes.��
  • TextMate theme file - .tmTheme��Ref:�https://code.visualstudio.com/api/extension-guides/color-theme

27 of 74

Workbench colors

28 of 74

29 of 74

Syntax colors

30 of 74

Creating a new theme

31 of 74

32 of 74

33 of 74

34 of 74

Code theming

Blog on ‘Night Owl’ theme:�https://css-tricks.com/creating-a-vs-code-theme/

35 of 74

Sample extensions - Autocomplete

36 of 74

37 of 74

38 of 74

39 of 74

40 of 74

41 of 74

Sample extensions - Snippet

42 of 74

Sample extensions - Snippet

  • File → Preferences → User Snippets��Select language and copy-paste snippets��OR��New Global Snippets file�

43 of 74

44 of 74

45 of 74

46 of 74

47 of 74

48 of 74

Adding key bindings

49 of 74

Additional points:

  • $1, $2 etc … are tabstops. The number denotes the order in which they will be visited.��$0 denotes the final cursor position.�� Multiple occurrences of the same tabstop are linked and updated in sync.��
  • Placeholders are tabstops with values. We can also give a set of choices, which are prompted to the user.

50 of 74

51 of 74

52 of 74

Programmatic Language features

53 of 74

Language Server Protocol

  • Language servers are typically implemented in native programming languages, however VS Code has a Node.js runtime.�
  • Language features can be resource intensive.�Eg - linter => parse + build AST + analyze.�
  • Language support for M languages in N editors, requires M * N work.�
  • LSP standardizes communication b/w language tooling and editor.�Therefore, LSP compliant language tooling ⇔ LSP compliant editor.�
  • Language server implemented in any language + run in own process.��Ref:�https://microsoft.github.io/language-server-protocol/

54 of 74

55 of 74

56 of 74

57 of 74

58 of 74

Sample extensions - LSP

59 of 74

60 of 74

61 of 74

62 of 74

63 of 74

64 of 74

65 of 74

66 of 74

67 of 74

68 of 74

69 of 74

70 of 74

71 of 74

72 of 74

73 of 74

74 of 74

That’s all folks! :)��Slides - bit.ly/vscode-ext-jslovers

https://snippet-generator.app/

Twitter - @namans_26���Medium - @naman.sancheti���Email - naman.sancheti@gmail.com