Language Configuration Guide

The contributes.languages Contribution Point allows you to define a language configuration that controls the following Declarative Language Features:

  • Comment toggling
  • Brackets definition
  • Autoclosing
  • Autosurrounding
  • Folding
  • Word pattern
  • Indentation Rules

Here is a Language Configuration Sample that configures the editing experience for JavaScript files. This guide explains the content of language-configuration.json:

Note: If your language configuration file name is or ends with language-configuration.json, you will get autocompletion and validation in VS Code.

  1. {
  2. "comments": {
  3. "lineComment": "//",
  4. "blockComment": ["/*", "*/"]
  5. },
  6. "brackets": [
  7. ["{", "}"],
  8. ["[", "]"],
  9. ["(", ")"]
  10. ],
  11. "autoClosingPairs": [
  12. { "open": "{", "close": "}" },
  13. { "open": "[", "close": "]" },
  14. { "open": "(", "close": ")" },
  15. { "open": "'", "close": "'", "notIn": ["string", "comment"] },
  16. { "open": "\"", "close": "\"", "notIn": ["string"] },
  17. { "open": "`", "close": "`", "notIn": ["string", "comment"] },
  18. { "open": "/**", "close": " */", "notIn": ["string"] }
  19. ],
  20. "autoCloseBefore": ";:.,=}])>` \n\t",
  21. "surroundingPairs": [
  22. ["{", "}"],
  23. ["[", "]"],
  24. ["(", ")"],
  25. ["'", "'"],
  26. ["\"", "\""],
  27. ["`", "`"]
  28. ],
  29. "folding": {
  30. "markers": {
  31. "start": "^\\s*//\\s*#?region\\b",
  32. "end": "^\\s*//\\s*#?endregion\\b"
  33. }
  34. },
  35. "wordPattern": "(-?\\d*\\.\\d\\w*)|([^\\`\\~\\!\\@\\#\\%\\^\\&\\*\\(\\)\\-\\=\\+\\[\\{\\]\\}\\\\\\|\\;\\:\\'\\\"\\,\\.\\<\\>\\/\\?\\s]+)",
  36. "indentationRules": {
  37. "increaseIndentPattern": "^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$",
  38. "decreaseIndentPattern": "^((?!.*?\\/\\*).*\\*/)?\\s*[\\}\\]].*$"
  39. }
  40. }

Comment toggling

VS Code offers two commands for comment toggling. Toggle Line Comment and Toggle Block Comment. You can specify comments.blockComment and comments.lineComment to control how VS Code should comment out lines / blocks.

  1. {
  2. "comments": {
  3. "lineComment": "//",
  4. "blockComment": ["/*", "*/"]
  5. }
  6. }

Brackets definition

When you move the cursor to a bracket defined here, VS Code will highlight that bracket together with its matching pair.

  1. {
  2. "brackets": [
  3. ["{", "}"],
  4. ["[", "]"],
  5. ["(", ")"]
  6. ]
  7. }

Moreover, when you run Go to Bracket or Select to Bracket, VS Code will use the definition above to find the nearest bracket and its matching pair.

Autoclosing

When you type ', VS Code creates a pair of single quotes and puts your cursor in the middle: '|'. This section defines such pairs.

  1. {
  2. "autoClosingPairs": [
  3. { "open": "{", "close": "}" },
  4. { "open": "[", "close": "]" },
  5. { "open": "(", "close": ")" },
  6. { "open": "'", "close": "'", "notIn": ["string", "comment"] },
  7. { "open": "\"", "close": "\"", "notIn": ["string"] },
  8. { "open": "`", "close": "`", "notIn": ["string", "comment"] },
  9. { "open": "/**", "close": " */", "notIn": ["string"] }
  10. ]
  11. }

The notIn key disables this feature in certain code ranges. For example, when you are writing the following code:

  1. // ES6's Template String
  2. `ES6's Template String`;

The single quote will not be autoclosed.

Users can tweak the autoclosing behavior with the editor.autoClosingQuotes and editor.autoClosingBrackets settings.

Autoclosing before

By default, VS Code only autocloses pairs if there is whitespace right after the cursor. So when you type { in the following JSX code, you would not get autoclose:

  1. const Component = () =>
  2. <div className={>
  3. ^ Does not get autoclosed by default
  4. </div>

However, this definition overrides that behavior:

  1. {
  2. "autoCloseBefore": ";:.,=}])>` \n\t"
  3. }

Now when you enter { right before >, VS Code autocloses it with }.

Autosurrounding

When you select a range in VS Code and enter an opening bracket, VS Code surrounds the selected content with a pair of brackets. This feature is called Autosurrounding, and here you can define the autosurrounding pairs for a specific language:

  1. {
  2. "surroundingPairs": [
  3. ["{", "}"],
  4. ["[", "]"],
  5. ["(", ")"],
  6. ["'", "'"],
  7. ["\"", "\""],
  8. ["`", "`"]
  9. ]
  10. }

Users can tweak the autosurrounding behavior with the editor.autoSurround setting.

Folding

In VS Code, there are three kinds of folding:

  • Indentation-based folding: This is VS Code’s default folding behavior. When it sees two lines of the same indentation level, it creates a folding marker that allows you to collapse that region.
  • Language configuration folding: When VS Code finds both the start and end regex defined in folding.markers, it creates a folding marker enclosing the content inside the pair. The following JSON creates folding markers for //#region and //#endregion.
  1. {
  2. "folding": {
  3. "markers": {
  4. "start": "^\\s*//\\s*#?region\\b",
  5. "end": "^\\s*//\\s*#?endregion\\b"
  6. }
  7. }
  8. }
  • Language server folding: The Language Server responds to the textDocument/foldingRange request with a list of folding ranges, and VS Code would render the ranges as folding markers. Learn more about the folding support in Language Server Protocol at the Programmatic Language Feature topic.

Word Pattern

wordPattern defines what’s considered as a word in the programming language. Code suggestion features will use this setting to determine word boundaries if wordPattern is set. Note this setting won’t affect word-related editor commands, which are controlled by the editor setting editor.wordSeparators.

  1. {
  2. "wordPattern": "(-?\\d*\\.\\d\\w*)|([^\\`\\~\\!\\@\\#\\%\\^\\&\\*\\(\\)\\-\\=\\+\\[\\{\\]\\}\\\\\\|\\;\\:\\'\\\"\\,\\.\\<\\>\\/\\?\\s]+)"
  3. }

Indentation Rules

indentationRules defines how the editor should adjust the indentation of current line or next line when you type, paste, and move lines.

  1. {
  2. "indentationRules": {
  3. "increaseIndentPattern": "^((?!\\/\\/).)*(\\{[^}\"'`]*|\\([^)\"'`]*|\\[[^\\]\"'`]*)$",
  4. "decreaseIndentPattern": "^((?!.*?\\/\\*).*\\*/)?\\s*[\\)\\}\\]].*$"
  5. }
  6. }

For example, if (true) { matches increaseIndentPattern, then if you press Enter after the open bracket {, the editor will automatically indent once, and your code will end up as:

  1. if (true) {
  2. console.log();

If there is no indentation rule set for the programming language, the editor will indent when the line ends with an open bracket and outdent when you type a closing bracket. The bracket here is defined by brackets.

Notice that editor.formatOnPaste setting is controlled by the DocumentRangeFormattingEditProvider and not affected by auto indentation.