GithubHelp home page GithubHelp logo

Split classnames on new lines about treesj HOT 3 CLOSED

wansmer avatar wansmer commented on June 18, 2024
Split classnames on new lines

from treesj.

Comments (3)

benwoodward avatar benwoodward commented on June 18, 2024 2

Here's a config that works for Svelte if anyone needs it (you'll need to override the max_join_length for the join to work, assuming you have a lot of atomic css classes):

  {
    'Wansmer/treesj',
    dependencies = { 'nvim-treesitter/nvim-treesitter' },
    config = function()
      local ts = require('vim.treesitter')

      require('treesj').setup({
        use_default_keymaps = false,
        cursor_behavior = 'start',
        max_join_length = 200,
        langs = {
          svelte = {
            ['quoted_attribute_value'] = {
              both = {
                enable = function(tsn)
                  return tsn:parent():type() == 'attribute'
                end,
              },
              split = {
                format_tree = function(tsj)
                  local str = tsj:child('attribute_value')
                  local words = vim.split(str:text(), ' ')
                  tsj:remove_child('attribute_value')
                  for i, word in ipairs(words) do
                    tsj:create_child({ text = word }, i + 1)
                  end
                end,
              },
              join = {
                format_tree = function(tsj)
                  local str = tsj:child('attribute_value')
                  local node_text = str:text()
                  tsj:remove_child('attribute_value')
                  tsj:create_child({ text = node_text }, 2)
                end,
              },
            },
          }
        }
      })
    end,
  },

from treesj.

Wansmer avatar Wansmer commented on June 18, 2024

Hi!
I'm not sure if that is a task to solving with TreeSJ (because tree-sitter recognize it just as string, without any children). But you can add something like this in your langs.javascript section in require('treesj').setup({ ... }):

local javascript = {
-- ...
  ['string'] = {
    both = {
      enable = function(tsn)
        return tsn:parent():type() == 'jsx_attribute'
      end,
    },
    split = {
      format_tree = function(tsj)
        local str = tsj:child('string_fragment')
        local words = vim.split(str:text(), ' ')
        tsj:remove_child('string_fragment')
        for i, word in ipairs(words) do
          tsj:create_child({ text = word }, i + 1)
        end
      end,
    },
  },
-- ...
}

(I'm not sure what is a valid string for jsx)

2023-03-17.16.16.13.mov

from treesj.

MaximilianLloyd avatar MaximilianLloyd commented on June 18, 2024

This is exactly what i needed 😄. Works perfectly. Thank you!

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.