From 2cd46d42ba357a42b11c5e4f5e16641bde0516d6 Mon Sep 17 00:00:00 2001 From: pugud <171756217+pugud@users.noreply.github.com> Date: Mon, 20 Oct 2025 18:20:05 +0900 Subject: [PATCH] feat(lang): add solidity language support (#4742) ## Description ### Summary This PR adds comprehensive support for Solidity development in Neovim using LazyVim. The following updates were made to streamline Solidity development: ### Changes 1. **Solidity File Type Detection** - Configured LazyVim to recognize Solidity projects by detecting `foundry.toml`, `hardhat.config.js`, and `hardhat.config.ts` files. 2. **Treesitter Support for Solidity** - Added `solidity` to the list of languages ensured by `nvim-treesitter`, providing syntax highlighting and better code structure understanding. 3. **Language Server Protocol (LSP) Setup** - Configured `nvim-lspconfig` to support `solidity_ls` as the LSP for Solidity files. - Set up `root_dir` detection based on project files or a `.git` directory to enable proper LSP functioning in Solidity projects. 4. **Solidity Formatter** - Integrated `forge_fmt` as a formatter for Solidity files using `conform.nvim`, which utilizes `forge fmt` to format code. ### Why use `forge fmt` instead of Prettier? - **Prettier's Solidity support requires extra plugins**: Prettier does not have native Solidity support and depends on `prettier-plugin-solidity`. Even with `vim.g.lazyvim_prettier_needs_config = false`, formatting won't work unless a `.prettierrc` file is configured, creating unnecessary complexity. - **`forge fmt` is the native tool for Solidity development**: `Forge` is a widely used tool within the Solidity ecosystem, especially when working with Foundry. It provides built-in formatting with no extra dependencies, making it simpler and more efficient to integrate. - **Hardhat uses `prettier-plugin-solidity`**: For users of Hardhat, `prettier-plugin-solidity` is required for code formatting. However, the integration with Prettier might add complexity, while `forge fmt` simplifies the process. - **Future customization**: We may allow users to choose between `prettier-plugin-solidity` and `forge fmt` for formatting in the future, depending on their preferred tools. ### Testing - Confirmed that Solidity files are detected and syntax-highlighted by Treesitter. - Verified LSP functionality for Solidity, including code navigation and error detection. - Tested `forge_fmt` for automatic code formatting on Solidity files. ### Motivation These changes provide a smoother and more robust Solidity development experience in Lazyvim, including syntax highlighting, code navigation, and automatic formatting. ## Related Issue(s) https://github.com/LazyVim/LazyVim/issues/1901 ## Checklist - [x] I've read the [CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md) guidelines. --------- Co-authored-by: Folke Lemaitre --- lua/lazyvim/plugins/extras/lang/solidity.lua | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lua/lazyvim/plugins/extras/lang/solidity.lua diff --git a/lua/lazyvim/plugins/extras/lang/solidity.lua b/lua/lazyvim/plugins/extras/lang/solidity.lua new file mode 100644 index 00000000..cbbc7b83 --- /dev/null +++ b/lua/lazyvim/plugins/extras/lang/solidity.lua @@ -0,0 +1,42 @@ +return { + recommended = function() + return LazyVim.extras.wants({ + ft = "solidity", + root = { + "foundry.toml", + "hardhat.config.js", + "hardhat.config.ts", + }, + }) + end, + -- Add Solidity & related to treesitter + { + "nvim-treesitter/nvim-treesitter", + opts = { ensure_installed = { "solidity" } }, + }, + + -- Correctly setup lspconfig for Solidity + { + "neovim/nvim-lspconfig", + opts = { + servers = { + solidity_ls = {}, + }, + }, + }, + -- Formatter for Solidity + { + "conform.nvim", + opts = function(_, opts) + opts.formatters_by_ft = opts.formatters_by_ft or {} + opts.formatters_by_ft.solidity = { "forge_fmt" } + + opts.formatters = { + forge_fmt = { + command = "forge", + args = { "fmt" }, + }, + } + end, + }, +}