Skip to content

Commit 2bdd0ae

Browse files
committed
auto-install tree-sitter parser on first .lf file open
The parser is now automatically downloaded when opening a .lf file for the first time if curl is available. Added on_done callback to install() for post-install actions like starting highlighting. Updated README with nvim-treesitter as optional dependency and auto-install docs.
1 parent f8e9ade commit 2bdd0ae

3 files changed

Lines changed: 31 additions & 10 deletions

File tree

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ For users who only want syntax highlighting:
3737
{
3838
"remifan/lf.nvim",
3939
ft = "lf",
40+
dependencies = {
41+
"nvim-treesitter/nvim-treesitter", -- Optional: for incremental selection and textobjects
42+
},
4043
config = function()
4144
require("lf").setup({
4245
enable_lsp = false, -- Disable LSP features
@@ -59,7 +62,8 @@ For the complete experience with LSP, diagrams, and all features (Mac/Linux):
5962
"remifan/lf.nvim",
6063
ft = "lf",
6164
dependencies = {
62-
"nvim-telescope/telescope.nvim", -- Optional: enhanced library browser
65+
"nvim-treesitter/nvim-treesitter", -- Optional: for incremental selection and textobjects
66+
"nvim-telescope/telescope.nvim", -- Optional: enhanced library browser
6367
},
6468
-- Note: Diagram dependencies build automatically on first use
6569
-- No need to specify build command!
@@ -115,15 +119,13 @@ For the complete experience with LSP, diagrams, and all features (Mac/Linux):
115119

116120
## 🌳 Tree-sitter Setup
117121

118-
For modern syntax highlighting, incremental selection, and textobjects:
122+
The tree-sitter parser is **automatically installed** the first time you open a `.lf` file (requires `curl`). You can also install or reinstall manually:
119123

120124
```vim
121125
:LFTSInstall
122126
```
123127

124-
This installs the LF tree-sitter parser and query files. Requires:
125-
- `nvim-treesitter` plugin
126-
- C compiler (gcc or clang) if compiling from source
128+
Pre-built parser binaries are downloaded from GitHub for Linux, macOS, and Windows (x64 and ARM64). Falls back to local compilation if the download fails.
127129

128130
**Incremental Selection** (with nvim-treesitter configured):
129131
- Press `<CR>` in normal mode to start selection

ftplugin/lf.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
-- Filetype plugin for Lingua Franca
22
-- Sets buffer-local options and configurations
33

4-
-- Start tree-sitter highlighting if parser is available
4+
-- Start tree-sitter highlighting if parser is available, otherwise auto-install
55
if pcall(vim.treesitter.language.inspect, "lf") then
66
vim.treesitter.start(vim.api.nvim_get_current_buf(), "lf")
7+
elseif vim.fn.executable("curl") == 1 then
8+
local bufnr = vim.api.nvim_get_current_buf()
9+
require("lf.treesitter").install({
10+
force = false,
11+
on_done = function()
12+
-- Start highlighting on the buffer that triggered the install
13+
if vim.api.nvim_buf_is_valid(bufnr) then
14+
pcall(vim.treesitter.start, bufnr, "lf")
15+
end
16+
end,
17+
})
718
end
819

920
-- Set comment string for commentary.vim and similar plugins

lua/lf/treesitter.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,29 +436,37 @@ local function install_from_local(source_path, opts, callback)
436436
end)
437437
end
438438

439-
local function on_install_success()
439+
local function on_install_success(on_done)
440440
vim.notify("[lf.nvim] LF treesitter parser installed successfully!", vim.log.levels.INFO)
441441
pcall(function()
442442
vim._ts_remove_language("lf")
443443
vim.treesitter.language.add("lf")
444444
end)
445+
if on_done then
446+
on_done()
447+
end
445448
end
446449

447450
-- Main install function
448451
function M.install(opts)
449452
opts = opts or {}
450453
local force = opts.force or false
454+
local on_done = opts.on_done
451455

452456
-- Check if already installed
453457
if M.is_installed() and M.queries_installed() and not force then
454-
vim.notify("[lf.nvim] LF treesitter parser is already installed. Use :LFTSInstall! to reinstall.", vim.log.levels.INFO)
458+
if on_done then
459+
on_done()
460+
else
461+
vim.notify("[lf.nvim] LF treesitter parser is already installed. Use :LFTSInstall! to reinstall.", vim.log.levels.INFO)
462+
end
455463
return
456464
end
457465

458466
-- Try GitHub download first
459467
install_from_github(function(ok, err)
460468
if ok then
461-
on_install_success()
469+
on_install_success(on_done)
462470
return
463471
end
464472

@@ -481,7 +489,7 @@ function M.install(opts)
481489

482490
install_from_local(source_path, opts, function(lok, lerr)
483491
if lok then
484-
on_install_success()
492+
on_install_success(on_done)
485493
else
486494
vim.notify("[lf.nvim] " .. (lerr or "Unknown error"), vim.log.levels.ERROR)
487495
end

0 commit comments

Comments
 (0)