Skip to content

tell delta what syntax/lanauge my file is #33

Open
@simonLeary42

Description

@simonLeary42

When I magit diff my ~/.emacs file, there is no syntax highlighting. I'm trying to find out the right way to make it have syntax highlighting. I know that emacs knows what language my file is, and I was thinking that maybe magit-delta could pass the --default-language argument to its delta subprocess.

Here's what I think I know:

  • magit-delta is built upon git-delta is built upon bat is built upon syntect
  • syntect allows the user to modify its understanding of languages using .sublime-syntax yaml files
    • part of this format is file_extensions
  • bat lets you put .sublime-syntax files in ~/.config/bat/syntaxes/
  • bat lets you use --syntax-map "<extension>:<syntax>" in the CLI and in ~/.config/bat/config
  • git-delta does not support bat's syntax map (open issue), but it does have a --default-syntax CLI option and delta.default-language git config option (discussion)
  • emacs determines the major mode for a file based on filename with auto-mode-alist, based on file contents with magic-mode-alist/magic-fallback-mode-alist, based on shebang with interpreter-mode-alist, or by the major-mode variable set using file-local variables, dir-locals, or any user elisp code.
    • snippet of auto-mode-alist:
       ("\\.ya?ml\\'" . yaml-ts-mode)
       ("\\.rs\\'" . rust-ts-mode)
       ("\\.py[iw]?\\'" . python-ts-mode)
       ("\\.lua\\'" . lua-ts-mode)
       ("\\.json\\'" . json-ts-mode)
       ("\\.js\\'" . js-ts-mode)

Determining the value for --default-language might not be doable in an elegant way. The problem is that the language string format is human readable and case sensitive, so a mapping from major-mode to bat-language-string is unlikely to be a simple regex replace and will probably require some amount of human maintenance effort.

Using --default-syntax would also not help if git-delta's detected syntax is different from what you want, but it would help when git-delta doesn't detect any syntax, like with .emacs.

One way to solve both of these problems would be to modify the file extensions given to git-delta. auto-mode-alist tells you at least one valid file extension for a major mode, which git-delta is likely to recognize. Creating symbolic links is ... doable in theory. Symbolic links have been possible without elevated privileges on windows for some time, but in my experience magit is effectively useless on windows anyways. I imagine that this blocking filesystem operation would not take terribly long, but still this feels like a hack.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions