From 7ac3d91eb2a7e9454087f17e1fadba350a86a208 Mon Sep 17 00:00:00 2001 From: kobarity Date: Sat, 22 Oct 2022 21:36:15 +0900 Subject: [PATCH] Disable completion/ElDoc/FFAP when Python program is running * lisp/progmodes/python.el (python-completion-at-point) (python-ffap-module-path, python-eldoc--get-doc-at-point): Add check using `python-util-comint-end-of-output-p'. (python-util-comint-end-of-output-p): New function. * test/lisp/progmodes/python-tests.el (python-tests-shell-wait-for-prompt): Use `python-util-comint-end-of-output-p'. (python-completion-at-point-while-running-1) (python-ffap-module-path-1) (python-ffap-module-path-while-running-1) (python-eldoc--get-doc-at-point-1) (python-eldoc--get-doc-at-point-while-running-1): New tests. (Bug#58713) --- lisp/progmodes/python.el | 17 ++++++- test/lisp/progmodes/python-tests.el | 71 +++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 11195894234..cec0d54a447 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -4375,7 +4375,9 @@ For this to work as best as possible you should call `python-shell-send-buffer' from time to time so context in inferior Python process is updated properly." (let ((process (python-shell-get-process))) - (when process + (when (and process + (python-shell-with-shell-buffer + (python-util-comint-end-of-output-p))) (python-shell-completion-at-point process)))) (define-obsolete-function-alias @@ -4800,6 +4802,8 @@ def __FFAP_get_module_path(objstr): (defun python-ffap-module-path (module) "Function for `ffap-alist' to return path for MODULE." (when-let ((process (python-shell-get-process)) + (ready (python-shell-with-shell-buffer + (python-util-comint-end-of-output-p))) (module-file (python-shell-send-string-no-output (format "%s\nprint(__FFAP_get_module_path(%s))" @@ -4918,7 +4922,9 @@ If not FORCE-INPUT is passed then what `python-eldoc--get-symbol-at-point' returns will be used. If not FORCE-PROCESS is passed what `python-shell-get-process' returns is used." (let ((process (or force-process (python-shell-get-process)))) - (when process + (when (and process + (python-shell-with-shell-buffer + (python-util-comint-end-of-output-p))) (let* ((input (or force-input (python-eldoc--get-symbol-at-point))) (docstring @@ -5664,6 +5670,13 @@ This is for compatibility with Emacs < 24.4." comint-last-prompt) (t nil))) +(defun python-util-comint-end-of-output-p () + "Return non-nil if the last prompt matches input prompt." + (when-let ((prompt (python-util-comint-last-prompt))) + (python-shell-comint-end-of-output-p + (buffer-substring-no-properties + (car prompt) (cdr prompt))))) + (defun python-util-forward-comment (&optional direction) "Python mode specific version of `forward-comment'. Optional argument DIRECTION defines the direction to move to." diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index 81c9217c62c..8330525394c 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -46,10 +46,7 @@ always located at the beginning of buffer." (defun python-tests-shell-wait-for-prompt () "Wait for the prompt in the shell buffer." (python-shell-with-shell-buffer - (while (not (if-let ((prompt (python-util-comint-last-prompt))) - (python-shell-comint-end-of-output-p - (buffer-substring-no-properties - (car prompt) (cdr prompt))))) + (while (not (python-util-comint-end-of-output-p)) (sit-for 0.1)))) (defmacro python-tests-with-temp-buffer-with-shell (contents &rest body) @@ -4478,6 +4475,21 @@ print('Hello') (insert "u") (should-not (nth 2 (python-completion-at-point)))))) +(ert-deftest python-completion-at-point-while-running-1 () + "Should not try to complete when a program is running in the Shell buffer." + (skip-unless (executable-find python-tests-shell-interpreter)) + (python-tests-with-temp-buffer-with-shell + " +import time + +time.sleep(3) +" + (let ((inhibit-message t)) + (python-shell-send-buffer) + (goto-char (point-max)) + (insert "time.") + (should-not (with-timeout (1 t) (completion-at-point)))))) + (ert-deftest python-completion-at-point-native-1 () (skip-unless (executable-find python-tests-shell-interpreter)) (python-tests-with-temp-buffer-with-shell @@ -4552,6 +4564,31 @@ import abc ;;; FFAP +(ert-deftest python-ffap-module-path-1 () + (skip-unless (executable-find python-tests-shell-interpreter)) + (python-tests-with-temp-buffer-with-shell + " +import abc +" + (let ((inhibit-message t)) + (python-shell-send-buffer) + (python-tests-shell-wait-for-prompt) + (should (file-exists-p (python-ffap-module-path "abc")))))) + +(ert-deftest python-ffap-module-path-while-running-1 () + "Should not get module path when a program is running in the Shell buffer." + (skip-unless (executable-find python-tests-shell-interpreter)) + (python-tests-with-temp-buffer-with-shell + " +import abc +import time + +time.sleep(3) +" + (let ((inhibit-message t)) + (python-shell-send-buffer) + (should-not (with-timeout (1 t) (python-ffap-module-path "abc")))))) + ;;; Code check @@ -4615,6 +4652,32 @@ some_symbol some_other_symbol (should (string= (python-eldoc--get-symbol-at-point) "some_symbol")))) +(ert-deftest python-eldoc--get-doc-at-point-1 () + (skip-unless (executable-find python-tests-shell-interpreter)) + (python-tests-with-temp-buffer-with-shell + " +import time +" + (let ((inhibit-message t)) + (python-shell-send-buffer) + (python-tests-shell-wait-for-prompt) + (python-tests-look-at "time") + (should (python-eldoc--get-doc-at-point))))) + +(ert-deftest python-eldoc--get-doc-at-point-while-running-1 () + "Should not get documentation when a program is running in the Shell buffer." + (skip-unless (executable-find python-tests-shell-interpreter)) + (python-tests-with-temp-buffer-with-shell + " +import time + +time.sleep(3) +" + (let ((inhibit-message t)) + (python-shell-send-buffer) + (python-tests-look-at "time") + (should-not (with-timeout (1 t) (python-eldoc--get-doc-at-point)))))) + ;;; Imenu -- 2.39.5