diff options
| author | Dmitry Gutov | 2012-12-14 10:58:15 +0400 |
|---|---|---|
| committer | Dmitry Gutov | 2012-12-14 10:58:15 +0400 |
| commit | bb808526ae2847f1e9aa6559835da2a10545a273 (patch) | |
| tree | 98fea65f31b2de5192163bea1d25beab29cb5044 /lisp/progmodes/ruby-mode.el | |
| parent | dbb530d9887fd51de9f8e338b325537d9eac0a3a (diff) | |
| download | emacs-bb808526ae2847f1e9aa6559835da2a10545a273.tar.gz emacs-bb808526ae2847f1e9aa6559835da2a10545a273.zip | |
* lisp/progmodes/ruby-mode.el (ruby-syntax-propertize-function):
Extract `ruby-syntax-propertize-expansions'.
(ruby-syntax-propertize-expansions): Only change syntax on
certain string delimiters, to punctuation. This way the common
functions like forward-word and thing-at-point still work.
(ruby-match-expression-expansion): Improve readability.
(ruby-block-contains-point): New function.
(ruby-add-log-current-method): Handle several edge cases.
* test/automated/ruby-mode-tests.el
Rename one interpolation test; add three more.
(ruby-with-temp-buffer): New macro, use it where appropriate.
(ruby-add-log-current-method-examples): Use "_" for target point.
Add four tests for ruby-add-log-current-method.
Diffstat (limited to 'lisp/progmodes/ruby-mode.el')
| -rw-r--r-- | lisp/progmodes/ruby-mode.el | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 6b9e921be67..8ac2f659058 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el | |||
| @@ -102,6 +102,10 @@ | |||
| 102 | '"\\(def\\|class\\|module\\)" | 102 | '"\\(def\\|class\\|module\\)" |
| 103 | "Regexp to match the beginning of a defun, in the general sense.") | 103 | "Regexp to match the beginning of a defun, in the general sense.") |
| 104 | 104 | ||
| 105 | (defconst ruby-singleton-class-re | ||
| 106 | "class\\s *<<" | ||
| 107 | "Regexp to match the beginning of a singleton class context.") | ||
| 108 | |||
| 105 | (eval-and-compile | 109 | (eval-and-compile |
| 106 | (defconst ruby-here-doc-beg-re | 110 | (defconst ruby-here-doc-beg-re |
| 107 | "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)" | 111 | "\\(<\\)<\\(-\\)?\\(\\([a-zA-Z0-9_]+\\)\\|[\"]\\([^\"]+\\)[\"]\\|[']\\([^']+\\)[']\\)" |
| @@ -384,7 +388,7 @@ and `\\' when preceded by `?'." | |||
| 384 | (when pos (goto-char pos)) | 388 | (when pos (goto-char pos)) |
| 385 | (forward-word -1) | 389 | (forward-word -1) |
| 386 | (and (or (bolp) (not (eq (char-before (point)) ?_))) | 390 | (and (or (bolp) (not (eq (char-before (point)) ?_))) |
| 387 | (looking-at "class\\s *<<")))) | 391 | (looking-at ruby-singleton-class-re)))) |
| 388 | 392 | ||
| 389 | (defun ruby-expr-beg (&optional option) | 393 | (defun ruby-expr-beg (&optional option) |
| 390 | "Check if point is possibly at the beginning of an expression. | 394 | "Check if point is possibly at the beginning of an expression. |
| @@ -1057,35 +1061,32 @@ For example: | |||
| 1057 | See `add-log-current-defun-function'." | 1061 | See `add-log-current-defun-function'." |
| 1058 | (condition-case nil | 1062 | (condition-case nil |
| 1059 | (save-excursion | 1063 | (save-excursion |
| 1060 | (let (mname mlist (indent 0)) | 1064 | (let ((indent 0) mname mlist |
| 1065 | (start (point)) | ||
| 1066 | (definition-re | ||
| 1067 | (concat "^[ \t]*" ruby-defun-beg-re "[ \t]+" | ||
| 1068 | "\\(" | ||
| 1069 | ;; \\. and :: for class methods | ||
| 1070 | "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)" | ||
| 1071 | "+\\)"))) | ||
| 1061 | ;; Get the current method definition (or class/module). | 1072 | ;; Get the current method definition (or class/module). |
| 1062 | (if (re-search-backward | 1073 | (when (re-search-backward definition-re nil t) |
| 1063 | (concat "^[ \t]*" ruby-defun-beg-re "[ \t]+" | 1074 | (goto-char (match-beginning 1)) |
| 1064 | "\\(" | 1075 | (when (ruby-block-contains-point start) |
| 1065 | ;; \\. and :: for class methods | 1076 | ;; We're inside the method, class or module. |
| 1066 | "\\([A-Za-z_]" ruby-symbol-re "*\\|\\.\\|::" "\\)" | 1077 | (setq mname (match-string 2)) |
| 1067 | "+\\)") | 1078 | (unless (string-equal "def" (match-string 1)) |
| 1068 | nil t) | 1079 | (setq mlist (list mname) mname nil))) |
| 1069 | (progn | 1080 | (setq indent (current-column)) |
| 1070 | (setq mname (match-string 2)) | 1081 | (beginning-of-line)) |
| 1071 | (unless (string-equal "def" (match-string 1)) | ||
| 1072 | (setq mlist (list mname) mname nil)) | ||
| 1073 | (goto-char (match-beginning 1)) | ||
| 1074 | (setq indent (current-column)) | ||
| 1075 | (beginning-of-line))) | ||
| 1076 | ;; Walk up the class/module nesting. | 1082 | ;; Walk up the class/module nesting. |
| 1077 | (while (and (> indent 0) | 1083 | (while (and (> indent 0) |
| 1078 | (re-search-backward | 1084 | (re-search-backward definition-re nil t)) |
| 1079 | (concat | ||
| 1080 | "^[ \t]*\\(class\\|module\\)[ \t]+" | ||
| 1081 | "\\([A-Z]" ruby-symbol-re "*\\)") | ||
| 1082 | nil t)) | ||
| 1083 | (goto-char (match-beginning 1)) | 1085 | (goto-char (match-beginning 1)) |
| 1084 | (if (< (current-column) indent) | 1086 | (when (ruby-block-contains-point start) |
| 1085 | (progn | 1087 | (setq mlist (cons (match-string 2) mlist)) |
| 1086 | (setq mlist (cons (match-string 2) mlist)) | 1088 | (setq indent (current-column)) |
| 1087 | (setq indent (current-column)) | 1089 | (beginning-of-line))) |
| 1088 | (beginning-of-line)))) | ||
| 1089 | ;; Process the method name. | 1090 | ;; Process the method name. |
| 1090 | (when mname | 1091 | (when mname |
| 1091 | (let ((mn (split-string mname "\\.\\|::"))) | 1092 | (let ((mn (split-string mname "\\.\\|::"))) |
| @@ -1104,7 +1105,14 @@ See `add-log-current-defun-function'." | |||
| 1104 | (setcdr (last mlist) (butlast mn)) | 1105 | (setcdr (last mlist) (butlast mn)) |
| 1105 | (setq mlist (butlast mn)))) | 1106 | (setq mlist (butlast mn)))) |
| 1106 | (setq mname (concat "." (car (last mn))))) | 1107 | (setq mname (concat "." (car (last mn))))) |
| 1107 | (setq mname (concat "#" mname))))) | 1108 | ;; See if the method is in singleton class context. |
| 1109 | (let ((in-singleton-class | ||
| 1110 | (when (re-search-forward ruby-singleton-class-re start t) | ||
| 1111 | (goto-char (match-beginning 0)) | ||
| 1112 | (ruby-block-contains-point start)))) | ||
| 1113 | (setq mname (concat | ||
| 1114 | (if in-singleton-class "." "#") | ||
| 1115 | mname)))))) | ||
| 1108 | ;; Generate the string. | 1116 | ;; Generate the string. |
| 1109 | (if (consp mlist) | 1117 | (if (consp mlist) |
| 1110 | (setq mlist (mapconcat (function identity) mlist "::"))) | 1118 | (setq mlist (mapconcat (function identity) mlist "::"))) |
| @@ -1112,6 +1120,12 @@ See `add-log-current-defun-function'." | |||
| 1112 | (if mlist (concat mlist mname) mname) | 1120 | (if mlist (concat mlist mname) mname) |
| 1113 | mlist))))) | 1121 | mlist))))) |
| 1114 | 1122 | ||
| 1123 | (defun ruby-block-contains-point (pt) | ||
| 1124 | (save-excursion | ||
| 1125 | (save-match-data | ||
| 1126 | (ruby-forward-sexp) | ||
| 1127 | (> (point) pt)))) | ||
| 1128 | |||
| 1115 | (defun ruby-brace-to-do-end (orig end) | 1129 | (defun ruby-brace-to-do-end (orig end) |
| 1116 | (let (beg-marker end-marker) | 1130 | (let (beg-marker end-marker) |
| 1117 | (goto-char end) | 1131 | (goto-char end) |