SmiteshP/nvim-gps

github github
plugin
star 141
stars
alert-circle 3
open issues
users 1
subscribers
git-branch 7
forks
CREATED

2021-08-22

UPDATED

2 days ago

packer

require('packer').startup(function()
  use 'SmiteshP/nvim-gps'
end)

paq

require "paq" { 
  'SmiteshP/nvim-gps'
}

๐Ÿ›ฐ๏ธ nvim-gps

Take this handy dandy gps with you on your coding adventures and always know where you are!

๐Ÿค” What is nvim-gps?

nvim-gps is a simple status line component that shows context of the current cursor position in file. It is similar to the statusline function provided by nvim-treesitter, but smarter. Using custom treesitter queries for each language, nvim-gps is able to show exact name of containing class, struct, function, method, etc. along with some fancy symbols!

example

Here is a barebones example how it looks in a statusline

nvim-gps-barebone-demo

Here is a example of how it can look in a fully configured statusline

nvim-gps-demo

โœ… Supported Languages

  • Bash (and Zsh)
  • C
  • C++
  • Elixir
  • Fennel
  • Go
  • Html
  • Java
  • Javascript (and jsx)
  • Lua
  • Ocaml
  • Python
  • Ruby
  • Rust
  • Typescript (and tsx)

โšก๏ธ Requirements

๐Ÿ“ฆ Installation

Install the plugin with your preferred package manager:

packer

-- Lua
use {
    "SmiteshP/nvim-gps",
    requires = "nvim-treesitter/nvim-treesitter"
}

vim-plug

" Vimscript
Plug "nvim-treesitter/nvim-treesitter"
Plug "SmiteshP/nvim-gps"

โš™๏ธ Configuration

nvim-gps provides a setup function that takes in a table with configuration options. The default configuration assumes that you are using a nerd font.

Note: setup function needs to be called once for nvim-gps to work.

-- Lua

-- Default config
require("nvim-gps").setup()
-- Lua

-- Customized config
require("nvim-gps").setup({
    icons = {
        ["class-name"] = '๏ – ',      -- Classes and class-like objects
        ["function-name"] = '๏ž” ',   -- Functions
        ["method-name"] = '๏šฆ ',     -- Methods (functions inside class-like objects)
        ["container-name"] = 'โ›ถ ',  -- Containers (example: lua tables)
        ["tag-name"] = '๏งป'         -- Tags (example: html tags)
    },
    -- Add custom configuration per language or
    -- Disable the plugin for a language
    -- Any language not disabled here is enabled by default
    languages = {
        -- ["bash"] = false, -- disables nvim-gps for bash
        -- ["go"] = false,   -- disables nvim-gps for golang
        -- ["ruby"] = {
        --    separator = '|', -- Overrides default separator with '|'
        --    icons = {
        --        -- Default icons not specified in the lang config
        --        -- will fallback to the default value
        --        -- "container-name" will fallback to default because it's not set
        --        ["function-name"] = '',    -- to ensure empty values, set an empty string
        --        ["tag-name"] = ''
        --        ["class-name"] = '::',
        --        ["method-name"] = '#',
        --    }
        --}
    },
    separator = ' > ',
})

๐Ÿš€ Usage

nvim-gps doesn't modify your statusline by itself, instead you are provided with two functions and it is left up to you to incorporate them into your statusline.

-- Lua
local gps = require("nvim-gps")

gps.is_available()  -- Returns boolean value indicating whether a output can be provided
gps.get_location()  -- Returns a string with context information

Few examples below

feline

-- Lua
table.insert(components.left.active, {
    provider = function()
        return require("nvim-gps").get_location()
    end,
    enabled = function()
        return require("nvim-gps").is_available()
    end
})

galaxyline

-- Lua
local gps = require("nvim-gps")

require('galaxyline').section.left[1]= {
    nvimGPS = {
        provider = function()
            return gps.get_location()
        end,
        condition = function()
            return gps.is_available()
        end
    }
}

lualine

-- Lua
local gps = require("nvim-gps")

require("lualine").setup({
    sections = {
            lualine_c = {
                { gps.get_location, condition = gps.is_available },
            }
    }
})

vimscript

" vimscript
func! NvimGps() abort
    return luaeval("require'nvim-gps'.is_available()") ?
        \ luaeval("require'nvim-gps'.get_location()") : ''
endf

set statusline+=%{NvimGps()}

windline

-- Lua
local gps = require("nvim-gps")

comps.gps = {
    function()
        if gps.is_available() then
            return gps.get_location()
        end
        return ''
    end,
    {"white", "black"}
}

๐Ÿ”ฅ Contributions

Is your favorite language not supported? Or did you find something not being captured by the nvim-gps? Please consider opening a issue, or even better make a PR and solve the issue!! ๐Ÿ˜„

Please read the CONTRIBUTING.md to understand how the treesitter queries work and how you can add/enhance the queries for your favorite programming languages!