Font-lock and disable js2-mode parsing node shebang line

旧巷老猫 提交于 2021-01-29 14:22:51

问题


How can I treat the a shebang line of the form

#! /usr/bin/env node

As a comment line, when using js2-mode? I've tried a couple things, one as a modification of something Stefan proposed on another thread (can't find it now), eg. a modification to the syntax-propertize-rules,

(defalias 'my-js2-syntax-propertize-shebang
  (syntax-propertize-rules
   ("\\`\\(#\\)!.*/[^ \t\n]+" (1 "!"))))

(defun my-js2-hook ()
  (add-function :before (local 'syntax-propertize-rules) 
                 #'my-js2-syntax-propertize-shebang))

Which doesn't seem to have any effect. so, I just added another regex via font-lock-add-keywords, eg.

(defun my-js2-font-lock-additions ()
  (font-lock-add-keywords
   nil
   '(("\\`\\(#!\\s-*.*/[^ \t\n]+\\)\\s-*\\([^ \t\n]+\\)\\s-*$"
      (1 font-lock-comment-face t)
      (2 font-lock-type-face t))))
  (font-lock-flush)
  (font-lock-ensure))

Which does result in the shebang line being fontified, but the js2-mode parser still tries to parse it, treats it as errors, and adds the relevant overlays. How can I tell js2-mode to ignore that line, or do something like mark it as external to the AST/buffer or something?

来源:https://stackoverflow.com/questions/60592024/font-lock-and-disable-js2-mode-parsing-node-shebang-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!