how to change dart line length in vscode when formatting dart files?
Asked Answered
B

8

98

i'm using VS Code for flutter development and one issue i have is code formatting (styling) which is not as customizable as it is in android-studio. my problem is that each time vs code saves dart files it will break my lines very short like below:

var tuple =
       settings.arguments as Tuple3<String, int, Field>;

obviously this is what i want :

var tuple = settings.arguments as Tuple3<String, int, Field>;

how can i solve this problem?

Backslide answered 23/12, 2019 at 13:47 Comment(0)
B
45

To change the line length in VSCode

open settings.json and add the following lines

"dart.lineLength": 120,
"[dart]": {
    "editor.rulers": [
        120
    ],
}

SIDE NOTE: if you wish to change the dart line length for a single project that you have in VSCode create a .vscode/settings.json file and add configuration written above in that file.

to change dart line length in Android Studio go to

Settings > Editor > Code Style > Dart and change line length

enter image description here

Backslide answered 30/9, 2021 at 15:20 Comment(2)
Is it possible to customize that for a single file?Monson
@MarwinLebensky I don't think soBackslide
M
169

You need to change 2 settings in settings.json:

"dart.lineLength": 150,
"[dart]": {
    "editor.rulers": [
        150
    ],
}

If you do not change the second one, you'll still see the vertical "ruler" at 80 chars width.

Metamorphism answered 13/7, 2020 at 10:28 Comment(6)
I've added both of these to my settings json and still not working ... it's weird, it used to work and then after reinstalling dart it no longer works no matter whatMusic
The " is missing before the [dart]" settings. This solution works for me. Chek for duplicates in the settings.json file tooMusic
@Music You need a pair of curly brackets { } closing everything.Antre
But how to get rid of the ruler altogether? I want the lineLength limit. I just don't want the ruler.Raver
There is User & Workspace setting in vscode, make sure you set both, workspace setting overrides user settingKakaaba
To completely remove it use the value 0, to remove the ruler you can just remove all values from editor.rulers.Vernverna
M
71

It seems like you are hitting line length limit.

Default maximum line length is classic 80 characters, so for your code you would need a lot of padding to hit the limit so formatter would break the line. If this is an issue - consider splitting your code.

This is properly formatted:

class MyApp {
  void insideclass() {
    if (true) {
      if (true) {
        if (true) {
          if (true) {
            if (true) {
              if (true) {
                if (true) {
                  if (true) {
                    var tuple =
                        settings.arguments as Tuple3<String, int, Field>;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

class MyApp2 {
  void insideclass() {
    if (true) {
      if (true) {
        if (true) {
          if (true) {
            if (true) {
              if (true) {
                if (true) {
                  var tuple = settings.arguments as Tuple3<String, int, Field>;
                }
              }
            }
          }
        }
      }
    }
  }
}

However if 80 is actually too small for you, you can also change that in VSCode in the extension's settings.

VSCode Dart&Flutter extension settings - line length

Mogador answered 23/12, 2019 at 14:41 Comment(0)
B
45

To change the line length in VSCode

open settings.json and add the following lines

"dart.lineLength": 120,
"[dart]": {
    "editor.rulers": [
        120
    ],
}

SIDE NOTE: if you wish to change the dart line length for a single project that you have in VSCode create a .vscode/settings.json file and add configuration written above in that file.

to change dart line length in Android Studio go to

Settings > Editor > Code Style > Dart and change line length

enter image description here

Backslide answered 30/9, 2021 at 15:20 Comment(2)
Is it possible to customize that for a single file?Monson
@MarwinLebensky I don't think soBackslide
M
4

For Android Studio

Android Studio -> Preferences -> Editor -> Code Style -> Dart -> Line length
Miscall answered 7/2, 2022 at 9:35 Comment(0)
S
4

For VSCode in settings: photo

For Android Studio: photo

Saddlery answered 14/9, 2022 at 6:42 Comment(1)
Welcome to SO! Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should vote it up once you have enough reputationYvette
L
3

Your settings.json should be like so,

{

 "dart.lineLength": 150,
    "[dart]": {
        "editor.defaultFormatter": "Dart-Code.dart-code",
        "editor.formatOnSave": true,
        "editor.formatOnType": true,
        "editor.tabSize": 2,
        "editor.rulers": [
            220
        ],
        "editor.detectIndentation": false,
        "editor.selectionHighlight": false,
        "editor.suggest.snippetsPreventQuickSuggestions": false,
        "editor.suggestSelection": "first",
        "editor.tabCompletion": "onlySnippets",
        "editor.wordBasedSuggestions": false
    },}

and after saving your settings you need to reload your project or reformat by:

  1. right click

  2. select format

Lesbian answered 21/10, 2022 at 10:36 Comment(0)
N
1

It's worth noting there is also a linting rule for enforcing LESS THAN 80 characters: lines_longer_than_80_chars

As other people said, you have to customize this value in the IDE settings, since there isn't any way to enforce another value. But if you want to enforce less than 80 characters, you can use the lint rule above.

I wanted to allow more than 80 characters, but I won't change it because of this interesting statement from this lint rule documentation:

If you really find yourself wanting lines longer than 80 characters, our experience is that your code is likely too verbose and could be a little more compact.

Notebook answered 23/10, 2023 at 18:7 Comment(0)
L
-2

Its a line kind of to show you where you should cut your code just change the "editor.rulers: [ 150 ]" in setting.json which you can find if you go to setting and search for example font ligature then click the edit in settings text which is underlined but there are many ways to find it of course then it will disappear. Its so annoying to look at.

Logy answered 20/8, 2020 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.