Is it possible to extend the YAML Formatter behavior by somehow extending the YAMLFormattingContext? The bundled YAML Formatter is a currently a bit limited. One of the things I'd like to add is to remove any empty lines between a key:value map and its first indented key.
The reason for this is that we're using a mapping to determine imports, and by now implementing the 'Optimize imports' intention I can remove unused imports automagically, however, the parent block isn't formatter nicely afterwards:
When the unusedMember is removed and subsequently the path-to-some-file: key-value container, the result is:
The most consistent way to fix this (in my opinion) would be to use the document formatter and have the settings such that the number of blank lines between a key:value and its first child is always 0.
My first guess was to simple register another lang.formatter for yaml and have it extend the YAMLFormattingModelBuilder. Then override the createModel with:
PsiFile file = formattingContext.getContainingFile();
CodeStyleSettings settings = formattingContext.getCodeStyleSettings();
Block rootBlock = createBlock(new MyCustomYamlFormattingContext(settings, file), formattingContext.getNode());
return new DocumentBasedFormattingModel(rootBlock, settings, file);
The problem is that the MyCustomYamlFormattingContext should be able to extend the YAMLFormattingContext and YAMLFormattingContext is not public.
Since the YAML formatter is quite extensive, especially with regards to all the injected language support, I really want to re-use the current implementation as much as possible.
Any suggestions how to continue?