4 ms·
Not exactly an extension, but a practice that I'm shocked many people don't seem to use is to set up autoformatting on save. I will hit ctrl+s after every few
by Waterluvian 5y ago
Not exactly an extension, but a practice that I'm shocked many people don't seem to use is to set up autoformatting on save.
I will hit ctrl+s after every few lines of code or so, allowing my "coding wingman" to tidy things up as I go. It releases me from caring, in the slightest, about formatting, so I'm focused entirely on writing syntactically valid code, and letting the formatter worry about the deterministic minutiae that is formatting.
The reason I like doing this in real-time rather than some `run formatting before committing` workflow is that I benefit instantly from tidy code as I'm writing it.
If you are _ever_ thinking about formatting and you spend time manipulating proper spacing and layout, semicolons, or other language-specific quirks yourself, you may be wasting time that could be spent on the non-trivial parts of writing code. Consider trying it!
- rolisz 5y agoI just tried enabling that this week, but I found that I often press Ctrl-S out of habit, even when I have syntactically invalid code and then the autoformatter messes things up royally and I get lost. For now, I just memorized the shortcut for autoformatting and I'm running it manually when I feel the need to "tidy up" the code.
- Waterluvian 5y agoI’m curious what language you’re working on and what formatter it uses? In my experience many formatters will not run if the AST is invalid.
- rolisz 5y agoI'm using Python and PyCharm. It happily underlines the invalid syntax and then reformats things more or less randomly.
- goodells 5y agoMine won't do any auto-formatting when the syntax is invalid, so when I hit CMD + S and it _doesn't_ format it's a cue to start looking for errors.
- Waterluvian 5y agoThe only issue I have with this is that I wish it would chirp more loudly when there's a syntax error. Every so often I don't notice red underline and I begin wondering why my autoformatter and autocomplete aren't working anymore. Maybe a red border or something any time a file's syntax is invalid.
- trevyn 5y agoI’m a big fan of Error Lens.
- randomluck040 5y agoThere is a package called „black“ (pip install black) that formats code according to PEP8 (most of the time). You might take a look if you’re interested. I’m using it occasionally and it didn’t do anything royally stupid until now. If the code isn’t syntactically correct it won’t run.
- dsteinman 5y agoPrettier has a VSCode extension to autoformat on save. https://prettier.io/docs/en/install.html https://prettier.io/docs/en/install.html
- Waterluvian 5y agoYes, very true. For TypeScript I do use Prettier so yeah, there _is_ an extension involved. For others, this is basically what it takes (plus making the conscious decision to enforce formatting on your repo, which I think you should!): Install the plugin and add this config: "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true }
- 0x008 5y agoWhat is the benefit of using prettier as opposed to the built in typescript formatter?
- Isthatablackgsd 5y agoI believe it is something to do with style formatting than the syntax formatting. I use VSCodium for HTML/CSS and I use Prettier to "style" it in a particular way. Some will style it into a long code and other will style it in chucks.
- vxNsr 5y agoAre you using the default built in beautifier in vscode? Does it allow you to set your preferred styles?
- Waterluvian 5y agoPython: Black TypeScript, JavaScript, JSON: Prettier HTML: Built-in Rust: rust-analyzer + whatever the formatter is built in to Rust's toolchain. XML: XML Tools They all operate using the same API, I think, so setting up `formatOnSave: true` is all I have to do for most of them.
- nknealk 5y agoDoes this work well for SQL?
- CSSer 5y agoAre you asking about pure SQL files (with .sql extension) or SQL embedded in expressions in other file types? The answer is a simple yes for the former with the right extension/configuration. Searching for adpyke.vscode-sql-formatter should give you a good one. Unfortunately, in the latter (and for me, more common) case, I can't find a good solution.
- frosted-flakes 5y agoI like to reformat frequently with Alt+Shift+F, but only save when my code will actually run, so as not to break the running program.
- Ciantic 5y agoI agree 100%. So much so, that I think every professional language should have it. Every tab, newline and space one has to do manually is lost effort. It still amazes me that C# doesn't have good opinionated formatter. It has lot of non-opinionated formatters, but I found one: Csharpier [1], check it out. It's slow, but at least the author is trying. [1]: https://github.com/belav/csharpier https://github.com/belav/csharpier
- belav 5y agoI am curious what you mean by slow. CSharpier started as a prettier plugin, and it was definitely slow then. ~30 minutes for a project with around 10k files if I remember correctly and the formatting was just barely implemented. At this point it is ~40s without --fast, or ~27s with it. CSharpier is stable enough that I should make --fast the default option. Individual file formatting is a bit slow which is the overhead of starting up a new .Net console app every time. NativeAOT is a potential solution, but probably not until .net7. Building plugins that keep csharpier running in the background and piping files to it is the other option. That is one of the next things on my list.
- dpweb 5y agoI just hooked up auto save I was hitting ctrl-s obsessively. I put it at 1000ms but I never see the dirty flag anymore on a tab.
- iudqnolq 5y agoHow do you make this work with a project you don't control?
- slmjkdbtl 5y agoI'm not doing this because I enjoy hand formatting and I find my formatting magnitudes prettier than prettier, only run prettier before commit so I don't have to witness them make my code less readable
- eurasiantiger 5y agoYour boss must love that you’re essentially doing work just to throw it away.