Git Product home page Git Product logo

Comments (7)

dawsers avatar dawsers commented on August 31, 2024 2

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.

dawsers avatar dawsers commented on August 31, 2024 1

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.

dawsers avatar dawsers commented on August 31, 2024 1

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.

AckslD avatar AckslD commented on August 31, 2024

Thanks a lot for the issue, this made it easy to find a fix in #27

from nvim-femaco.lua.

bew avatar bew commented on August 31, 2024

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.

AckslD avatar AckslD commented on August 31, 2024

@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.

AckslD avatar AckslD commented on August 31, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.