unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Hugo Thunnissen <devel@hugot.nl>
To: emacs-devel@gnu.org
Subject: Fontification using a syntax tree
Date: Sat, 18 Sep 2021 15:37:21 +0000	[thread overview]
Message-ID: <87bl4pc2tq.fsf@hugot.nl> (raw)


Hi all,

In the past weeks I've been improving phpinspect.el (my php
parser/completion package) to a point where the completion is functional
in most general cases for OOP code.

At the moment the parsing is done fairly "dumb" in the sense that the
entire buffer is parsed until the current point every time an eldoc
string or a completion needs to be provided. This is not a problem in
100-1000 line files, but once you're editing the last function in a 2000
line PHP class, you're bound to get a little annoyed by the
hiccups. For reference, parsing a 2000 line class takes about 0.3s on
my ryzen 5 3600, while a 400 line class takes only 0.06s.

To optimize this process, I am going to store my syntax tree and the
point -start and -end positions for its tokens in between parser
invocations. That will allow me to invalidate my syntax tree starting
from the token that is enclosing the start point of the edited region,
and "refresh" the invalidated part of the tree by parsing from that
point onwards.

Now, since I am going to store start and end positions of tokens, I was
thinking that from a performance standpoint it might be beneficial to
also use this information to provide fontification. My question to you
is, how should I go about doing this? From what I understand, font-lock
works with syntax tables, but if I use a syntax table for font-lock, I'm
letting font-lock take care of the parsing while I have a perfectly fine
syntax tree ready to use, right? Theoretically, with my stored tokens,
fontification would be as simple as: (pseudocode)

    ;; buffer-local alist with token objects as key.
    (setq phpinspect--token-positions `((,token . (start . end)) ...))

    (dolist (token-cons phpinspect--token-positions)
        (let ((token (car token-cons))
              (point-start  (cadr token-cons))
              (point-end (cddr token-cons)))
           (when (is-an-eligible-token-for-fontification-p token)
             (put-text-property point-start point-end
                                '(whatever property for this token)))))


Before I look deeper into this, is there a way to make this work with
font-lock? Or would I have to implement my own fontification mode? At
that point, is it even a thing that I should want to be doing? Is it a
bad idea / bad practice to not use font-lock?  Any advice or thoughts
are most welcome.

-Hugo



             reply	other threads:[~2021-09-18 15:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-18 15:37 Hugo Thunnissen [this message]
2021-09-18 15:48 ` Fontification using a syntax tree Yuan Fu
2021-09-18 16:34   ` Hugo Thunnissen
2021-09-18 17:16     ` Clément Pit-Claudel
2021-09-18 18:14 ` Stefan Monnier

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87bl4pc2tq.fsf@hugot.nl \
    --to=devel@hugot.nl \
    --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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).