nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.77k stars 833 forks source link

grep fails to apply syntax highlighting from `after` config #2520

Closed casonadams closed 1 year ago

casonadams commented 1 year ago

Description

Steps to reproduce

-- ~/.config/nvim/after/ftdetect/brs.lua

local callback = function()
  vim.bo.filetype = "brs"
  vim.bo.syntax = "vb"
  vim.bo.commentstring = "'%s"
  vim.bo.ts = 4
  vim.bo.sw = 4
  vim.cmd("runtime! indent/vb.vim")
end

vim.api.nvim_create_autocmd({ "BufNewFile", "BufFilePost", "BufRead" }, {
  pattern = "*.brs",
  callback = callback,
})

Expected Results

No syntax highlights applied *.brs files

Actual Results

Syntax highlights SHOULD be applied to *.brs files

Notes

Syntax highlighting is correct when finding files.

Neovim version

NVIM v0.9.0
Build type: Release
LuaJIT 2.1.0-beta3

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/Cellar/neovim/0.9.0/share/nvim"

Run :checkhealth for more info

Operating system and version

13.3.1

Telescope version / branch / rev

master

checkhealth telescope

telescope: require("telescope.health").check()

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

Checking external dependencies ~
- OK rg: found ripgrep 13.0.0
- OK fd: found fd 8.7.0

Steps to reproduce

-- ~/.config/nvim/after/ftdetect/brs.lua

local callback = function()
  vim.bo.filetype = "brs"
  vim.bo.syntax = "vb"
  vim.bo.commentstring = "'%s"
  vim.bo.ts = 4
  vim.bo.sw = 4
  vim.cmd("runtime! indent/vb.vim")
end

vim.api.nvim_create_autocmd({ "BufNewFile", "BufFilePost", "BufRead" }, {
  pattern = "*.brs",
  callback = callback,
})

Expected behavior

No syntax highlights applied *.brs files

Actual behavior

Syntax highlighting is correct when finding files.

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()]]
jamestrew commented 1 year ago

I think this is due to previewers actually not opening files as buffers so the events for your autocmd doesn't actually trigger. The contents are just read and nvim_buf_set_lines'ed into the previewer buffer. This is done for performance reasons (LSPs attaching to buffers, arbitrary autocmds, etc).

I'm not sure if there's a workaround for this. I thought maybe using vim.filetype.add with/without ftplugin might work but it doesn't in my testing.

boydkelly commented 1 year ago

Seem to be running into this as well. I just managed to get a custom search going and it works great. Just no syntax highlighting.

I have txt, adoc, md, yet none show any syntax highlighting. Which for my use would be very useful. Note that live_grep does work fine. Just that I have to be a bit more lazy and retype the word....

Thanks for any eventual work around or solution!!!!

function M.manden_string()
  require("utils.functions").SetCALayout()
  require("telescope.builtin").grep_string {
    file_ignore_patterns = {
      ".xhtml",
      ".svg",
      ".idx",
      ".dat",
      ".dic",
      ".all.txt",
      ".delete-sed.txt",
    },
    cwd = "$HOME/Documents/Jula/search",
    path_display = { "hidden" },
    prompt_title = "Mandenkan",
    -- preview_title = false,
    layout_strategy = "vertical",
    layout_config = {
      -- preview_cutoff = 120,
      vertical = {
        height = vim.o.lines,
        width = vim.o.columns,
        prompt_position = "top",
        preview_height = 0.4,
        mirror = true,
      },
    },
  }
end
Conni2461 commented 1 year ago

so yeah, we now changed our filetype detection on latest master from plenary.filetype to vim.filetype so we should be able to correctly apply highlighting if you register your filetypes using vim.filetype.add (which is the preferred way anyway).

We will never run "BufNewFile", "BufFilePost", "BufRead" autocommands for previewer, because these autocommands could have unwanted side effects and negative performance impact.

Also please read :help telescope.changelog-2529

jamestrew commented 1 year ago

I don't think we run the on_detect function returned from vim.filetype.match so you have to return set the returning filetype as the filetype of the syntax you want but something like this will work.

vim.filetype.add({
  extension = {
    brs = function(path, bufnr)
      return "vb", function(bufnr) -- return ft="vb" here to get "vb" syntax highlighting in telescope previewer
        vim.bo[bufnr].filetype = "brs" -- re-set filetype to "brs" if preferred
        vim.bo[bufnr].syntax = "vb"
        -- more settings
      end
    end
  }
})
jamestrew commented 1 year ago

@boydkelly markdown syntax highlight works for me using that custom grep_string function image with this min 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

  vim.api.nvim_create_user_command("TestGS", function()
    require("telescope.builtin").grep_string({
      file_ignore_patterns = {
        ".xhtml",
        ".svg",
        ".idx",
        ".dat",
        ".dic",
        ".all.txt",
        ".delete-sed.txt",
      },
      -- cwd = "$HOME/Documents/Jula/search",
      path_display = { "hidden" },
      prompt_title = "Mandenkan",
      -- preview_title = false,
      layout_strategy = "vertical",
      layout_config = {
        -- preview_cutoff = 120,
        vertical = {
          height = vim.o.lines,
          width = vim.o.columns,
          prompt_position = "top",
          preview_height = 0.4,
          mirror = true,
        },
      },
    })
  end, {})
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()]])
casonadams commented 1 year ago

I don't think we run the on_detect function returned from vim.filetype.match so you have to return set the returning filetype as the filetype of the syntax you want but something like this will work.

vim.filetype.add({
  extension = {
    brs = function(path, bufnr)
      return "vb", function(bufnr) -- return ft="vb" here to get "vb" syntax highlighting in telescope previewer
        vim.bo[bufnr].filetype = "brs" -- re-set filetype to "brs" if preferred
        vim.bo[bufnr].syntax = "vb"
        -- more settings
      end
    end
  }
})

Thanks!

However, now when the previewer tries to display a .brs file. It says it can't display binary files. Not sure how to get around that.

.brs is not a binary it is the extension for Roku Brightscript.

jamestrew commented 1 year ago

You probably just have to update telescope

boydkelly commented 1 year ago

@boydkelly markdown syntax highlight works for me using that custom grep_string function with this min config

hmmm. Maybe my issue is a bit different. So its not actually the 'Grep Preview window but the Results window where the search term is not highlighted. This example is an adoc file. First image with live_grep, second with grep_string. Is this somehow an issue with the highlight group?

Screenshot from 2023-06-13 08-06-54

Screenshot from 2023-06-13 08-14-07

Conni2461 commented 1 year ago

Thats a completly different issue and we are currently working on that

casonadams commented 1 year ago

You probably just have to update telescope

I am running the latest version

jamestrew commented 1 year ago

Are you sure? I'm able to get a proper file preview with syntax highlighting using the minimal config + the vim.filetype.add from above.

image

casonadams commented 1 year ago

It appears there is a commit that fixes unknown file types just recently. So this is resolved.