Skip to content

Commit

Permalink
Edit: Floating Window
Browse files Browse the repository at this point in the history
Added ocaml lsp, and added floating window for _FOX_IDE_RUN function.
  • Loading branch information
Mr-Fox-h committed Dec 27, 2024
1 parent fbe0a99 commit 7cdd527
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ require("user.neovide")
require("user.outline")
require("user.formatter")
require("user.foxide")
require("user.run-window")
3 changes: 3 additions & 0 deletions lua/user/formatter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ require 'lspconfig'.rubocop.setup {}

-- C & CPP Setup --
require 'lspconfig'.clangd.setup {}

-- Ocaml Setup --
require 'lspconfig'.ocamllsp.setup {}
5 changes: 2 additions & 3 deletions lua/user/foxide.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ function _FOX_IDE_RUN()
vim.notify("Running: " .. jsonData.program.run, vim.log.levels.INFO)
if jsonData.program.build ~= '' then
local buildHandle = io.popen(jsonData.program.build)
local buildOutput = buildHandle:read("*a")
-- local buildOutput = buildHandle:read("*a")
buildHandle:close()
vim.notify("Build Output:\n" .. buildOutput, vim.log.levels.INFO)
end
local runHandle = io.popen(jsonData.program.run)
local runOutput = runHandle:read("*a")
runHandle:close()

vim.notify("Run Output:\n" .. runOutput, vim.log.levels.INFO)
return runOutput
end

-- Settings --
2 changes: 1 addition & 1 deletion lua/user/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ keymap('n', "<Space>m", ":lua _BTOP_TOGGLE() <CR>", opts)

-- Fox ide run --
keymap('n', "<Space><Space>x", ":.lua <CR>", opts)
keymap('n', "<Space>x", ":lua _FOX_IDE_RUN()<CR>", opts)
keymap('n', "<Space>x", ":Run <CR>", opts)
65 changes: 65 additions & 0 deletions lua/user/run-window.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
vim.keymap.set("t", "<esc><esc>", "<c-\\><c-n>")

local state = {
floating = {
buf = -1,
win = -1,
}
}

local function create_floating_window(opts)
opts = opts or {}
local width = opts.width or math.floor(vim.o.columns * 0.8)
local height = opts.height or math.floor(vim.o.lines * 0.8)

-- Calculate the position to center the window
local col = math.floor((vim.o.columns - width) / 2)
local row = math.floor((vim.o.lines - height) / 2)

-- Create a buffer
local buf = nil
if vim.api.nvim_buf_is_valid(opts.buf) then
buf = opts.buf
else
buf = vim.api.nvim_create_buf(false, true) -- No file, scratch buffer
end

-- Define window configuration
local win_config = {
relative = "editor",
width = width,
height = height,
col = col,
row = row,
style = "minimal", -- No borders or extra UI elements
border = "rounded",
}

-- Create the floating window
local win = vim.api.nvim_open_win(buf, true, win_config)

return { buf = buf, win = win }
end

local toggle_floating_run = function()
if not vim.api.nvim_win_is_valid(state.floating.win) then
state.floating = create_floating_window { buf = state.floating.buf }
vim.api.nvim_buf_set_lines(state.floating.buf, 0, -1, false, { "Running your code..." })
vim.defer_fn(function()
local success, output = pcall(_FOX_IDE_RUN)
if success then
-- Display the output in the floating window
vim.api.nvim_buf_set_lines(state.floating.buf, 0, -1, false, vim.split(output, "\n"))
else
-- Display the error in the floating window
vim.api.nvim_buf_set_lines(state.floating.buf, 0, -1, false, { "Error:", output })
end
end, 100) -- Slight delay to show the floating window before running
else
vim.api.nvim_win_hide(state.floating.win)
end
end

-- Example usage:
-- Create a floating window with default dimensions
vim.api.nvim_create_user_command("Run", toggle_floating_run, {})

0 comments on commit 7cdd527

Please sign in to comment.