Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable LSP and Treesitter for the log buffer by default #571

Open
dradtke opened this issue May 1, 2024 · 9 comments
Open

Disable LSP and Treesitter for the log buffer by default #571

dradtke opened this issue May 1, 2024 · 9 comments

Comments

@dradtke
Copy link

dradtke commented May 1, 2024

The log buffer is great for scrolling through output, but there are a few annoyances with it when using LSP and Treesitter:

  • LSP will report errors like "unrecognized symbol," which don't really make sense within the context of the log buffer
  • Treesitter can cause the log buffer to become basically unresponsive if it has to show a super-long line

My solution to these problems currently looks like this (unrelated sections of my config omitted for brevity):

-- init.lua

function is_conjure_log(buf)
    bufname = vim.fn.fnamemodify(vim.api.nvim_buf_get_name(buf), ":t")
    return string.sub(bufname, 1, 12) == "conjure-log-"
end

-- Set up LSP, but not for the log buffer
require("lspconfig").clojure_lsp.setup({
    on_attach = function(client, bufnr)
        if is_conjure_log(bufnr) then
            vim.lsp.buf_detach_client(bufnr, client.id)
        end
    end
})

-- Set up Treesitter, but not for the log buffer
require("nvim-treesitter.configs").setup{
    ensure_installed = { "clojure" },
    auto_install = true,
    highlight = {
        enable = true,
        disable = function(lang, buf)
            return is_conjure_log(buf)
        end,
    },
    incremental_selection = {
        enable = true,
        disable = function(lang, buf)
            return is_conjure_log(buf)
        end,
    },
    textobjects = {
        enable = true,
        disable = function(lang, buf)
            return is_conjure_log(buf)
        end,
    },
    indent = {
        enable = true,
        disable = function(lang, buf)
            return is_conjure_log(buf)
        end,
    },
}

It would be nice if Conjure were able to handle these scenarios on its own, or at the very least recommend a solution like this in the README.

The Treesitter solution was inspired by this post, so it appears others have run into this as well and built their own solutions for it.

@practicalli-johnny
Copy link

practicalli-johnny commented May 1, 2024

I used an autocmd in the AstroNvim Community Clojure pack to disable diagnostics in any buffer matching clojure-log-*

https://github.com/practicalli/astronvim-user-config/blob/401744a2d137c9cf06b6247de5ae72c38f9a1be1/lua/plugins/clojure.lua#L217

I assume something similar to disable Treesitter may be effective (but just a guess). I didnt notice an issue with Treesitter though (not a heavy user of the REPL log)

NOTE: I'll remove the autocmd in favour of the Conjure approach

@PaterJason
Copy link

On LSP, the lspconfig plugin sets autocommands on FileType to autostart servers. There is a catch for the buffer option buftype=nofile, but for log buffers this looks like it's set after the autocmd fires.

I quickly hacked this together to demonstrate. nvim.fn.bufload was setting the filetype, and I had to explicitly set the filetype after.
PaterJason@768580b#diff-e0903b23fec5b0ccc884fe0be027056d0a7bb5c55bb73ecc58a0d56377353811

@Olical
Copy link
Owner

Olical commented May 6, 2024

Oh interesting! So maybe if I just change the order of the buftype setting it'd help some people out. I kind of like the LSP / linting in the log buffer because it helps me autocomplete, doc lookup and edit the contents in the same way as I would a normal buffer.

I get that some people wouldn't want that but this is why I opted (in the past) to say "disable it yourself" rather than disabling it by default in such a way that people couldn't turn it back on.

If I change the order and set buftype before the autocmd fires, I guess it turns off LSP for the log buffer? (in theory)

In doing so, do I remove the ability for anyone to use LSP in the log without some hacking? Is there an easy way to then selectively turn it on for the users that do want the log to have LSP?

Maybe if nofile doesn't really do much for us (I feel like it does?) we could move it so it's set at the right time and then make it optional so people can ignore the nofile option, thus kinda making an LSP toggle, although an indirect one.

Of course this behaviour will change / break if the plugins we're trying to toggle change their behaviour. Which is another reason I never wanted to do this from Conjure's side. Conjure makes a Clojure buffer, your plugins hook into that buffer, I mostly feel like it's the duty of those plugins to be easily configurable to NOT fire for some buffers or patterns and I shouldn't try to actively block plugins from hooking into the log buffer since there could be infinitely many plugins and they may change behaviour from under me.

So I'm not 100% settled on the way forward yet, but this is really interesting information and I'd like to find a solution that keeps both camps happier for the long term (people who want linting in the log and people who don't).

@dradtke
Copy link
Author

dradtke commented May 6, 2024

One thing to note here is that there are really two different kinds of Clojure: one that you might see in a source file, and one that you see in a REPL (or the log buffer). As a quick example, here's what I see when I evaluate a simple source file:

image

clojure-lsp understands the source file, but it doesn't seem to really understand the log buffer. This is the type of diagnostic warning that I was looking to avoid, since evaluating larger files results in the log buffer filling up with errors.

So, one possible solution for me would be to somehow tell clojure-lsp that the log buffer should be evaluated within the context of the project, rather than as a standalone file. I'm not sure if that's possible, but it would be nice to keep LSP and diagnostics enabled if they can actually be useful.

@PaterJason
Copy link

PaterJason commented May 6, 2024

Having a look, lspconfig doesn't want to attach at all to nofile buffers, using either :LspStart or the autostart. Stopping and starting wouldn't attach to an open log buffer for example. Which makes sense, a nofile is not really part of the workspace. You can force it by starting the server using the builtin lua, which is relatively simple nowadays.

vim.lsp.start({
  name = "clojure_lsp",
  cmd = { "clojure-lsp" },
  root_dir = vim.fs.root(
    0,
    { "project.clj", "deps.edn", "build.boot", "shadow-cljs.edn", ".git", "bb.edn" }
  ),
})

And trigger that by autocmd. But if you wanted to do that for each filetype I can see it being a pain.

Personally I'd set the buffer options before FileType autocmds, cos setting it after would potentially circumvent the intent in the lspconfig or other plugin/config.

@Olical
Copy link
Owner

Olical commented May 19, 2024

I've pushed a change to develop that turns off diagnostics in the log by default. There is a config option to turn them back on. This way we only remove diagnostics but can still use doc lookup and go to definition on various symbols in the log.

Olical added a commit that referenced this issue May 19, 2024
… system #571

I'm unsure if setting syntax=on enables the old syntax highlighting in
this one buffer or not. Maybe it re-activates TS?
@Olical
Copy link
Owner

Olical commented May 19, 2024

Also added a flag for treesitter although this one is NOT off by default because I think it's mostly useful and people will want it on. It's worth turning off if you end up doing lots of work in one project that causes your editor to slow down. This varies by machine too. So diagnostics: Off by default. TS: On by default.

I also am not sure if I'm fully disabling treesitter or not, I'll need someone to check that for me with something that used to be too slow with it enabled.

@philomates
Copy link
Contributor

I tried f6e4eec and it helped with displaying large output to log buffer, making it no longer crash neovim. Thanks for this work!

@Olical
Copy link
Owner

Olical commented May 22, 2024

Nice! I do think the LSP disabling call needs to be called more often though, looks like as I open and close the log the diagnostics come back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants