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

Avoiding putting the cursor on different lines after textsubjects-smart? #22

Open
lf- opened this issue Jul 30, 2022 · 1 comment
Open

Comments

@lf-
Copy link
Contributor

lf- commented Jul 30, 2022

Hi! I love your plugin and I'm currently implementing a Haskell query for it so I can use it at work. I'll PR it upstream after I have a week or two to smoke test it at work. Here's my current progress: https://github.com/lf-/dotfiles/blob/main/configs/nvim/after/queries/haskell/textsubjects-smart.scm

A trouble that I have is that when I hit v., the plugin will put the cursor on the next or preceding line. This is particularly biting me because I'd like to do v.. to expand the selection to grab the function along with its signature, but v. moves the cursor outside of the syntax nodes where the range would apply.

I'm not quite sure whose bug this is, or if it is an issue with my query in some way. It doesn't seem to happen when there's not whitespace surrounding the lines in question.

Minimal textsubjects query to reproduce the issue:

([
  (signature)
  (function)
 ] @_start @_end
 (#make-range! "range" @_start @_end))

; signature + function
(
 ((signature) @_start
  .
  (function) @_end)
 (#make-range! "range" @_start @_end))

Sample haskell code that could show it:

module Test where
-- ↓↓↓ cursor goes on the "test :: Int" line, then press `v.` and it will go on the line above

test :: Int
test = 1

Relevant parts of syntax tree:

signature [256, 0] - [256, 11]
  name: variable [256, 0] - [256, 4]
  type: "::" [256, 5] - [256, 7]
  type: type_name [256, 8] - [256, 11]
    type [256, 8] - [256, 11]
function [257, 0] - [257, 8]
  name: variable [257, 0] - [257, 4]
  "=" [257, 5] - [257, 6]
  rhs: exp_literal [257, 7] - [257, 8]
    integer [257, 7] - [257, 8]
@RRethy
Copy link
Owner

RRethy commented Jul 30, 2022

The issue is with how nvim-treesitter-textsubjects selects whitespace that surrounds the object. The plugin tries to be as smart of possible with whitespace and how it selects it, it also tries to somewhat mimic Neovim's behaviour with its other textobjects. If we take a look at a basic text file:

foobar

bazbin
tictac

If we put our cursor on bazbin and do a vap, it'll highlight the last two lines along with the whitespace line above it since for most common uses cases that would be the correct behaviour. This is mimic'd in nvim-treesitter-textsubjects and the behaviour you're seeing (see relevant code here ).

The placement of the cursor doesn't actually matter for your description (although fwiw we break from Neovim and put the cursor at the top of the selection not the bottom).

For now, I'd say maybe just put the function and signature together to get selected with v.. I'll leave this issue open though because I have a general idea on a non-invasive solution but it's going to take a bit of work but I'm busy rn. Essentially we should be able to modify this does_surround check to use prev_selections instead of the visual selection if the user is in the process of visually selecting text. I'll need to think about this a bit more though to consider any hidden ramifications.

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

No branches or pull requests

2 participants