Skip to content

Commit 1741c40

Browse files
committed
Add eca-chat-add-context
Fixes #52
1 parent bc90ec5 commit 1741c40

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ Not all, but most relevant functions and variables:
5151

5252
### Functions
5353

54-
- `eca-chat-add-context-at-point`: Add the current function or selected lines to chat as context.
55-
- `eca-chat-add-file-context`: Add the current file to chat as context.
54+
- `eca-chat-add-context`: Add file/dirs to context checking multiple modes and region.
5655
- `eca-chat-tool-call-accept-all`: Acceppt all pending tool calls in chat.
5756
- `eca-chat-tool-call-accept-next`: Acceppt next pending tool call in chat.
5857
- `eca-chat-tool-call-reject-next`: Reject next pending tool call in chat.

eca-chat.el

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,48 @@ Just open if FORCE-OPEN? is non-nil."
20172017
(when-let ((ov (eca-chat--expandable-content-at-point)))
20182018
(eca-chat--expandable-content-toggle (overlay-get ov 'eca-chat--expandable-content-id) (when force-open? t) (not force-open?)))))
20192019

2020+
(declare-function dired-get-marked-files "dired")
2021+
(declare-function treemacs-node-at-point "treemacs")
2022+
(declare-function treemacs-button-get "treemacs")
2023+
2024+
;;;###autoload
2025+
(defun eca-chat-add-context ()
2026+
"Add context to chat in a DWIM manner.
2027+
2028+
- If a region selected, at file with lines range selected.
2029+
- If in Dired, add the marked files/dirs or current file/dir at point.
2030+
- If in Treemacs, add selected file/dir.
2031+
- Else add current file."
2032+
(interactive)
2033+
(eca-assert-session-running (eca-session))
2034+
(let* ((contexts (cond
2035+
((and (buffer-file-name)
2036+
(use-region-p))
2037+
(-let (((start . end) `(,(line-number-at-pos (region-beginning)) . ,(line-number-at-pos (region-end)))))
2038+
(list
2039+
(list :type "file"
2040+
:path (buffer-file-name)
2041+
:linesRange (list :start start :end end)))))
2042+
2043+
((derived-mode-p 'dired-mode)
2044+
(--map (list :type (if (f-dir? it) "directory" "file")
2045+
:path it)
2046+
(dired-get-marked-files)))
2047+
2048+
((derived-mode-p 'treemacs-mode)
2049+
(when-let (path (-some-> (treemacs-node-at-point)
2050+
(treemacs-button-get :path)))
2051+
(list
2052+
(list :type (if (f-dir? path) "directory" "file")
2053+
:path path))))
2054+
2055+
((buffer-file-name)
2056+
(list
2057+
(list :type "file" :path (buffer-file-name)))))))
2058+
(eca-chat--with-current-buffer (eca-chat--get-last-buffer (eca-session))
2059+
(seq-doseq (context contexts)
2060+
(eca-chat--add-context context)))))
2061+
20202062
;;;###autoload
20212063
(defun eca-chat-add-context-at-point ()
20222064
"Add file content with range at point to chat as context.
@@ -2029,9 +2071,9 @@ Consider the defun at point unless a region is selected."
20292071
`(,(line-number-at-pos s) . ,(line-number-at-pos e)))))
20302072
(path (buffer-file-name)))
20312073
(eca-chat--with-current-buffer (eca-chat--get-last-buffer (eca-session))
2032-
(eca-chat--add-context (list :type "file"
2033-
:path path
2034-
:linesRange (list :start start :end end))))))
2074+
(eca-chat--add-context (list :type "file"
2075+
:path path
2076+
:linesRange (list :start start :end end))))))
20352077

20362078
;;;###autoload
20372079
(defun eca-chat-add-file-context (&optional arg)

0 commit comments

Comments
 (0)