News for hackers. Stuff that matters.
Re:Ask HN: What extensions/packs of VS Code are must for web... by Waterluvian on Friday November 19, 2021 @11:22AM (#29278843)
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!

Re:Ask HN: What extensions/packs of VS Code are must for web... by rolisz on Friday November 19, 2021 @11:32AM (#29278992)
I 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.
Re:Ask HN: What extensions/packs of VS Code are must for web... by goodells on Friday November 19, 2021 @11:39AM (#29279067)
Mine 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.
Re:Ask HN: What extensions/packs of VS Code are must for web... by Waterluvian on Friday November 19, 2021 @11:43AM (#29279128)
The 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.

Re:Ask HN: What extensions/packs of VS Code are must for web... by trevyn on Friday November 19, 2021 @11:02PM (#29285458)
I’m a big fan of Error Lens.
Re:Ask HN: What extensions/packs of VS Code are must for web... by Waterluvian on Friday November 19, 2021 @11:35AM (#29279011)
I’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.
Re:Ask HN: What extensions/packs of VS Code are must for web... by rolisz on Friday November 19, 2021 @11:46AM (#29279178)
I'm using Python and PyCharm. It happily underlines the invalid syntax and then reformats things more or less randomly.
Re:Ask HN: What extensions/packs of VS Code are must for web... by randomluck040 on Friday November 19, 2021 @07:20PM (#29284387)
There 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.
Re:Ask HN: What extensions/packs of VS Code are must for web... by Ciantic on Friday November 19, 2021 @01:36PM (#29280646)
I 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

Re:Ask HN: What extensions/packs of VS Code are must for web... by belav on Sunday November 21, 2021 @12:32PM (#29298013)
I 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.

Re:Ask HN: What extensions/packs of VS Code are must for web... by frosted-flakes on Friday November 19, 2021 @12:20PM (#29279648)
I 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.
Re:Ask HN: What extensions/packs of VS Code are must for web... by dsteinman on Friday November 19, 2021 @11:44AM (#29279150)
Prettier has a VSCode extension to autoformat on save.

https://prettier.io/docs/en/install.html

Re:Ask HN: What extensions/packs of VS Code are must for web... by Waterluvian on Friday November 19, 2021 @11:46AM (#29279187)
Yes, 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
  }
Re:Ask HN: What extensions/packs of VS Code are must for web... by 0x008 on Friday November 19, 2021 @03:49PM (#29282270)
What is the benefit of using prettier as opposed to the built in typescript formatter?
Re:Ask HN: What extensions/packs of VS Code are must for web... by Isthatablackgsd on Friday November 19, 2021 @04:06PM (#29282495)
I 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.
Re:Ask HN: What extensions/packs of VS Code are must for web... by slmjkdbtl on Saturday November 20, 2021 @01:44PM (#29290703)
I'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
Re:Ask HN: What extensions/packs of VS Code are must for web... by eurasiantiger on Sunday November 21, 2021 @01:52AM (#29294659)
Your boss must love that you’re essentially doing work just to throw it away.
Re:Ask HN: What extensions/packs of VS Code are must for web... by iudqnolq on Saturday November 20, 2021 @03:08AM (#29286425)
How do you make this work with a project you don't control?
Re:Ask HN: What extensions/packs of VS Code are must for web... by dpweb on Friday November 19, 2021 @06:23PM (#29283899)
I 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.
Re:Ask HN: What extensions/packs of VS Code are must for web... by vxNsr on Friday November 19, 2021 @11:46AM (#29279183)
Are you using the default built in beautifier in vscode? Does it allow you to set your preferred styles?
Re:Ask HN: What extensions/packs of VS Code are must for web... by Waterluvian on Friday November 19, 2021 @11:50AM (#29279226)
Python: 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.

Re:Ask HN: What extensions/packs of VS Code are must for web... by nknealk on Friday November 19, 2021 @11:47AM (#29279193)
Does this work well for SQL?
Re:Ask HN: What extensions/packs of VS Code are must for web... by CSSer on Friday November 19, 2021 @12:10PM (#29279520)
Are 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.