SmiteshP / nvim-gps

Simple statusline component that shows what scope you are working inside
Apache License 2.0
479 stars 46 forks source link

Need help with statusline. #80

Closed dharmx closed 2 years ago

dharmx commented 2 years ago

I was trying to integrate this to my custom statusline and at first it was working perfectly fine:

local M = {}

local config = require("statusline.config").treesitter

function M.treesitter()
  local format = "%#Statusline#"
  if _G.packer_plugins and _G.packer_plugins["nvim-gps"].loaded then
    local scopes = require "nvim-gps"
    local fallback = string.format(
      "%%#StatusLineScopesOFF#%s%%#StatusLineScopes#%%#StatusLineScopesReverse#%s%%#Statusline#",
      config.disabled_icon,
      config.style.left
    )

    local location = string.format(
      "%%#StatusLineScopesON#%s%%#StatusLineScopes#%s%%#StatusLineScopesReverse#%s%%#Statusline#",
      config.enabled_icon,
      scopes.get_location(),
      config.style.left
    )

    format = scopes.is_available() and location or fallback
  end

  return format
end

return M

But the problem with the above snippet is that it throws error for filetypes that do not have a treesitter parser.

E5108: Error executing lua ...nvim/site/pack/packer/opt/nvim-gps/lua/nvim-gps/init.lua:358: bad argument #1 to 'pairs' (table expected, got string)
stack traceback:
        [C]: in function 'pairs'
        ...nvim/site/pack/packer/opt/nvim-gps/lua/nvim-gps/init.lua:358: in function 'get_location'
        ...maker/.config/nvim/lua/statusline/modules/treesitter.lua:25: in function 'treesitter'
        /home/maker/.config/nvim/lua/statusline/init.lua:29: in function </home/maker/.config/nvim/lua/statusline/init.lua:20>

I used a workaround to get through this by the way:

local parser_location = stdpath "data" .. string.format("/site/pack/packer/opt/nvim-treesitter/parser/%s.so", bo.filetype)
if not exists(parser_location) then return format end

Now everything was fine for a solid 3 days, until I opened a markdown file. Now, it again threw that same error. So, I verified if a parser was installed, and it was. To workaround this I excluded mardown completely.

StatusLine = function(state)
  set_colors(api.nvim_get_mode().mode)
  local combined = "%#StatusLineInactive#%=" .. modules.filename(true) .. "%="
  if state == "active" then
    local ok, ts = pcall(modules.treesitter)
    local left = table.concat {
      modules.mode(),
      modules.dirname(),
      modules.filename(),
      ok and ts or "%#Statusline#",
    }
    local right = table.concat {
      "%=",
      modules.diagnostics(),
      modules.lsp(),
      modules.git(),
      modules.position(),
    }
    combined = left .. right
  elseif state == "inactive" then
    combined = "%#StatuslineNC#"
  end
  return combined
end

Additional Information

Neovim version

NVIM v0.7.0-dev+1282-g2ab52bd98
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions     -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security     -fstack-clash-protection -fcf-protection -Wp,-U_FORTIFY_SOURCE -Wp,-D_FORTIFY_SOURCE=1 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -O2 -g -Og -g -Wall -Wextra -pedantic
-Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/home/maker/.cache/yay/neovim-git/src/build/config -I/home/maker/.cache/yay/neovim-git/src/neovim-git/src -I/usr/include -I/home/maker/.cache/yay/neovim-git/src/build/src/nvim/auto -I/home/maker/.cache/yay/neovim-git/src/build/include
Compiled by maker

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

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

Run :checkhealth for more info

Lazy loading treesitter using packer

use {
  "nvim-treesitter/nvim-treesitter",
  wants = {
    "nvim-treesitter-refactor",
    "nvim-ts-rainbow",
    "nvim-treesitter-textobjects",
    "playground",
    "nvim-ts-autotag",
    "nvim-treesitter-context",
    "nvim-gps",
    "spellsitter.nvim",
  },
  config = function()
    require "configs.treesitter"
  end,
  opt = true,
}

use {
  "SmiteshP/nvim-gps",
  opt = true,
  config = function()
    require "configs.treesitter.plugins.gps"
  end,
}

GPS Config

local present, gps = pcall(require, "nvim-gps")

if not present then
  return
end

local config = {
  disable_icons = false,
  icons = {
    ["class-name"] = " ",
    ["function-name"] = " ",
    ["method-name"] = " ",
    ["container-name"] = " ",
    ["tag-name"] = " ",
  },
  languages = {
    ["json"] = {
      icons = {
        ["array-name"] = " ",
        ["object-name"] = " ",
        ["null-name"] = " ",
        ["boolean-name"] = " ",
        ["number-name"] = " ",
        ["string-name"] = " ",
        ["float-name"] = " ",
      },
    },
    ["toml"] = {
      icons = {
        ["table-name"] = " ",
        ["array-name"] = " ",
        ["boolean-name"] = " ",
        ["date-name"] = "﬑ ",
        ["date-time-name"] = " ",
        ["float-name"] = " ",
        ["inline-table-name"] = " ",
        ["integer-name"] = " ",
        ["string-name"] = " ",
        ["time-name"] = " ",
      },
    },
    ["verilog"] = { icons = { ["module-name"] = "﬙ " } },
    ["yaml"] = {
      icons = {
        ["mapping-name"] = " ",
        ["sequence-name"] = " ",
        ["null-name"] = " ",
        ["boolean-name"] = " ",
        ["integer-name"] = " ",
        ["float-name"] = " ",
        ["string-name"] = " ",
      },
    },
    ["bash"] = false,
    ["sh"] = false,
    ["fish"] = false,
    ["zsh"] = false,
    ["perl"] = false,
    ["ruby"] = {
      separator = "▌",
      icons = {
        ["function-name"] = " ",
        ["tag-name"] = "ﮉ ",
        ["class-name"] = " ",
        ["method-name"] = " ",
      },
    },
  },
  separator = "  ",
  depth = 7,
  depth_limit_indicator = "..",
}

gps.setup(config)

Other Information

My statusline configuration here

Video demo

https://user-images.githubusercontent.com/80379926/159715051-16feec94-48cb-48cc-b1ea-f95efdbe9453.mp4

I am not really sure if this is a bug or, not so, I would love to have some insight! Maybe a treesitter API utility function that looks if the parser supports X or, not(?)

SmiteshP commented 2 years ago

I haven't looked at the config closely, but this error

E5108: Error executing lua ...nvim/site/pack/packer/opt/nvim-gps/lua/nvim-gps/init.lua:358: bad argument #1 to 'pairs' (table expected, got string)
stack traceback:

will occur only when the get_location function is called when is_available is false.

SmiteshP commented 2 years ago

But the problem with the above snippet is that it throws error for filetypes that do not have a treesitter parser.

You could try replacing the call to get_location with a custom function like this

local functionl my_gps()
    if gps.is_available() then
        return gps.get_location()
    else
        return ""
    end
end
dharmx commented 2 years ago

This is so embarassing! I wish I could delete this issue!!! This is the code now... so much shorter.

function M.treesitter()
  local fallback = "%%#StatusLineScopesOFF#%s%%#StatusLineScopes#%%#StatusLineScopesReverse#%s%%#Statusline#"
  local available = "%%#StatusLineScopesON#%s%%#StatusLineScopes#%s%%#StatusLineScopesReverse#%s%%#Statusline#"
  if packer_plugins and packer_plugins["nvim-gps"].loaded then
    local scopes = require "nvim-gps"
    return scopes.is_available()
        and string.format(available, config.enabled_icon, scopes.get_location(), config.style.left)
      or string.format(fallback, config.disabled_icon, config.style.left)
  end
  return "%#Statusline#"
end

Thanks for the quick response by the way! Closing...

SmiteshP commented 2 years ago

Haha no worries 😄