Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable coalton-mode inside coalton-toplevel in lisp-mode. #1747

Merged
merged 2 commits into from
Jan 14, 2025

Conversation

fukamachi
Copy link
Collaborator

@fukamachi fukamachi commented Jan 14, 2025

I need help to make coalton-mode work as a sub-mode of lisp-mode.

I'm using after-syntax-scan-hook to enable coalton-mode partially only inside coalton-toplevel, however it doesn't work for some reason.

Enable coalton-mode (added in #1744) as a submode inside coalton-toplevel in lisp-mode.

For example, in the following file, the type Integer should be highlighted even in lisp-mode.

;; Just for testing: test.lisp
(defpackage foo
  (:use :coalton-prelude))
(in-package :foo)

(coalton-toplevel
  (declare foo (Integer -> Integer))
  ...
  )

TODO

  • Make it work even in the middle of coalton-toplevel by looking backward

@cxxxr
Copy link
Member

cxxxr commented Jan 14, 2025

It appears that recursive-check needs to be set to nil.

diff --git a/extensions/coalton-mode/coalton-mode.lisp b/extensions/coalton-mode/coalton-mode.lisp
index ba083762..dd7d0849 100644
--- a/extensions/coalton-mode/coalton-mode.lisp
+++ b/extensions/coalton-mode/coalton-mode.lisp
@@ -350,6 +350,7 @@
     (lem-lisp-mode/autodoc:lisp-autodoc)))

 (defun after-syntax-scan (start end)
+  (ignore-errors
     (when (eq 'lem-lisp-mode:lisp-mode
               (buffer-major-mode (point-buffer start)))
       (with-point ((p start))
@@ -361,6 +362,7 @@
                    (lem:character-offset p -1)
                    (set-region-major-mode start p 'coalton-mode)
                    (syntax-scan-region start p
-                                     :syntax-table *syntax-table*))))))
+                                       :syntax-table *syntax-table*
+                                       :recursive-check nil)))))))

@fukamachi fukamachi force-pushed the coalton-toplevel-in-lisp-mode branch from d32be80 to 46e2959 Compare January 14, 2025 07:42
@fukamachi fukamachi changed the title [Help wanted] Trying to enable coalton-mode inside coalton-toplevel in lisp-mode. Enable coalton-mode inside coalton-toplevel in lisp-mode. Jan 14, 2025
@fukamachi
Copy link
Collaborator Author

@cxxxr Thanks! I've fixed it to work correctly.

@fukamachi fukamachi marked this pull request as draft January 14, 2025 07:46
@fukamachi fukamachi changed the title Enable coalton-mode inside coalton-toplevel in lisp-mode. [WIP] Enable coalton-mode inside coalton-toplevel in lisp-mode. Jan 14, 2025
@fukamachi
Copy link
Collaborator Author

@cxxxr Is it possible to make it work even if (coalton-toplevel is far away at the top of the viewport?

(coalton-toplevel


;;; A lot of Coalton code.......
;;;
;;; ***
;;; and it is like here is the beginning line of the view port.

  (declare fib (Integer -> Integer))

  )

I changed the code to scan a list backward, but it throws a control stack overflow at syntax-scan-region.

@cxxxr
Copy link
Member

cxxxr commented Jan 14, 2025

I sse, this worked.

❯ git diff -w
diff --git a/extensions/coalton-mode/coalton-mode.lisp b/extensions/coalton-mode/coalton-mode.lisp
index dede144d..a5559c6b 100644
--- a/extensions/coalton-mode/coalton-mode.lisp
+++ b/extensions/coalton-mode/coalton-mode.lisp
@@ -350,11 +350,15 @@
   (when (eql #\space (get-self-insert-char))
     (lem-lisp-mode/autodoc:lisp-autodoc)))

+(defvar *recursive* nil)
+
 (defun after-syntax-scan (start end)
+  (unless *recursive*
     (ignore-errors
       (when (eq 'lem-lisp-mode:lisp-mode
                 (buffer-major-mode (point-buffer start)))
         (with-point ((p start))
+          (lem-lisp-syntax:beginning-of-defun p -1)
           (loop while (lem:search-forward p "(coalton-toplevel" end)
                 unless (lem:in-string-or-comment-p p)
                 do (with-point ((start p))
@@ -362,8 +366,9 @@
                      (lem:scan-lists p 1 0)
                      (lem:character-offset p -1)
                      (set-region-major-mode start p 'coalton-mode)
+                     (let ((*recursive* t))
                        (syntax-scan-region start p
                                            :syntax-table *syntax-table*
-                                       :recursive-check nil)))))))
+                                           :recursive-check nil)))))))))

 (add-hook (variable-value 'after-syntax-scan-hook :global) 'after-syntax-scan)

@fukamachi
Copy link
Collaborator Author

Fantastic! Thank you!

@fukamachi fukamachi changed the title [WIP] Enable coalton-mode inside coalton-toplevel in lisp-mode. Enable coalton-mode inside coalton-toplevel in lisp-mode. Jan 14, 2025
@fukamachi fukamachi marked this pull request as ready for review January 14, 2025 08:52
@cxxxr
Copy link
Member

cxxxr commented Jan 14, 2025

Can I merge this PR?

@fukamachi
Copy link
Collaborator Author

Can I merge this PR?

Absolutely, if it looks fine to you!

@cxxxr cxxxr merged commit 88b23d0 into main Jan 14, 2025
4 checks passed
@fukamachi
Copy link
Collaborator Author

Thank you for your support. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants