aboutsummaryrefslogtreecommitdiff
path: root/ftplugin/jai.lua
blob: f3fb7ebf5726cae3f521deeef4491d2ccecb0f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
---- Miscellaneous settings and keymaps
-- Recognise jai file type
vim.filetype.add({ extension = { jai = 'jai' }})

---- Jails: the LSP server for Jai
local SETUP_LSP = true
if SETUP_LSP then
  -- https://github.com/SogoCZE/Jails.git
  -- Install to path, or put the full path in cmd below
  vim.lsp.config.jails = {
    cmd = { 'jails' },
    root_markers = { '.git', 'build.jai' },
    filetypes = { 'jai' }
  }
  vim.lsp.enable('jails')
  -- Check if things are working with `:checkhealth lsp`
  -- Restart with `:lsp restart` (nvim 0.12+)
end

---- Tree-sitter highlighting
local SETUP_TREESITTER = true
if SETUP_TREESITTER then
  -- Register {language} with {filetype}
  vim.treesitter.language.register('jai', 'jai')

  -- Build https://github.com/constantitus/tree-sitter-jai
  --[[
      git clone https://github.com/constantitus/tree-sitter-jai ~/opt/tree-sitter-jai
      cd ~/opt/tree-sitter-jai
      cmake . -B build -DCMAKE_BUILD_TYPE=Release
      cmake --build build
      # DLL now inside build/

      # You must also copy the queries/ directory to your configuration path
      # for highlighting etc. to work
      cp ~/opt/tree-sitter-jai/queries/*  ~/.config/nvim-jai/queries/jai/
    ]]
  -- Tell neovim about the treesitter plugin and its DLL path
  vim.treesitter.language.add('jai', { path = vim.fn.expand('~/opt/tree-sitter-jai/build/libtree-sitter-jai.so') })

  -- Automatically start treesitter on filetypes that we have a treesitter installation for
  vim.api.nvim_create_autocmd('FileType', {
    pattern = '*',
    group = vim.api.nvim_create_augroup('config-autotreesitter', { clear = true }),
    callback = function(ev)
      local filetype = vim.bo[ev.buf].filetype
      local tsname = vim.treesitter.language.get_lang(filetype)
      if tsname and vim.treesitter.language.add(tsname) then
        vim.treesitter.start(ev.buf)
        -- If treesitter AND vim regex highlighting modes are desired, turn it on like this
        -- vim.bo[ev.buf].syntax = 'ON'
      end
    end,
  })
end

---- Debugger
local SETUP_DEBUGGER = true
if SETUP_DEBUGGER then
  -- Install plugins
  vim.pack.add({
    'https://github.com/mfussenegger/nvim-dap',
  })

  -- List of debug adaptor installation guides:
  -- https://codeberg.org/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation
  --   For codelldb, extract the .vsix file somewhere, and update the path below.
  --   https://github.com/vadimcn/codelldb/releases
  local dap = require('dap')
  dap.adapters.codelldb = {
    type = 'executable',
    command = vim.fn.expand('~/opt/codelldb/extension/adapter/codelldb'),
    -- On windows you may have to uncomment this:
    -- detached = false,
  }

  dap.configurations.jai = {
    {
      name = 'Launch file',
      type = 'codelldb',
      request = 'launch',
      program = '${command:pickFile}',
      cwd = '${workspaceFolder}',
      stopOnEntry = false,
      sourceLanguages = { 'jai' },
    }
  }
  dap.configurations.c = dap.configurations.jai
  dap.configurations.cpp = dap.configurations.jai
  dap.configurations.rust = dap.configurations.jai

  vim.keymap.set("n", "<F1>", dap.continue, { desc = 'Debug: continue' })
  vim.keymap.set("n", "<F5>", dap.continue, { desc = 'Debug: continue' })
  vim.keymap.set("n", "<F2>", dap.step_into, { desc = 'Debug: step into' })
  vim.keymap.set("n", "<F3>", dap.step_over, { desc = 'Debug: step over' })
  vim.keymap.set("n", "<F4>", dap.step_out, { desc = 'Debug: step out' })
  vim.keymap.set("n", "<F6>", dap.step_back, { desc = 'Debug: step back' })
  vim.keymap.set("n", "<F8>", dap.restart, { desc = 'Debug: restart' })
  vim.keymap.set("n", "<F9>", dap.close, { desc = 'Debug: stop' })

  vim.keymap.set("n", "<space>dc", dap.run_to_cursor, { desc = 'Debug: Run to cursor' })
  vim.keymap.set('n', '<space>db', dap.toggle_breakpoint, { desc = 'Debug: Toggle Breakpoint' })
  vim.keymap.set('n', '<space>dB', function()
    dap.set_breakpoint(vim.fn.input 'Breakpoint condition: ')
  end, { desc = 'Debug: Set Breakpoint with condition' })
  vim.keymap.set('n', '<space>dg', function() require('dap').set_breakpoint(nil, nil, vim.fn.input('Log point message: ')) end, { desc = 'Debug: log point' })
  vim.keymap.set('n', '<space>dr', function() require('dap').repl.open() end, { desc = 'Debug: open REPL'} )
  vim.keymap.set('n', '<space>dl', function() require('dap').run_last() end, { desc = 'Debug: run last' })
  vim.keymap.set({'n', 'v'}, '<space>dh', function()
    require('dap.ui.widgets').hover()
  end)
  vim.keymap.set({'n', 'v'}, '<space>dp', function()
    require('dap.ui.widgets').preview()
  end)

  -- Fancy UI. Adds multiple panes such as watch window, locals, ... when the debugger starts
  local SETUP_DEBUGGER_FANCY_UI = true
  if SETUP_DEBUGGER_FANCY_UI then
    vim.pack.add({
      'https://github.com/rcarriga/nvim-dap-ui',
      'https://github.com/nvim-neotest/nvim-nio', -- dependency of nvim-dap-ui
    })

    local dapui = require('dapui')
    dapui.setup()
    dap.listeners.before.attach.dapui_config = function() dapui.open() end
    dap.listeners.before.launch.dapui_config = function() dapui.open() end
    dap.listeners.before.event_terminated.dapui_config = function() dapui.close() end
    dap.listeners.before.event_exited.dapui_config = function() dapui.close() end

    vim.keymap.set('n', '<F7>', dapui.toggle, { desc = 'Debug: See last session result.' })
    -- Eval variable under cursor
    vim.keymap.set({'n', 'v'}, "<space>dd", function()
      require("dapui").eval()
    end, { desc = 'Debug: Evaluate' })
  end
end

---- Settings and keymaps
-- Error format for compiler message recognition
local jai_errfmt = [[%f:%l\,%v: %t%\a\*:%m]]
vim.o.errorformat = jai_errfmt .. ',' .. vim.o.errorformat

-- keymap to open the Jai installation directory in a new tab, for :grep
vim.keymap.set('n', '<space>jm', function()
  local jaipath = '~/opt/jai'
  vim.cmd.tabedit(jaipath .. '/modules/Basic/Print.jai')
  vim.cmd.tcd(jaipath)
end, { desc = 'Open Jai modules (and how_tos) directory' })

-- Select a jai file to build
vim.keymap.set('n', '<space>jb', function()
  local jai_files = vim.fs.find(function(name, path)
    return string.match(name, '.*%.jai$')
  end, { limit = 50, type = 'file' })
  if jai_files then
    vim.ui.select(jai_files, { prompt='Select a file to build'}, function(item)
      if item then
        vim.opt.makeprg = 'jai "' .. item .. '"'
        print("Make program set to " .. vim.opt.makeprg:get())
        vim.cmd.make() -- optional, build it too
      end
    end)
  else
    print("No Jai files found")
  end
end, { desc = 'Select a Jai file to build' })

-- m<enter> to call :make, to build what we just put into the makeprg above
vim.keymap.set('n', 'm<CR>', '<cmd>make<CR>')

vim.keymap.set({'n', 'v'}, '<F12>', function() vim.lsp.buf.definition() end, { desc = 'LSP: Go to definition' })

-- Recommended to use UI2 if available. See :help ui2
local _, ui2 = pcall(require, 'vim._core.ui2')
if ui2 then ui2.enable() end

-- Just for me ;)
vim.opt_global.background = 'light'