Skip to content

Commit

Permalink
Refactor and add get_main_file (#29)
Browse files Browse the repository at this point in the history
* design-doc and restructuring

* wrote some design-doc

* server/

* commands.lua

* only editor.lua left

* completed and vaguely tested

* bumpped version in README.md

* fixed sync_with_cursor and removed duplicated function definition

* updated README with new config

* updated doc

* smol style
  • Loading branch information
chomosuke authored May 6, 2024
1 parent 1394f5d commit 0f3e432
Show file tree
Hide file tree
Showing 18 changed files with 574 additions and 393 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test.pdf
*.pdf
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ https://github.com/chomosuke/typst-preview.nvim/assets/38484873/9f8ecf0f-aa1c-4e
{
'chomosuke/typst-preview.nvim',
lazy = false, -- or ft = 'typst'
version = '0.1.*',
version = '0.3.*',
build = function() require 'typst-preview'.update() end,
}
```
Expand All @@ -29,15 +29,15 @@ https://github.com/chomosuke/typst-preview.nvim/assets/38484873/9f8ecf0f-aa1c-4e
```lua
use {
'chomosuke/typst-preview.nvim',
tag = 'v0.1.*',
tag = 'v0.3.*',
run = function() require 'typst-preview'.update() end,
}
```

**vim-plug:**

```vim
Plug 'chomosuke/typst-preview.nvim', {'tag': 'v0.1.*', do: ':TypstPreviewUpdate'}
Plug 'chomosuke/typst-preview.nvim', {'tag': 'v0.3.*', do: ':TypstPreviewUpdate'}
```

## 🚀 Usage
Expand Down Expand Up @@ -98,9 +98,18 @@ require 'typst-preview'.setup {
invert_colors = 'never',

-- This function will be called to determine the root of the typst project
get_root = function(bufnr_of_typst_buffer)
return vim.fn.getcwd()
get_root = function(path_of_main_file)
return vim.fn.fnamemodify(path_of_main_file, ':p:h')
end,

-- This function will be called to determine the main file of the typst
-- project.
get_main_file = function(path_of_buffer)
return path_of_buffer
end,

-- Whether the preview will follow the cursor in the source file
follow_cursor = true,
}
```

Expand Down
16 changes: 16 additions & 0 deletions design-doc.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- `server/`
- `server/factory.lua` makes servers, which are objects each representing a
file that a server is watching.
- `server/inventory.lua` store servers and index them by their absolute path.
- `server/init.lua` contains the class definition and all its methods. The
methods, it encapsulate event specific logic.
- `commands.lua` register user command and map them to functions. It should not
contain too much logic.
- `fetch.lua` do everything that needs to be done in terms of managing binaries.
- `events/`
- `events/editor.lua` handles editor initiated events. It register listeners
through autocmds on all file with filetype `typst`.
- `events/server.lua` handles server initiated events. It register listeners
on all servers.
- All package inside a folder should not be accessed by those outside except for
`init.lua`.
23 changes: 20 additions & 3 deletions doc/typst-preview.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ CONTENTS *typst-preview*

Start or stop scrolling preview as cursor moves.

Calls: `require 'typst-preview'.set_follow_cursor(not init.get_follow_cursor())`.
Calls:
`require 'typst-preview'.set_follow_cursor(
not require 'typst-preview'.get_follow_cursor()
)`.

*:TypstPreviewSyncCursor*

Expand Down Expand Up @@ -129,16 +132,30 @@ Set to `'always'` to enable.
Set to `'auto'` to enable if environment (usually browser) has enabled darkmode.
Type: `string`, Default: `'never'`

*typst-preview.get_main_file*
This function will be called to determine the main file of the typst project

Parameters: ~
- {path} (string) The path of the current buffer. This is the buffer that
was focused when `:TypstPreview` is called.

Return: ~
(string) The path to the main file of the typst project.

Type: `function`,
Default: `function(path) return path end`

*typst-preview.get_root*
This function will be called to determine the root of the typst project

Parameters: ~
- {bufnr} (integer) The bufnr of the typst file that being previewed.
- {path} (string) The path to the main file. This is the string returned by
|typst-preview.get_main_file|.

Return: ~
(string) The path to the root of the typst project.

Type: `function`,
Default: `function(bufnr_of_typst_buffer) return vim.fn.getcwd() end`
Default: `function(path) return vim.fn.fnamemodify(path_of_main_file, ':p:h') end`

vim:tw=78:ts=2:sw=2:et:ft=help:norl:
71 changes: 36 additions & 35 deletions lua/typst-preview/commands.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
local events = require 'typst-preview.events'
local fetch = require 'typst-preview.fetch'
local utils = require 'typst-preview.utils'
local init = require 'typst-preview.init'
local config = require 'typst-preview.config'
local server = require 'typst-preview.server'

local M = {}

local previewing = {}
---Scroll all preview to cursor position.
function M.sync_with_cursor()
for _, ser in pairs(server.get_all()) do
server.sync_with_cursor(ser)
end
end

---Create user commands
function M.create_commands()
local function preview_off(bufnr)
bufnr = bufnr or vim.fn.bufnr()
if previewing[bufnr] then
previewing[bufnr] = false
events.stop(bufnr)
local function preview_off()
local path = utils.get_buf_path(0)

if path ~= '' and server.remove(config.opts.get_main_file(path)) then
utils.print 'Preview stopped'
else
utils.print 'Preview not running'
end
end

local function preview_on()
local bufnr = vim.fn.bufnr()
-- check if binaries are available and tell them to fetch first
for _, bin in pairs(fetch.bins_to_fetch()) do
if
Expand All @@ -35,54 +40,50 @@ function M.create_commands()
end
end

if not previewing[bufnr] then
utils.create_autocmds('typst-preview-autocmds-unload-' .. bufnr, {
{
event = 'BufUnload',
opts = {
callback = function()
-- preview_off is the source of truth of cleaning up everything.
preview_off(bufnr)
end,
buffer = bufnr,
},
},
})
previewing[bufnr] = true
events.watch(bufnr, function(link)
previewing[bufnr] = link
local path = utils.get_buf_path(0)
if path == '' then
print 'Can not preview an unsaved buffer.'
return
end

path = config.opts.get_main_file(path)
local s = server.get(path)
if s == nil then
server.init(path, function(ser)
events.listen(ser)
end)
elseif type(previewing[bufnr]) == 'string' then
else
print 'Opening another frontend'
utils.visit(previewing[bufnr])
utils.visit(s.link)
end
end

vim.api.nvim_create_user_command('TypstPreviewUpdate', init.update, {})
vim.api.nvim_create_user_command('TypstPreviewUpdate', function()
fetch.fetch(nil)
end, {})

vim.api.nvim_create_user_command('TypstPreview', preview_on, {})
vim.api.nvim_create_user_command('TypstPreviewStop', function()
preview_off()
end, {})
vim.api.nvim_create_user_command('TypstPreviewStop', preview_off, {})
vim.api.nvim_create_user_command('TypstPreviewToggle', function()
if previewing[vim.fn.bufnr()] then
local path = utils.get_buf_path(0)
if path ~= '' and server.get(config.opts.get_main_file(path)) ~= nil then
preview_off()
else
preview_on()
end
end, {})

vim.api.nvim_create_user_command('TypstPreviewFollowCursor', function()
init.set_follow_cursor(true)
config.set_follow_cursor(true)
end, {})
vim.api.nvim_create_user_command('TypstPreviewNoFollowCursor', function()
init.set_follow_cursor(false)
config.set_follow_cursor(false)
end, {})
vim.api.nvim_create_user_command('TypstPreviewFollowCursorToggle', function()
init.set_follow_cursor(not init.get_follow_cursor())
config.set_follow_cursor(not config.get_follow_cursor())
end, {})
vim.api.nvim_create_user_command('TypstPreviewSyncCursor', function()
init.sync_with_cursor()
M.sync_with_cursor()
end, {})
end

Expand Down
20 changes: 18 additions & 2 deletions lua/typst-preview/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@ local M = {
['typst-preview'] = nil,
['websocat'] = nil,
},
get_root = function(_)
return vim.fn.getcwd()
get_root = function(path)
return vim.fn.fnamemodify(path, ':p:h')
end,
get_main_file = function(path)
return path
end,
follow_cursor = true,
},
}

function M.config(opts)
M.opts = vim.tbl_deep_extend('force', M.opts, opts or {})
end

---Set the way preview scrolling respond to cursor movement
---@param enabled boolean
function M.set_follow_cursor(enabled)
M.opts.follow_cursor = enabled
end

---Get current preview scrolling mode
---@return boolean
function M.get_follow_cursor()
return M.opts.follow_cursor
end

return M
112 changes: 0 additions & 112 deletions lua/typst-preview/events/autocmds.lua

This file was deleted.

Loading

0 comments on commit 0f3e432

Please sign in to comment.