(defun gptel--rewrite-directive-buffer-aware ()
"Directive for rewriting or refactoring that includes the current
buffer as context.
These are instructions not specific to any particular required
change.
The returned list (system message followed by canned exchanges)
is interpreted as the directive for the rewrite request. To use
your own, add a different directive to `gptel-directives', which
see."
(let* ((lang (downcase (gptel--strip-mode-suffix major-mode)))
(article (if (and lang (not (string-empty-p lang))
(memq (aref lang 0) '(?a ?e ?i ?o ?u)))
"an" "a")))
(list
(format (concat "You are %s %s programmer. "
"Follow my instructions and refactor %s code I provide.\n"
"- Generate ONLY %s code as output, without "
"any explanation or markdown code fences.\n"
"- Generate code in full, do not abbreviate or omit code.\n"
"- Do not ask for further clarification, and make "
"any assumptions you need to follow instructions.\n\n"
"For context, I will first provide you with code as it appears AFTER and BEFORE the text to be rewritten."
" Do not repeat any of the AFTER or BEFORE code, only rewrite the text in the region")
article lang lang lang)
nil "What is the code AFTER the cursor?"
(format "AFTER\n```\n%s\n```\n"
(buffer-substring-no-properties
(if (use-region-p) (max (point) (region-end)) (point))
(point-max)))
"And what is the code BEFORE the cursor?"
(format "BEFORE\n```%s\n%s\n```\n" lang
(buffer-substring-no-properties
(point-min)
(if (use-region-p) (min (point) (region-beginning)) (point))))
"What is the code to be refactored?")))
(add-to-list 'gptel-directives
`(rewrite-buffer-aware . ,#'gptel--rewrite-directive-buffer-aware))