Skip to content

Conversation

@neojski
Copy link

@neojski neojski commented Jan 6, 2026

Ocaml function textobjects updates.

I actually tested those when I wrote the change in meain/evil-textobj-tree-sitter#134 but I was kindly asked to upstream here.

@neojski neojski changed the base branch from master to main January 6, 2026 22:23
@kiyoon
Copy link
Collaborator

kiyoon commented Jan 7, 2026

Thanks for contributing.

Can you describe what it fixes and what the previous function capture was doing?

@neojski neojski force-pushed the ocaml-function-textobjects branch from 3c981dd to 9743310 Compare January 7, 2026 22:34
@neojski
Copy link
Author

neojski commented Jan 7, 2026

Added some inline comments to make it easier for future devs to understand it as well.

@neojski
Copy link
Author

neojski commented Jan 7, 2026

I would also appreciate some guidance on how to test it directly in this repo. README explains how to install it but it looks like it mostly focuses on the official package and not the locally developed one.

@neojski
Copy link
Author

neojski commented Jan 7, 2026

I managed to test it locally:

  1. I needed to install nvim-treesitter-parser from AUR
  2. ran nvim --clean -u scripts/minimal_init.lua test.ml

@neojski
Copy link
Author

neojski commented Jan 7, 2026

So, I think this doesn't actually cover some cases, e.g.

List.map ~f:(fun x -> x * x)

It won't match fun x -> x * x for inner function. I'll add those soon

@kiyoon
Copy link
Collaborator

kiyoon commented Jan 8, 2026

A good way to design a query is

  1. open :InspectTree
  2. press o to open query editor/viewer
image

@neojski neojski force-pushed the ocaml-function-textobjects branch from 9743310 to e3da70a Compare January 8, 2026 20:54
@neojski
Copy link
Author

neojski commented Jan 8, 2026

That's really useful, would you mind if I add that to https://github.com/nvim-treesitter/nvim-treesitter-textobjects?tab=contributing-ov-file?

I pushed the changes to handle the "standalone" fun & function now, so I think it's ready.

@neojski neojski changed the title Ocaml function textobjects Extend ocaml textobjects Jan 8, 2026
@neojski
Copy link
Author

neojski commented Jan 8, 2026

I also added struct & module support, which worked great locally, but those seem to be rejected 😢

I could go with using class for module, perhaps, and block for struct but that would be a breaking change for block. But I don't think parenthesized_expression is useful given that vi( just works.

@neojski
Copy link
Author

neojski commented Jan 8, 2026

I just did.

@neojski neojski force-pushed the ocaml-function-textobjects branch from 446915d to be417ac Compare January 8, 2026 21:58
@kiyoon
Copy link
Collaborator

kiyoon commented Jan 9, 2026

That's really useful, would you mind if I add that to https://github.com/nvim-treesitter/nvim-treesitter-textobjects?tab=contributing-ov-file?

Sure, please change the CONTRIBUTING.md file here.

I opened #856

@kiyoon
Copy link
Collaborator

kiyoon commented Jan 9, 2026

assignment.inner should also select lhs as a separate query

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends OCaml textobject support by adding patterns for more function definition styles and introducing assignment textobjects. The changes aim to provide better navigation and text selection capabilities for OCaml code in tree-sitter-based editors.

Key changes:

  • Added function textobject patterns for lambda expressions (fun and function syntax) in both standalone and parenthesized contexts
  • Added assignment textobjects with left-hand side, right-hand side, inner, and outer captures
  • Added module definition textobjects using the @class capture
  • Refined existing function textobjects to only match actual functions (with parameters)
  • Removed generic parenthesized expression block pattern in favor of built-in editor support

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
queries/ocaml/textobjects.scm Extended textobject queries with new patterns for functions, assignments, modules, and blocks
BUILTIN_TEXTOBJECTS.md Updated documentation to reflect newly supported assignment textobjects for OCaml

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@neojski
Copy link
Author

neojski commented Jan 9, 2026

All done, I think?

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