GithubHelp home page GithubHelp logo

Fallback to `mini.splitjoin` about treesj HOT 2 CLOSED

wansmer avatar wansmer commented on June 18, 2024
Fallback to `mini.splitjoin`

from treesj.

Comments (2)

Wansmer avatar Wansmer commented on June 18, 2024

To add fallback for specific nodes, use the fallback option in the node preset (example of usage).

Another way to add fallback to mini.splitjoin is how I personally use it.

Since both solutions depend on a third-party plugin, I will not to add this to the default configuration. API methods will do.

from treesj.

savchenko avatar savchenko commented on June 18, 2024

Here is a code snippet if anyone stumbles upon the same issue in future:

local function get_pos_lang(node)
    local c = vim.api.nvim_win_get_cursor(0)
    local range = { c[1] - 1, c[2], c[1] - 1, c[2] }
    local buf = vim.api.nvim_get_current_buf()
    local ok, parser = pcall(
        vim.treesitter.get_parser,
        buf,
        vim.treesitter.language.get_lang(vim.bo[buf].ft)
    )
    if not ok then
        return ""
    end
    local current_tree = parser:language_for_range(range)
    return current_tree:lang()
end

vim.keymap.set({ 'n', 'v' }, "<leader>m", function()
    local tsj_langs = require("treesj.langs")["presets"]
    local lang = get_pos_lang()
    if lang ~= "" and tsj_langs[lang] then
        require("treesj").toggle()
    else
        require("mini.splitjoin").toggle()
    end
end)

from treesj.

Related Issues (20)

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.