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

chore: update the nvim config #211

Merged
merged 5 commits into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 55 additions & 29 deletions src/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,27 @@ the following command (assuming your Neovim configuration directory is
`~/.config/nvim`):

```shell
git clone https://github.com/neovim/nvim-lspconfig \
git clone https://github.com/neovim/nvim-lspconfig \
~/.config/nvim/pack/nvim/start/nvim-lspconfig
```

If you are on Windows, you can run:

```shell
cd C:/Users/<USER>/AppData/Local/nvim/pack/nvim/start
cd %LOCALAPPDATA%/nvim/pack/nvim/start
git clone https://github.com/neovim/nvim-lspconfig
```

#### Step 3: Configure Flix LSP in Neovim

Add the following minimal configuration to your `~/.config/nvim/init.lua`:

If you are on Windows, the file should be stored at:

```shell
%LOCALAPPDATA%/nvim/init.lua
```

```lua
local lspconfig = require("lspconfig")
local configs = require("lspconfig.configs")
Expand All @@ -99,14 +105,15 @@ if not configs.flix then
default_config = {
cmd = start_cmd,
filetypes = { "flix" },
root_dir = function(fname)
local root_dir = vim.fs.dirname(vim.fs.find("flix.toml", { path = fname, upward = true })[1])
or vim.loop.cwd()
-- Check if flix.jar exists
local flix_jar_path = root_dir .. "/flix.jar"
root_dir = function(fname)
-- Search for flix.toml/flix.jar upwards recursively, with a fallback to the current directory
local root_dir = vim.fs.dirname(vim.fs.find({"flix.toml", "flix.jar"}, { path = fname, upward = true })[1])
or vim.fs.dirname(fname)
local flix_jar_path = vim.fs.joinpath(root_dir, "flix.jar")
-- Make sure flix.jar is found in the root directory, otherwise return nil to prevent the LSP server from starting
if vim.loop.fs_stat(flix_jar_path) == nil then
print("Failed to start the lsp server: flix.jar not found in project root (" .. root_dir .. ")!")
return nil -- Prevents LSP from starting
print("Failed to start the LSP server: flix.jar not found in project root (" .. root_dir .. ")!\n")
return nil
end
return root_dir
end,
Expand Down Expand Up @@ -146,6 +153,7 @@ lspconfig.flix.setup({
vim.keymap.set("n", "<leader>h", vim.lsp.buf.document_highlight, bufopts)
vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts)
vim.keymap.set('i', '<C-a>', '<C-x><C-o>', bufopts)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, bufopts)
vim.keymap.set("n", "<leader>d", vim.diagnostic.open_float, bufopts)
vim.keymap.set("n", "<leader>ws", vim.lsp.buf.workspace_symbol, bufopts)
Expand All @@ -155,38 +163,55 @@ lspconfig.flix.setup({
})
```

If you are on Windows, the file should be stored at:

```shell
C:/Users/<USER>/AppData/Local/nvim/init.lua
```

You can verify that `nvim-lspconfig` and the Flix language server is installed
correctly by running: `nvim` and then running the command `:LspInfo`.

#### Step 4: Programming with Flix with Neovim

You can now open any `*.flix` file provided that the Flix compiler jar
(`flix.jar`) is located in the same directory as the Flix.

(`flix.jar`) is located in the same directory as the file or in one of its parent directories.
When you open a Flix, you should see message "Flix LSP attached to buffer
<buffer_number>" in the status line. Moreover, the opened file should be syntax
highlighted.

The default Flix LSP configuration includes the following keybindings:

| Keybinding | Action |
|-----------------|-----------------------|
| `gd` | Go to definition |
| `gD` | Go to declaration |
| `gi` | Go to implementation |
| `gr` | Find references |
| `gy` | Go to type definition |
| `ctrl+x,ctrl+o` | Trigger auto-complete |
| `shift+k` | Hover |
| `<leader>rn` | Rename symbol |
| `<leader>ca` | Code actions |
| `<leader>e` | Show diagnostics |
| Keybinding | Action |
|-----------------|--------------------------|
| `gd` | Go to definition |
| `gi` | Go to implementation |
| `gr` | Find references |
| `ctrl+a` | Trigger auto-complete |
| `shift+k` | Hover |
| `<leader>rn` | Rename symbol |
| `<leader>ca` | Code actions |
| `<leader>cl` | Run Code lens |
| `<leader>ws` | Show workspace symbols |
| `<leader>ds` | Show document symbols |
| `<leader>d` | Show diagnostics |
| `<leader>h` | Show document highlight |

#### Step 5: Integrating with other Neovim plugins

There are many other neovim plugins that can enhance your Flix programming experience. We recommend the following:
- [nvim-cmp](https://github.com/hrsh7th/nvim-cmp): which will trigger auto-completion as you typing. Just remember to add lsp to the sources of completions:
```lua
cmp.setup({
sources = cmp.config.sources({
{ name = "nvim_lsp" }, -- lsp
...
}),
})
```
- [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim): which is a fuzzy finder over lists. It can be used to view and search for symbols, references, and definitions in your Flix project. Just call Telescope over the LSP commands:
```lua
keymap.set("n", "gr", "<cmd>Telescope lsp_references<CR>", opts)
keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts)
keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts)
keymap.set("n", "<leader>d", "<cmd>Telescope diagnostics bufnr=0<CR>", opts)
keymap.set("n", "<leader>ds", "<cmd>Telescope lsp_document_symbols<CR>", opts)
keymap.set("n", "<leader>ws", "<cmd>Telescope lsp_workspace_symbols<CR>", opts)
```

### Using Flix from the Command Line

Expand All @@ -197,6 +222,7 @@ Flix can also be used from the command line. Follow these steps:
> 3. Enter the created directory (e.g. `cd my-flix-project`) and run `java -jar flix.jar init` to create an empty Flix project.
> 4. Run `java -jar flix.jar run` to compile and run the project.


### Using nix

Flix can also be installed using the [nix package manager](https://nixos.org/).
Expand Down