Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to load syntax highlighting #303

Open
ladrua opened this issue Oct 18, 2021 · 1 comment
Open

Fail to load syntax highlighting #303

ladrua opened this issue Oct 18, 2021 · 1 comment

Comments

@ladrua
Copy link

ladrua commented Oct 18, 2021

I have a strange issue I cant seem to solve:
NB: seems to be only happening to script, using eighter js-mode or js2-mode

When I load a file that is using polymode, it only applies syntax highlighting to my first block, the rest are just white text, though I see it triggers the right major-mode when I hit the block.

I need to run revert-buffer while standing in the first block with correct syntax. After that when cursor hits past my next block, it triggers the correct highlighting. I then have to do this the same number of times as I have different mode blocks.

Emacs: GNU Emacs 25.2.2

I use polymode for .vue files here is my config.
vue.el plugin file:

(require 'polymode)
(require 'js2-mode)
(require 'vue-html-mode)


(defconst poly-vue-generic-head-regexp
  (rx bol "<%s"
      (? (and (+ space)
              "lang="
              (any "\"'")
              (group (+ (any alpha)))
              (any "\"'")))
      (* space)
      ">" eol))

(defconst poly-vue-template-head-regexp
  (format poly-vue-generic-head-regexp "template"))

(defconst poly-vue-script-head-regexp
  (format poly-vue-generic-head-regexp "script"))

(defconst poly-vue-style-head-regexp
  (rx bol "<style"
      (* (and space (or "scoped"
                        "module"
                        (and (+ (any wordchar))
                             "="
                             (any "\"'")
                             (*? anything)
                             (any "\"'")))))
      (and (+ space)
           "lang="
           (any "\"'")
           (group (+ (any alpha)))
           (any "\"'"))
      (* (and space (or "scoped"
                        "module"
                        (and (+ (any wordchar))
                             "="
                             (any "\"'")
                             (*? anything)
                             (any "\"'")))))
      (* space)
      ">" eol))

(defcustom poly-vue-template-languages
  '(("html" . vue-html-mode))
  "Alist of language names and major modes supported in templates."
  :type '(alist :key-type string
                :value-type symbol)
  :group 'poly-vue)

(defcustom poly-vue-script-languages
  '(("" . js2-mode))
  "Alist of language names and major modes supported in scripts."
  :type '(alist :key-type string
                :value-type symbol)
  :group 'poly-vue)

(defcustom poly-vue-style-languages
  '(("scss" . css-mode))
  "Alist of language names and major modes supported in stylesheets."
  :type '(alist :key-type string
                :value-type symbol)
  :group 'poly-vue)

(define-hostmode poly-vue-hostmode
  :mode 'sgml-mode
  :protect-syntax nil
  :protect-font-lock nil)

(defun poly-vue-template-mode-matcher ()
  "Matcher for the template tag in Vue files."
  (when (re-search-forward poly-vue-template-head-regexp (point-at-eol) t)
    (let ((lang (match-string-no-properties 1)))
      (or (cdr (assoc lang poly-vue-template-languages))
          lang))))

(defun poly-vue-script-mode-matcher ()
  "Matcher for the script tag in Vue files."
  (when (re-search-forward poly-vue-script-head-regexp (point-at-eol) t)
    (let ((lang (match-string-no-properties 1)))
      (or (cdr (assoc lang poly-vue-script-languages))
          lang))))

(defun poly-vue-style-mode-matcher ()
  "Matcher for the style tag in Vue files."
  (when (re-search-forward poly-vue-style-head-regexp (point-at-eol) t)
    (let ((lang (match-string-no-properties 1)))
      (or (cdr (assoc lang poly-vue-style-languages))
          lang))))

(define-auto-innermode poly-vue-template-innermode
  :fallback-mode 'vue-html-mode
  :head-mode 'host
  :tail-mode 'host
  :head-matcher "^<template.*?>\n"
  :tail-matcher "^</template>"
  :mode-matcher #'poly-vue-template-mode-matcher
  :body-indent-offset 'vue-html-extra-indent)

(define-auto-innermode poly-vue-script-innermode
  :fallback-mode 'js2-mode
  :head-mode 'host
  :tail-mode 'host
  :head-matcher "^<script.*?>\n"
  :tail-matcher "^</script>"
  :mode-matcher #'poly-vue-script-mode-matcher
  :body-indent-offset 0)

(define-auto-innermode poly-vue-style-innermode
  :fallback-mode 'css-mode
  :head-mode 'host
  :tail-mode 'host
  :head-matcher "^<style.*?>\n"
  :tail-matcher "^</style>"
  :mode-matcher #'poly-vue-style-mode-matcher
  :body-indent-offset 0)

;;;###autoload  (autoload 'poly-vue-mode "poly-vue")
(define-polymode poly-vue-mode
  :hostmode 'poly-vue-hostmode
  :innermodes '(poly-vue-template-innermode
                poly-vue-script-innermode
                poly-vue-style-innermode))

 ;;;###autoload
(add-to-list 'auto-mode-alist '("\\.vue\\'" . poly-vue-mode))

(provide 'poly-vue)

@vspinu
Copy link
Collaborator

vspinu commented Jan 18, 2022

@ladrua thanks for the report but I would need a comprehensive file example to figure this one out. Is it emacs 25 problem only? It's a bit of an old kit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants