question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[pug] add auto indent

See original GitHub issue
  • VSCode Version: 1.30.0
  • OS Version: Windows 10 x64 (1803)

The auto indentation when using pug is not working. Here is an example of VSCode (1.30.0) vs Sublime Text (3.1.1) and Atom (1.33.0) showing the expected behavior: example_plug_image

And here a gif showing what happen in each one while typing (the tab and space keys were not used): example_pug-2-3

Steps to Reproduce:

  1. Open a tab and select Pug syntax
  2. Type some tags like the next example without using tab or space keys:
h1 test
.example
    ul
        li

Does this issue occur when all extensions are disabled?: Yes

Extensions enabled:
  • advanced-new-file
  • Bookmarks
  • Bracket Pair Colorizer
  • Code Spell Checker
  • colorize
  • Identical Sublime Monokai C# theme and colorizer
  • Path Autocomplete
  • Relative Path
  • Sublime Text Keymap and Settings Importer

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
timdmackeycommented, Jan 6, 2019

Okay, so I found a complete regex pattern for pug in the original repo that vscode’s pug extension is based on: davidrios/pug-tmbundle. This is the relevant file: /Preferences/Miscellaneous.tmPreferences

Here’s the the original regex definition for increaseIndentPattern:

(?x) # enable free-spacing and comments
^\s*(  # start with any whitespace
    ([\w#.-]+(\(.*?\))?  # first tag
    (:\s*[\w#.-]+(\(.*?\))?)*  # additional tags
    \.?$)  # end tag with . or end of line
    |(:[a-z]\w*($|\())  # filters
    |(//-?$)  # multiline comments
    |(-\s+(if|else|while)(\s+|$))  # embedded js control flow
    |((for|if|else if|else|each|until|while|unless|case|when|block|append|prepend)(\s+|$))  # pug block first-class
)

and for decreaseIndentPattern:

^\s*(- )?(else)\b.*$

and here are the reformatted regex definitions:

"indentationRules": {
    "increaseIndentPattern": "^\\s*(([\\w#.-]+(\\(.*?\\))?(:\\s*[\\w#.-]+(\\(.*?\\))?)*\\.?$)|(:[a-z]\\w*($|\\())|(\\/\\/-?$)|(-\\s+(if|else|while)(\\s+|$))|((for|if|else if|else|each|until|while|unless|case|when|block|append|prepend)(\\s+|$)))",
    "decreaseIndentPattern": "^\\s*(- )?(else)\b.*$",
    "disableIndentNextLinePattern": ""
}

I reformatted the regex pattern, removing the comments, newlines, and free-spacing mode modifier, and properly escaped everything. It appears to be working correctly based on my testing. Anyone else care to try it out?

I made a pull request with the incorporated changes: https://github.com/Microsoft/vscode/pull/66099

0reactions
timdmackeycommented, Jan 6, 2019

I made a really hacky fix for myself with the following addition to /extensions/pug/language-configuration.json:

"indentationRules": {
    "increaseIndentPattern": ".",
    "decreaseIndentPattern": "."
}

Ideally, these rules would filter for tags which are not self closing, so that if an element cannot have any children adding a newline doesn’t auto-indent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to make the generated HTML file auto-indent in ...
Ctrl + Alt + L on Windows and Linux. Option + Command + L on Mac OS X. See Reformatting Source Code. Updated....
Read more >
Attributes - Pug.js
If passing in attributes from a mixin call, this is done automatically. Get an update when we release new features.
Read more >
Pug autoformat breaks template : WEB-53742
WEB-32975 Pug: line after buffered code loses its indent on formatting. Consider the following pug/jade ... Thanks for taking the time to create...
Read more >
@prettier/plugin-pug - npm
If you have tags at the top/root that are indented, they will lose indentation due to a technical limitation of pug. Please check...
Read more >
Pug HTML Template Engine: A Beginner's Guide - SitePoint
CodePen, for example, has Pug support baked right in. Simply create a new pen, then select Settings > HTML and choose Pug as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found