@@ -651,35 +651,39 @@ automatically, browse to %s." user-code verification-uri))
651651
652652(defun copilot--get-source ()
653653 " Get source code from current buffer."
654- (let* ((p (point ))
655- (pmax (point-max ))
656- (pmin (point-min ))
657- (half-window (/ copilot-max-char 2 )))
658- (when (and (>= copilot-max-char 0 )
659- (> pmax copilot-max-char))
660- (let ((msg (format " %s size exceeds 'copilot-max-char' (%s ), copilot completions may not work"
661- (current-buffer ) copilot-max-char)))
662- (if copilot-max-char-warning-disable
663- (message msg)
664- (display-warning '(copilot copilot-exceeds-max-char) msg))))
665- (cond
666- ; ; using whole buffer
667- ((or (< copilot-max-char 0 ) (< pmax copilot-max-char))
668- (setq-local copilot--line-bias 1 )
669- (buffer-substring-no-properties pmin pmax))
670- ; ; truncate buffer head
671- ((< (- pmax p) half-window)
672- (setq-local copilot--line-bias (line-number-at-pos (- pmax copilot-max-char)))
673- (buffer-substring-no-properties (- pmax copilot-max-char) pmax))
674- ; ; truncate buffer tail
675- ((< (- p pmin) half-window)
676- (setq-local copilot--line-bias 1 )
677- (buffer-substring-no-properties pmin (+ pmin copilot-max-char)))
678- ; ; truncate head and tail
679- (t
680- (setq-local copilot--line-bias (line-number-at-pos (- p half-window)))
681- (buffer-substring-no-properties (- p half-window)
682- (+ p half-window))))))
654+ (save-restriction
655+ (widen )
656+ (let* ((p (point ))
657+ (pmax (point-max ))
658+ (pmin (point-min ))
659+ (half-window (/ copilot-max-char 2 )))
660+ (when (and (>= copilot-max-char 0 )
661+ (> pmax copilot-max-char))
662+ (let ((msg (format " %s size exceeds 'copilot-max-char' (%s ), copilot completions may not work"
663+ (current-buffer ) copilot-max-char)))
664+ (if copilot-max-char-warning-disable
665+ (message msg)
666+ (display-warning '(copilot copilot-exceeds-max-char) msg))))
667+ (cond
668+ ; ; using whole buffer
669+ ((or (< copilot-max-char 0 ) (< pmax copilot-max-char))
670+ (setq-local copilot--line-bias 1 )
671+ (buffer-substring-no-properties pmin pmax))
672+ ; ; truncate buffer head
673+ ((< (- pmax p) half-window)
674+ (let ((start (max pmin (- pmax copilot-max-char))))
675+ (setq-local copilot--line-bias (line-number-at-pos start))
676+ (buffer-substring-no-properties start pmax)))
677+ ; ; truncate buffer tail
678+ ((< (- p pmin) half-window)
679+ (setq-local copilot--line-bias 1 )
680+ (buffer-substring-no-properties pmin (min pmax (+ pmin copilot-max-char))))
681+ ; ; truncate head and tail
682+ (t
683+ (let ((start (max pmin (- p half-window)))
684+ (end (min pmax (+ p half-window))))
685+ (setq-local copilot--line-bias (line-number-at-pos start))
686+ (buffer-substring-no-properties start end)))))))
683687
684688(defun copilot--get-minor-mode-language-id ()
685689 " Get language ID from minor mode if available."
0 commit comments