Skip to content

Commit

Permalink
breaking_change: remove on_attach function
Browse files Browse the repository at this point in the history
  • Loading branch information
aspeddro committed Mar 4, 2023
1 parent e6ddf5c commit 5bbdd8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 57 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ use 'aspeddro/rescript-tools.nvim'

The [rescript-vscode](https://github.com/rescript-lang/rescript-vscode) plugin contains a language server than can be power other editors. I recommend using [mason.nvim](https://github.com/williamboman/mason.nvim) to install the language server. It will download the `vsix` file from the latest stable release and add it to the Neovim `PATH` as `rescript-lsp`.

## Setup
## Usage

```lua
local on_attach = function(client, bufnr)
-- on_attach function will create buffer commands when LSP is attached
require('rescript-tools').on_attach(client, bufnr)
end

--Setup rescript LSP
require'lspconfig'.rescriptls.setup{
-- Using mason.nvim plugin
Expand All @@ -37,7 +32,21 @@ require'lspconfig'.rescriptls.setup{
-- '/home/username/path/to/server/out/server.js',
-- '--stdio'
-- }
on_attach = on_attach
on_attach = on_attach,
commands = {
ResOpenCompiled = {
require('rescript-tools').open_compiled,
description = 'Open Compiled JS',
},
ResCreateInterface = {
require('rescript-tools').create_interface,
description = 'Create Interface file',
},
ResSwitchImplInt = {
require('rescript-tools').switch_impl_intf,
description = 'Switch Implementation/Interface',
},
},
}
```

Expand Down
50 changes: 0 additions & 50 deletions lua/rescript-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,54 +173,4 @@ M.switch_impl_intf = function(ask)
end
end

---This function can be used to create command when LSP is attached to buffer.
---@param client table LSP client
---@param bufnr number buffer number
---@param opts? { enable: boolean, names: table<string, function> }
---@usage [[
--- -- Default options
--- require('rescript-tools').on_attach(client, bufnr, {
--- commands = {
--- enable = true,
--- names = {
--- ResOpenCompiled = rescript('rescript-tools').open_compiled
--- ResCreateInterface = rescript('rescript-tools').create_interface
--- ResSwitchImplInt = rescript('rescript-tools').switch_impl_intf
--- }
--- }
--- })
--- -- Overriding the command name
--- require('rescript-tools').on_attach(client, bufnr, {
--- commands = {
--- names = {
--- ResOpenJS = rescript('rescript-tools').open_compiled
--- ResCreateInt = rescript('rescript-tools').create_interface
--- ResSwitch = rescript('rescript-tools').switch_impl_intf
--- }
--- }
--- })
---@usage ]]
M.on_attach = function(client, bufnr, opts)
opts = vim.tbl_deep_extend("force", {
commands = {
enable = true,
names = {
ResOpenCompiled = M.open_compiled,
ResCreateInterface = M.create_interface,
ResSwitchImplInt = M.switch_impl_intf,
},
},
}, opts or {})
if client.name == "rescriptls" then
if opts.commands.enable then
for name, fn in pairs(opts.commands.names) do
vim.api.nvim_buf_create_user_command(bufnr, name, fn, {
nargs = 0,
desc = "ReScript: " .. name,
})
end
end
end
end

return M

0 comments on commit 5bbdd8d

Please sign in to comment.