Home > OS >  [Dart][Flutter] How to exclude multi-lines from "Format On Save" in Visual Studio Code?
[Dart][Flutter] How to exclude multi-lines from "Format On Save" in Visual Studio Code?

Time:02-01

I am working on a Flutter project where the Formatting is set to the default Dart recommends VSCode formatter setting. Is there any way to exclude multi-lines from formatting when VS is set to format modified code/file on save?

I refer to this answer to exclude each line. The code below worked:

      msg.pose.covariance = [
        0.25, 0.0, 0.0, 0.0, 0.0, 0.0, // noqa
        0.0, 0.25, 0.0, 0.0, 0.0, 0.0, // noqa
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, // noqa
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, // noqa
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, // noqa
        0.0, 0.0, 0.0, 0.0, 0.0, 2.25 // noqa
      ];

Now I want to exclude multi-lines with similar this answer. The expected code like Python (black formatter):

      // dart: fmt_off
      msg.pose.covariance = [
        0.25, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.25, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 2.25
      ];
      // dart: fmt_on

CodePudding user response:

As explained in the Dart formatter's FAQ, you just need to have a single comment within the collection literal to prevent the formatter from reformatting it:

So, if you have a collection that you have carefully split into lines, add at least one line comment somewhere inside it to get it to preserve all of the newlines in it.

  • Related