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

fix!: wrap functions returns different node depending on cursor position #26

Merged
merged 3 commits into from
Sep 4, 2023

Conversation

armed
Copy link
Collaborator

@armed armed commented Aug 27, 2023

Fixes issue when cursor is not placed correctly in some cases.

Solution: return range instead of node.

  • all wrap functions now returns a wrapped range instead of a node (breaking)
  • cursor.place_cursor now accepts either range as a table or a TSNode (backwards compatible)

This PR fixes the issue when ts.get_node_at_cursor returns different nodes depending on where the cursor is located.

Here are two tests, first one fails (when the cursor at the beginning of a node), but the second test works as expected.

  it("ts check fails", function()
    prepare_buffer({
      content = { "(a (b))" },
      cursor = { 1, 3 },
    })

    paredit.wrap.wrap_element_under_cursor("( ", ")")

    local parser = vim.treesitter.get_parser(0, vim.bo.filetype)
    parser:parse()
    local node = ts.get_node_at_cursor()
    -- here the returned node is `( (b))` which is not expected
    assert.are.equal("(b)", vim.treesitter.get_node_text(node, 0))
  end)

  it("ts check success", function()
    prepare_buffer({
      content = { "(a (b))" },
      cursor = { 1, 5 },
    })

    paredit.wrap.wrap_element_under_cursor("( ", ")")

    local parser = vim.treesitter.get_parser(0, vim.bo.filetype)
    parser:parse()
    local node = ts.get_node_at_cursor()

    assert.are.equal("(b)", vim.treesitter.get_node_text(node, 0))
  end)

- all wrap functions now returns a wrapped range instead of a node
- cursor.place_cursor now accepts either range as a table or a TSNode
@armed armed requested a review from julienvincent August 27, 2023 16:52
@armed
Copy link
Collaborator Author

armed commented Aug 27, 2023

ts.node_at_cursor returns (b) in both this cases (a ( |(b))) and (a ( (b)|)) if buffer is initilally at this state, but fails if it happend after buffer modification by wrap function + parser:parse(), not sure why.

@armed armed merged commit 55cdf13 into master Sep 4, 2023
@armed armed deleted the fix/form-element-wraps-range branch September 4, 2023 09:02
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.

2 participants