all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Adam Niederer <adam.niederer@gmail.com>
To: emacs-devel@gnu.org
Subject: [PATCH] Update js-mode function heading regexes
Date: Sat, 17 Jun 2017 03:30:50 -0400	[thread overview]
Message-ID: <7ad22b11-406f-1193-c948-faaf01268710@gmail.com> (raw)

Hello,

I've modified the js-mode function heading regular expressions to accept
some modern additions to JavaScript. This causes function names to be
highlighted in more cases, and makes js-beginning-of-defun and
js-end-of-defun more consistent. The changelog and behavioral changes
are as follows:

* js--function-heading-1-re - Accept "async function foo" as well as
"function foo". This should only affect highlighting.

* js--function-heading-2-re - Accept any character before the beginning
of a function in an object. This causes the function's name to be
correctly highlighted.

Pre-patch, beginning-of-defun (C-M-a) with the point in location 1
(below) will place point at the beginning of the function keyword,
whereas at location 2 it will place point at the beginning of "bur".

Post-patch, beginning-of-defun at location 1 will place point at the
beginning of "foo", and at location 2 it will place point at the
beginning of "bur"

{ foo: function() {<point location 1>}
  bur: function() {<point location 2>} }

* js--function-heading-3-re - Accept let, const, and async anonymous
functions

Pre-patch, beginning-of-defun (C-M-a) with the point in location 1
(below) will place point at the beginning of "x", whereas at locations
2, 3 and 4, it will place point at the beginning of the respective
function keywords.

Post-patch, beginning-of-defun (C-M-a) with the point in location 1
(below) will place point at the beginning of "x", whereas at location 2,
3 and 4, it will place point at the beginning of "y", "z" and "w",
respectively.

var x = function() {<point location 1>}
var y = async function() {<point location 2>}

let z = function() {<point location 3>}
let w = async function() {<point location 4>}

Below is the file I used to test these changes. make check is failing
for me, but it looks like the failing tests are unrelated (filenotify &
inotify).

This is my first contribution via a mailing list, so please let me know
if I'm missing anything :).

-Adam

{ foo: function() {}
  bar: function() {} }

[{ foo: function() {}
   bar: function() {} }]

let x = [{ foo: function() {}
           bar: function() {} }]

function foo() {}
async function bar() {}

var x = function() {}
var x = async function() {}

let x = function() {}
let x = async function() {}

const x = function() {}
const x = async function() {}

diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el
index bae9e52bf0..126181f5bb 100644
--- a/lisp/progmodes/js.el
+++ b/lisp/progmodes/js.el
@@ -250,20 +250,20 @@ js--available-frameworks
 
 (defconst js--function-heading-1-re
   (concat
-   "^\\s-*function\\(?:\\s-\\|\\*\\)+\\(" js--name-re "\\)")
+   "^\\s-*\\(?:async\\s-+\\)?function\\(?:\\s-\\|\\*\\)+\\("
js--name-re "\\)")
   "Regexp matching the start of a JavaScript function header.
 Match group 1 is the name of the function.")
 
 (defconst js--function-heading-2-re
   (concat
-   "^\\s-*\\(" js--name-re "\\)\\s-*:\\s-*function\\_>")
+   "^.*?\\(" js--name-re "\\)\\s-*:\\s-*function\\_>")
   "Regexp matching the start of a function entry in an associative array.
 Match group 1 is the name of the function.")
 
 (defconst js--function-heading-3-re
   (concat
-   "^\\s-*\\(?:var\\s-+\\)?\\(" js--dotted-name-re "\\)"
-   "\\s-*=\\s-*function\\_>")
+   "^\\s-*\\(?:\\(?:var\\|let\\|const\\)\\s-+\\)?\\("
js--dotted-name-re "\\)"
+   "\\s-*=\\s-*\\(?:async\\s-+\\)?function\\_>")
   "Regexp matching a line in the JavaScript form \"var MUMBLE = function\".
 Match group 1 is MUMBLE.")
 




             reply	other threads:[~2017-06-17  7:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-17  7:30 Adam Niederer [this message]
2017-06-18 23:59 ` [PATCH] Update js-mode function heading regexes Dmitry Gutov
2017-06-19  7:04   ` Adam Niederer
2017-09-28 21:02     ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7ad22b11-406f-1193-c948-faaf01268710@gmail.com \
    --to=adam.niederer@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.