summaryrefslogtreecommitdiff
path: root/lua/99/window/init.lua
blob: 6451edea67b892b321c46bd4fb5575416fa66763 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
local Agents = require("99.extensions.agents")

--- @class _99.window.Module
--- @field active_windows _99.window.Window[]
local M = {
  active_windows = {},
}
local nsid = vim.api.nvim_create_namespace("99.window.error")
local nvim_win_is_valid = vim.api.nvim_win_is_valid

--- @class _99.window.Config
--- @field width number
--- @field height number
--- @field row number?
--- @field col number?
--- @field anchor string?

--- @class _99.window.Window
--- @field config _99.window.Config
--- @field win_id number
--- @field buf_id number

--- @param lines string[]
--- @return string[]
local function ensure_no_new_lines(lines)
  local display_lines = {}
  for _, line in ipairs(lines) do
    local split_lines = vim.split(line, "\n")
    for _, clean_line in ipairs(split_lines) do
      table.insert(display_lines, clean_line)
    end
  end
  return display_lines
end

--- @return number
--- @return number
local function get_ui_dimensions()
  local ui = vim.api.nvim_list_uis()[1]
  return ui.width, ui.height
end

--- @return _99.window.Config
local function create_window_top_config()
  local width, _ = get_ui_dimensions()
  return {
    width = width - 2,
    height = 3,
    anchor = "NE",
  }
end

--- @return _99.window.Config
local function create_window_top_left_config()
  local width, _ = get_ui_dimensions()
  return {
    width = math.floor(width / 3),
    height = 3,
    anchor = "NE",
  }
end

--- @return _99.window.Config
local function create_window_full_screen()
  local width, height = get_ui_dimensions()
  return {
    width = width - 2,
    height = height - 2,
    anchor = "NE",
  }
end

--- @param win _99.window.Window
---@param offset_bottom number | nil
--- @return _99.window.Config
---@diagnostic disable-next-line
local function create_window_inside(win, offset_bottom)
  local config = win.config
  offset_bottom = offset_bottom or 0
  return {
    width = config.width - 2,
    height = 1,
    row = config.row + config.height - offset_bottom,
    col = config.col + 1,
    anchor = config.anchor,
  }
end

--- @return _99.window.Config
local function create_centered_window()
  local width, height = get_ui_dimensions()
  local win_width = math.floor(width * 2 / 3)
  local win_height = math.floor(height / 3)
  return {
    width = win_width,
    height = win_height,
    row = math.floor((height - win_height) / 2),
    col = math.floor((width - win_width) / 2),
  }
end

--- @param config _99.window.Config
---@diagnostic disable-next-line: undefined-doc-name
--- @param win_config vim.api.keyset.win_config
--- @return _99.window.Window
local function create_floating_window(config, win_config)
  local buf_id = vim.api.nvim_create_buf(false, true)
  local win_id = vim.api.nvim_open_win(buf_id, true, {
    relative = "editor",
    width = config.width,
    height = config.height,
    row = config.row or 0,
    col = config.col or 0,
    anchor = config.anchor,
    style = "minimal",
    ---@diagnostic disable-next-line: undefined-field
    border = win_config.border,
    ---@diagnostic disable-next-line: undefined-field
    title = win_config.title,
    title_pos = "center",
    ---@diagnostic disable-next-line: undefined-field
    zindex = win_config.zindex,
  })
  local window = {
    config = config,
    win_id = win_id,
    buf_id = buf_id,
  }
  vim.wo[win_id].wrap = true

  table.insert(M.active_windows, window)
  return window
end

--- @param window _99.window.Window
local function highlight_error(window)
  local line_count = vim.api.nvim_buf_line_count(window.buf_id)

  if line_count > 0 then
    vim.api.nvim_buf_set_extmark(window.buf_id, nsid, 0, 0, {
      end_row = 1,
      hl_group = "Normal",
      hl_eol = true,
    })
  end

  if line_count > 1 then
    vim.api.nvim_buf_set_extmark(window.buf_id, nsid, 1, 0, {
      end_row = line_count,
      hl_group = "ErrorMsg",
      hl_eol = true,
    })
  end
