Skip to content

feat: Add support for user-defined comment directives#566

Open
Otard95 wants to merge 6 commits intorest-nvim:mainfrom
Otard95:feat/custom-directives
Open

feat: Add support for user-defined comment directives#566
Otard95 wants to merge 6 commits intorest-nvim:mainfrom
Otard95:feat/custom-directives

Conversation

@Otard95
Copy link

@Otard95 Otard95 commented Mar 4, 2026

Adds a custom_directives config option that lets users register handlers for # @directive comments.

This would be a table of keyed functions where the key is the directive and the function gets called with the space separated arguments that follow.

For example, given:

custom_directives = { var = function(ctx, name, value) ctx:set_local(name, value) end }

# @var foo bar would be equivalent to @foo = bar.

Personally I'd find this very useful for securely getting values out of my password store (example below). But I see this being quite useful in general. And as you can see the implementation is really quite simple.
Of course, you could technically achieve something similar with a post-request script. This feature is more so to handle the case where you want to do mostly the same action many times or in many places, at that point copy-pasting the script around becomes very verbose and not to mention hard to maintain.

Password store example:

vim.g.rest_nvim = {
  custom_directives = {
    pass = function(ctx, variable, pass_name)
      if not (variable and pass_name) then
        print('Directive @pass expects exactly 2 arguments')
        return
      end

      local cmd = { 'pass', 'show', pass_name }
      local result = vim.system(cmd, { text = true }):wait()

      if result.code ~= 0 then
        print('Failed to get password \'' .. pass_name .. '\': ' ..
          vim.trim(result.stdout) .. vim.trim(result.stderr))
        return
      end

      ctx:set_local(variable, vim.trim(result.stdout))
    end
  }
}

Then:

# @pass token secret/api/key
GET https://api.example.com
Authorization: Bearer {{token}}

@Otard95
Copy link
Author

Otard95 commented Mar 6, 2026

I suppose one consideration should be: Should we reimplement the @prompt directive as a default to this option?
This would allow users to override it, which may or may not be desirable.

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 this pull request may close these issues.

1 participant