Skip to content

Instantly share code, notes, and snippets.

@bruchmann
Last active January 12, 2024 21:34
Show Gist options
  • Select an option

  • Save bruchmann/026e034a045d7155c37168aa04e82df0 to your computer and use it in GitHub Desktop.

Select an option

Save bruchmann/026e034a045d7155c37168aa04e82df0 to your computer and use it in GitHub Desktop.
Setting up Neovim LSP for Godot

Setting up Neovim LSP for Godot

Godot

Go to Editor > Editor Settings… > Text Editor > External.

Use the following settings

  • Use External Editor
  • Exec Path: nvim
  • Exec Flags: --server /tmp/godot.pipe --remote-send "<C-\><C-N>:n {file}<CR>{line}G{col}|"

Screenshot of the Godot Engine showing the settings screen mentioned above

Neovim

Open your init.lua in neovim. I use Lazy.nvim as my neovim package manager.

Here are the relevant settings from my init.lua:

require('lazy').setup({
		'habamax/vim-godot',
		{
			'neovim/nvim-lspconfig',
			dependencies = {
				{ 'williamboman/mason.nvim', config = true },
				'williamboman/mason-lspconfig.nvim',

				{ 'j-hui/fidget.nvim',       tag = 'legacy', opts = {} },

				'folke/neodev.nvim',
				'SmiteshP/nvim-navic',
			},
		},

		{
			'hrsh7th/nvim-cmp',
			dependencies = {
				'L3MON4D3/LuaSnip',
				'saadparwaiz1/cmp_luasnip',
				'hrsh7th/cmp-nvim-lsp',
				'rafamadriz/friendly-snippets',
				'hrsh7th/cmp-path',
				'hrsh7th/cmp-buffer',
				'petertriho/cmp-git',
				'andersevenrud/compe-tmux',
				'lukas-reineke/cmp-under-comparator',
			},
		},
}, {})

-- nvim-cmp supports additional completion capabilities, so broadcast that to servers
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)

require 'lspconfig'.gdscript.setup { capabilities = require('cmp_nvim_lsp').default_capabilities(vim.lsp.protocol.make_client_capabilities()) }

-- [[ Configure nvim-cmp ]]
-- See `:help cmp`
local cmp = require 'cmp'
local luasnip = require 'luasnip'
require('luasnip.loaders.from_vscode').lazy_load()
luasnip.config.setup {}

cmp.setup {
	snippet = {
		expand = function(args)
			luasnip.lsp_expand(args.body)
		end,
	},
	mapping = cmp.mapping.preset.insert {
		['<C-n>'] = cmp.mapping.select_next_item(),
		['<C-p>'] = cmp.mapping.select_prev_item(),
		['<C-d>'] = cmp.mapping.scroll_docs(-4),
		['<C-f>'] = cmp.mapping.scroll_docs(4),
		['<C-Space>'] = cmp.mapping.complete {},
		['<CR>'] = cmp.mapping.confirm {
			behavior = cmp.ConfirmBehavior.Replace,
			select = true,
		},
		['<Tab>'] = cmp.mapping(function(fallback)
			if cmp.visible() then
				cmp.select_next_item()
			elseif luasnip.expand_or_locally_jumpable() then
				luasnip.expand_or_jump()
			else
				fallback()
			end
		end, { 'i', 's' }),
		['<S-Tab>'] = cmp.mapping(function(fallback)
			if cmp.visible() then
				cmp.select_prev_item()
			elseif luasnip.locally_jumpable(-1) then
				luasnip.jump(-1)
			else
				fallback()
			end
		end, { 'i', 's' }),
	},
	sources = {
		{ name = 'nvim_lsp' },
		{ name = 'luasnip' },
		{ name = 'buffer' },
		{ name = 'path' },
		{ name = 'git' },
	},
	window = {
		completion = cmp.config.window.bordered(),
		documentation = cmp.config.window.bordered(),
	},
}

Usage

  1. Open your project in Godot
  2. Open your terminal and cd to the root of your project. (Directory containing the project.godot file.)
  3. Start neovim by calling nvim --listen /tmp/godot.pipe
  4. ???
  5. Profit
-- ~/.config/nvim/after/ftplugin/gdscript.lua
-- Bonus settings for habamax/vim-godot
vim.opt_local.foldmethod = 'expr'
vim.opt_local.indentexpr = ''
vim.opt_local.shiftwidth = 4
vim.opt_local.tabstop = 4
vim.keymap.set('n', '<F4>', ':GodotRunLast<CR>', { buffer = true, desc = 'Run Previous Scene' })
vim.keymap.set('n', '<F5>', ':GodotRun<CR>', { buffer = true, desc = 'Run Main Scene' })
vim.keymap.set('n', '<F6>', ':GodotRunCurrent<CR>', { buffer = true, desc = 'Run Current Scene' })
vim.keymap.set('n', '<F7>', ':GodotRunFZF<CR>', { buffer = true, desc = 'Run Scene via FZF selection' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment