Skip to content

fix(parser): detect filetype from existing buffer#69

Merged
barrettruth merged 1 commit intomainfrom
fix/filetype-from-existing-buffer
Feb 5, 2026
Merged

fix(parser): detect filetype from existing buffer#69
barrettruth merged 1 commit intomainfrom
fix/filetype-from-existing-buffer

Conversation

@barrettruth
Copy link
Owner

@barrettruth barrettruth commented Feb 5, 2026

Files detected via shebang or modeline (e.g., build with #!/bin/bash) weren't getting syntax highlighting in diffs because vim.filetype.match({ filename = ... }) only does filename-based detection.

Repro:

  1. Create a script with shebang but no extension
  2. Open it in neovim (filetype detected as sh/bash)
  3. Modify it, open :G, expand the diff
  4. Before: no syntax highlighting. After: bash keywords colored.

Closes #63

Thanks to @phanen for identifying this issue.

Files detected via shebang or modeline (e.g., `build` with `#!/bin/bash`)
weren't getting syntax highlighting because `vim.filetype.match()` only
does filename-based detection.

Now `get_ft_from_filename()` first checks if a buffer already exists for
the file and uses its detected filetype. This requires knowing the repo
root to construct the full path, so:

- `parse_buffer()` reads `b:diffs_repo_root` or `b:git_dir` (fugitive)
- `commands.lua` sets `b:diffs_repo_root` on diff buffers it creates

Co-authored-by: phanen <phanen@qq.com>
@barrettruth barrettruth merged commit a6d4dcf into main Feb 5, 2026
7 checks passed
@barrettruth barrettruth deleted the fix/filetype-from-existing-buffer branch February 5, 2026 05:16
barrettruth added a commit that referenced this pull request Feb 5, 2026
When a file has no extension but contains a shebang (e.g., `#!/bin/bash`),
filetype detection now reads the first 10 lines from disk and uses
`vim.filetype.match({ filename, contents })` for content-based detection.

This enables syntax highlighting for files like `build` scripts that rely
on shebang detection, even when the file isn't open in a buffer.

Detection order:
1. Existing buffer's filetype (already implemented in #69)
2. File content (shebang/modeline) - NEW
3. Filename extension only

Also adds `filetype on` to test helpers to ensure `vim.g.ft_ignore_pat`
is set, which is required for shell detection.
barrettruth added a commit that referenced this pull request Feb 5, 2026
When a file has no extension but contains a shebang (e.g., `#!/bin/bash`),
filetype detection now reads the first 10 lines from disk and uses
`vim.filetype.match({ filename, contents })` for content-based detection.

This enables syntax highlighting for files like `build` scripts that rely
on shebang detection, even when the file isn't open in a buffer.

Detection order:
1. Existing buffer's filetype (already implemented in #69)
2. File content (shebang/modeline) - NEW
3. Filename extension only

Also adds `filetype on` to test helpers to ensure `vim.g.ft_ignore_pat`
is set, which is required for shell detection.
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.

1 participant