Use StandardRB to Format Ruby Code #6560
Replies: 10 comments 16 replies
-
Hey @willkoehler, thanks for this. Heads up I just released 1.25.1 which should fix the |
Beta Was this translation helpful? Give feedback.
-
By the way, since standard ruby ships with an LSP server, it'd be neat to see support for it in Zed. As an example, I recently published this VS Code extension that uses the LSP to avoid shelling out repeatedly for formatting and diagnostics |
Beta Was this translation helpful? Give feedback.
-
This doesn't work for me. I'm getting a "failed to format via external command" error from zed. I'm guessing because stderr is not empty? I put the following in a bash file and call that instead: #!/bin/bash
standardrb --fix --stdin ignored --stderr 2>/dev/null
exit 0 |
Beta Was this translation helpful? Give feedback.
-
I have tried this and also through Solargraph but no matter how I configure this, I get this in the logs:
I have tested calling this externally and it takes about 1.5s to spit out the formatted code. I know that isn't the fastest turn around but it doesn't seem entirely unreasonable. What is Zed's timeout for formatting? Is it configurable? |
Beta Was this translation helpful? Give feedback.
-
Thumbs up on this one, it's one of the last remaining things really holding me back from truly giving an attempt at daily driving Zed. Formatting is important but so are the LSP diagnostics so really the path forward is explicit support for the standardrb LSP or generic, configurable LSP support. EDIT: Standard has recently shipped a Ruby LSP plugin which is intended, in the long term, to replace the built-in Standard LSP, which sounds great. Seems then that the goal should be to make sure that Ruby LSP works as well as possible, including any addons (though I'm not sure how those work, conceptually). |
Beta Was this translation helpful? Give feedback.
-
Threw together a standard-RB extension for Zed. It is very barebones, but it works fine for me. |
Beta Was this translation helpful? Give feedback.
-
Standard now ships with a ruby-lsp plugin and I can see the language server picking it up in the logs, but so far the formatting isn't working. Trying to work out what's going on with that currently. Here's Zed's language server logs: I have the following in my config: "languages": {
"Ruby": {
"tab_size": 2,
"language_servers": ["ruby-lsp", "!solargraph", "!rubocop"],
"formatter": "language_server"
},
},
"lsp": {
"ruby-lsp": {
"initialization_options": {
"enabledFeatures": {
"diagnostics": false
}
}
}
} Completion is working, so ruby-lsp is working, but I'm not seeing anything about formatting in here. Anyone have any ideas? |
Beta Was this translation helpful? Give feedback.
-
I have formatting working through Ruby LSP via the standard addon: "ruby-lsp": {
"initialization_options": {
"experimentalFeaturesEnabled": true,
"formatter": "standard",
"linters": ["standard"],
"enabledFeatures": {
"codeActions": true,
"codeLens": true,
"completion": true,
"definition": true,
"diagnostics": false, // until Zed supports pull-type diagnostics
"documentHighlights": true,
"documentLink": true,
"documentSymbols": true,
"foldingRanges": true,
"formatting": true,
"hover": true,
"inlayHint": true,
"onTypeFormatting": true,
"selectionRanges": true,
"semanticHighlighting": true,
"signatureHelp": true,
"typeHierarchy": true,
"workspaceSymbol": true
},
"featuresConfiguration": {
"inlayHint": {
"implicitHashValue": true,
"implicitRescue": true
}
}
}
}, "Ruby": {
"tab_size": 2,
"language_servers": [
"ruby-lsp",
"!solargraph",
"!rubocop",
"..."
]
} |
Beta Was this translation helpful? Give feedback.
-
For me @jarednorman's suggestion was fine #6560 (reply in thread). However, it didn't provide the squirly lines when there's an issue, or an explaination on hover, it only fixed (fixable) issues on saving. Not sure if I have done something wrong there. My solution is to use Solagraph + Rubocop, but to tell Rubocop to use standard's rules. It works nice for me. You get the green squirly lines, the explaination on hover, and Rubocop also reports other useful things e.g. when you've defined the same method twice in a class.
settings.json "lsp": {
"solargraph": {
"initialization_options": {
"diagnostics": true,
"formatting": true
}
}
} .rubocop.ymlinherit_gem:
standard: config/base.yml Edit: see https://github.com/standardrb/standard?tab=readme-ov-file#running-standards-rules-via-rubocop for other things you may want in .rubocop.yml |
Beta Was this translation helpful? Give feedback.
-
Hi! I can't make zed to format on save my {
"assistant": {
"default_model": {
"provider": "copilot_chat",
"model": "gpt-4o"
},
"version": "2"
},
"telemetry": {
"metrics": false
},
"ui_font_size": 16,
"buffer_font_size": 16,
"theme": {
"mode": "system",
"light": "One Light",
"dark": "One Dark"
},
"languages": {
"Ruby": {
"remove_trailing_whitespace_on_save": true,
"ensure_final_newline_on_save": true,
"tab_size": 2,
"language_servers": ["ruby-lsp", "!solargraph", "!rubocop"],
"formatter": "language_server",
"format_on_save": "on"
},
"JavaScript": {
"ensure_final_newline_on_save": true,
"remove_trailing_whitespace_on_save": true,
"code_actions_on_format": {
"source.fixAll.eslint": true
}
},
"TypeScript": {
"ensure_final_newline_on_save": true,
"remove_trailing_whitespace_on_save": true,
"code_actions_on_format": {
"source.fixAll.eslint": true
}
}
},
"lsp": {
"ruby-lsp": {
"initialization_options": {
"enabledFeatures": {
"codeActions": true,
"codeLens": true,
"completion": true,
"definition": true,
"diagnostics": false, // until Zed supports pull-type diagnostics
"documentHighlights": true,
"documentLink": true,
"documentSymbols": true,
"foldingRanges": true,
"formatting": true,
"hover": true,
"inlayHint": true,
"onTypeFormatting": true,
"selectionRanges": true,
"semanticHighlighting": true,
"signatureHelp": true,
"typeHierarchy": true,
"workspaceSymbol": true
},
"formatter": "standard",
"linters": ["standard"]
}
}
},
"prettier": {
// Whether to consider prettier formatter or not when attempting to format a file.
"allowed": true,
//
// Use regular Prettier json configuration.
// If Prettier is allowed, Zed will use this for its Prettier instance for any applicable file, if
// the project has no other Prettier installed.
"plugins": [],
//
// Use regular Prettier json configuration.
// If Prettier is allowed, Zed will use this for its Prettier instance for any applicable file, if
// the project has no other Prettier installed.
"tabWidth": 2,
"bracketSpacing": true,
"trailingComma": "es5",
"semi": false,
"jsxSingleQuote": false,
"singleQuote": false
}
} It just doesn't format at all while VS code works just fine. I'd love to use zed... this is my only blocker atm 😭 Any ideas? Thanks! |
Beta Was this translation helpful? Give feedback.
-
For people that use StandardRB (https://github.com/testdouble/standard), here's the configuration I'm using.
Make sure you have
format_on_save
turned on (see caveats below)A Few Caveats
parallel: false
). This is the default. Just make sure you didn't turn it on at some point. Otherwise StandardRB will run quitely but you won't see the fixes in Zedbundler
because I don't think you can control what path Zed runs commands from (to be fair I'm mostly guessing, based on comments here https://github.com/zed-industries/community/discussions/1017#discussioncomment-5292800)ignored
is required by Rubocop as a parameter for--stdin
. It's supposed to be "...a path, relative to the root of the project. RuboCop will use this path to determine which cops are enabled" But, from what I can tell, this is ignored by StandardRB./cc @searls Zed looks really promising. I'm excited to start using it and helping to improve the Ruby experience. Integration with StandardRB was one of the first things on my list.
Beta Was this translation helpful? Give feedback.
All reactions