From f8caae895c55227800c2b24fb4fd13344f44351d Mon Sep 17 00:00:00 2001 From: Justin Barclay Date: Sat, 13 Apr 2024 23:38:29 -0700 Subject: [PATCH 1/4] Fix header --- test/user-submitted-cases.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/user-submitted-cases.el b/test/user-submitted-cases.el index 7e405eb..b05ed26 100644 --- a/test/user-submitted-cases.el +++ b/test/user-submitted-cases.el @@ -1,4 +1,4 @@ -;;; indent-parinfer-tests.el --- Auto generates tests based on a json file -*- lexical-binding: nil; -*- +;;; user-submitted-cases.el --- User submitted test cases -*- lexical-binding: nil; -*- ;; Copyright (C) 2019 Justin Barclay ;; Author: Justin Barclay From 5b95a1fe339e66b2ced14dad0da09ff3df73d034 Mon Sep 17 00:00:00 2001 From: Justin Barclay Date: Sat, 13 Apr 2024 23:39:20 -0700 Subject: [PATCH 2/4] Fix potential recursive parinfer-rust-mode call --- parinfer-rust-helper.el | 2 +- parinfer-rust-mode.el | 60 ++++++++++++++++++++++------------------- test/test-helper.el | 3 +-- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/parinfer-rust-helper.el b/parinfer-rust-helper.el index 711b8f1..8db5f9b 100644 --- a/parinfer-rust-helper.el +++ b/parinfer-rust-helper.el @@ -240,7 +240,7 @@ mode to better emulate users." (when (eq (current-buffer) (window-buffer (selected-window))) (remove-hook 'window-selection-change-functions #'parinfer-rust--defer-loading t) - (parinfer-rust-mode))) + (parinfer-rust-mode-enable))) ;; Disable fill column warning only for this buffer to enable long strings of text without ;; having to do a weird mapconcat. ;; Local Variables: diff --git a/parinfer-rust-mode.el b/parinfer-rust-mode.el index ea107a7..ee5db74 100644 --- a/parinfer-rust-mode.el +++ b/parinfer-rust-mode.el @@ -503,7 +503,7 @@ Checks if MODE is a valid Parinfer mode, and uses (parinfer-rust--set-default-state) (parinfer-rust--dim-parens)) -(defun parinfer-rust-mode-enable () +(defun parinfer-rust-mode-setup () "Enable Parinfer." (setq-local parinfer-rust-enabled t) (parinfer-rust--detect-troublesome-modes) @@ -535,6 +535,35 @@ This includes stopping tracking of all changes." (setq-local parinfer-rust--disable nil) (setq-local parinfer-rust--disable t))) +(defun parinfer-rust-mode-enable () + "Enable Parinfer." + ;; Make sure the library is installed at the appropriate location or offer to download it + (when (parinfer-rust--check-for-library parinfer-rust-supported-versions + parinfer-rust-library + parinfer-rust--lib-name + parinfer-rust-auto-download) + (require 'parinfer-rust parinfer-rust-library t)) + ;; Check version and prompt to download latest version if out of date Problem: Emacs can't + ;; reload dynamic libraries, which means that if we download a new library the user has to + ;; restart Emacs for changes to take effect. + (parinfer-rust--check-version parinfer-rust-supported-versions + (parinfer-rust-version) + parinfer-rust-library + parinfer-rust--lib-name) + (parinfer-rust-mode-setup) + (cond ((or (eq 'defer parinfer-rust-check-before-enable) + buffer-read-only) + ;; Defer checking for changes until a user changes the buffer + (setq-local parinfer-rust--disable t) + (add-hook 'before-change-functions #'parinfer-rust--check-for-issues t t)) + + ((eq 'immediate parinfer-rust-check-before-enable) + (setq-local parinfer-rust--disable t) + (parinfer-rust--check-for-issues)) + + (t (let ((parinfer-rust--mode "paren")) + (parinfer-rust--execute))))) + ;;;###autoload (defun parinfer-rust-switch-mode () "Switch to a different Parinfer mode. @@ -572,37 +601,12 @@ not available." (cond (parinfer-rust-enabled (parinfer-rust-mode-disable)) + ;; Don't do anything if the buffer is not selected ((not (eq (current-buffer) (window-buffer (selected-window)))) (add-hook 'window-selection-change-functions #'parinfer-rust--defer-loading nil t)) (t - (progn - ;; Make sure the library is installed at the appropriate location or offer to download it - (when (parinfer-rust--check-for-library parinfer-rust-supported-versions - parinfer-rust-library - parinfer-rust--lib-name - parinfer-rust-auto-download) - (require 'parinfer-rust parinfer-rust-library t)) - ;; Check version and prompt to download latest version if out of date Problem: Emacs can't - ;; reload dynamic libraries, which means that if we download a new library the user has to - ;; restart Emacs for changes to take effect. - (parinfer-rust--check-version parinfer-rust-supported-versions - (parinfer-rust-version) - parinfer-rust-library - parinfer-rust--lib-name) - (parinfer-rust-mode-enable) - (cond ((or (eq 'defer parinfer-rust-check-before-enable) - buffer-read-only) - ;; Defer checking for changes until a user changes the buffer - (setq-local parinfer-rust--disable t) - (add-hook 'before-change-functions #'parinfer-rust--check-for-issues t t)) - - ((eq 'immediate parinfer-rust-check-before-enable) - (setq-local parinfer-rust--disable t) - (parinfer-rust--check-for-issues)) - - (t (let ((parinfer-rust--mode "paren")) - (parinfer-rust--execute)))))))) + (parinfer-rust-mode-enable)))) (provide 'parinfer-rust-mode) ;;; parinfer-rust-mode.el ends here diff --git a/test/test-helper.el b/test/test-helper.el index 708d6d2..446b41d 100644 --- a/test/test-helper.el +++ b/test/test-helper.el @@ -143,10 +143,9 @@ it makes no sense to convert it to a string using parinfer-rust-library parinfer-rust--lib-name) (indent-tabs-mode 0) - (parinfer-rust-mode-enable) ;; Disable checks for deferral and do not run --execute on initialization ;; this breaks a lot of test because they expect the buffer to be in a specific state - ))) + (parinfer-rust-mode-setup)))) (defun simulate-parinfer-in-another-buffer--without-changes (test-string mode) "Run parinfer on buffer using text and cursor position From 946bf56b662e5a3dae0501007f818069071e60e7 Mon Sep 17 00:00:00 2001 From: Justin Barclay Date: Sun, 14 Apr 2024 23:33:26 -0700 Subject: [PATCH 3/4] Update helper function to return true early if the library exists Now we can use the check for the result of this function call --- parinfer-rust-helper.el | 23 +++++++++--------- parinfer-rust-mode.el | 54 ++++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/parinfer-rust-helper.el b/parinfer-rust-helper.el index 8db5f9b..e2f0e34 100644 --- a/parinfer-rust-helper.el +++ b/parinfer-rust-helper.el @@ -68,17 +68,18 @@ download LIB-NAME for the user. Automatically downloads if AUTO-DOWNLOAD is supplied or parinfer-rust runs in test mode, otherwise will promt user. Return non-nil if the parinfer-rust library was downloaded." - (when (and (not (file-exists-p library-location)) - (or - auto-download - (parinfer-rust--test-p) - (yes-or-no-p parinfer-rust--ask-to-download))) - (parinfer-rust--download-from-github (car supported-versions) - ;; This is a hold over because I am lazy. I've stuctured - ;; this data such that my version is the first one in the - ;; list - library-location lib-name) - t)) + (if (file-exists-p library-location) + t + (when (or + auto-download + (parinfer-rust--test-p) + (yes-or-no-p parinfer-rust--ask-to-download)) + (parinfer-rust--download-from-github (car supported-versions) + ;; This is a hold over because I am lazy. I've stuctured + ;; this data such that my version is the first one in the + ;; list + library-location lib-name) + t))) ;; This function has a problem: Emacs can't reload dynamic libraries. This means that if we download ;; a new library the user has to restart Emacs. (defun parinfer-rust--check-version (supported-versions current-version library-location lib-name) diff --git a/parinfer-rust-mode.el b/parinfer-rust-mode.el index ee5db74..eacea6f 100644 --- a/parinfer-rust-mode.el +++ b/parinfer-rust-mode.el @@ -538,31 +538,35 @@ This includes stopping tracking of all changes." (defun parinfer-rust-mode-enable () "Enable Parinfer." ;; Make sure the library is installed at the appropriate location or offer to download it - (when (parinfer-rust--check-for-library parinfer-rust-supported-versions - parinfer-rust-library - parinfer-rust--lib-name - parinfer-rust-auto-download) - (require 'parinfer-rust parinfer-rust-library t)) - ;; Check version and prompt to download latest version if out of date Problem: Emacs can't - ;; reload dynamic libraries, which means that if we download a new library the user has to - ;; restart Emacs for changes to take effect. - (parinfer-rust--check-version parinfer-rust-supported-versions - (parinfer-rust-version) - parinfer-rust-library - parinfer-rust--lib-name) - (parinfer-rust-mode-setup) - (cond ((or (eq 'defer parinfer-rust-check-before-enable) - buffer-read-only) - ;; Defer checking for changes until a user changes the buffer - (setq-local parinfer-rust--disable t) - (add-hook 'before-change-functions #'parinfer-rust--check-for-issues t t)) - - ((eq 'immediate parinfer-rust-check-before-enable) - (setq-local parinfer-rust--disable t) - (parinfer-rust--check-for-issues)) - - (t (let ((parinfer-rust--mode "paren")) - (parinfer-rust--execute))))) + (if (parinfer-rust--check-for-library parinfer-rust-supported-versions + parinfer-rust-library + parinfer-rust--lib-name + parinfer-rust-auto-download) + (progn + (require 'parinfer-rust parinfer-rust-library t) + ;; Check version and prompt to download latest version if out of date Problem: Emacs can't + ;; reload dynamic libraries, which means that if we download a new library the user has to + ;; restart Emacs for changes to take effect. + (parinfer-rust--check-version parinfer-rust-supported-versions + (parinfer-rust-version) + parinfer-rust-library + parinfer-rust--lib-name) + (parinfer-rust-mode-setup) + (cond ((or (eq 'defer parinfer-rust-check-before-enable) + buffer-read-only) + ;; Defer checking for changes until a user changes the buffer + (setq-local parinfer-rust--disable t) + (add-hook 'before-change-functions #'parinfer-rust--check-for-issues t t)) + + ((eq 'immediate parinfer-rust-check-before-enable) + (setq-local parinfer-rust--disable t) + (parinfer-rust--check-for-issues)) + + (t (let ((parinfer-rust--mode "paren")) + (parinfer-rust--execute))))) + (progn + (message "Unable to load library parinfer-rust disabling parinfer-rust-mode") + (parinfer-rust-mode-disable)))) ;;;###autoload (defun parinfer-rust-switch-mode () From bca579b2f3f1734664249ab9b380383c57f231d2 Mon Sep 17 00:00:00 2001 From: Justin Barclay Date: Sun, 14 Apr 2024 23:35:31 -0700 Subject: [PATCH 4/4] Fix bug where parinfer-rust-mode tried to load itself in wrong buffers window-selection-change-functions run when a window change happens. This means it happens when you enter into a window and when you leave. This should have been obvious but it wasn't and I wasted a day hunting this down --- parinfer-rust-helper.el | 6 ++++-- parinfer-rust-mode.el | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/parinfer-rust-helper.el b/parinfer-rust-helper.el index e2f0e34..b9abc0d 100644 --- a/parinfer-rust-helper.el +++ b/parinfer-rust-helper.el @@ -238,8 +238,10 @@ mode to better emulate users." (t num)))) (defun parinfer-rust--defer-loading (&rest _) "Defer loading of `parinfer-rust-mode' until the buffer is in focus." - (when (eq (current-buffer) - (window-buffer (selected-window))) + ;; This is a parinfer enabled buffer that started in the background and has now been moved to the foreground + (when (and parinfer-rust-enabled + (eq (current-buffer) + (window-buffer (selected-window)))) (remove-hook 'window-selection-change-functions #'parinfer-rust--defer-loading t) (parinfer-rust-mode-enable))) ;; Disable fill column warning only for this buffer to enable long strings of text without diff --git a/parinfer-rust-mode.el b/parinfer-rust-mode.el index eacea6f..7e83089 100644 --- a/parinfer-rust-mode.el +++ b/parinfer-rust-mode.el @@ -606,8 +606,12 @@ not available." (parinfer-rust-enabled (parinfer-rust-mode-disable)) ;; Don't do anything if the buffer is not selected + ;; TODO: Come up with a better way to defer and disable loading + ;; Defer waits for window selection change and disabled waits for a change event + ;; there is also the idea of deferring the running of parinfer vs deferring the loading ((not (eq (current-buffer) (window-buffer (selected-window)))) + (setq-local parinfer-rust-enabled t) (add-hook 'window-selection-change-functions #'parinfer-rust--defer-loading nil t)) (t (parinfer-rust-mode-enable))))