magicmonty / sonicpi.nvim

A neovim Plugin for [Sonic Pi](https://sonic-pi.net)
41 stars 7 forks source link

LazyNvim #9

Open Yggdrasill501 opened 5 months ago

Yggdrasill501 commented 5 months ago

Hey I would like to make this work with more package managers for example Lazy, I just wanted to ask if it is welcomed and what are some contributing guideline’s ?

Also I am sorry if it does not belong here but i didn't see anything like forum etc... Btw amazing implementation. 🫡🥺

igorgue commented 2 months ago

@Yggdrasill501 do you have a config?

sluedecke commented 2 weeks ago

This is my snippet for lazy.nvim:

require("lazy").setup({

...

  -- Live Coding: sonic-pi music programming
  {
    'magicmonty/sonicpi.nvim',
    config = function()
      require('sonicpi').setup({
        server_dir = '/opt/sonic-pi/app/server',
        lsp_diagnostics = true,
        mappings = {
          { 'n', '<leader>d', ':SonicPiStartDaemon<CR>',                    { desc = 'Sonic Pi: start daemon' } },
          { 'n', '<leader>s', require('sonicpi.remote').stop,               { desc = 'Sonic Pi: stop' } },
          { 'i', '<M-s>',     require('sonicpi.remote').stop,               { desc = 'Sonic Pi: stop' } },
          { 'n', '<leader>r', require('sonicpi.remote').run_current_buffer, { desc = 'Sonic Pi: run' } },
          { 'i', '<M-r>',     require('sonicpi.remote').run_current_buffer, { desc = 'Sonic Pi: run' } },
          { 'n', '<leader>R', ':SonicPiSendBuffer<CR>',                     { desc = 'Sonic Pi: send buffer' } },
          { 'i', '<M-R>',     ':SonicPiSendBuffer<CR>',                     { desc = 'Sonic Pi: send buffer' } },
        },
        single_file = true,
      })
    end,
    dependencies = {
      'hrsh7th/nvim-cmp',
      'kyazdani42/nvim-web-devicons'
    },
  },

...

})

HTH

igorgue commented 2 weeks ago

@sluedecke I don't think the server_dir = '/opt/sonic-pi/app/server', should be implied... This is mine: /run/current-system/sw/bin/sonic-pi NixOS, other distros might not put software in /opt

igorgue commented 2 weeks ago

@sluedecke this is what I did for sonic-pi and LazyVim:

local function get_server_path()
  local exepath = vim.fn.exepath("sonic-pi")
  ---@diagnostic disable-next-line: undefined-field
  local realpath = vim.loop.fs_realpath(exepath)
  local pkg_root = vim.fn.fnamemodify(realpath, ":h:h")
  return pkg_root .. "/app/server"
end

local server_path = get_server_path()

require("lazyvim.util").lsp.on_attach(function(client, _)
  if client.name == "solargraph" then
    require("sonicpi").lsp_on_init(client, { server_dir = server_path })
  end
end)

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
  pattern = { "*.sonicpi" },
  callback = function()
    if require("sonicpi.opts").remote.lifecycle.daemon_started == 1 then
      vim.cmd("SonicPiSendBuffer")
    end
  end,
})

