aheber / tree-sitter-sfapex

Tree-sitter implementation for Salesforce's Apex, SOQL, and SOSL
MIT License
70 stars 14 forks source link

[Question] Folds #23

Open FedeAbella opened 9 months ago

FedeAbella commented 9 months ago

I've just set up nvim so that treesitter takes care of folds:

vim.opt.foldmethod = "expr"
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"

This works fine on other languages (i.e.: lua), but on apex it will just fold the entire file, not caring for classes, methods, or anything. Is this something that's missing from the apex parser?

Thanks!

aheber commented 9 months ago

@FedeAbella I don't use NVIM so unfortunately I can't help much here. I think it is powered by the queries defined here: https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/apex/folds.scm

You could help improve that set of queries to get the folds working as expected.

@finxxi is this something you have any insight on?

xixiaofinland commented 9 months ago

not right off the bat. I don't like folding at all, I totally disabled it in Nvim. It seems Nvim will default to fold everything like mentioned here? https://www.jmaguire.tech/posts/treesitter_folding/ If we unfold one level, does the file have correct folding as expected?

Up to OP to figure out, or I might wanna study and tweak it one day....

aheber commented 9 months ago

You could look at the ECMAscript (Javascript) fold definitions here, it looks like as long as you know the node names you could improve this. https://github.com/nvim-treesitter/nvim-treesitter/blob/master/queries/ecma/folds.scm

I'm happy to help explore the correct node names/types if you set some expectations of what should be foldable? @FedeAbella

FedeAbella commented 9 months ago

@aheber I'm new to nvim, and don't know treesitter enough to understand queries yet (had the same issue with a bracket colorizer plugin where I didn't really know how to help them), but I can tell you I'd mostly expect the following to be folded:

I'd say those would cover most use cases. I think some people would argue for being able to fold queries, but I'd say if you need to fold your queries in Apex, you're probably doing something really wrong (or really corner case), and it doesn't merit the effort.

As an aside, after commenting that I've been trying out nvim-ufo, which uses nvim's lsp to define folds, and have found it works alright, so if this is too much work, I wouldn't push for it. However, if you plan on tackling it, I'd be happy to help in anything I can.

@xixiaofinland I'm tackling trying out your sf.nvim plugin next :smile: I'm working towards building an environment I'm comfortable enough leaving VSCode for

xixiaofinland commented 9 months ago

@aheber I'm new to nvim, and don't know treesitter enough to understand queries yet (had the same issue with a bracket colorizer plugin where I didn't really know how to help them), but I can tell you I'd mostly expect the following to be folded:

  • Class body
  • Method body
  • if ... else if ... else statements
  • for statements
  • while statements
  • do ... while statements
  • try ... catch statements

I'd say those would cover most use cases. I think some people would argue for being able to fold queries, but I'd say if you need to fold your queries in Apex, you're probably doing something really wrong (or really corner case), and it doesn't merit the effort.

As an aside, after commenting that I've been trying out nvim-ufo, which uses nvim's lsp to define folds, and have found it works alright, so if this is too much work, I wouldn't push for it. However, if you plan on tackling it, I'd be happy to help in anything I can.

@xixiaofinland I'm tackling trying out your sf.nvim plugin next 😄 I'm working towards building an environment I'm comfortable enough leaving VSCode for

I often work with legacy Apex class with methods having large number of lines, so I also thought folding was a must. Then I realized that Nvim actually has a couple of plugins facilitating code navigation, such as jump to previous/next method/if-else/loop/var, jump one indentation up, telescope list all methods and jump to selected one. Now I really don't miss folding at all. But it's just my 5cents.

Welcome to give sf.nvim a try, it's in my daily use and actively maintained. Feel free to leave issues/comments there :)!

surajp commented 1 month ago

@FedeAbella you can add your own fold queries to your init.lua file. For example

vim.treesitter.query.set("apex","folds"," [ (constructor_declaration) (class_body) (block) (argument_list) (array_initializer) (annotation_argument_list) ] @fold")