kovisoft / paredit

Official mirror of Paredit versions released on vim.org
84 stars 23 forks source link

Support for JSON filetypes? #45

Open stevenproctor opened 1 year ago

stevenproctor commented 1 year ago

I was wondering if you would be open to having support for JSON?

I don't mind taking a shot at it and opening up a PR, but I wanted to see if there was any feedback you might have about supporting JSON filetypes for paredit.

kovisoft commented 1 year ago

Paredit was originally part of the slimv plugin, I have made it available as a separate plugin upon request, but its main purpose remains to support the lisp family of languages. Therefore I myself don't plan to extend it beyond supporting the lisp dialects.

Nevertheless I'm happy to accept any PR that adds functionality to paredit, so long as it does not break anything lisp related stuff.

stevenproctor commented 1 year ago

I have something that has been working for JSON.

One other question before I open the PR.

After becoming accustomed to paredit, I frequently want the slurp/barf/etc. capability in other languages (HCL for Terraform, Ruby, JavaScript).

In adding the JSON support for the additional brackets, is there any reason for that not to be the default behavior?

Would it also break things if supported in the Lisps that don't use the extended set?

If they don't use it, the cursory thought is it wouldn't hurt if they did support it since that brace/bracket isn't found in the code.

I wanted to run these ideas by you to see if anything resonates in these ideas about general paredit usefulness across any language if someone tried to enable it for other languages besides Lisp-based ones.

kovisoft commented 1 year ago

Some lisp dialects (e.g. clojure, scheme, racket) already use [] or {}, so paredit has support for these kind of brackets. The corresponding filetypes are defined here:

let s:fts_balancing_all_brackets = '.*\(clojure\|hy\|scheme\|racket\|shen\|lfe\|fennel\|janet\).*'

I'm not sure what else needed for JSON. If that is enough, then we can just add the json filetype to the above regex, and some initialization for the json filetype.

I'm afraid things are not so simple with languages that have some important stuff outside the brackets, e.g. languages of the C-family, including javascript. For example here the function names or some special keywords (like for/while/if) are outside the parens/curly braces. I don't think the paredit logic can be used here.

Nevertheless, if you want to enable paredit to filetype mydesiredfiletype, then you can do it by adding this line to your .vimrc file:

au FileType mydesiredfiletype call PareditInitBuffer()

But currently this will enable paredit only for the parentheses. Maybe we can add a new option for balancing all kind of brackets by default. We cannot make it the default for common lisp, because square brackets are reserved for user defined syntactic extensions.