HOME » » » » HERE

サムネイル画像
コラム_VSCodeでOmnisharpが効かなくなって困ったときのはなし(この画像は、https://www.pakutaso.com/さまから借用しています)

こんにちわ、横浜すみっこクリエイターととです。

今回は、最近なぜかVS Codeで入力補完が効かなくなったり、コードフォーマッターが妙な動きをし始めたので、その是正方法についてまとめました。

Contents

  1. 動きがおかしくなった原因
  2. omnisharpがなくなって困ること
  3. 方法1:C#のバージョンダウン
  4. 方法2:omnisharpの復活(フォーマッターは非適用)
  5. 方法3:IntelliCodeとEditorConfigの導入
  6. 「omnisharp.json」の設定例
  7. 参考サイト

動きがおかしくなった原因

プログラミングコラム(拡張機能「C#」のバージョンアップ) 拡張機能「C#」のバージョン変更

拡張機能C#のメジャーバージョンアップに伴い、omnisharpの挙動が変わったことが原因のようです。

omnisharpがなくなって困ること

omnisharpは、主に次の2つをつかさどっていました。

  • 入力補完機能(IntelliSense)
  • コードフォーマット

拡張機能の変更により、omnisharpがそのまま使用できなくなったため、上記機能も挙動が変わってしまっています。

うまく補完機能が効かなくなり、omnisharp.json で指定していたコードフォーマッターも無効になってしまっていました。

これらに対処する方法を解説していきます。

方法1:C#のバージョンダウン

拡張機能C#のバージョンを下げることで、今まで通りの挙動にすることができます。

具体的には、拡張機能C#のバージョンを1.260以前のものに戻します。

プログラミングコラム(別バージョンをインストール) 別バージョンを選択

画面左のアクティビティバーのwindow(拡張機能)をクリック、拡張機能C#をクリックして、拡張機能の説明ページを開きます。

アンインストール横のをクリック、別のバージョンをインストール...を選択します。

プログラミングコラム(バージョンのリスト) バージョンリスト

表示されるバージョンリストから、1.26.0をクリックします。

これで入力補完(IntelliSense)コードフォーマットomnisharp.jsonで設定したものが有効になります。

最後のほうに私の設定しているomnisharp.jsonの中身を公開しておきますので、参考にしてみてください。

方法2:omnisharpの復活

次に紹介する方法は、C#拡張機能のバージョンは最新のまま、omnisharpを有効にできます。ただコードフォーマッターは効かないままなので、あまり有用な手段ではないかもしれません。

詳細は割愛しつつ、手順のみ紹介します。

  1. 設定(setting.json)で下記設定
    • "omnisharp.useModernNet": false
    • "dotnet.server.useOmnisharp": true
  2. 拡張機能C# Dev Kitインストール済みの場合、無効 or アンインストール
  3. VS Code再起動
プログラミングコラム(omnisharpがインストールされます) omnisharpのインストール

再起動時にomnisharpが再インストールされていることが分かります。

これにより入力補完(IntelliSense)は復活しますが、コードフォーマットについては適用されない状態になります。

方法3:IntelliCodeとEditorConfigの導入

本命

この方法が本命となります。

  • 予測機能IntelliCodeへ移行
  • 拡張機能EditorConfigを導入し、コードフォーマットを規定

IntelliCodeは、IntelliSenseの強化版のようなもので、AI支援の入った入力補完機能です。

具体的な手順は、次のとおりです。

  • 拡張機能C# Dev Kitのインストール
  • 拡張機能EditorConfigのインストール
  • 設定(setting.json)を変更
  • .editorconfigファイルの作成

次に詳細を解説していきます。

「C# Dev Kit」のインストール

プログラミングコラム(拡張機能を検索します) 拡張機能C# Dev Kitの検索

画面左のアクティビティバーのwindow(拡張機能)をクリックして、検索バーにC# Dev Kitと入力します。

プログラミングコラム(拡張機能のインストール) 拡張機能C# Dev Kitのインストール
C# Dev Kitをインストールすると、自動的にIntelliCode for C# Dev Kitもインストールされます。 C# Dev Kitは、特定のライセンス下で使用される必要があります。「C# Dev Kitライセンス規約」によると、250のユーザー数もしくは100万米ドルの年間収益を有する場合は有償となります。

拡張機能「EditorConfig」のインストール

1つ前と同様の手順で、拡張機能EditorConfig for VS Codeをインストールします。

プログラミングコラム(拡張機能のインストール) 拡張機能EditorConfig for VS Codeのインストール

EditorConfig for VS Codeは、コードフォーマッターツールの1つで、プログラミング言語それぞれにコーディングスタイルを規定できるものです。

C#でのフォーマッターにEditorConfig for VS Codeを使用することについては、VS Codeの公式ページにも掲載されており、公式なものと思われます。 余談ですが、EditorConfigの本領は、エディター間でのコーディングスタイルの統一にあります。複数人で開発する際に、エディターが異なっていてもこの拡張機能を解することで、フォーマットを維持することができます。

設定(setting.json)を変更

プログラミングコラム(設定の開き方)

上の図のとおり、設定(setting.json)を開きます。

次の記述がある場合、行ごと削除してください。

  1. "omnisharp.useModernNet"
  2. "dotnet.server.useOmnisharp"
  3. "omnisharp.useEditorFormattingSettings"

「.editorconfig」ファイルの作成

プロジェクトのルートフォルダに.editorconfigという名前のファイルを作成します。

Unityでは、プロジェクトフォルダーの一番上の階層になります。

ファイルの作成ができたら、下記リンクの表記にのっとりファイルの中身を記述します。

フォーマッターの設定例

私が使用しているコーディングスタイルを羅列しておきますので、参考にしてみてください。

root = true

# ---------- CSharp formatting rules: ----------
[*.cs]
charset = utf-8
end_of_line = lf
indent_style = tab
tab_width = 4
indent_size = 4

# ----- New Line Options -----
csharp_new_line_before_open_brace = none
csharp_new_line_before_else = false
csharp_new_line_before_catch = false
csharp_new_line_before_finally = false
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# ----- Indentation Options -----
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_switch_labels = true
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = false
csharp_indent_labels = no_change

# ----- Spacing Options -----
csharp_space_before_comma = false
csharp_space_after_comma = true
csharp_space_before_dot = false
csharp_space_after_dot = false
csharp_space_between_parentheses = none
csharp_space_around_binary_operators = before_and_after
csharp_space_before_open_square_brackets = false
csharp_space_between_square_brackets = false
csharp_space_between_empty_square_brackets = false
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_before_semicolon_in_for_statement = false
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_declaration_statements = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true

# ----- Wrap Options -----
csharp_preserve_single_line_statements = false
csharp_preserve_single_line_blocks = true

「omnisharp.json」の設定例

最後にomnisharp.jsonの設定について、私が使用していたコーディングスタイルを記載しておきます。方法1を適用する方はこちらを参考にしてください。

{
  "FormattingOptions": {
    "EnableEditorConfigSupport": false,
    "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,
    "IndentSwitchCaseSectionWhenBlock": true,
    "LabelPositioning": "NoIndent",
    "WrappingPreserveSingleLine": true,
    "WrappingKeepStatementsOnSingleLine": false,
    "NewLinesForBracesInTypes": true,
    "NewLinesForBracesInMethods": false,
    "NewLinesForBracesInProperties": false,
    "NewLinesForBracesInAccessors": false,
    "NewLinesForBracesInAnonymousMethods": false,
    "NewLinesForBracesInControlBlocks": false,
    "NewLinesForBracesInAnonymousTypes": true,
    "NewLinesForBracesInObjectCollectionArrayInitializers": false,
    "NewLinesForBracesInLambdaExpressionBody": false,
    "NewLineForElse": false,
    "NewLineForCatch": false,
    "NewLineForFinally": false,
    "NewLineForMembersInObjectInit": true,
    "NewLineForMembersInAnonymousTypes": true,
    "NewLineForClausesInQuery": true,
    "OrganizeImports": true
  }
}

参考サイト

記事執筆にあたり、次のサイトを参考にさせていただきました。

まとめ

今回は、VS Codeの拡張機能C#のバージョンアップによって、入力補完やコードフォーマッターが上手く機能しなくなった際の解決方法について解説してきました。

私のように、Unityでのゲーム開発にVS Codeを使用していて、omnisharpを頼りにしている場合に必ず直面する問題ですが、解決方法がまとまっているトコロを見つけられなかったので、まとめてみました。

どなたかの助けになれば幸いです。

コメント