Kichik tartibda murakkab sintaksikni ta'kidlashni qanday qo'shish mumkin?

Turli yuzlar bilan kodni kichik rejimda ta'kidlashni istardim.

Men istagan narsaga yaqin bo'lgan ekran tasvirini:

python-syntax-highlight

Yo'qotilgan narsalardan biri # ning sharhidagi charslarga ega shrift-lock-sharh-yuz . Fikr, "tegishli" matnni aniq matn sifatida ko'rsatgan holda, o'qishni osonlashtiradi. Qachonki odatdagicha kamroq ko'zga tashlanadigan yuzlari bilan muntazam muloqat qiladilar.

Men foydalanadigan kod:

(setq-local font-lock-defaults
            '(python-font-lock-keywords
              nil nil nil nil
              (font-lock-syntactic-face-function
               . lpy-font-lock-syntactic-face-function)))

(defun lpy-font-lock-syntactic-face-function (state)
  "Return syntactic face given STATE.
Returns 'defalt face for comments that belong to an outline."
  (cond ((nth 3 state)
         (if (python-info-docstring-p state)
             font-lock-doc-face
           font-lock-string-face))
        ((save-excursion
           (while (and (> (point) (point-min))
                       (progn (move-beginning-of-line 0)
                              (eq (char-after) ?\#))))
           (forward-line 1)
           (looking-at "#\\*+ "))
         'default)
        (t
         font-lock-comment-face)))

Aslida, interfeysi haqida hech qanday maslahatim yo'q shrift-lock-sintaktik-yuz-funktsiya ishlaydi, unda boshqasi murakkab ma'lumotlar tuzilishi state , turli nuqta holatiga ega va qaytaradi bir yuz.

Kimdir bu interfeysni tushuntirishi mumkin? Ehtimol, yaxshiroqmi?

9

2 javoblar

font-lock-syntactic-face-function is a regular variable from Font Lock, more specifically from the Syntactic Font Lock phase (emphasis mine):

Agar bu o'zgaruvchiga nil bo'lmagan bo'lsa, u qaysi sintaktik element uchun ishlatilishini aniqlash uchun funktsiya bo'lishi kerak (string yoki izoh). Qiymat odatda shrift-lock-defaults-da boshqa-vars elementida o'rnatiladi.

     

Funktsiya bitta argument bilan chaqiriladi, parolni parse-partial-sexp tomonidan qaytarilgan nuqtada ko'rib chiqamiz va yuzni ga qaytarishi kerak. Standart qiymat shrift-lock-comment-face-ni va satrlari uchun font-lock-string-yuzini qaytaradi (qarang: Font Lock uchun yuzlar).

parse-partial-sexp in turn returns a list which describes Emacs' current syntactic state, which is essentially the result of the application of the syntax table to the current buffer. The list is rather complex, hence I'll spare it here; you can see the complete reference in the docstring of parse-partial-sexp. The purpose of this function is to change the face applied to a syntactic element under certain rules. The beginning of your function demonstrates this: If the current string is a docstring, use a different face for it.

Biroq, yuz har doim ham butun sintaktik elementga, ya'ni butun mag'lubiyatga yoki sharhga nisbatan qo'llaniladi. Ushbu funksiya bilan alohida qismlarni ta'kidlash mumkin emas va shu maqsadda shunga o'xshash (python-info-docstring-p shtat) uchun ushbu kodni tekshirish kerak kodingizda. emas, balki bu joydan foydalaning; Men hatto nuqtasi qiymatining shrift-qulflashning ushbu bosqichida ham to'g'ri aniqlanganligini ishonchim komil emas.


Parchalarni birlashtirib, siz maqsadingiz uchun noto'g'ri funksiyadan foydalanmoqdasiz, shuning uchun siz uni ishlashga qodir bo'lmaysiz.

Sizga kerakli yoritishni amalga oshirishga harakat qilmadim, lekin sizning maqsadingiz uchun juda chuqur yo'lni topgansiz deb o'ylayman. Agar narsalarni to'g'ri tushunsam, sharhning asosiy qismini yoqtirasan.

Agar men haq bo'lsam, shunchaki shrift-lock-keywords -ga maxsus ravishda kerak:

(my/find-outline-in-comment-p 0 'outline-face t)

bu erda outline-face - bosh sahifaga murojaat qilishni xohlagan yuz, t - bu yerda oldindan belgilangan shriftlarni blokirovkalashni va my/> o'rtasidagi tushuntirishni birinchi manzilni qidiradigan va qidiruvga ega bo'lgan, font-lock-defaults docstring-ga qarang. (nuqta) va bu manzilni belgilang, chizilgan kengaytmalarini o'yin ma'lumotlarida ta'kidlangan bo'lishi kerak.

Anahatni topish uchun siz sharhlaringizni ( shrift-lock-comment-face yoki sintaktik holatni ishlatib) oldinga siljitib, keyin look-at dan foydalanasiz. sharhning tasviri bor.

6
qo'shib qo'ydi

Quyidagi kabi shrift-lock-syntactic-face-function ni aniqlashni o'ylab ko'ring:

(setq font-lock-syntactic-face-function
      (lambda (state)
    (cond ((nth 3 state)
           font-lock-string-face)
          ((and (nth 4 state)(nth 8 state))
            MY-COMMENT-FACE
          (t  font-lock-comment-face))))

Bu python-mode.el bilan sinovdan o'tkazilib, izohni sharhlamasdan "# *" bilan boshlangan bo'limni qoldirdi:

(setq py--font-lock-syntactic-face-function
      (lambda (state)
    (cond ((nth 3 state)
           font-lock-string-face)
          ((and (nth 4 state)(nth 8 state)
            (progn (save-excursion
                 (goto-char (nth 8 state))
                 (looking-at (concat comment-start (regexp-quote "*"))))))
           nil)
          (t font-lock-comment-face))))

Bu rejim bilan boshqariladi:

(font-lock-syntactic-face-function
                    . py--font-lock-syntactic-face-function)

nil o'rniga, har qanday haqiqiy yuz ishlashi kerak.

0
qo'shib qo'ydi