Skip to content

Commit 481156a

Browse files
committed
initial
0 parents  commit 481156a

File tree

7 files changed

+238
-0
lines changed

7 files changed

+238
-0
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Setup
2+
3+
```lua
4+
require("rescript").setup{
5+
6+
}
7+
```
8+
9+
10+
## API
11+
12+
```lua
13+
local rescript = require("rescript")
14+
15+
rescript.open_compiled_file()
16+
rescript.create_interface()
17+
rescript.switch_impl_intf()
18+
```
19+
20+
21+
## TODO
22+
23+
- [x] Open Compiled JS
24+
- [x] Create Interface File
25+
- [x] Switch Interface/implementation file
26+
- [ ] Reanalyze/Code Analysis
27+
- [ ] Snippets

lua/rescript/cache.lua

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local M = {}
2+
3+
M.client = nil
4+
5+
return M

lua/rescript/handlers.lua

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local M = {}
2+
3+
4+
5+
M.create_interface = function (err, result, ctx)
6+
-- bufnr = bufnr or vim.api.nvim_get_current_buf()
7+
-- local name = vim.api.nvim_buf_get_name(bufnr)
8+
9+
-- if err ~= nil then
10+
-- vim.notify('Failed to create interface file', vim.log.levels.ERROR)
11+
-- return
12+
-- end
13+
14+
P{err, result, ctx}
15+
16+
17+
-- if vim.loop.fs_stat(name .. 'i') ~= nil then
18+
-- vim.notify('Interface file already exists', vim.log.levels.INFO)
19+
-- return
20+
-- end
21+
22+
-- client.request(M.methods.create_interface, vim.lsp.util.make_text_document_params(), function (err, result)
23+
--
24+
-- end, bufnr)
25+
26+
-- vim.lsp.buf_request(bufnr, M.methods.create_interface, vim.lsp.util.make_text_document_params(), function (err, result, ctx)
27+
-- P{err, result, ctx}
28+
-- end)
29+
end
30+
31+
return M

lua/rescript/init.lua

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local cache = require("rescript.cache")
2+
local reanalyze = require("rescript.reanalyze")
3+
local M = {}
4+
5+
M.on_attach = function(client, bufnr, opts)
6+
opts =
7+
vim.tbl_deep_extend("force", { autoRunCodeAnalysis = false }, opts or {})
8+
if client.name == "rescriptls" then
9+
cache.client = client
10+
if opts.autoRunCodeAnalysis then
11+
reanalyze.run()
12+
end
13+
end
14+
end
15+
16+
return M

lua/rescript/lsp.lua

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
local cache = require("rescript.cache")
2+
3+
local METHODS = {
4+
createInterface = "rescript-vscode.create_interface",
5+
openCompiled = "rescript-vscode.open_compiled",
6+
}
7+
8+
local M = {}
9+
10+
local get_extension = function(name)
11+
return string.match(name, ".[^.]+$")
12+
end
13+
14+
local open = function(target, fallback)
15+
if vim.api.nvim_buf_get_name(0) == target then
16+
return
17+
end
18+
19+
local buffers = vim.fn.getbufinfo({ buflisted = 1 })
20+
21+
if #buffers > 1 then
22+
for _, buf in ipairs(buffers) do
23+
if buf.name == target then
24+
if buf.hidden == 0 then
25+
vim.api.nvim_set_current_win(buf.windows[1])
26+
else
27+
vim.api.nvim_set_current_buf(buf.bufnr)
28+
end
29+
end
30+
end
31+
end
32+
33+
fallback(target)
34+
end
35+
36+
M.create_interface = function(opts)
37+
local client = cache.client
38+
39+
opts = vim.tbl_deep_extend(
40+
"force",
41+
{ bufnr = 0, open = true, callback = vim.cmd.edit },
42+
opts or {}
43+
)
44+
45+
local name = vim.api.nvim_buf_get_name(opts.bufnr)
46+
47+
if vim.loop.fs_stat(name .. "i") ~= nil then
48+
vim.notify("rescript: Interface file already exists", vim.log.levels.INFO)
49+
return
50+
end
51+
52+
client.request(
53+
METHODS.createInterface,
54+
vim.lsp.util.make_text_document_params(opts.bufnr),
55+
function(_, result, ctx)
56+
if result then
57+
local interface_file = vim.uri_to_fname(ctx.params.uri) .. "i"
58+
if opts.open then
59+
opts.callback(interface_file)
60+
end
61+
end
62+
end,
63+
opts.bufnr
64+
)
65+
end
66+
67+
M.open_compiled = function(opts)
68+
opts = vim.tbl_deep_extend(
69+
"force",
70+
{ bufnr = 0, callback = vim.cmd.edit },
71+
opts or {}
72+
)
73+
local client = cache.client
74+
75+
client.request(
76+
METHODS.openCompiled,
77+
vim.lsp.util.make_text_document_params(opts.bufnr),
78+
function(_, result, _)
79+
if result then
80+
open(result.uri, opts.callback)
81+
end
82+
end,
83+
opts.bufnr
84+
)
85+
end
86+
87+
M.switch_impl_intf = function(opts)
88+
opts = vim.tbl_deep_extend(
89+
"force",
90+
{ bufnr = 0, ask = false, callback = vim.cmd.edit },
91+
opts or {}
92+
)
93+
94+
if vim.filetype.match({ buf = opts.bufnr }) ~= "rescript" then
95+
vim.notify(
96+
"rescript: This command only can run on *.res or *.resi files.",
97+
vim.log.levels.INFO
98+
)
99+
return
100+
end
101+
102+
local name = vim.api.nvim_buf_get_name(opts.bufnr)
103+
104+
local file_extension = get_extension(name)
105+
106+
if file_extension == ".resi" then
107+
-- Go to implementation .res
108+
open(string.sub(name, 0, string.len(name) - 1), opts.callback)
109+
return
110+
elseif file_extension == ".res" then
111+
-- Go to Interface .resi
112+
-- if interface doesn't exist, ask the user before creating.
113+
local target = name .. 'i'
114+
local interface_exists = vim.loop.fs_stat(target) ~= nil
115+
if not interface_exists and opts.ask then
116+
vim.ui.select({ "No", "Yes" }, {
117+
prompt = "Do you want to create an interface *.resi:",
118+
}, function(choice)
119+
if choice == "Yes" then
120+
M.create_interface()
121+
end
122+
end)
123+
end
124+
125+
if interface_exists then
126+
open(target, opts.callback)
127+
return
128+
end
129+
else
130+
vim.notify(
131+
"rescript: Faield to detect extension for " .. name,
132+
vim.log.levels.ERROR
133+
)
134+
end
135+
end
136+
137+
return M

lua/rescript/reanalyze.lua

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local M = {}
2+
3+
M.run = function()
4+
local group =
5+
vim.api.nvim_create_augroup("recript/compilationFinished", { clear = true })
6+
vim.api.nvim_create_autocmd("BufWritePost", {
7+
group = group,
8+
callback = function()
9+
-- vim.lsp.buf.format({
10+
-- timeout_ms = 2000,
11+
-- async = true,
12+
-- })
13+
end,
14+
buffer = bufnr,
15+
})
16+
end
17+
18+
return M

stylua.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
column_width = 80
2+
line_endings = "Unix"
3+
indent_type = "Spaces"
4+
indent_width = 2

0 commit comments

Comments
 (0)