-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathapi.lua
More file actions
133 lines (119 loc) · 3.48 KB
/
Copy pathapi.lua
File metadata and controls
133 lines (119 loc) · 3.48 KB
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
local boot = require("tidal.core.boot")
local message = require("tidal.core.message")
local notify = require("tidal.util.notify")
local select = require("tidal.util.select")
local state = require("tidal.core.state")
local util = require("tidal.util")
-- Lazily require highlight module to ensure 'setup' is called before
local M = {}
--- Begin a Tidal session
--- Will start an sclang instance if specified in config
---@param args TidalBootConfig
function M.launch_tidal(args)
local current_win = vim.api.nvim_get_current_win()
if state.launched then
notify.warn("Tidal is already running")
return
end
if args.tidal.enabled then
boot.tidal(args.tidal, args.split)
end
if args.sclang.enabled then
-- invert the split option if tidal is opened already
boot.sclang(args.sclang, args.split == "v" and args.tidal.enabled and "h" or "v")
end
vim.api.nvim_set_current_win(current_win)
state.launched = true
end
--- Quit Tidal session
function M.exit_tidal()
if not state.launched then
notify.warn("Tidal is not running. Launch with ':TidalLaunch'")
return
end
for _, proc in ipairs({ state.ghci, state.sclang }) do
if proc then
proc:exit()
end
end
state.launched = false
end
---@return message.TidalRepl | message.SclangRepl
local function ft_to_repl()
local ft = vim.api.nvim_get_option_value("filetype", { buf = 0 })
if ft == "supercollider" then
return message.sclang
end
if ft == "haskell" or ft == "tidal" then
return message.tidal
end
-- default to tidal repl
return message.tidal
end
--- Send text to the interpreter for the current filetype
--- @param text string
function M.send(text)
local repl = ft_to_repl()
if repl then
repl.send_line(text)
end
end
-- Send 'd{count} silence' to tidal interpreter to silence a pattern d1-d16
function M.send_silence()
message.tidal.send_line(string.format("d%d silence", vim.v.count1))
end
--- Send multiline to the interpreter for the current filetype
--- @param lines string[]
function M.send_multiline(lines)
local repl = ft_to_repl()
if repl then
repl.send_multiline(lines)
end
end
--- Send the current line to the interpreter for the current filetype
function M.send_line()
local line = select.get_current_line()
local text = line.lines[1]
if #text > 0 then
require("tidal.core.highlight").apply_highlight(line.start, line.finish)
local repl = ft_to_repl()
if repl then
repl.send_line(text)
end
end
end
--- Send the last visual selection to the interpreter for the current filetype
function M.send_visual()
local visual = select.get_visual()
if visual then
require("tidal.core.highlight").apply_highlight(visual.start, visual.finish)
local repl = ft_to_repl()
if repl then
repl.send_multiline(visual.lines)
end
end
end
--- Send the current block to the interpreter for the current filetype
function M.send_block()
if util.is_empty(vim.api.nvim_get_current_line()) then
return
end
local block = select.get_block()
require("tidal.core.highlight").apply_highlight(block.start, block.finish)
local repl = ft_to_repl()
if repl then
repl.send_multiline(block.lines)
end
end
--- Send current expression node to the interpreter for the current filetype
function M.send_node()
local block = select.get_node()
if block then
require("tidal.core.highlight").apply_highlight(block.start, block.finish)
local repl = ft_to_repl()
if repl then
repl.send_multiline(block.lines)
end
end
end
return M