Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ai text object #2

Open
mawkler opened this issue Jul 12, 2022 · 4 comments
Open

Add ai text object #2

mawkler opened this issue Jul 12, 2022 · 4 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@mawkler
Copy link

mawkler commented Jul 12, 2022

Thanks for making a great plugin! I just switched to this one from vim-indent-object, and I noticed that this plugin doesn't support the ai text-object like vim-indent-object does. It is very useful when in visual mode and you've selected an indentation with ii, because then you can keep hitting ai to select the next indentation level. The mnemonic for ai is "around indendation", just like how (at least to me) ii reads "inside indendation".

For example, pressing vii with the cursor on the line with the comment "cursor here", and then pressing ai expands your selection to the next indendation level:

table.insert(active_right, {
  provider = 'filetype' -- 3. Pressing `ai` a second time selects this indendation level
  left_sep = left_sep,
  right_sep = right_sep,
  hl = { bg = 'line_bg' },
  enabled = has_file_type,
  icon = function()
    return {            -- 2. Pressing `ai` selects this indendation level
      str = get_icon(), -- 1. Put cursor here and press `vii`
      always_visible = true,
    }
  end,
  truncate_hide = true,
  priority = 1
})
@arsham arsham added the good first issue Good for newcomers label Jul 12, 2022
@arsham
Copy link
Owner

arsham commented Jul 12, 2022

Thank you for the kind words, and using my plugin!

You are raising two valid points here. Let's have a discussion to understands what needs to be done.

Regarding ai

You are correct, this needs to be implemented. The reason I didn't get to implement this was because of problems handling this. Imaging you have the following contents (cursor is represented by |):

11
  aa
    bb|
    bb
  cc
22

The cii produces:

11
  aa
  |cc
22

Which is logical, and when the NL at the end of the second indented line is removed, cc will be in front of the caret.
Now the question is, how does it look like when the around area of the indentation is removed? In vim's language, it should be the text object plus the white spaces. (actually, leading or trailing white spaces).

Considering there is another indentation text-object (11-22) is after, you are supposed to be removing any white space after the cursor. The other option is to remove the before one and break the rule:

11
  aa|cc
22

I am not arguing that this is bad, I just want to understand what is the right way of dealing with this.

Regarding expansion motion

That's a great idea, and is doable. I have a couple of questions though:

  1. Is it only valid if the visual selection was initiated by this plugin?
  2. What if the user changes the visually selected area and put both ends in different levels, to which direction should it grow?

@mawkler
Copy link
Author

mawkler commented Jul 13, 2022

First of all I realize now that your plugin operates in visual mode and not in visual-line mode. I think it makes more sense to operate in visual-line mode since each indent is per line. I think in your example

11
  aa
    bb|
    bb
  cc
22

it makes more sense to me if doing cii yields this:

11
  aa
    |
  cc
22

i.e. that the indentation level is preserved. My guess is that switching to visual-line mode will give this behaviour more or less automagically.

Most of my thoughts here come from using the plugin vim-indent-object. I think it is very intuitive to use, perhaps give it a try to get a feel for what feels intuitive.

I realize also that the behaviour of ai isn't quite what I described earlier. In vim-indent-object it is ii that expands to the "next" indendation level, but only if you've already selected the entire current indendation level. ai targets the current indendation level, and the line above. I'm not sure what the use case is for this. Perhaps it would make more sense for ai to select the current indendation level and any ledaing or trailing new lines (up to but not including the next indendation level). Here < indicates the lines that get selected:

11

  aa
                     <
                     <
    bb|      < ii    < ai
    bb       <       <
             <       <
    bb       <       <
                     <
  cc
22
  1. Is it only valid if the visual selection was initiated by this plugin?

Either that, or ii could check if every correct line is already selected (i.e. if the selection wouldn't have changed on ii), and in that case expands to the next indendation level.

  1. What if the user changes the visually selected area and put both ends in different levels, to which direction should it grow?

vim-indent-object seems to select based on the "lowest" (leftmost) indendation level that is selected, but it would also make sense to me to select only based on the indendation level of the line that the cursor is on (i.e. as if the selection was made from normal mode).

@arsham
Copy link
Owner

arsham commented Jul 15, 2022

Hi, sorry for the delay. You are making good points, let me play around with your ideas and will get back to you.

arsham added a commit that referenced this issue Jul 15, 2022
This commit fixes the cii when it is not correctly putting the cursor in
the right place after the change. Thanks to @melkster for the idea.

Ref #2
arsham added a commit that referenced this issue Jul 15, 2022
This commit expects the opts table to provide a table for the ai and ii
text object settings. This will break if you have a previous version,
which used to accept a string.

Ref #2
@arsham
Copy link
Owner

arsham commented Jul 15, 2022

I have implemented the ai and fixed the visual-line problem.

Let me think about the ii and ai after the visual selection idea you have, which makes sense by the way, and will get back to you. In the meanwhile, if you have any more input please feel free to comment here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants