Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude multi-lines from "Format On Save" in Visual Studio Code?

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
like image 300
Đức Hải Trần Avatar asked Jun 06 '26 22:06

Đức Hải Trần


1 Answers

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.

like image 175
jamesdlin Avatar answered Jun 10 '26 08:06

jamesdlin