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

JSON files without .json extension show as "Binary cannot be previewed" #2221

Closed
kamalmarhubi opened this issue Nov 5, 2022 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@kamalmarhubi
Copy link
Contributor

kamalmarhubi commented Nov 5, 2022

Description

The default configuration will incorrectly show the Binary cannot be previewed message for a JSON file whose extension is not json.

The mime-type check results in JSON being treated as non-text as its type is application/json, whose pre-slash element is not text. This happens here:

local output = capture(string.format([[file --mime-type -b "%s"]], filepath))
local mime_type = vim.split(output, "/")[1]
if mime_type ~= "text" and mime_type ~= "inode" then

Since the default has no mime_hook, the Binary cannot be previewed message shows instead of the file contents.

I came across this because I was opening a JSON .lock file, and was surprised not to see the preview. There are a few ecosystems that have .lock files that are JSON. Some examples: NPM's package-lock.json, Nix's flake.lock, pipenv's Pipfile.lock among others.


Not sure if the solution should be a hard-coded list of known-to-be-text mime types, a configuration option, or a combination. Requiring users to add a mime_hook just for this is pretty annoying, but I suppose that's also a solution!

Neovim version

NVIM v0.8.0-1210-gd367ed9b2
Build type: Release
LuaJIT 2.1.0-beta3
Compiled by runner@Mac-1664551586262.local

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/share/nvim"

Run :checkhealth for more info

Operating system and version

macOS 12.4

Telescope version / branch / rev

master (4bd4205)

checkhealth telescope

telescope: require("telescope.health").check()
========================================================================
## Checking for required plugins
  - OK: plenary installed.
  - WARNING: nvim-treesitter not found. 

## Checking external dependencies
  - OK: rg: found ripgrep 13.0.0
  - OK: fd: found fd 8.4.0

## ===== Installed extensions =====

## Telescope Extension: `fzf`
  - INFO: No healthcheck provided

Steps to reproduce

  1. echo '{"testJson":"content"}' > example.notjson
  2. nvim -u minimal.lua .
  3. :Telescope find_files
  4. search for example.notjson

Expected behavior

Content of file is shown in preview.

Actual behavior

No preview, instead the Binary cannot be previewed message.

Minimal config

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvim/site]]
local package_root = '/tmp/nvim/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'
local function load_plugins()
  require('packer').startup {
    {
      'wbthomason/packer.nvim',
      {
        'nvim-telescope/telescope.nvim',
        requires = {
          'nvim-lua/plenary.nvim',
          { 'nvim-telescope/telescope-fzf-native.nvim', run = 'make' },
        },
      },
      -- ADD PLUGINS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
    },
    config = {
      package_root = package_root,
      compile_path = install_path .. '/plugin/packer_compiled.lua',
      display = { non_interactive = true },
    },
  }
end
_G.load_config = function()
  require('telescope').setup()
  require('telescope').load_extension('fzf')
  -- ADD INIT.LUA SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
end
if vim.fn.isdirectory(install_path) == 0 then
  print("Installing Telescope and dependencies.")
  vim.fn.system { 'git', 'clone', '--depth=1', 'https://github.com/wbthomason/packer.nvim', install_path }
end
load_plugins()
require('packer').sync()
vim.cmd [[autocmd User PackerComplete ++once echo "Ready!" | lua load_config()]]
@kamalmarhubi kamalmarhubi added the bug Something isn't working label Nov 5, 2022
@kamalmarhubi kamalmarhubi changed the title JSON files without .json extension are not previewed with default configuration JSON files without .json extension show as "Binary cannot be previewed" Nov 5, 2022
Conni2461 pushed a commit that referenced this issue May 14, 2023
@yavorski
Copy link

yavorski commented Jun 12, 2023

Hi, this issue is valid for more file types in general, not only json.
Here are some examples:

  • json
  • jsonc
  • conf
  • config
  • settings
  • sublime-settings
  • stylus
  • pem

and maybe more.

Maybe it would be best to consider, some type of user option where we can map the specific file types so they can at least render somehow/anyhow in the preview pane, instead of just showing Binary cannot be previewed.

Cheers!

@jamestrew
Copy link
Contributor

should be closed by #2567

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants