zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.4k stars 1.16k forks source link

Reintroduce `header` patterns for filetype detection #3208

Closed dmaluka closed 3 months ago

dmaluka commented 3 months ago

Replacing header patterns with signature patterns in #2819 was a mistake, since both are quite different from each other, and both have their uses. In fact, this caused a serious regression: for such files as shell scripts without *.sh extension but with #!/bin/sh inside (and other similar common use cases that have no filename matches and thus were relying on header matches), filetype detection does not work at all anymore.

Since both header and signature patterns are useful, reintroduce support for header patterns while keeping support for signature patterns as well, and make both work nicely together.

Also, unlike in the old implementation (before signatures were introduced), ensure that filename matches take precedence over header matches, i.e. if there is at least one filename match found, all header matches are ignored. This makes the behavior more deterministic and prevents previously observed issues like https://github.com/zyedidia/micro/issues/2894 and https://github.com/zyedidia/micro/issues/3054: wrongly detected filetypes caused by some overly general header patterns.

Precisely, the new behavior is:

  1. if there is at least one filename match, use filename matches only
  2. if there are no filename matches, use header matches
  3. in both cases, try to use signatures to find the best match among multiple filename or header matches

Changed signature patterns back to header patterns in almost all syntax files, except C++ and Objective-C (for which signature was actually introduced).

Also done a bit of refactoring and bugfixing of the code in UpdateRules() (see commit messages for the details).

Fixes #3201

JoeKar commented 3 months ago

Thank you for taking care and cleaning it up that far!