kevinhwang91 / nvim-ufo

Not UFO in the sky, but an ultra fold in Neovim.
BSD 3-Clause "New" or "Revised" License
2.18k stars 38 forks source link

Is there a way to fold multiple lines string of lua? #126

Closed scottming closed 1 year ago

scottming commented 1 year ago

Feature description

I want to fold the string like this(starts with [[):

local local_call_query_scm = [[
    ;; query
    (call
      target: (identifier) @ignore
      (#not-any-of? @ignore 
      ;; def
      "def"
      "defp"
      "defdelegate"
      "defguard"
      "defguardp"
      "defmacro"
      "defmacrop"
      "defn"
      "defnp"
      "defmodule"
      "defprotocol"
      "defimpl"
      "defstruct"
      "defexception"
      "defoverridable"
      ;; keywords
      "alias"
      "case"
      "cond"
      "else"
      "for"
      "if"
      "import"
      "quote"
      "raise"
      "receive"
      "require"
      "reraise"
      "super"
      "throw"
      "try"
      "unless"
      "unquote"
      "unquote_splicing"
      "use"
      "with"
      ;; test
      "doctest"
      "test"
      "describe"
      "assert"
      "setup"
    ]]

Describe the solution you'd like

local local_call_query_scm = [[...]]

Additional context

This is my ufo setup

ufo.setup({
    provider_selector = function(bufnr, filetype, buftype)
        return { "treesitter", "indent" }
    end,
})

I've checked https://github.com/nvim-treesitter/nvim-treesitter/issues/3326

but I don't know how to write a query to determine there's a multiple lines string quoted in [[]].

rockyzhang24 commented 1 year ago

You could write your own provider, a function returning the folding range based on your own requirement.

Here is a collection of examples. Also, you may get inspired by checking other issues where other users discussed and shared their ideas regarding how to customize a provider (e.g., https://github.com/kevinhwang91/nvim-ufo/issues/125)

kevinhwang91 commented 1 year ago
diff --git a/queries/lua/folds.scm b/queries/lua/folds.scm
index d8f0b42d..934bd9a8 100644
--- a/queries/lua/folds.scm
+++ b/queries/lua/folds.scm
@@ -8,3 +8,5 @@
  (function_definition)
  (table_constructor)
 ] @fold
+
+(expression_list value: (string) @fold)

h vim.treesitter.query.set