microsoft/compose-language-service

Formatting Documents does not fix indentation

Closed this issue · 4 comments

When the compose file looks like this
image

Pressing shift alt F gives me this

image

Also, a weird quirky bug, but after I format the document, sometimes when hitting control Z (undo)

I get null on some of the attributes. This may be because I hadn't put a value there yet but I'm not totally sure

All of this is working as designed. In your above example, dockerfile is a value in the context map. That it's an incorrect compose document is not really relevant to formatting; the job of formatting is to fix spacing on an already-syntactically correct document.

null will show up when a map has no values; this is also by-design.

image

and after Ctrl + Shift + F the document still looks the same

image

After hovering over the next red line, it is giving a corrective error, but our document formatter doesn't fix this. It was my assumption that this document formatter would be a mix of Prettify with Format. I haven't been able to understand where I can use the Ctrl + Shift + F, because it hasn't really done much besides closing down spaces yet.

Is format document not our code? Does it have a different job than what I'm thinking? I was thinking you wrote in some kind of Prettify for Compose files? In the blog I originally said "Format a compose document with proper indentation", but I'm seeing that the behavior is different than I thought.

The formatter intentionally does nothing if the document is not syntactically correct, because we would just as likely make it worse as fix it. We could show an error toast in this case, but I don't think that's a good idea because we can't tell the difference between formatting initiated manually (i.e. Shift+Alt+F) and formatting initiated automatically (i.e. on save). Having the toast appear all the time when saving would be obnoxious; I'd want it to only appear on manual formatting.

Keep in mind, there's a difference between being syntactically correct (valid YAML), vs. being schematically correct (valid Compose document). For instance, this is syntactically correct but schematically incorrect:

services:
  foo:
    build:
      image: redis

The formatter will never do anything as far as schema fixing goes. It would take months of work to support that; the RoI is just not there.

For some context: the formatter works by parsing the YAML and then rewriting the document with a given indentation depth; if the YAML doesn't parse completely (i.e. syntax errors) it won't be able to do so. AFAIK this is how most formatters work but with varying tolerances for syntax errors.