;; Dmitry Gutov <dgutov@yandex.ru>
;; URL: https://github.com/mooz/js2-mode/
;; http://code.google.com/p/js2-mode/
-;; Version: 20130219
+;; Version: 20130228
;; Keywords: languages, javascript
;; Package-Requires: ((emacs "24.1"))
(and (>= pos (point-at-bol))
(<= pos (point-at-eol))))
-(defun js2-same-line-2 (p1 p2)
- "Return t if P1 is on the same line as P2."
- (save-excursion
- (goto-char p1)
- (js2-same-line p2)))
-
(defun js2-code-bug ()
"Signal an error when we encounter an unexpected code path."
(error "failed assertion"))
(put 'cl-struct-js2-scope 'js2-visitor 'js2-visit-block)
(put 'cl-struct-js2-scope 'js2-printer 'js2-print-none)
-(defun js2-scope-set-parent-scope (scope parent)
- (setf (js2-scope-parent-scope scope) parent
- (js2-scope-top scope) (if (null parent)
- scope
- (js2-scope-top parent))))
-
(defun js2-node-get-enclosing-scope (node)
"Return the innermost `js2-scope' node surrounding NODE.
Returns nil if there is no enclosing scope node."
(js2-node-root parent)
n)))
-(defun js2-node-position-in-parent (node &optional parent)
- "Return the position of NODE in parent's block-kids list.
-PARENT can be supplied if known. Positioned returned is zero-indexed.
-Returns 0 if NODE is not a child of a block statement, or if NODE
-is not a statement node."
- (let ((p (or parent (js2-node-parent node)))
- (i 0))
- (if (not (js2-block-node-p p))
- i
- (or (js2-position node (js2-block-node-kids p))
- 0))))
-
(defsubst js2-node-short-name (n)
"Return the short name of node N as a string, e.g. `js2-if-node'."
(substring (symbol-name (aref n 0))
((js2-call-node-p node)
(js2-call-node-lp node))
((js2-paren-node-p node)
- (js2-node-pos node))
+ 0)
((js2-switch-node-p node)
(js2-switch-node-lp node))
((js2-catch-node-p node)
((js2-call-node-p node)
(js2-call-node-rp node))
((js2-paren-node-p node)
- (+ (js2-node-pos node) (js2-node-len node)))
+ (1- (js2-node-len node)))
((js2-switch-node-p node)
(js2-switch-node-rp node))
((js2-catch-node-p node)
(setq node (js2-node-parent node)))
node))
-(defun js2-nested-function-p (node)
- "Return t if NODE is a nested function, or is inside a nested function."
- (unless (js2-ast-root-p node)
- (js2-function-node-p (if (js2-function-node-p node)
- (js2-node-parent-script-or-fn node)
- (js2-node-parent-script-or-fn
- (js2-node-parent-script-or-fn node))))))
-
-(defun js2-mode-shift-kids (kids start offset)
- (dolist (kid kids)
- (if (> (js2-node-pos kid) start)
- (incf (js2-node-pos kid) offset))))
-
-(defun js2-mode-shift-children (parent start offset)
- "Update start-positions of all children of PARENT beyond START."
- (let ((root (js2-node-root parent)))
- (js2-mode-shift-kids (js2-node-child-list parent) start offset)
- (js2-mode-shift-kids (js2-ast-root-comments root) start offset)))
-
(defun js2-node-is-descendant (node ancestor)
"Return t if NODE is a descendant of ANCESTOR."
(while (and node
(t
(aref js2-side-effecting-tokens tt))))))
-(defun js2-member-expr-leftmost-name (node)
- "For an expr such as foo.bar.baz, return leftmost node foo.
-NODE is any `js2-node' object. If it represents a member expression,
-which is any sequence of property gets, element-gets, function calls,
-or xml descendants/filter operators, then we look at the lexically
-leftmost (first) node in the chain. If it is a name-node we return it.
-Note that NODE can be a raw name-node and it will be returned as well.
-If NODE is not a name-node or member expression, or if it is a member
-expression whose leftmost target is not a name node, returns nil."
- (let ((continue t)
- result)
- (while (and continue (not result))
- (cond
- ((js2-name-node-p node)
- (setq result node))
- ((js2-prop-get-node-p node)
- (setq node (js2-prop-get-node-left node)))
- ;; TODO: handle call-nodes, xml-nodes, others?
- (t
- (setq continue nil))))
- result))
-
(defconst js2-stmt-node-types
(list js2-BLOCK
js2-BREAK
(js2-xml-discard-string)
nil)))
-(defun js2-scanner-get-line ()
- "Return the text of the current scan line."
- (buffer-substring (point-at-bol) (point-at-eol)))
-
;;; Highlighting
(defun js2-set-face (beg end face &optional record)
(push (list beg end face) js2-mode-fontifications)
(put-text-property beg end 'font-lock-face face))))
-(defun js2-set-kid-face (pos kid len face)
- "Set-face on a child node.
-POS is absolute buffer position of parent.
-KID is the child node.
-LEN is the length to fontify.
-FACE is the face to fontify with."
- (js2-set-face (+ pos (js2-node-pos kid))
- (+ pos (js2-node-pos kid) (js2-node-len kid))
- face))
-
-(defsubst js2-fontify-kwd (start length)
- (js2-set-face start (+ start length) 'font-lock-keyword-face))
-
(defsubst js2-clear-face (beg end)
(remove-text-properties beg end '(font-lock-face nil
help-echo nil
(regexp-opt '("var" "let" "const") 'words)
"Regular expression matching variable declaration keywords.")
-;; This function has horrible results if you're typing an array
-;; such as [[1, 2], [3, 4], [5, 6]]. Bounce indenting -really- sucks
-;; in conjunction with electric-indent, so just disabling it.
-(defsubst js2-code-at-bol-p ()
- "Return t if the first character on line is non-whitespace."
- nil)
-
-(defun js2-insert-and-indent (key)
- "Run command bound to KEY and indent current line.
-Runs the command bound to KEY in the global keymap and indents
-the current line."
- (interactive (list (this-command-keys)))
- (let ((cmd (lookup-key (current-global-map) key)))
- (if (commandp cmd)
- (call-interactively cmd)))
- ;; don't do the electric keys inside comments or strings,
- ;; and don't do bounce-indent with them.
- (let ((parse-state (syntax-ppss (point)))
- (js2-bounce-indent-p (js2-code-at-bol-p)))
- (unless (or (nth 3 parse-state)
- (nth 4 parse-state))
- (indent-according-to-mode))))
-
(defun js2-re-search-forward-inner (regexp &optional bound count)
"Auxiliary function for `js2-re-search-forward'."
(let (parse saved-point)
(skip-chars-backward " \t" (point-at-bol)))
(let ((pt (point)))
(back-to-indentation)
+ (when (looking-at "}[ \t]*")
+ (goto-char (match-end 0)))
(and (looking-at js2-possibly-braceless-keywords-re)
(= (match-end 0) pt)
(not (js2-end-of-do-while-loop-p))))))
(skip-chars-forward " \t")
(looking-at "case\\s-.+:")))
-(defun js2-syntax-bol ()
- "Return the point at the first non-whitespace char on the line.
-Returns `point-at-bol' if the line is empty."
- (save-excursion
- (beginning-of-line)
- (skip-chars-forward " \t")
- (point)))
-
(defun js2-bounce-indent (normal-col parse-status &optional backwards)
"Cycle among alternate computed indentation positions.
PARSE-STATUS is the result of `parse-partial-sexp' from the beginning
(skip-chars-backward " \t")
(goto-char (point-at-eol))))
-(defsubst js2-mode-inside-string ()
- "Return non-nil if inside a string.
-Actually returns the quote character that begins the string."
- (nth 3 (syntax-ppss)))
-
-(defun js2-mode-inside-comment-or-string ()
- "Return non-nil if inside a comment or string."
- (or
- (let ((comment-start
- (save-excursion
- (goto-char (point-at-bol))
- (if (re-search-forward "//" (point-at-eol) t)
- (match-beginning 0)))))
- (and comment-start
- (<= comment-start (point))))
- (let ((parse-state (syntax-ppss)))
- (or (nth 3 parse-state)
- (nth 4 parse-state)))))
-
-
-(defun js2-insert-catch-skel (try-pos)
- "Complete a try/catch block after inserting a { following a try keyword.
-Rationale is that a try always needs a catch or a finally, and the catch is
-the more likely of the two.
-
-TRY-POS is the buffer position of the try keyword. The open-curly should
-already have been inserted."
- (insert "{")
- (let ((try-col (save-excursion
- (goto-char try-pos)
- (current-column))))
- (insert "\n")
- (undo-boundary)
- (js2-indent-line) ;; indent the blank line where cursor will end up
- (save-excursion
- (insert "\n")
- (indent-to try-col)
- (insert "} catch (x) {\n\n")
- (indent-to try-col)
- (insert "}"))))
-
-
(defun js2-mode-wait-for-parse (callback)
"Invoke CALLBACK when parsing is finished.
If parsing is already finished, calls CALLBACK immediately."
(save-restriction
(widen) ;; `blink-matching-open' calls `narrow-to-region'
(js2-reparse))
- (let ((scan-msg "Containing expression ends prematurely")
+ (let (forward-sexp-function
node (start (point)) pos lp rp child)
(cond
;; backward-sexp
((and arg (minusp arg))
(dotimes (i (- arg))
(js2-backward-sws)
- (forward-char -1) ; enter the node we backed up to
+ (forward-char -1) ; Enter the node we backed up to.
(when (setq node (js2-node-at-point (point) t))
(setq pos (js2-node-abs-pos node))
(let ((parens (js2-mode-forward-sexp-parens node pos)))
(setq lp (car parens)
- rp (cdr parens))))
- (goto-char
- (or (when (and lp (> start lp))
- (if (and rp (<= start rp))
- (if (setq child (js2-node-closest-child node (point) lp t))
- (js2-node-abs-pos child)
- (goto-char start)
- (signal 'scan-error (list scan-msg lp lp)))
- lp))
- pos
- (point-min)))))
+ rp (cdr parens)))
+ (when (and lp (> start lp))
+ (if (and rp (<= start rp))
+ ;; Between parens, check if there's a child node we can jump.
+ (when (setq child (js2-node-closest-child node (point) lp t))
+ (setq pos (js2-node-abs-pos child)))
+ ;; Before both parens.
+ (setq pos lp)))
+ (let ((state (parse-partial-sexp start pos)))
+ (goto-char (if (not (zerop (car state)))
+ ;; Stumble at the unbalanced paren if < 0, or
+ ;; jump a bit further if > 0.
+ (scan-sexps start -1)
+ pos))))
+ (unless pos (goto-char (point-min)))))
(t
;; forward-sexp
- (js2-forward-sws)
(dotimes (i arg)
(js2-forward-sws)
(when (setq node (js2-node-at-point (point) t))
(setq pos (js2-node-abs-pos node))
(let ((parens (js2-mode-forward-sexp-parens node pos)))
(setq lp (car parens)
- rp (cdr parens))))
- (goto-char
- (or (when (and rp (<= start rp))
- (if (> start lp)
- (if (setq child (js2-node-closest-child node (point) rp))
- (js2-node-abs-end child)
- (goto-char start)
- (signal 'scan-error (list scan-msg rp (1+ rp))))
- (1+ rp)))
- (and pos
- (+ pos
- (js2-node-len
+ rp (cdr parens)))
+ (or
+ (when (and rp (<= start rp))
+ (if (> start lp)
+ (when (setq child (js2-node-closest-child node (point) rp))
+ (setq pos (js2-node-abs-end child)))
+ (setq pos (1+ rp))))
+ ;; No parens or child nodes, looks for the end of the curren node.
+ (incf pos (js2-node-len
(if (js2-expr-stmt-node-p (js2-node-parent node))
- ;; stop after the semicolon
+ ;; Stop after the semicolon.
(js2-node-parent node)
node))))
- (point-max))))))))
+ (let ((state (save-excursion (parse-partial-sexp start pos))))
+ (goto-char (if (not (zerop (car state)))
+ (scan-sexps start 1)
+ pos))))
+ (unless pos (goto-char (point-max))))))))
(defun js2-mode-forward-sexp-parens (node abs-pos)
+ "Return a cons cell with positions of main parens in NODE."
(cond
((or (js2-array-node-p node)
(js2-object-node-p node)