Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lua/livepreview/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ function M.start(filepath, port)
return true
end

function M.pick()
--- Create picker object and open the picker
---@param port number [Port number to open on, this is optional]
function M.pick(port)
-- Adding port fallback if the parameter is nil
port = port or config.config.port
local picker = require("livepreview.picker")

local pick_callback = function(pick_value)
Expand All @@ -101,12 +105,12 @@ function M.pick()
vim.notify("No file picked", vim.log.levels.INFO)
return
end
M.start(filepath, config.config.port)
M.start(filepath, port)
vim.cmd.edit(filepath)
utils.open_browser(
string.format(
"http://localhost:%d/%s",
config.config.port,
port,
config.config.dynamic_root and vim.fs.basename(filepath) or filepath
),
config.config.browser
Expand Down
50 changes: 47 additions & 3 deletions plugin/livepreview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if vim.g.loaded_livepreview then
end

vim.g.loaded_livepreview = true
local PORT_PREFIX = "++port="

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use uppercase letters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the lua JIT, there are no constant types. Thus naming convention must be used to indicate when something is a constant. In this case the prefix is a constant.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lua 5.1 doesn't have any convention for constant, and there is no need to invent one. Not to say that using uppercase letter for local constant is a bad idea, because you won't get LuaLS diagnostic if you mistype it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using capital letters for constants is a well-established convention in many programming languages (including C, C++, Rust, Python, Java, and JavaScript) to make them immediately distinguishable from variables whose values may change.

This improves readability and makes the intent explicit, especially in collaborative codebases. It’s not inventing a new convention, it’s applying a long standing one.

If i want to i can probably find examples in lua codebases as well.

While Lua doesn’t have an officially standardized convention for constants, borrowing this approach is not a bad idea.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to say that using uppercase letter for local constant is a bad idea, because you won't get LuaLS diagnostic if you mistype it.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LuaCAT doesn't have constant annotation, but maybe this hack works

---@type "++port="
local port_prefix = "++port="

Haven't tested though


local health = require("livepreview.health")
local cmd = "LivePreview"
Expand Down Expand Up @@ -35,7 +36,30 @@ api.nvim_create_user_command(cmd, function(cmd_opts)

if subcommand == "start" then
local filepath
if cmd_opts.fargs[2] ~= nil then
local port = Config.port

-- determines if the options given is a port or filepath
Comment thread
Ren-B-7 marked this conversation as resolved.
for i = 2, 3 do
local arg = cmd_opts.fargs[i]
if arg then
if arg:sub(1, #PORT_PREFIX) == PORT_PREFIX then
-- If the substring is not a number, this returns nil
local numb = tonumber(arg:sub(#PORT_PREFIX + 1))
if numb then
port = numb
else
vim.notify(
"Error: LivePreview couldnt parse ++port option. Invalid Number.",
vim.log.levels.WARN
)
end
else
filepath = arg
end
end
end

if filepath ~= nil then
filepath = cmd_opts.fargs[2]
if not utils.is_absolute_path(filepath) then
filepath = fs.joinpath(vim.uv.cwd(), filepath)
Expand All @@ -54,7 +78,7 @@ api.nvim_create_user_command(cmd, function(cmd_opts)
end
end
filepath = fs.normalize(filepath)
if not lp.start(filepath, Config.port) then
if not lp.start(filepath, port) then
return
end

Expand All @@ -69,7 +93,27 @@ api.nvim_create_user_command(cmd, function(cmd_opts)
lp.close()
print("Live preview stopped")
elseif subcommand == "pick" then
lp.pick()
-- Create port option, with the default port, then if arguments were
-- given it checks that it is a valid number and then changes the port
-- option to the given argument
local port = Config.port
-- Must set opt to "" as default else the sub statement returns an error
local opt = cmd_opts.fargs[2] or ""
if opt:sub(1, #PORT_PREFIX) == PORT_PREFIX then
-- If the substring is not a number, this returns nil
local numb = tonumber(opt:sub(#PORT_PREFIX + 1))
if numb then
port = numb
else
vim.notify("Error: LivePreview couldnt parse ++port option. Invalid Number.", vim.log.levels.WARN)

@brianhuster brianhuster Aug 13, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does "Error" use "WARN" highlight?

@Ren-B-7 Ren-B-7 Aug 13, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in my head i thaught it would have fallback to the basic port option, and still open on the default port if it isnt in use. Which means its not an error, since the program didnt have to terminate early.

But when typing it out my head defaulted to Error: ...

It is your call on which you would prefer to change. Either the notification header from error to warning or the log level from vim.log.levels.WARN to ERROR in which case we would also have to think of stopping the command from executing.

end
else
vim.notify(
"Error: LivePreview couldnt parse parameter. Parameters should start be [++port=number].",
vim.log.levels.WARN
)
end
lp.pick(port)
else
lp.help()
end
Expand Down