GithubHelp home page GithubHelp logo

Comments (9)

Wansmer avatar Wansmer commented on June 15, 2024 1

I'm planning to implement foreach option which will execute callback for each node. It'll allow transforming text and change brackets to do or end, e.t.c.

I think, this features will solve this case

from treesj.

Wansmer avatar Wansmer commented on June 15, 2024 1

I created draft PR (#68) what can handle block and do_block. Can you add more examples with same case, please?
If you know examples no only on ruby – it will be excellent.

from treesj.

cbochs avatar cbochs commented on June 15, 2024 1

One more ruby example:

Ruby: Block without arguments

Code

# Joined
10.times { puts "foo" }

# Split
10.times do
  puts "foo"
end

Treesitter

call [0, 0] - [0, 23]
  receiver: integer [0, 0] - [0, 2]
  operator: "." [0, 2] - [0, 3]
  method: identifier [0, 3] - [0, 8]
  block: block [0, 9] - [0, 23]
    "{" [0, 9] - [0, 10]
    body: block_body [0, 11] - [0, 21]
      call [0, 11] - [0, 21]
        method: identifier [0, 11] - [0, 15]
        arguments: argument_list [0, 16] - [0, 21]
          string [0, 16] - [0, 21]
            """ [0, 16] - [0, 17]
            string_content [0, 17] - [0, 20]
            """ [0, 20] - [0, 21]
    "}" [0, 22] - [0, 23]
call [2, 0] - [4, 3]
  receiver: integer [2, 0] - [2, 2]
  operator: "." [2, 2] - [2, 3]
  method: identifier [2, 3] - [2, 8]
  block: do_block [2, 9] - [4, 3]
    "do" [2, 9] - [2, 11]
    body: body_statement [3, 2] - [3, 12]
      call [3, 2] - [3, 12]
        method: identifier [3, 2] - [3, 6]
        arguments: argument_list [3, 7] - [3, 12]
          string [3, 7] - [3, 12]
            """ [3, 7] - [3, 8]
            string_content [3, 8] - [3, 11]
            """ [3, 11] - [3, 12]
    "end" [4, 0] - [4, 3]

Python: multiline strings

As for other languages, the only similar constructs I can think of are single-line vs. multi-line strings. For example:

Code

# Joined
some_string = "I am\na\nmultiline\nstring"

# Split
some_string = """
I am
a
multiline
string
"""

Treesitter

expression_statement [0, 0] - [0, 42]
  assignment [0, 0] - [0, 42]
    left: identifier [0, 0] - [0, 11]
    "=" [0, 12] - [0, 13]
    right: string [0, 14] - [0, 42]
      """ [0, 14] - [0, 15]
      escape_sequence [0, 19] - [0, 21]
      escape_sequence [0, 22] - [0, 24]
      escape_sequence [0, 33] - [0, 35]
      """ [0, 41] - [0, 42]
expression_statement [2, 0] - [7, 3]
  assignment [2, 0] - [7, 3]
    left: identifier [2, 0] - [2, 11]
    "=" [2, 12] - [2, 13]
    right: string [2, 14] - [7, 3]
      """ [2, 14] - [2, 17]
      """ [7, 0] - [7, 3]

Edit: @Wansmer I updated this comment to also contain the TS nodes

from treesj.

Wansmer avatar Wansmer commented on June 15, 2024 1

@Wansmer I found a case that appears to be incorrectly splitting:

It is not incorrectly: recursive is false by default for split.
It can be activated with:

  block = u.set_preset_for_dict({
    split = {
      recursive = true,
      recursive_ignore = { 'hash' }, -- not splitting hash
    },
  }),

from treesj.

cbochs avatar cbochs commented on June 15, 2024

Also, here is a snippet of code and its corresponding treesitter representation:

Code

x.each { |y| puts y }

x.each do |y|
  puts y
end

Treesitter

call [0, 0] - [0, 21]
  receiver: identifier [0, 0] - [0, 1]
  operator: "." [0, 1] - [0, 2]
  method: identifier [0, 2] - [0, 6]
  block: block [0, 7] - [0, 21]
    "{" [0, 7] - [0, 8]
    parameters: block_parameters [0, 9] - [0, 12]
      "|" [0, 9] - [0, 10]
      identifier [0, 10] - [0, 11]
      "|" [0, 11] - [0, 12]
    body: block_body [0, 13] - [0, 19]
      call [0, 13] - [0, 19]
        method: identifier [0, 13] - [0, 17]
        arguments: argument_list [0, 18] - [0, 19]
          identifier [0, 18] - [0, 19]
    "}" [0, 20] - [0, 21]
call [2, 0] - [4, 3]
  receiver: identifier [2, 0] - [2, 1]
  operator: "." [2, 1] - [2, 2]
  method: identifier [2, 2] - [2, 6]
  block: do_block [2, 7] - [4, 3]
    "do" [2, 7] - [2, 9]
    parameters: block_parameters [2, 10] - [2, 13]
      "|" [2, 10] - [2, 11]
      identifier [2, 11] - [2, 12]
      "|" [2, 12] - [2, 13]
    body: body_statement [3, 2] - [3, 8]
      call [3, 2] - [3, 8]
        method: identifier [3, 2] - [3, 6]
        arguments: argument_list [3, 7] - [3, 8]
          identifier [3, 7] - [3, 8]
    "end" [4, 0] - [4, 3]

from treesj.

cbochs avatar cbochs commented on June 15, 2024

Awesome! Looking forward to it.

from treesj.

cbochs avatar cbochs commented on June 15, 2024

@Wansmer I found a case that appears to be incorrectly splitting:

Code (Ruby)

# Original
let(:some_array) do
  [
    { bar: "bar" }
  ]
end

# Joined
let(:some_array) { [ { bar: "bar" } ] }

# Split again
let(:some_array) do
  [ { bar: "bar" } ]
end

Expected: split should be reversible and not change the inner array ([])

from treesj.

Wansmer avatar Wansmer commented on June 15, 2024

Done in #68

from treesj.

cbochs avatar cbochs commented on June 15, 2024

This works fantastic. Thanks 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.