AckslD / nvim-trevJ.lua

Nvim-plugin for doing the opposite of join-line (J) of arguments, powered by treesitter
MIT License
167 stars 10 forks source link

Split python method chaining #38

Open raffaem opened 5 months ago

raffaem commented 5 months ago

Is it possible to split on Python method chains as well?

For example:

s = "hello, how are you?"
s2 = s.replace("hello", "hi").replace("you", "we").title().lower().strip()

To become:

s = "hello, how are you?"
s2 = s.replace("hello", "hi") \
         .replace("you", "we") \
         .title() \
         .lower() \
         .strip()
AckslD commented 5 months ago

Hi @raffaem, hmm, this would be difficult to do the way trevj works at the moment since it basically just adds newlines between children of some treesitter (container) node. However in this case the syntax tree is nested:

(expression_statement) ; [1:1 - 25]
 (assignment) ; [1:1 - 25]
  left: (identifier) ; [1:1 - 1]
  right: (string) ; [1:5 - 25]
   (string_start) ; [1:5 - 5]
   (string_content) ; [1:6 - 24]
   (string_end) ; [1:25 - 25]
(expression_statement) ; [2:1 - 74]
 (assignment) ; [2:1 - 74]
  left: (identifier) ; [2:1 - 2]
  right: (call) ; [2:6 - 74]
   function: (attribute) ; [2:6 - 72]
    object: (call) ; [2:6 - 66]
     function: (attribute) ; [2:6 - 64]
      object: (call) ; [2:6 - 58]
       function: (attribute) ; [2:6 - 56]
        object: (call) ; [2:6 - 50]
         function: (attribute) ; [2:6 - 37]
          object: (call) ; [2:6 - 29]
           function: (attribute) ; [2:6 - 14]
            object: (identifier) ; [2:6 - 6]
            attribute: (identifier) ; [2:8 - 14]
           arguments: (argument_list) ; [2:15 - 29]
            (string) ; [2:16 - 22]
             (string_start) ; [2:16 - 16]
             (string_content) ; [2:17 - 21]
             (string_end) ; [2:22 - 22]
            (string) ; [2:25 - 28]
             (string_start) ; [2:25 - 25]
             (string_content) ; [2:26 - 27]
             (string_end) ; [2:28 - 28]
          attribute: (identifier) ; [2:31 - 37]
         arguments: (argument_list) ; [2:38 - 50]
          (string) ; [2:39 - 43]
           (string_start) ; [2:39 - 39]
           (string_content) ; [2:40 - 42]
           (string_end) ; [2:43 - 43]
          (string) ; [2:46 - 49]
           (string_start) ; [2:46 - 46]
           (string_content) ; [2:47 - 48]
           (string_end) ; [2:49 - 49]
        attribute: (identifier) ; [2:52 - 56]
       arguments: (argument_list) ; [2:57 - 58]
      attribute: (identifier) ; [2:60 - 64]
     arguments: (argument_list) ; [2:65 - 66]
    attribute: (identifier) ; [2:68 - 72]
   arguments: (argument_list) ; [2:73 - 74]