From 1b9d6163b023aaefd15d38ea28e968a113202402 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Wed, 9 Mar 2016 16:43:51 +0200 Subject: [PATCH] Propertize operator symbol names with symbol syntax class * lisp/progmodes/ruby-mode.el (ruby-syntax-propertize): Do it here. (ruby-font-lock-keywords): Instead of handling them here. Leave highlighting them to the "normal" matcher, because now we can. (ruby-smie--forward-token, ruby-smie--backward-token): Likewise, don't special-case operator symbols anymore. (ruby-smie--args-separator-p): Simplify the regexp, match operator names with \s_. (ruby-smie--implicit-semi-p): Handle the special cases of ? and = at EOL the same way: check if the character has been assigned the symbol syntax class by syntax-propertize. --- lisp/progmodes/ruby-mode.el | 33 +++++++++++---------------------- test/indent/ruby.rb | 6 ++++++ 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 914703d5f1b..0c5e8fe8054 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el @@ -431,17 +431,11 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." (not (or (bolp) (memq (char-before) '(?\[ ?\()) (and (memq (char-before) - '(?\; ?- ?+ ?* ?/ ?: ?. ?, ?\\ ?& ?> ?< ?% ?~ ?^)) - ;; Not a binary operator symbol. - (not (eq (char-before (1- (point))) ?:)) - ;; Not the end of a regexp or a percent literal. - (not (memq (car (syntax-after (1- (point)))) '(7 15)))) - (and (eq (char-before) ?\?) - (equal (save-excursion (ruby-smie--backward-token)) "?")) - (and (eq (char-before) ?=) - ;; Not a symbol :==, :!=, or a foo= method. - (string-match "\\`\\s." (save-excursion - (ruby-smie--backward-token)))) + '(?\; ?- ?+ ?* ?/ ?: ?. ?, ?\\ ?& ?> ?< ?% ?~ ?^ ?= ??)) + ;; Not a binary operator symbol like :+ or :[]=. + ;; Or a (method or symbol) name ending with ?. + ;; Or the end of a regexp or a percent literal. + (not (memq (car (syntax-after (1- (point)))) '(3 7 15)))) (and (eq (char-before) ?|) (member (save-excursion (ruby-smie--backward-token)) '("|" "||"))) @@ -485,7 +479,7 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." "else" "elsif" "do" "end" "and") 'symbols)))) (memq (car (syntax-after pos)) '(7 15)) - (looking-at "[([]\\|[-+!~]\\sw\\|:\\(?:\\sw\\|\\s.\\)"))))) + (looking-at "[([]\\|[-+!~:]\\(?:\\sw\\|\\s_\\)"))))) (defun ruby-smie--at-dot-call () (and (eq ?w (char-syntax (following-char))) @@ -511,8 +505,6 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." (save-excursion (ruby-smie--args-separator-p (prog1 (point) (goto-char pos))))) " @ ") - ((looking-at ":\\s.+") - (goto-char (match-end 0)) (match-string 0)) ;bug#15208. ((looking-at "\\s\"") "") ;A string. (t (let ((dot (ruby-smie--at-dot-call)) @@ -562,8 +554,6 @@ It is used when `ruby-encoding-magic-comment-style' is set to `custom'." (dot (ruby-smie--at-dot-call))) (when dot (setq tok (concat "." tok))) - (when (and (eq ?: (char-before)) (string-match "\\`\\s." tok)) - (forward-char -1) (setq tok (concat ":" tok))) ;; bug#15208. (cond ((member tok '("unless" "if" "while" "until")) (if (ruby-smie--bosp) @@ -1861,11 +1851,13 @@ It will be properly highlighted even when the call omits parens.") (ignore (goto-char (match-end 1))) (string-to-syntax "\\")))) - ;; Part of symbol when at the end of a method name. + ;; Symbols with special characters. + ("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\)\\)" + (3 (string-to-syntax "_"))) + ;; Part of method name when at the end of it. ("[!?]" (0 (unless (save-excursion (or (nth 8 (syntax-ppss (match-beginning 0))) - (eq (char-before) ?:) (let (parse-sexp-lookup-properties) (zerop (skip-syntax-backward "w_"))) (memq (preceding-char) '(?@ ?$)))) @@ -2183,7 +2175,7 @@ See `font-lock-syntax-table'.") ;; Keywords that evaluate to certain values. ("\\_<__\\(?:LINE\\|ENCODING\\|FILE\\)__\\_>" (0 font-lock-builtin-face)) - ;; Symbols with symbol characters. + ;; Symbols. ("\\(^\\|[^:]\\)\\(:@?\\(?:\\w\\|_\\)+\\)\\([!?=]\\)?" (2 font-lock-constant-face) (3 (unless (and (eq (char-before (match-end 3)) ?=) @@ -2191,9 +2183,6 @@ See `font-lock-syntax-table'.") ;; bug#18644 font-lock-constant-face) nil t)) - ;; Symbols with special characters. - ("\\(^\\|[^:]\\)\\(:\\([-+~]@?\\|[/%&|^`]\\|\\*\\*?\\|<\\(<\\|=>?\\)?\\|>[>=]?\\|===?\\|=~\\|![~=]?\\|\\[\\]=?\\)\\)" - 2 font-lock-constant-face) ;; Special globals. (,(concat "\\$\\(?:[:\"!@;,/\\._><\\$?~=*&`'+0-9]\\|-[0adFiIlpvw]\\|" (regexp-opt '("LOAD_PATH" "LOADED_FEATURES" "PROGRAM_NAME" diff --git a/test/indent/ruby.rb b/test/indent/ruby.rb index 49478602e41..3fdb68064be 100644 --- a/test/indent/ruby.rb +++ b/test/indent/ruby.rb @@ -168,6 +168,12 @@ if x == :!= something end +qux :+, + bar, + :[]=, + bar, + :a + # Example from http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html d = 4 + 5 + # no '\' needed 6 + 7 -- 2.39.2