tintinweb / vscode-interactive-graphviz

Interactive Graphviz Dot Preview for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=tintinweb.graphviz-interactive-preview
GNU General Public License v3.0
143 stars 22 forks source link

Formatter deletes comments and all whitespace on save #110

Closed protosam closed 1 year ago

protosam commented 2 years ago

The problem is that if your graphviz file looks like this and you save it:

strict digraph {
  /* This block comment is important or something. */

  // I want to remember this later....
  // layout = "dot";
  node [
    fillcolor = "#ffffff",
  ];

  // general note related to nothing important

  // cool info here about this connection
  "hello" -> "graph";
}

Gets transformed into this:

strict digraph {
  node [
    fillcolor = "#ffffff",
  ];
  // cool info here about this connection
  "hello" -> "graph";
}

The assertions being made by the formatter are really wild.

Temporary solution for users is to just disable it.

bigbug commented 2 years ago

Problem confirmed. Seems to be caused by the underlying libraries https://github.com/ts-graphviz/parser and https://github.com/ts-graphviz/ts-graphviz. I will try to investigate this further and report the problems there.

bigbug commented 2 years ago

We used the parser from ts-graphviz/parser and the toDot method from ts-graphviz/ts-graphviz. Inbetween these two the comments get lost.

The following code uses parse and stringify from ts-graphviz/parser. This fixes the comments getting lost. However, the indentation seems to be off.

const d = AST.parse(document.getText());
const dot = AST.stringify(d);
bigbug commented 2 years ago

Output of formatter has been adapted. Given the input from the start of the issue, the formatter now generates the following code by default (an option called graphviz-interactive-preview.format.condenseAttributes has been introduced, which has an impact on how attributes are shown):

strict digraph {
    /**
     * This block comment is important or something.
     */

    // I want to remember this later....
    // layout = "dot";

    node [fillcolor = "#ffffff";];

    // general note related to nothing important

    // cool info here about this connection
    "hello" -> "graph";
}
protosam commented 2 years ago

Nice update! Found bug with the changes. Going to continue to conversation in the PR.