Skip to content
This repository has been archived by the owner on Jul 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #101 from wookayin/easy-type
Browse files Browse the repository at this point in the history
Make get_data() and get_location() returns nil if not available
  • Loading branch information
SmiteshP authored May 29, 2022
2 parents b77453b + 6a030da commit 8f95088
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,14 @@ nvim-gps doesn't modify your statusline by itself, instead you are provided with
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
gps.get_location() -- Returns a string with context information (or nil if not available)

-- example output: "mystruct > sum"
```


<details>
<summary> You can also pass optional arguments to <code>get_location</code> function to override options given in setup function </summary>
<summary> You can also pass optional arguments to <code>get_location</code> function to override options given in setup function: </summary>

```lua
opts = {
Expand All @@ -229,15 +232,15 @@ gps.get_location(opts)

</details>

These two functions should satisfy the needs of most users, however if you want the raw intermediate data for custom usage you can use the following function
These two functions should satisfy the needs of most users, however if you want the raw intermediate data for custom usage you can use the following function:

```lua
gps.get_data() -- Returns an intermediate representation of data (which is used by get_location)
-- Table of tables that contain 'text', 'type' and 'icon' for each context
gps.get_data() -- Returns a table of intermediate representation of data (which is used by get_location)
-- Table of tables that contain 'text', 'type' and 'icon' for each context
```

<details>
<summary> example output of <code>get_data</code> function </summary>
<summary>An example output of <code>get_data</code> function: </summary>

```lua
{
Expand All @@ -256,12 +259,14 @@ gps.get_data() -- Returns an intermediate representation of data (which is

</details>

Few examples below


## Examples of Integrating with Other Plugins

### [feline](https://github.com/famiu/feline.nvim)

<details>
<summary> example feline setup </summary>
<summary>An example feline setup </summary>

```lua
-- Lua
Expand All @@ -282,7 +287,7 @@ table.insert(components.active[1], {
### [galaxyline](https://github.com/glepnir/galaxyline.nvim)

<details>
<summary> example galaxyline setup </summary>
<summary>An example galaxyline setup </summary>

```lua
-- Lua
Expand All @@ -305,7 +310,7 @@ require('galaxyline').section.left[1]= {
### [lualine](https://github.com/hoob3rt/lualine.nvim)

<details>
<summary> example lualine setup </summary>
<summary>An example lualine setup </summary>

```lua
-- Lua
Expand Down
16 changes: 10 additions & 6 deletions lua/nvim-gps/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ local function setup_language_configs()
}
end

local data_cache_value = ""
local location_cache_value = ""
local data_cache_value = nil -- table
local location_cache_value = "" -- string
local data_prev_loc = {0, 0}
local location_prev_loc = {0, 0}
local setup_complete = false
Expand Down Expand Up @@ -290,7 +290,7 @@ local update_tree = ts_utils.memoize_by_buf_tick(function(bufnr)
return parser:parse()
end)

-- returns the data in table format
---@return table|nil the data in table format, or nil if gps is not available
function M.get_data()
-- Inserting text cause error nodes
if vim.api.nvim_get_mode().mode == 'i' then
Expand All @@ -311,7 +311,7 @@ function M.get_data()
local config = configs[filelang]

if not gps_query then
return "error"
return nil
end

-- Request treesitter parser to update the syntax tree for the current buffer.
Expand All @@ -325,7 +325,7 @@ function M.get_data()
local function add_node_data(pos, capture_name, capture_node)
local text = ""

if vim.fn.has("nvim-0.7") then
if vim.fn.has("nvim-0.7") > 0 then
text = vim.treesitter.query.get_node_text(capture_node, 0)
if text == nil then
return data_cache_value
Expand Down Expand Up @@ -389,7 +389,7 @@ function M.get_data()
return data_cache_value
end

-- Returns the pretty statusline component
---@return string|nil the pretty statusline component, or nil if not available
function M.get_location(opts)
if vim.api.nvim_get_mode().mode == 'i' then
return location_cache_value
Expand All @@ -406,6 +406,10 @@ function M.get_location(opts)
local config = configs[filelang]
local data = M.get_data()

if not data then
return nil
end

local depth = config.depth
local separator = config.separator
local disable_icons = config.disable_icons
Expand Down

0 comments on commit 8f95088

Please sign in to comment.