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

Compatibility with norg? #16

Closed
jaidetree opened this issue Sep 19, 2022 · 14 comments · Fixed by #17
Closed

Compatibility with norg? #16

jaidetree opened this issue Sep 19, 2022 · 14 comments · Fixed by #17

Comments

@jaidetree
Copy link

Trying to use this with https://github.com/nvim-neorg/neorg

The good news is using :FeMaco does work and displays the float with the code, and I think it's highlighted correctly.

The bad news if I invoke some form of :wq after adding some lines, it replaces the @end tag with the new line.

2022-09-19 19 22 11

@AckslD
Copy link
Owner

AckslD commented Sep 20, 2022

Thanks for reporting! I'm not able to reproduce this, are you on latest FeMaco, nvim-treesitter and neorg?

If so could you send me what the output of:

lua =vim.treesitter.get_node_text(require('nvim-treesitter.query').get_matches(0, 'injections')[1].content.node, 0)

and

lua =vim.treesitter.get_node_range(require('nvim-treesitter.query').get_matches(0, 'injections')[1].content.node)

are. Assuming that this is the first injection in your file. If not change the 1 to what's needed to match the code-block your on.

@jaidetree
Copy link
Author

Will get those values, but for clarity, what is the intended way to accept what's in the float and send it back to the main file?

I was using :wq, is that correct?

@AckslD
Copy link
Owner

AckslD commented Sep 20, 2022

Yes, all of :q, :wq and w should work (where the last just updates without closing).

@jaidetree
Copy link
Author

Tried running that command, but got an error. I run this after editing the first code block in a norg document with FeMaco?

E5108: Error executing lua [string ":lua"]:1: attempt to index field 'content' (a nil value)
stack traceback:
	[string ":lua"]:1: in main chunk

@AckslD
Copy link
Owner

AckslD commented Sep 21, 2022

Thanks for trying, I suspect the index is not correct. Would you mind running the following instead to print all the injections:

local bufnr = 1
for _, match in ipairs(require('nvim-treesitter.query').get_matches(bufnr, 'injections')) do
  for capture_name, capture in pairs(match) do
    local node = capture.node
    print(capture_name)
    print('  text: ', vim.treesitter.get_node_text(node, bufnr))
    print('  range: ', vim.treesitter.get_node_range(node))
  end
  print()
end

adjusting the bufnr to the neorg buffer. For example you can but this in a file and source it with :source %.

@jaidetree
Copy link
Author

jaidetree commented Sep 21, 2022

Set the bufnr to 0 for current buffer

norg_meta
  text:  title: Neo Vim Configuration
^Idescription:
^Iauthor: eccentric-J
^Icreated: 2022-09-16
^Itangle: {
^I languages: {
^I   fennel: ./fnl/config/core.fnl
^I }
^I scope: tagged
^I}
  range:  1 1 11 0
_tagname
  text:  document.meta
  range:  0 1 0 14
language
  text:  vim
  range:  79 10 79 13
_tagname
  text:  code
  range:  79 5 79 9
content
  text:  " Execute the command and get its output
    let cmd = a:cmd
    redir => output
    silent exe cmd
    redir END
  range:  80 4 85 0
language
  text:  fennel
  range:  124 9 124 15
_tagname
  text:  code
  range:  124 4 124 8
content
  text:  [{:name "null-ls" :root "/Users/j/projects/crunchy-price-calc"}]
  range:  125 3 126 0
language
  text:  shell
  range:  208 8 208 13
_tagname
  text:  code
  range:  208 3 208 7
content
  text:  make
  range:  209 2 210 0
language
  text:  fennel
  range:  214 8 214 14
_tagname
  text:  code
  range:  214 3 214 7
content
  text:  ;; DO NOT EDIT THIS FILE
  ;; Generated from ../neovim.norg
  range:  215 2 217 0
language
  text:  fennel
  range:  220 8 220 14
_tagname
  text:  code
  range:  220 3 220 7
content
  text:  (local configdir (vim.fn.stdpath "config"))
  range:  221 2 222 0
language
  text:  fennel
  range:  229 8 229 14
_tagname
  text:  code
  range:  229 3 229 7
content
  text:  (let [rtp (vim.api.nvim_get_option "runtimepath")
        custompaths [(.. configdir "/fnl")
                     (.. configdir "/lua")]
        customrtp (table.concat custompaths ",")]
    (vim.api.nvim_set_option "runtimepath" (.. customrtp "," rtp)))
  range:  230 2 235 0
language
  text:  fennel
  range:  242 8 242 14
_tagname
  text:  code
  range:  242 3 242 7
content
  text:  (global fennel (require :config.fennel))
  range:  243 2 244 0
language
  text:  fennel
  range:  250 8 250 14
_tagname
  text:  code
  range:  250 3 250 7
content
  text:  (let [fnldir (.. configdir "/fnl")]
    (each [_ dir (ipairs ["/?.fnl" "/?/init.fnl"])]
      (tset fennel :path (.. fnldir dir ";" fennel.path))))
  range:  251 2 254 0
language
  text:  fennel
  range:  261 8 261 14
_tagname
  text:  code
  range:  261 3 261 7
content
  text:  (table.insert package.loaders 1 fennel.searcher)
  range:  262 2 263 0
language
  text:  fennel
  range:  268 8 268 14
_tagname
  text:  code
  range:  268 3 268 7
content
  text:  (local cfg (require :config.core))
  range:  269 2 270 0
language
  text:  fennel
  range:  275 8 275 14
_tagname
  text:  code
  range:  275 3 275 7
content
  text:  cfg
  range:  276 2 277 0

@AckslD
Copy link
Owner

AckslD commented Sep 21, 2022

Oh hold, I see now, the @end is not replaced, it's at the end of the last line of the code block. Which makes sense you removed the last newline of the floating buffer. Since we also support inline injections we can't assume that there should be a newline at the end of the content, so you need to keep it there. Does that make sense?

You can try again but just end with a newline.

@jaidetree
Copy link
Author

jaidetree commented Sep 21, 2022

I think I follow, I opened the femaco UI, kept the newline at the end, and tried to insert text above it but it still joined the the @end to the end of the newly entered line

2022-09-21 10 01 55

@jaidetree
Copy link
Author

I've tested with a few other languages and it does seem to work a bit better. For some reason not working with fennel.

Does the same happen to you with fennel or is it unique to my config?

@AckslD
Copy link
Owner

AckslD commented Sep 21, 2022

Could it be that you have some formatter which removes newlines at the end of a file upon save for fennel? It does not happen for me but I have no setup for fennel.

@jaidetree
Copy link
Author

That's what I was thinking too. Will try disabling it in those buffers.

@jaidetree
Copy link
Author

Created a toggle system for the autoformatting and disabled it in the FeMaco buffer. With autoformatting disabled, it was working so it must have been stripping out the whitespace on pre-save.

That said, there is another issue I've noticed where the indentation is off after saving in neorg:

image

@AckslD
Copy link
Owner

AckslD commented Sep 22, 2022

If it is useful for you, maybe we can add an optional setting to always include the newline when updating the buffer. Then you perhaps wouldn't have to disable formatting (which is a pity).

What's the new issue actually?

@AckslD
Copy link
Owner

AckslD commented Sep 22, 2022

@eccentric-j I opened #17 to optionally be able to ensure newlines. So for example you can configure this as:

require('femaco').setup({
  ensure_newline = function(base_filetype)
    return base_filetype == 'norg'
  end,
})

and then you don't have to disable the formatting.

Let me know if that works for you.

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 a pull request may close this issue.

2 participants