0

I'm using Visual Studio Code 1.9.0 on Kubuntu and have issues when formatting code manually. From here I learned, that Ctrl + Shift + I is the shortcut for code formatting. But it doesn't format the code well, instead it's more uglified!

As an example, I have the following C# model saved in a .cs file

namespace MyApp
{
    public class TimetableSubject
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public TimetableSubject(int? id = null, string name = "")
        {
            Name = name;
            Id = id.HasValue ? id.Value : 0;
        }
    }
}

Now I press the keyboard shortcut and my code is a real mess:

namespace MyApp {
public class TimetableSubject {
public int Id {get; set; }
public string Name {get; set; }

public TimetableSubject(int?id = null, string name = "") {
Name = name; 
Id = id.HasValue?id.Value:0; 
}
}
}

What am I doing wrong? I want clean format according to the C# conventions, like Visual Studio does on Ctrl+E, Ctrl+D shortcut. I already installed the official C# extension called ms-vscode.csharp. This gave me VS features like showing the usage of methods/fields, but no working formatting.

Community
  • 1
  • 1
Lion
  • 16,606
  • 23
  • 86
  • 148

1 Answers1

0

Found a way: Press Ctrl + P, enter >Format, choose Formatter. Now formatter.json file opens, which contains rules, how Visual Studio Code would format code. Here you can append own rules for C# files like this:

"cs": {
     "indent_size": 4,
      "indent_char": " ",
}
Lion
  • 16,606
  • 23
  • 86
  • 148