Generate a Comment in a Language Dynamically?

Working an org-tangle functionality for neovim orgmode. In a nutshell, it’s like tagging all the fenced code blocks in a markdown file to actual code files, then those code blocks get concatenated into their respective targets. For example, I’ve got a that tangles to, tmux.conf, and base.conf.

I just got to the point where it’s parsing header args from document directives, property drawers, and directly on code blocks. My implementation is based on nvim-personal-config/org.lua at cc2a12075eca8a72927f28703df5d0746c10ac07 · andreadev-it/nvim-personal-config · GitHub.

Now that it’s working, there’s one last feature I really want to include:

When tangling, I’d really like it to prepend each write to the file with a comment indicating the org file it was tangled from and the line where the code block starts.

Since most the heavy lifting was done by tree-sitter I got the filename, and the line number already. What I need some help/pointers/links/examples for are two problems:

  1. How would I generate a valid comment in a target language to write to files when tangling?
  2. Are there any built-ins for calculating relative paths from pwd to the abspath of a file? In node I’d use path.relative(cwd, absoluteDest).

Any hints or advice would be helpful!

1 Like

Decided to check out what commenting libraries do. Found Comment.nvim/ft.lua at 6821b3ae27a57f1f3cf8ed030e4a55d70d0c4e43 · numToStr/Comment.nvim · GitHub which just hardcodes the formats.

Is there a better way?

My go-to is the venerable vim-commentary by Tim Pope.