return {
  {
    "nvim-treesitter/nvim-treesitter",
    ft = { "sonicpi" },
    optional = true,
    opts = function(_, opts)
      vim.treesitter.language.register("ruby", "sonicpi")

      opts.indent = {
        enable = true,
        disable = false,
      }
      opts.highlight.additional_vim_regex_highlighting = true

      return opts
    end,
  },
  {
    "stevearc/conform.nvim",
    ft = { "sonicpi" },
    optional = true,
    opts = {
      formatters_by_ft = {
        sonicpi = { "rubocop" },
      },
    },
  },
  {
    "neovim/nvim-lspconfig",
    opts = {
      servers = {
        ruby_lsp = {
          filetypes = { "ruby", "sonicpi" },
          single_file_support = true,
        },
        solargraph = {
          enabled = true,
          filetypes = { "ruby", "sonicpi" },
          diagnostics = true,
          single_file_support = true,
        },
        rubocop = {
          enabled = true,
          filetypes = { "ruby", "sonicpi" },
          single_file_support = true,
        },
      },
    },
  },
  {
    "magicmonty/sonicpi.nvim",
    -- dir = "~/Code/sonicpi.nvim",
    ft = { "sonicpi" },
    dependencies = {
      "kyazdani42/nvim-web-devicons",
      "nvim-lua/plenary.nvim",
    },
    config = function(_, opts)
      local wk = require("which-key")

      local mappings = {
        { "n", "<M-s>", require("sonicpi.remote").stop, { desc = "Sonic Pi Stop" } },
        { "i", "<M-s>", require("sonicpi.remote").stop, { desc = "Sonic Pi Stop" } },
        { "n", "<M-r>", require("sonicpi.remote").run_current_buffer, { desc = "Sonic Pi Run" } },
        { "i", "<M-r>", require("sonicpi.remote").run_current_buffer, { desc = "Sonic Pi Run" } },
      }
      opts = vim.tbl_extend("force", opts, { mappings = mappings })

      require("sonicpi").setup(opts)
      require("luasnip").filetype_extend("sonicpi", { "ruby" })

      wk.add({
        { "<leader>S", group = "sonicpi", icon = { icon = " ", color = "cyan" } },
      })
    end,
    opts = {
      server_dir = server_path,
      single_file = true,
      lsp_diagnostics = true,
    },
    keys = {
      { "<cr>", "<cmd>SonicPiSendBuffer<CR>", desc = "Sonic Pi send buffer", ft = "sonicpi" },
      {
        "<s-cr>",
        function()
          require("sonicpi.remote").stop()
        end,
        desc = "Sonic Pi stop clock",
        ft = "sonicpi",
      },
      { "<leader>Ss", "<cmd>SonicPiStartDaemon<CR>", desc = "Sonic Pi start daemon", ft = "sonicpi" },
      { "<leader>SS", "<cmd>SonicPiStopDaemon<CR>", desc = "Sonic Pi stop daemon", ft = "sonicpi" },
      {
        "<leader>;",
        function()
          require("sonicpi.remote").stop()
          require("sonicpi.remote").run_current_buffer()
        end,
        desc = "Sonic Pi restart clock",
        ft = "sonicpi",
      },
    },
  },
}
igorgue commented 2 weeks ago

I also had to make a .vim file with the syntax definitions, looks better, you can check my config here: https://github.com/igorgue/dotnvim/blob/main/lua/plugins/extras/sonicpi.lua https://github.com/igorgue/dotnvim/blob/main/indent/sonicpi.vim https://github.com/igorgue/dotnvim/blob/main/after/ftplugin/sonicpi.lua

sluedecke commented 2 weeks ago

@igorgue nice stuff, thanks for sharing!

And you are right, I do not need to specify the server_dir, sonicpi.nvim already searches a list of known paths: https://github.com/magicmonty/sonicpi.nvim/blob/9204aec1461520ccb42deb6354a514ecf7eadad8/lua/sonicpi/init.lua#L4-L10

I too use treesitter and lsp and it took me a while to get solargraph working correctly with mason, but it works fine now.

Some more snippets: setting up solargraph (which has been installed using the :Mason command as Ruby LSP):

local lspconfig = require('lspconfig')

-- from :h mason-lspconfig-automatic-server-setup
require('mason-lspconfig').setup_handlers({
  function(server_name) -- default handler (optional)

    -- 2024-10-29 Solargraph shall handle ruby AND sonicpi files, add on_init according to documentation
    if server_name == "solargraph" then
      lspconfig[server_name].setup {
        config = {
          on_init = function(client)
            require('sonicpi').lsp_on_init(client, {})
          end
        },
        filetypes = { 'ruby', 'sonicpi' },
      }
    end

    ....

  end
})