Repository: godot
Issue: LSP x Neovim crash after saving and reloading a script
Labels: bug,topic:gdscript,topic:editor,crash

### Tested versions

v4.2.stable.official [46dc27791]

### System information

Godot v4.2.stable - Windows 10.0.22621 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3050 Ti Laptop GPU (NVIDIA; 31.0.15.4626) - AMD Ryzen 5 5600H with Radeon Graphics (12 Threads). Neovim v0.9.4

### Issue description

The issue frequently happens once i save a script file and reload it in the engine (see video below).

https://github.com/godotengine/godot/assets/7879594/7bed5475-0de0-4d53-85be-c93aac6abe66



### Steps to reproduce

Do the same as on the video. It can work well but sometimes this issue happens.

`init.vim` i use. `ncat` required to get LSP work properly on Windows.
```
" Common configuration
set mouse=
set number
set splitright
set laststatus=2
set tabstop=2
set shiftwidth=2
set expandtab
set autoindent
set guitablabel=%t
set nowrap
set exrc
set background=dark
set cmdheight=0
set nocompatible

filetype plugin indent on
syntax sync fromstart

let &shell = '"C:/Program Files/Git/bin/bash.exe"'
tnoremap <C-w> <C-\><C-n>


" VIM Plug
call plug#begin()

Plug 'catppuccin/nvim', { 'as': 'catppuccin' }
Plug 'folke/tokyonight.nvim', { 'branch': 'main' }
Plug 'nvim-tree/nvim-web-devicons'
Plug 'nvim-tree/nvim-tree.lua'
Plug 'itchyny/lightline.vim'
Plug 'mattn/emmet-vim'
Plug 'github/copilot.vim'
Plug 'projekt0n/github-nvim-theme'
Plug 'sheerun/vim-polyglot'
Plug 'habamax/vim-godot'
Plug 'editorconfig/editorconfig-vim'

" HL
Plug 'othree/html5.vim'
Plug 'pangloss/vim-javascript'
Plug 'evanleck/vim-svelte', {'branch': 'main'}

" LSP
Plug 'williamboman/mason.nvim'
Plug 'williamboman/mason-lspconfig.nvim'
Plug 'neovim/nvim-lspconfig'

Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/nvim-cmp'

" FZF
Plug 'ctrlpvim/ctrlp.vim'

call plug#end()

" Color scheme
colorscheme github_dark_dimmed
let g:lightline = {'colorscheme': 'catppuccin'}

" CTRl-p
let g:ctrlp_custom_ignore = '\v[\/](node_modules|target|dist)|(\.(swp|ico|git|svn|md))$'

" Nvim-tree setup
nnoremap <C-n> :NvimTreeToggle<CR>

" Additions syntax
au BufNewFile,BufRead *.nut setf squirrel " squirrel
au BufNewFile,BufRead *.nut setf cs " squirrel
au BufNewFile,BufRead *.gd setf gdscript " gdscript

let g:html_indent_script1 = "inc" 
let g:html_indent_style1 = "inc" 

lua << EOF
-- examples for your init.lua

-- disable netrw at the very start of your init.lua (strongly advised)
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- set termguicolors to enable highlight groups
vim.opt.termguicolors = true

-- OR setup with some options
require("nvim-tree").setup({
  sort_by = "case_sensitive",
  view = {
    width = 60,
    mappings = {
      list = {
        { key = "u", action = "dir_up" },
				{ key = "s", action = "vsplit"}
      },
    },
  },
  renderer = {
    group_empty = true,
  },
  filters = {
    dotfiles = true,
  },
})

require('mason').setup({
  PATH = "prepend",
});
require('mason-lspconfig').setup();

local port = os.getenv('GDScript_Port') or '6005';
local cmd = {'ncat', '127.0.0.1', port};
local pipe = [[\\.\pipe\godot.pipe]]

require('lspconfig').tsserver.setup({
  on_attach = function(client)
    client.server_capabilities.documentFormattingProvider = false
  end
})
require('lspconfig').volar.setup({})

require'lspconfig'.gdscript.setup{
  capabilities = require('cmp_nvim_lsp').default_capabilities(),
  cmd = cmd,
  root_dir = function()
    return vim.fs.dirname(vim.fs.find({ 'project.godot', '.git' }, { upward = true })[1]);
  end
};

require('lspconfig').svelte.setup({
  on_attach = function(client)
    vim.api.nvim_create_autocmd("BufWritePost", {
      pattern = { "*.js", "*.ts" },
      callback = function(ctx)
        client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.file })
      end,
    })
  end
})

vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    -- Buffer local mappings.
    -- See `:help vim.lsp.*` for documentation on any of the below functions
    local opts = { buffer = ev.buf }
    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
    vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
    vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
    vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
    vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
    vim.keymap.set('n', '<space>wl', function()
      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, opts)
    vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
    vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
    vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)
    vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
    vim.keymap.set('n', '<space>f', function()
      vim.lsp.buf.format { async = true }
    end, opts)
  end,
})

local border = {
      {"╭", "FloatBorder"},
      {"─", "FloatBorder"},
      {"╮", "FloatBorder"},
      {"│", "FloatBorder"},
      {"╯", "FloatBorder"},
      {"─", "FloatBorder"},
      {"╰", "FloatBorder"},
      {"│", "FloatBorder"},
}

-- To instead override globally
local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview
function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
  opts = opts or {}
  opts.border = opts.border or border
  return orig_util_open_floating_preview(contents, syntax, opts, ...)
end

local cmp = require('cmp');
cmp.setup({
    window = {
      completion = cmp.config.window.bordered(),
      documentation = cmp.config.window.bordered(),
    },
    snippet = { expand = function() end },
    mapping = cmp.mapping.preset.insert({
      ['<C-b>'] = cmp.mapping.scroll_docs(-4),
      ['<C-f>'] = cmp.mapping.scroll_docs(4),
      ['<C-Space>'] = cmp.mapping.complete(),
      ['<C-e>'] = cmp.mapping.abort(),
      ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
    }),
    sources = cmp.config.sources({
      { name = 'nvim_lsp' },
      -- { name = 'vsnip' }, -- For vsnip users.
      -- { name = 'luasnip' }, -- For luasnip users.
      -- { name = 'ultisnips' }, -- For ultisnips users.
      -- { name = 'snippy' }, -- For snippy users.
    }, {
      { name = 'buffer' },
    })
})
EOF

```

### Minimal reproduction project (MRP)

N/A