Skip to content

neovim CSharp

lamt edited this page May 22, 2023 · 13 revisions

neovim CSharp

Settings

Category Name
Language Server omnisharp
Debugger netcoredbg
Linter omnisharp(roslyn-analyzers)
Formatter omnisharp(dotnet-format)

Compiling

dotnet new console -o hello-cs
cd hello-cs
# Optional
# You can keep the version of asdf.
asdf local dotnet <your version>
dotnet build
# Optional
# Run
dotnet run

Debugging

Default configuration

vi Program.cs
  1. F9: Set Breakpoint
  2. F5: Start Debug
  3. Input net<your version>/hello-cs.dll
  4. Stop at Breakpoint
  5. F10: Step over
  6. Exit Debug

.vscode/launch.json

.vscode/launch.json is possible to make it by opening the project root directory in VSCode.

nvim-dap load .vscode/launch.json by default. And other debug adapters can do the same (need to set up the appropriate .vscode/launch.json).

mkdir .vscode
vi .vscode/launch.json
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": ".NET Core Launch (console)",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "build",
      "program": "${workspaceFolder}/bin/Debug/net<your version>/hello-cs.dll",
      "args": [],
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "stopAtEntry": false
    }
  ]
}
vi Program.cs
  1. F9: Set Breakpoint
  2. leader d c: Show configurations
  3. Input coreclr: .NET Core Launch (console)
  4. Stop at Breakpoint
  5. F10: Step over
  6. Exit Debug

(F6 : Run with the last configuration)

Linting

Unlike VSCode, Linter and Formatter will not work without this setting.

vi omnisharp.json
{
  "RoslynExtensionsOptions": {
    "enableAnalyzersSupport": true
  },
  "FormattingOptions": {
    "enableEditorConfigSupport": true
  }
}
vi Bad.cs
public class Bad
{
    public int GetNum()
    {
        return 1;
    }
}

Show the troule window (leader x x).

See: omnisharp-roslyn -- Configuration.

See also: roslyn-analyzers.

See also: Microsoft -- Overview of .NET source code analysis.

Formatting

vi .editorconfig
[*.cs]
indent_size = 2
indent_style = space
vi Bad.cs

:w or space f.

# Option
# Also runnable on the command line
dotnet format

See: Microsoft -- editorconfig.

See also: Roslyn team's editorconfig.

Additional Information

See: StyleCopAnalyzers.