Skip to content

Change init.lua, such that you can directly call the livepreview api#315

Closed
Ren-B-7 wants to merge 15 commits intobrianhuster:mainfrom
Ren-B-7:main
Closed

Change init.lua, such that you can directly call the livepreview api#315
Ren-B-7 wants to merge 15 commits intobrianhuster:mainfrom
Ren-B-7:main

Conversation

@Ren-B-7
Copy link
Copy Markdown
Contributor

@Ren-B-7 Ren-B-7 commented Jul 17, 2025

Made it such that calling require("livepreview").start() has the same effect as :LivePreview start

Making outcomes more uniform, and enabling easier forming keymaps

Now calling start(filename, 5000) is allowed along with start()

@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Jul 20, 2025

Fixed the luacheck error. Added more fail safes where i was able to force uncaught errors, made the lua command for start operate the same as the vim command, without affecting any of its usual operations (we can potentially remove the now redundant code in the auto command for :LivePreview start). Also worked on one of your todo statements. I have more commits and changes in the pipeline.

Copy link
Copy Markdown
Contributor Author

@Ren-B-7 Ren-B-7 left a comment

Choose a reason for hiding this comment

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

The port = port or 5000 should be 5500 to align with the LivePreview way, or potentially be related to the default config.

@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Jul 22, 2025

Added port options and changed the default 5000 to 5500 (My mistake)

Comment thread lua/livepreview/utils.lua
@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Jul 22, 2025

Done

@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Jul 22, 2025

There is one edge case that doesnt get properly handled and throws an error

In the rare case where using :LivePreview start /path/to/file and the file is outside of the current working directory, and dynamic root = false then it will throw an error

@brianhuster
Copy link
Copy Markdown
Owner

In the rare case where using :LivePreview start /path/to/file and the file is outside of the current working directory, and dynamic root = false then it will throw an error

True, I think I have never attempted to support that use case. You can open another issue

@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Jul 22, 2025

Pull request can be closed then, will work on the improvement to path
later.

@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Jul 22, 2025

Just realized that first commit is absolute garbage, all i did was add more checks, it doesnt mean that require("livepreview").start() can be called directly

Also will have to make a small change to the start() function, since in some cases where the server fails to initiate it isnt caught.

Luckily it doesnt break anything and merging is still safe, but yeah i didnt test properly and the commit message is a fraud. Apologies.

@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Jul 25, 2025

I dont feel like adding more commits to this pull request. As it is in a working state. And next changes will be related to a different part of the live-preview system.

Recommend merging and closing. Further changes will be on a different pull-request.

Comment thread lua/livepreview/init.lua Outdated
Comment thread lua/livepreview/_spec.lua
utils.get_relative_path("/home/user/.config/nvim/lua/livepreview/utils.lua", "/home/user/.config/nvim/")
assert(relative_path == "lua/livepreview/utils.lua", "should return the relative path")
relative_path = utils.get_relative_path("/home/user/project/file.lua", "/home/user/project/file.lua")
assert(relative_path == "home/user/project/file.lua", "should return the fullpath when full_path == parent_path")
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.

Sorry, I don't understand what the point of this is. Why should it return full path?

Comment thread plugin/livepreview.lua
LivePreview = {
config = vim.deepcopy(config.default),
}
local LivePreview = {}
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.

LivePreview is intended to be global (see the doc)

Comment thread lua/livepreview/init.lua
---@param port number: port to run the server on
---@return boolean?
function M.start(filepath, port)
filepath = filepath or vim.fn.fnamemodify(vim.api.nvim_buf_get_name(0), ":p")
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.

I don't understand the point of this, doesn't nvim_buf_get_name() return full path by default? Why use yet another modifier that just convert path to full path?

@brianhuster
Copy link
Copy Markdown
Owner

Sorry, but after reviewing it many times, I can't understand what the point of this PR is. It seems like you are trying to bring many changes that are unrelated to each other, to a single PR, which makes it much harder for me to review.

@Ren-B-7
Copy link
Copy Markdown
Contributor Author

Ren-B-7 commented Aug 1, 2025

Agreed this is a few too many unrelated commits that was undocumented. Will restructure and comment reasons for changes.

@Ren-B-7 Ren-B-7 closed this Aug 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants