Skip to content

Avoid over-selecting when include_surrounding_whitespace - #902

Open
yjyao wants to merge 1 commit into
nvim-treesitter:mainfrom
yjyao:main
Open

Avoid over-selecting when include_surrounding_whitespace#902
yjyao wants to merge 1 commit into
nvim-treesitter:mainfrom
yjyao:main

Conversation

@yjyao

@yjyao yjyao commented Sep 3, 2026

Copy link
Copy Markdown

Fixes #575
Fixes #850

The fix

When expanding to include surrounding whitespaces,

  • Under the "charwise" selection_mode, only include whitespaces from the current line;
  • Under the "linewise" selection_mode, include surrounding blank lines, but not non-empty lines with leading/trailing whitespaces.

Important

I'm not sure what's the intended usecase for blockwise selection when it comes to selecting textobjects, and how include_surrounding_whitespace is designed to work in that mode. This change currently DOES NOT handle blockwise mode correctly. PLEASE let me know what's the best way to handle it.

Tests

The following examples come from issues #575 and #850:


     1	def a():
     2	  x = 1
     3	
     4	  def b():
     5	
     6	    pass
     7	
     8	  pass

5Gvaf (af for linewise @function.outer)
selects lines [4,7],
correctly omitting line 8 which starts with leading whitespaces.


     1	def a():
     2	  pass␣␣
     3	
     4	def c():
     5	  pass

5Gvaf (af for linewise @function.outer)
selects lines [3,5],
correctly omitting line 2 which ends with trailing whitespaces.


     1	arr = [
     2	  x,  # comment␣␣
     3	  y,
     4	]

5Gvac (ac for charwise @comment.outer)
selects only # comment ,
correctly omitting the leading whitespaces on line 3.

…e=true`.

Fixes nvim-treesitter#575
Fixes nvim-treesitter#850

The following examples come from issues nvim-treesitter#575 and nvim-treesitter#850:

- nvim-treesitter#575 (comment)
- nvim-treesitter#850 (comment)

---

```python
     1	def a():
     2	  x = 1
     3	
     4	  def b():
     5	
     6	    pass
     7	
     8	  pass
```

`5Gvaf` (`af` for linewise `@function.outer`)
selects lines [4,7],
correctly omitting line 8 which starts with leading whitespaces.

---

```python
     1	def a():
     2	  pass␣␣
     3	
     4	def c():
     5	  pass
```

`5Gvaf` (`af` for linewise `@function.outer`)
selects lines [3,5],
correctly omitting line 2 which ends with trailing whitespaces.

---

```python
     1	arr = [
     2	  x,  # comment␣␣
     3	  y,
     4	]
```

`5Gvac` (`ac` for charwise `@comment.outer`)
selects only `# comment  `,
correctly omitting the leading whitespaces on line 3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FR: include_surrounding_whitespace should adjust whitespace characters included based on mode Delete @function.outer deletes more than it should

1 participant