SmiteshP / nvim-navic

Simple winbar/statusline plugin that shows your current code context
Apache License 2.0
1.35k stars 49 forks source link

ref: extract data formatting into own function #127

Closed leon-richardt closed 11 months ago

leon-richardt commented 11 months ago

Hello!

Thank you for the plugin, it has been very pleasant to use.

Motivation

When working in C++, I often find myself in nested namespaces two or three levels deep. Using nvim-navic's native get_location() takes up quite a bit of my lualine in that scenario. To reduce some of that space usage, I wrote a function to parse get_data()'s output and combine namespace components into a single component. However, there was no way to format that data in the same way it would have been rendered by get_location().

Solution

This PR extracts formatting of the data into its own function such that a caller can format their own data in the same way that get_location() would.

Example

lualine usage example for the namespace scenario discussed above ```lua lualine.setup { sections = { lualine_c = { { -- Customized navic.get_location() that combines namespaces -- into a single string function() local navic = require("nvim-navic") local old_data = navic.get_data() local new_data = {} local cur_ns = nil local ns_comps = {} for _, comp in ipairs(old_data) do if comp.type == "Namespace" then cur_ns = comp table.insert(ns_comps, comp.name) else -- On the first non-namespace component $c$, collect -- previous NS components into a single one and -- insert it in front of $c$. if cur_ns ~= nil then -- Concatenate name and insert local num_comps = #ns_comps local comb_name = "" for idx = 1, num_comps do local ns_name = ns_comps[idx] -- No "::" in front of first component local join = (idx == 1) and "" or "::" if idx ~= num_comps then comb_name = comb_name .. join .. ns_name:sub(1, 1) else comb_name = comb_name .. join .. ns_name end end cur_ns.name = comb_name table.insert(new_data, cur_ns) cur_ns = nil end table.insert(new_data, comp) end end return navic.format_data(new_data) end, cond = function() return package.loaded["nvim-navic"] and require("nvim-navic").is_available() end, }, }, }, } ```
SmiteshP commented 11 months ago

Also add small info snippet about this function to the README as well

leon-richardt commented 11 months ago

Also add small info snippet about this function to the README as well

I went ahead and tried to emulate the style of the existing info in the README. I also took the liberty to include the motivating example as well.