Comments (7)
I think there is a way to build the plugin without queries. What you need is very simple, and LanguageTree
provides all the information. This script, using part of your code from edit.lua
does the basic stuff without floating windows or anything complicated. It is a proof of concept. Please, use whatever you want from it if you think it's worth it.
local file_parser = vim.treesitter.get_parser()
if not file_parser then
return
end
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local range = { row, col, row, col }
local ltree = file_parser:language_for_range(range)
if not ltree then
return
end
local trees = ltree:trees()
for _, tree in ipairs(trees) do
local node = tree:root()
if vim.treesitter.node_contains(node, range) then
local mdbufnr = vim.fn.bufnr()
local srow, scol, erow, ecol = node:range(false)
local lines = vim.api.nvim_buf_get_lines(mdbufnr, srow, erow, false)
local filetype = ltree:lang()
vim.cmd('split')
local win = vim.api.nvim_get_current_win()
local bufnr = vim.api.nvim_create_buf(true, true)
vim.api.nvim_buf_set_option(bufnr, 'filetype', filetype)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, lines)
vim.api.nvim_win_set_buf(win, bufnr)
-- Calculate cursor position in new window
vim.api.nvim_win_set_cursor(win, { row - srow, col - scol })
-- Auto commands to update main buffer
vim.api.nvim_create_autocmd({'BufWritePost', 'WinClosed'}, {
buffer = bufnr,
callback = function()
local nlines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
vim.api.nvim_buf_set_lines(mdbufnr, srow, erow, true, nlines)
end,
})
-- make sure the buffer is deleted when we close the window
-- useful if user has hidden set
vim.api.nvim_create_autocmd('BufHidden', {
buffer = bufnr,
callback = function()
vim.schedule(function()
if vim.api.nvim_buf_is_loaded(bufnr) then
vim.cmd(string.format('bdelete! %d', bufnr))
end
end)
end,
})
end
end
It also works with other injections, like javascript or CSS in HTML. Save the script as test.lua
and run it on the file you want to test, luafile ./test.lua
. It will open a new buffer in a split window with the contents of the code block that contains the cursor, and the correct file type.
from nvim-femaco.lua.
I wrote a small plugin that implements the proposal in case you want to try it and take pieces of it here
from nvim-femaco.lua.
Of course! You can merge anything you want from it, that's why I included the link. I just wrote it as an example, and covers my very basic needs, which don't include floating windows etc. I won't be extending it except for org
support. The org
tree-sitter parser is not complete, so the general LanguageTree
mechanism doesn't work yet. and I am writing a special case function. That will probably be the only update I do to edit-code-block.nvim
for the time being. I use org
for my configuration file and notes.
from nvim-femaco.lua.
Thanks a lot for the issue, this made it easy to find a fix in #27
from nvim-femaco.lua.
Wait.. Why is this plugin dependent on the specifics of a query from nvim-treesitter
?
I thought it was now only getting injected regions through the injections API (with languagetree & co), as it's supposed to work with any language injection in any language now.
=> Why a change of the markdown injection query breaks this plugin?
from nvim-femaco.lua.
@bew, the reason is that we get all the injections by:
local matches = require('nvim-treesitter.query').get_matches(vim.api.nvim_get_current_buf(), 'injections')
and then we need to figure out what is the language of any of the matches which can be either in:
match.language
match._lang
- or
match
is a table mapping languages to the injected content
Is there a better way to do this to avoid the dependency on the structure of the matches?
from nvim-femaco.lua.
That's awesome @dawsers! Are you planning to keep working on that plugin or should we try to merge it in here? Whatever you prefer :)
from nvim-femaco.lua.
Related Issues (18)
- Add alternate styling to render window in-buffer using virtual lines HOT 3
- Error: attempt to call field 'get_node_range' (a nil value) HOT 8
- Compatibility with norg? HOT 14
- Detect FeMaco buffer on LSP attach HOT 1
- Dim main buffer to focus on code HOT 1
- [feat] use same buffer for multiple code chunks (+ collaboration ideas) HOT 6
- Expand `float_opts` with user's options HOT 4
- Handle language specific settings HOT 2
- Incorrect indentation in org-mode HOT 2
- Can `FeMaco` add a mechanism for clean temp file HOT 2
- Extend functionality to any buffer with injected language blocks? HOT 13
- Question: future of nvim-FeMaco.lua HOT 2
- Error when use command `:Femaco` HOT 5
- Question: When I use FeMaco to input a code, it will automatically insert `v:null` HOT 5
- Nothing happend after call `:FeMaco` HOT 1
- Split or separate buffer instead of floating window HOT 3
- Support for inline injections
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nvim-femaco.lua.