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

janet.vim cannot properly indent this block of code. #15

Open
amano-kenji opened this issue Jan 20, 2024 · 12 comments
Open

janet.vim cannot properly indent this block of code. #15

amano-kenji opened this issue Jan 20, 2024 · 12 comments

Comments

@amano-kenji
Copy link

amano-kenji commented Jan 20, 2024

If I select this block visually and press ==,

(forever
  (let [conn (net/accept server)
             recv (msg/make-recv conn json/decode)]
    (ev/spawn
      (defer (:close conn)
             (def {"id" id "message" msg} (recv))
             (when (not (ev/full events))
               (ev/give events {:type "message" :id id :data msg}))))))

it doesn't become

(forever
  (let [conn (net/accept server)
        recv (msg/make-recv conn json/decode)]
    (ev/spawn
      (defer (:close conn)
        (def {"id" id "message" msg} (recv))
        (when (not (ev/full events))
          (ev/give events {:type "message" :id id :data msg}))))))

If I select this block visually and press ==,

(forever
  (let [conn (net/accept server)
        recv (msg/make-recv conn json/decode)]
    (ev/spawn
      (defer (:close conn)
        (def {"id" id "message" msg} (recv))
        (when (not (ev/full events))
          (ev/give events {:type "message" :id id :data msg}))))))

it becomes

(forever
  (let [conn (net/accept server)
             recv (msg/make-recv conn json/decode)]
    (ev/spawn
      (defer (:close conn)
             (def {"id" id "message" msg} (recv))
             (when (not (ev/full events))
               (ev/give events {:type "message" :id id :data msg}))))))
@sogaiu
Copy link

sogaiu commented Jan 20, 2024

Thanks for the details.

I tried both cases and got the same results.

@MaxGyver83
Copy link

Can anybody explain what's the intended behavior?

For example, =ip aligns the line after (defn with the function name:

(defn match-mailboxes [line]
      (def mailbox

Looking at boot.janet, the line after (defn is always indented with two spaces:

(defn defmacro :macro
  "Define a macro."
  [name & more]

boot.janet#L45-L47

I guess, the rule is to indent always by two spaces except after some certain functions: tuple, setdyn!?

What's the meaning of g:janet_fuzzy_indent_patterns? (indent/janet.vim#L31)

@amano-kenji
Copy link
Author

amano-kenji commented Feb 21, 2024

janet-ts-mode for emacs indents janet code properly.

I always want indentation by two space characters for macros..... like match and case and let.

For functions, no....

@sogaiu
Copy link

sogaiu commented Feb 21, 2024

janet_fuzzy_indent_patterns seems to have a fair bit of overlap with this part of spork/fmt.

There is a docstring right above that bit that has the text:

A list of forms that are control forms and should be indented two spaces.

May be that's a clue?

@amano-kenji
Copy link
Author

amano-kenji commented Feb 21, 2024

I think fuzzy indent is not applied when I press = on a block of selection. It kicks in only when I press enter in insert mode.

@MaxGyver83
Copy link

janet_fuzzy_indent_patterns seems to have a fair bit of overlap with this part of spork/fmt.

There is a docstring right above that bit that has the text:

A list of forms that are control forms and should be indented two spaces.

May be that's a clue?

I have tried to overwrite g:janet_fuzzy_indent_patterns in my .vimrc:

let g:janet_fuzzy_indent_patterns = ['^let', '^while', '^if', '^unless', '^with', '^fn$', '^var', '^each', '^case$', '^for$', '^loop$', '^match', '^seq', '^generate', '^edefer', '^cond', '^try', '^when', '^ev/spawn-thread$', '^ev/do-thread$', '^declare', '^forever$', '^compwhen$', '^compif$']

No matter if I include ^defn or not, the next line is always indented as described above.

@MaxGyver83
Copy link

janet_fuzzy_indent_patterns seems to have a fair bit of overlap with this part of spork/fmt.

Do you use fmt.janet? If yes, how do you use it?

@sogaiu
Copy link

sogaiu commented Feb 22, 2024

No, I don't use it via an editor.

Below are some related ideas though...

spork comes with the janet-format script which wraps spork/fmt, so in combination with some vim / neovim feature / plugin for using an external formatter, may be something appropriate can be achieved.

There is also an in-development LSP thing for Janet which apparently got formatting based on spork/fmt recently, so perhaps that's a possibility.

@MaxGyver83
Copy link

This sounds good! But I still want to understand how use the auto-indentation correctly.

@sogaiu
Copy link

sogaiu commented Feb 22, 2024

I'm sorry, I don't know.

May be someone else who does will come along.

Or perhaps asking at the Janet repository discussions or may be the Zulip instance might get better answers.


For using janet-format, I did find the following to work without any external plugins.

  1. Install spork via jpm so that janet-format is on your PATH
  2. In vim / neovim, :set equalprg=janet-format
  3. Open some .janet file. I put the following in a file and opened it for my test:
    (defn a
    [x]
    (+ x 1))
  4. Enter the sequence gg=G -- I guess gg is "goto the first line of the file" and =G is ={motion} (so indent through the end of the buffer may be?)

The content of my buffer then became:

(defn a
  [x]
  (+ x 1))

@MaxGyver83
Copy link

Awesome! I didn't know that it's so easy.

@sogaiu
Copy link

sogaiu commented Feb 22, 2024

Me too :)

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

No branches or pull requests

3 participants