コードの自動フォーマッティングの設定を行う
環境
Macbook Pro sierra 10.12.6
VSCode 1.19.1
やりたかったこと
editor.formatOnTypeを有効にすると以下のようにフォーマットされてしまう。
理想
> public static bool IsSampleCode() {
> > if (true) {
> > > // 左側がタブ
> > } else {
> > >
> > }
> }
現実
・・・・public static bool IsSampleCode()
・・・・{
・・・・・・・・if (true)
・・・・・・・・{
・・・・・・・・・・・・// 左側がスペース
・・・・・・・・}
・・・・・・・・else
・・・・・・・・{
・・・・・・・・・・・・
・・・・・・・・}
・・・・}
設定ファイルの追加
~/.vscode/extensions/ms-vscode.csharp-1.13.1
の下にbin/omnisharp/config.jsonがあると書かれている記事がありますが、
自分のMacにはありませんでした。
なので、
~/.omnisharp以下にomnisharp.jsonを作成
omnisharp.json(設定ファイル)の中身
{
"FormattingOptions": {
"NewLine": "\n",
"UseTabs": true,
"TabSize": 4,
"IndentationSize": 4,
"SpacingAfterMethodDeclarationName": false,
"SpaceWithinMethodDeclarationParenthesis": false,
"SpaceBetweenEmptyMethodDeclarationParentheses": false,
"SpaceAfterMethodCallName": false,
"SpaceWithinMethodCallParentheses": false,
"SpaceBetweenEmptyMethodCallParentheses": false,
"SpaceAfterControlFlowStatementKeyword": true,
"SpaceWithinExpressionParentheses": false,
"SpaceWithinCastParentheses": false,
"SpaceWithinOtherParentheses": false,
"SpaceAfterCast": false,
"SpacesIgnoreAroundVariableDeclaration": false,
"SpaceBeforeOpenSquareBracket": false,
"SpaceBetweenEmptySquareBrackets": false,
"SpaceWithinSquareBrackets": false,
"SpaceAfterColonInBaseTypeDeclaration": true,
"SpaceAfterComma": true,
"SpaceAfterDot": false,
"SpaceAfterSemicolonsInForStatement": true,
"SpaceBeforeColonInBaseTypeDeclaration": true,
"SpaceBeforeComma": false,
"SpaceBeforeDot": false,
"SpaceBeforeSemicolonsInForStatement": false,
"SpacingAroundBinaryOperator": "single",
"IndentBraces": false,
"IndentBlock": true,
"IndentSwitchSection": true,
"IndentSwitchCaseSection": true,
"LabelPositioning": "oneLess",
"WrappingPreserveSingleLine": true,
"WrappingKeepStatementsOnSingleLine": true,
"NewLinesForBracesInTypes": true,
"NewLinesForBracesInMethods": true,
"NewLinesForBracesInProperties": true,
"NewLinesForBracesInAccessors": true,
"NewLinesForBracesInAnonymousMethods": true,
"NewLinesForBracesInControlBlocks": true,
"NewLinesForBracesInAnonymousTypes": true,
"NewLinesForBracesInObjectCollectionArrayInitializers": true
"NewLinesForBracesInLambdaExpressionBody": true,
"NewLineForElse": true,
"NewLineForCatch": true,
"NewLineForFinally": true,
"NewLineForMembersInObjectInit": true,
"NewLineForMembersInAnonymousTypes": true,
"NewLineForClausesInQuery": true
}
}
設定ファイルを再読込
- VSCode上でcmd+shift+pで検索窓を出す
- 「restart omnisharp」でエンター
以上
記事検索用
formatter
tabs
vscode