jpellerin / emacs-crystal-mode

A minimal crystal mode for emacs, based on ruby-mode (of course)
GNU General Public License v3.0
23 stars 13 forks source link

[Enhancement] Support `struct`, `abstract`, etc. #7

Open js-ojus opened 8 years ago

js-ojus commented 8 years ago

I tried modifying the mode a little, with a little success. Here is the diff.

--- crystal-mode.el.orig        2015-09-11 22:12:51.921843474 +0530
+++ crystal-mode.el     2015-09-21 11:59:49.652919958 +0530
@@ -55,7 +55,7 @@
   :group 'languages)

 (defconst crystal-block-beg-keywords
-  '("class" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do" "macro")
+  '("class" "struct" "module" "def" "if" "unless" "case" "while" "until" "for" "begin" "do" "macro" "abstract")
   "Keywords at the beginning of blocks.")

 (defconst crystal-block-beg-re
@@ -67,7 +67,7 @@
   "Regexp to match keywords that nest without blocks.")

 (defconst crystal-indent-beg-re
-  (concat "^\\(\\s *" (regexp-opt '("class" "module" "def" "macro")) "\\|"
+  (concat "^\\(\\s *" (regexp-opt '("class" "struct" "module" "def" "macro" "abstract")) "\\|"
           (regexp-opt '("if" "unless" "case" "while" "until" "for" "begin"))
           "\\)\\_>")
   "Regexp to match where the indentation gets deeper.")
@@ -103,7 +103,7 @@
 (defconst crystal-block-end-re "\\_<end\\_>")

 (defconst crystal-defun-beg-re
-  '"\\(def\\|class\\|module\\|macro\\)"
+  '"\\(def\\|class\\|struct\\|module\\|macro\\|abstract\\)"
   "Regexp to match the beginning of a defun, in the general sense.")

 (defconst crystal-singleton-class-re
@@ -381,7 +381,10 @@
        (exp3 ("def" insts "end")
              ("begin" insts-rescue-insts "end")
              ("do" insts "end")
-             ("class" insts "end") ("module" insts "end")
+             ("abstract class" insts "end")
+             ("class" insts "end")
+             ("struct" insts "end")
+             ("module" insts "end")
              ("[" expseq "]")
              ("{" hashvals "}")
              ("{" insts "}")
@@ -719,8 +722,8 @@
     (`(:before . ";")
      (message "Before ;")
      (cond
-      ((smie-rule-parent-p "def" "begin" "do" "class" "module" "{%for%}"
-                           "while" "until" "unless" "macro"
+      ((smie-rule-parent-p "def" "begin" "do" "class" "struct" "module" "abstract"
+                           "{%for%}" "while" "until" "unless" "macro"
                            "if" "then" "elsif" "else" "when" "{%if%}"
                            "{%elsif%}" "{%else%}" "{%unless%}"
                            "rescue" "ensure" "{")
@@ -820,7 +823,7 @@
   (let ((index-alist '()) (case-fold-search nil)
         name next pos decl sing)
     (goto-char beg)
-    (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t)
+    (while (re-search-forward "^\\s *\\(\\(class\\s +\\|\\(class\\s *<<\\s *\\)\\|module\\s +\\|struct\\s +\\)\\([^\(<\n ]+\\)\\|\\(def\\|alias\\)\\s +\\([^\(\n ]+\\)\\)" end t)
       (setq sing (match-beginning 3))
       (setq decl (match-string 5))
       (setq next (match-end 0))
@@ -1750,7 +1753,7 @@
                           "\\([A-Za-z_]" crystal-symbol-re "*\\|\\.\\|::" "\\)"
                           "+\\)")))
                (definition-re (funcall make-definition-re crystal-defun-beg-re))
-               (module-re (funcall make-definition-re "\\(class\\|module\\)")))
+               (module-re (funcall make-definition-re "\\(class\\|struct\\|module\\)")))
           ;; Get the current method definition (or class/module).
           (when (re-search-backward definition-re nil t)
             (goto-char (match-beginning 1))
@@ -2161,6 +2164,7 @@
        crystal-font-lock-keyword-beg-re
        (regexp-opt
         '("->"
+          "abstract"
           "alias"
           "and"
           "begin"
@@ -2189,6 +2193,7 @@
           "retry"
           "return"
           "then"
+          "struct"
           "super"
           "unless"
           "undef"

However, I am no good at Emacs Lisp, and could not make much progress in the time that I had on hand.