|
| 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 |
0 commit comments