supermaven-inc / supermaven-nvim

The official Neovim plugin for Supermaven
https://supermaven.com/
MIT License
755 stars 37 forks source link

I would like to have Supermaven default to off #81

Open jellis206 opened 3 months ago

jellis206 commented 3 months ago

Issue

I tried a lot of different things like putting require("supermaven-nvim.api").stop() in my setup function, even in my init.lua or other places like my nvim-cmp setup. However, there must be an autocmd or something similar that is starting supermaven when the first buffer is opened because every time I started up nvim and checked the status it would not be running and then when I opened a file and checked the status again supermaven would be running.

Temporary Solution

In the end I had to make my own autocmd that runs when the first buffer is opened. I used vim.schedule to make sure my setup function is scheduled to run after everything else. Anyways I thought it would be a nice little feature for you guys to add to the plugin setup to be able to set a global flag so that whatever is starting supermaven on buffer open doesn't get called.

If it is helpful here is my neovim setup where I am using folke's lazyvim + some of my own custom stuff.

This is my lua/config/autocmds.lua

local has_run_setup = false

local function after_all_setup()
  if not has_run_setup then
    vim.schedule(function()
      local supermaven = require("supermaven-nvim.api")
      if supermaven.is_running() then
        supermaven.stop()
      end

      -- Copilot was fine to be disabled in my normal plugin setup
      -- I just put it here so my AI stuff is together
      local copilot = require("copilot.command")
      if not require("copilot.client").is_disabled() then
        copilot.disable()
      end
      has_run_setup = true
    end)
  end
end

vim.api.nvim_create_augroup("AfterAllSetup", { clear = true })

vim.api.nvim_create_autocmd("BufNew", {
  group = "AfterAllSetup",
  callback = after_all_setup,
})

Summary

It is possible for plugin users to solve this themselves like I have, but I think it would be really useful to be able to simply default to being turned off. I like AI, obviously enough to be trying this out to see if I like it better than copilot, but lately I have wanted to be more opt in to AI rather than opt out which is why I went down this path of trying to figure out how to have my coding assistants disabled by default.

iamreinder commented 3 months ago

@jellis206 With lazy.nvim, you can configure it like this:

{
    "supermaven-inc/supermaven-nvim",
    cmd = {
        "SupermavenStart",
    },
    opts = {
        --- Your configuration options
    }
}

The cmd section makes it that the setup function is only called when you run one of the configured commands. I agree that it would be nice to have a built-in function in Supermaven to setup but not start by default. But this solution works well and is easier than configuring auto commands.

Edit: I asked if you used Lazy.nvim as plugin manager, but after I posted this message I saw you linked your config and that it uses LazyVim. I altered my message to knowing you use Lazy.nvim as plugin manager.

tom-gora commented 2 months ago

@jellis206

local conf = require("tomeczku.configs.supermaven_conf")

M = {
    "supermaven-inc/supermaven-nvim",
    cmd = { "SupermavenStart", "SupermavenToggle" },
    config = conf.config_function,
}

return M

my lazy plugin config, adding toggling command to the above solution if you have a keymap for quick toggling on and off. Works like a charm.

tigion commented 2 months ago

In https://github.com/supermaven-inc/supermaven-nvim/blob/338e1597b5575f3728865ddd5834fdba3d66b05f/lua/supermaven-nvim/init.lua#L55 is Supermaven started in setup().

So I have also a stop after setup in my config shown below.

In https://github.com/supermaven-inc/supermaven-nvim/blob/338e1597b5575f3728865ddd5834fdba3d66b05f/lua/supermaven-nvim/document_listener.lua#L24 is an autocmd with BufEnter, which starts supermaven everytime on BufEnter. Even if Supermaven is stopped or toggled before.

In my setup is only a condition function with a toggle keymap persistent:

    opts = {
      ...,
      condition = function()
        -- use global variable for stop condition
        return not vim.g.supermaven_enable
      end,
    }
    config = function(_, opts)
      require('supermaven-nvim').setup(opts)
      local api = require('supermaven-nvim.api')

      -- stop supermaven at start if running
      -- because it will start automatically through setup()
      if api.is_running() then api.stop() end

      vim.keymap.set('n', '<Leader>ts', function()
        -- toggle global variable for stop condition
        -- first toggle sets the none existing variable to true
        vim.g.supermaven_enable = not vim.g.supermaven_enable
        -- stop or start supermaven
        if vim.g.supermaven_enable then
          api.start()
        else
          api.stop()
        end
      end, { desc = 'Toggle Supermaven' })
    end,
tom-gora commented 2 months ago

@tigion Amazing! Would not guess that all it needed was consistently stored state to get it to turn off at startup an finally start toggling predictably -_-
Thank you :)

I will offer my additions:

user_commands.lua

local toggleSupermaven = function()
    -- to not conflict with api = vim.api used in this module already
    local sm_api = require("supermaven-nvim.api")
    -- toggle global variable for stop condition
    -- first toggle sets the none existing variable to true
    vim.g.supermaven_enable = not vim.g.supermaven_enable
    -- stop or start supermaven
    local noti = require("notify")
    local noti_opts = { title = "Supermaven", icon = "", timeout = 1000, hide_from_history = true }
    if vim.g.supermaven_enable then
        noti("ON", "info", noti_opts)
        sm_api.start()
    else
        noti("OFF", "error", noti_opts)
        sm_api.stop()
    end
end

api.nvim_create_user_command("ToggleSupermaven", toggleSupermaven, { range = false })

keymaps.lua

vim.keymap.set("n", "<leader>S", "<cmd>ToggleSupermaven<cr>", { desc = " Toggle Supermaven" })

components.lua (in my custom statusline)

* should be easily adaptable to custom component for plugins, after all it only returns a string conditionally
_** the %#SomeString# bit in a statusline resolves to a highlight so you adapt visual indicator of your choice for on and off states

M.ai_status = function()
    if not vim.g.supermaven_enable then
        return "%#St_gitIcons#  "
    end
    return "%#St_CommandModeSep#  "
end
tigion commented 2 months ago

I fixed a logical error in the condition function (previous comment is also edited):

opts = {
      ...,
      condition = function()
        -- use global variable for stop condition
        return not vim.g.supermaven_enable
      end,
    }
elliot40404 commented 2 months ago

Ability to enable or disable globally This pr should give you that ability