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

Syntax highlighting that plays well with font-lock #36

Merged
merged 43 commits into from
May 1, 2020

Conversation

ubolonton
Copy link
Collaborator

@ubolonton ubolonton commented Apr 11, 2020

This is a continuation of #16.

  • Add a minor mode tree-sitter-hl-mode.
  • Add mechanism for major modes to customize syntax highlighting: tree-sitter-hl-default-patterns.
  • Add default syntax highlighting patterns to the language bundle tree-sitter-langs. This will be left for later, after improving the binary distribution mechanism.
  • Face mapping
    • Define faces for most common capture names (scopes) defined in the language repos, e.g. @function.method would map to tree-sitter-hl-face:function.method.
    • Prioritize captures in earlier patterns, when multiple patterns match.
    • Allow end user to add more query patterns: tree-sitter-hl-add-patterns.
    • Map capture names to face names automatically. For example @:font-lock-keyword-face can be mapped to the face font-lock-keyword-face. This will be done later, after discussing with upstream tree-sitter about standardization/conventions.
    • Allow end user to selectively disable certain capture names.
  • Respect custom keywords added by user through font-lock-add-keywords. This is too difficult currently, even for font-lock-mode itself (see font-lock-refresh-defaults).
  • Support code blocks in org-mode and markdown-mode.
  • Support embedded blocks of other languages (injections). This will be left for later.

For now, trying this requires cloning the language repos into the directory repos/ next to tree-sitter-langs.el (regardless of installation method).

I've been using this, and haven't noticed any performance difference from font-lock-mode.
In fact, sometimes it feels faster, in large C buffers.

@ubolonton ubolonton force-pushed the syntax-highlighting branch from 9eef772 to 688de32 Compare April 12, 2020 07:44
"Disable `tree-sitter' in the current buffer."
(remove-hook 'after-change-functions #'tree-sitter--after-change :local)
(remove-hook 'before-change-functions #'tree-sitter--before-change :local)
(setq tree-sitter-tree nil
tree-sitter-parser nil
tree-sitter-language nil))

(defmacro tree-sitter--error-protect (body-form &rest error-forms)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want to use condition-case instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't handle the error. It runs some cleanup code and lets the error propagate.

The closest condition-case equivalent I can think of doesn't preserve the backtrace:

`(condition-case err
    ,body-form
   (error
    ,@error-forms
    (signal (car err) (cdr err))))

@ubolonton
Copy link
Collaborator Author

Extra query patterns can now be added dynamically:

(add-hook 'rust-mode-hook
          (lambda ()
            (tree-sitter-hl-add-patterns
             [(call_expression
               function: (field_expression
                          field: (field_identifier) @function.call))
              (let_declaration (identifier) @variable)
              "?" @keyword
              (self) @keyword])))

Before:
rust default
After:
rust custom

@ubolonton ubolonton force-pushed the syntax-highlighting branch from f79ec29 to 5a9a841 Compare April 20, 2020 16:48
@ubolonton ubolonton changed the title [WIP] Syntax highlighting that plays well with font-lock Syntax highlighting that plays well with font-lock May 1, 2020
@ubolonton ubolonton merged commit 73257de into master May 1, 2020
@shackra
Copy link
Collaborator

shackra commented May 15, 2020

amazing! didn't keep an eye on the project and suddenly, BANG! Syntax highlight is mostly done.

@ubolonton ubolonton deleted the syntax-highlighting branch October 3, 2020 10:29
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

Successfully merging this pull request may close these issues.

3 participants