end

--- @param error_text string
--- @return _99.window.Window
function M.display_error(error_text)
  local window = create_floating_window(create_window_top_config(), {
    title = " 99 Error ",
    border = "rounded",
  })
  local lines = vim.split(error_text, "\n")

  table.insert(lines, 1, "")
  table.insert(
    lines,
    1,
    "99: Fatal operational error encountered (error logs may have more in-depth information)"
  )

  vim.api.nvim_buf_set_lines(window.buf_id, 0, -1, false, lines)
  highlight_error(window)
  return window
end

--- @param window _99.window.Window
local function window_close(window)
  if nvim_win_is_valid(window.win_id) then
    vim.api.nvim_win_close(window.win_id, true)
  end
  if vim.api.nvim_buf_is_valid(window.buf_id) then
    vim.api.nvim_buf_delete(window.buf_id, { force = true })
  end
end

--- @param text string
function M.display_cancellation_message(text)
  local config = create_window_top_left_config()
  local window = create_floating_window(config, {
    title = " 99 Cancelled ",
    border = "rounded",
  })
  local lines = vim.split(text, "\n")

  vim.api.nvim_buf_set_lines(window.buf_id, 0, -1, false, lines)

  vim.api.nvim_buf_set_extmark(window.buf_id, nsid, 0, 0, {
    end_row = vim.api.nvim_buf_line_count(window.buf_id),
    hl_group = "WarningMsg",
    hl_eol = true,
  })

  vim.defer_fn(function()
    if nvim_win_is_valid(window.win_id) then
      M.clear_active_popups()
    end
  end, 5000)

  return window
end

--- TODO: i dont like how the other interfaces have text being passed in
--- but this one is lines.  probably need to revisit this
--- @param lines string[]
function M.display_full_screen_message(lines)
  --- TODO: i really dislike that i am closing and opening windows
  --- i think it would be better to perserve the one that is already open
  --- but i just want this to work and then later... ohh much later, ill fix
  --- this basic nonsense
  M.clear_active_popups()
  local window = create_floating_window(create_window_full_screen(), {
    title = " 99 ",
    border = "rounded",
  })
  local display_lines = ensure_no_new_lines(lines)
  vim.api.nvim_buf_set_lines(window.buf_id, 0, -1, false, display_lines)
end

--- @return _99.window.Window
--- @return _99.window.Config
function M.create_centered_window()
  M.clear_active_popups()
  local config = create_centered_window()
  local window = create_floating_window(config, {
    title = " 99 ",
    border = "rounded",
  })
  return window, config
end

--- @param message string[]
function M.display_centered_message(message)
  M.clear_active_popups()
  local config = create_centered_window()
  local window = create_floating_window(config, {
    title = " 99 ",
    border = "rounded",
  })
  local display_lines = ensure_no_new_lines(message)

  vim.api.nvim_buf_set_lines(window.buf_id, 0, -1, false, display_lines)

  return window
end

--- @param win _99.window.Window
--- @param name string
local function set_defaul_win_options(win, name)
  vim.api.nvim_buf_set_name(win.buf_id, name)
  vim.wo[win.win_id].number = true
  vim.bo[win.buf_id].filetype = "99"
  vim.bo[win.buf_id].buftype = "acwrite"
  vim.bo[win.buf_id].bufhidden = "wipe"
  vim.bo[win.buf_id].swapfile = false
end

