zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
49.29k stars 2.99k forks source link

Add a setting to keep brackets on the same line instead of a new line when formatting #11877

Open leftbones opened 5 months ago

leftbones commented 5 months ago

Check for existing issues

Describe the feature

I've noticed if you are putting something in brackets and you delete the closing bracket that Zed put there for you, then add one yourself, the open bracket is automatically moved one line down. Everyone has their own preferences, mine is to have all of the open brackets on the same line as the stuff before them, rather than the next line, a setting for this would be nice!

If applicable, add mockups / screenshots to help present your vision of the feature

https://github.com/zed-industries/zed/assets/77086654/4ce0aa60-614c-4331-bd94-2cf9930968b6

0x2CA commented 5 months ago

Hi, this is usually a formatting style setting for lsp, which should normally be achieved by configuring lsp to

JosephTLyons commented 5 months ago

Right - Zed isn't the thing making the changes in this case - which language server are you using?

leftbones commented 5 months ago

Right - Zed isn't the thing making the changes in this case - which language server are you using?

Ah! Sorry, I wasn't really sure, I need to study more about how language servers work in an application like Zed.

It looks like the C# extension in Zed uses OmniSharp, hopefully there's a way to customize this behavior! I know it has to be possible, because I've also used OmniSharp in Visual Studio Code and didn't have this problem.

dyskette commented 2 months ago

To configure omnisharp, you can use an omnisharp.json file. More information here: https://github.com/OmniSharp/omnisharp-roslyn/wiki/Configuration-Options

The location of the file is the following:

Within the file you can enable .editorconfig support.

{
  "FormattingOptions": {
    "EnableEditorConfigSupport": true
  }
}

You might be interested in enabling the roslyn analyzers. That way zed shows warnings and code actions for omnisharp.

{
  "RoslynExtensionsOptions": {
    "enableAnalyzersSupport": true
  }
}

Zed's inlay hints can be enabled for omnisharp in the same json file with the inlayHintsOptions section, within the roslyn extension options.

{
  "RoslynExtensionsOptions": {
    "inlayHintsOptions": {
        "enableForParameters": true,
        "forLiteralParameters": true,
        "forIndexerParameters": true,
        "forObjectCreationParameters": true,
        "forOtherParameters": true,
        "suppressForParametersThatDifferOnlyBySuffix": false,
        "suppressForParametersThatMatchMethodIntent": false,
        "suppressForParametersThatMatchArgumentName": false,
        "enableForTypes": true,
        "forImplicitVariableTypes": true,
        "forLambdaParameterTypes": true,
        "forImplicitObjectCreation": true
    }
  }
}

The complete file can look like this:

{
  "FormattingOptions": {
    "EnableEditorConfigSupport": true
  },
  "RoslynExtensionsOptions": {
    "enableDecompilationSupport": true,
    "enableImportCompletion": true,
    "enableAnalyzersSupport": true,
    "inlayHintsOptions": {
        "enableForParameters": true,
        "forLiteralParameters": true,
        "forIndexerParameters": true,
        "forObjectCreationParameters": true,
        "forOtherParameters": true,
        "suppressForParametersThatDifferOnlyBySuffix": false,
        "suppressForParametersThatMatchMethodIntent": false,
        "suppressForParametersThatMatchArgumentName": false,
        "enableForTypes": true,
        "forImplicitVariableTypes": true,
        "forLambdaParameterTypes": true,
        "forImplicitObjectCreation": true
    }
  }
}

Notes:


Once you have enabled .editorconfig support, create or edit an .editorconfig file in your project, and set formatting and analyzer options in there.

For example, to keep the braces in the same line for C# files:

[*.cs]
csharp_new_line_before_open_brace = none

This is the result:

image

Cheers.