--- @param win _99.window.Window
--- @param rules _99.Agents.Rules
--- @param group any
local function highlight_rules_found(win, rules, group)
  local rule_nsid = vim.api.nvim_create_namespace("99.window.rules")
  local function check_and_highlight_rules()
    if not nvim_win_is_valid(win.win_id) then
      return
    end

    vim.api.nvim_buf_clear_namespace(win.buf_id, rule_nsid, 0, -1)

    local lines = vim.api.nvim_buf_get_lines(win.buf_id, 0, -1, false)
    local buffer_text = table.concat(lines, "\n")
    local rules_and_names = Agents.by_name(rules, buffer_text)
    local found_rules = rules_and_names.rules
    if not found_rules or vim.tbl_isempty(found_rules) then
      return
    end

    local rule_names = rules_and_names.names
    for line_num, line in ipairs(lines) do
      for _, rule_name in ipairs(rule_names) do
        local start_col = 0
        while true do
          local found_start, found_end =
            string.find(line, rule_name, start_col + 1, true)
          if not found_start then
            break
          end

          -- Highlight the matched rule
          vim.api.nvim_buf_set_extmark(
            win.buf_id,
            rule_nsid,
            line_num - 1,
            found_start - 1,
            {
              end_col = found_end,
              hl_group = "Search",
            }
          )

          start_col = found_end --[[ @as number ]]
        end
      end
    end
  end

  vim.api.nvim_create_autocmd("InsertLeave", {
    group = group,
    buffer = win.buf_id,
    callback = function()
      check_and_highlight_rules()
    end,
  })

  vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
    group = group,
    buffer = win.buf_id,
    callback = function()
      check_and_highlight_rules()
    end,
  })
end

--- @class _99.window.CaptureInputOpts
--- @field cb fun(success: boolean, result: string): nil
--- @field on_load? fun(): nil
--- @field rules _99.Agents.Rules

--- @param name string
--- @param opts _99.window.CaptureInputOpts
function M.capture_input(name, opts)
  M.clear_active_popups()

  local config = create_centered_window()
  local win = create_floating_window(config, {
    title = string.format(" 99 %s ", name),
    border = "rounded",
  })
  set_defaul_win_options(win, "99-prompt")
  vim.api.nvim_set_current_win(win.win_id)

  local group = vim.api.nvim_create_augroup(
    "99_present_prompt_" .. win.buf_id,
    { clear = true }
  )

  highlight_rules_found(win, opts.rules, group)
  vim.api.nvim_create_autocmd("BufLeave", {
    group = group,
    buffer = win.buf_id,
    callback = function()
      if nvim_win_is_valid(win.win_id) then
        vim.api.nvim_set_current_win(win.win_id)
      else
        M.clear_active_popups()
      end
    end,
  })

  vim.api.nvim_create_autocmd("BufWriteCmd", {
    group = group,
    buffer = win.buf_id,
    callback = function()
      if not nvim_win_is_valid(win.win_id) then
        return
      end
      local lines = vim.api.nvim_buf_get_lines(win.buf_id, 0, -1, false)
      local result = table.concat(lines, "\n")
      M.clear_active_popups()
      opts.cb(true, result)
    end,
  })

  vim.api.nvim_create_autocmd("BufUnload", {
    group = group,
    buffer = win.buf_id,
    callback = function()
      if not nvim_win_is_valid(win.win_id) then
        return
      end
      vim.api.nvim_del_augroup_by_id(group)
    end,
  })

  vim.api.nvim_create_autocmd("WinClosed", {
    group = group,
    pattern = tostring(win.win_id),
    callback = function()
      if not nvim_win_is_valid(win.win_id) then
        return
      end
      M.clear_active_popups()
      opts.cb(false, "")
    end,
  })

  vim.keymap.set("n", "q", function()
    M.clear_active_popups()
    opts.cb(false, "")
  end, { buffer = win.buf_id, nowait = true })

  if opts.on_load then
    vim.schedule(opts.on_load)
  end
end

function M.clear_active_popups()
  for _, window in ipairs(M.active_windows) do
    window_close(window)
  end
  M.active_windows = {}
end

function M.status_window()
  M.clear_active_popups()
end

return M