Skip to content

feat: add indent guides as configurable component, closes #44 #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 95 additions & 3 deletions doc/neo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ Mappings .................... |neo-tree-mappings|
Filter .................... |neo-tree-filter|
Configuration ............... |neo-tree-configuration|
Setup ..................... |neo-tree-setup|
Component Configs ......... |neo-tree-component-configs|
Git Status ................ |neo-tree-git-status|
Diagnostics ............... |neo-tree-diagnostics|
Indent markers ............ |neo-tree-indent-markers|
Highlights ................ |neo-tree-highlights|
Events .................... |neo-tree-events|
Components and Renderers .. |neo-tree-renderers|
Expand Down Expand Up @@ -323,7 +325,36 @@ you still may want to dump it to a blank .lua file just to read it as
documentation.


GIT STATUS *neo-tree-git-status*
COMPONENT CONFIGS *neo-tree-component-configs*

The visual display of a node is made up of a series of components rendered in a
certain order and with certain configuration options. See |neo-tree-components|
for a deeper dive into customizing this aspect. If you wish to configure those
components in a universal way, the best place to do that is in the
`default_component_configs` section of the config.

For example, to add indent markers, you can apply your settings in each renderer
for each source, or just do it once in the default_component_configs section:

>
require("neo-tree").setup({
default_component_configs = {
indent = {
with_markers = true,
indent_marker = "│",
last_indent_marker = "└",
indent_size = 2,
},
},
})
<
See |neo-tree-indent-markers| for more details.

The default config has more examples of component configuration, use
|NeoTreePasteConfig| to view that default config.


GIT STATUS *neo-tree-git-status*

By default, Neo-tree will attempt to get the git status for files in the
current directory. It will use this information to add markers to the right of
Expand All @@ -337,7 +368,7 @@ component of your renderer(s).
See also: |neo-tree-git-status-source|


DIAGNOSTICS *neo-tree-diagnostics*
DIAGNOSTICS *neo-tree-diagnostics*

By default, Neo-tree will display diagnostic symbols next to files. It will
display the highest severity level for files, and errors only for directories.
Expand All @@ -359,6 +390,62 @@ To disable this feature entirely, set `enable_diagnostics = false` in your
config when calling the setup function.


INDENT MARKERS *neo-tree-indent-markers*

By default, indent markers (aka indent guides) are disabled. In Neo-tree
indent is a component, so to enable indent markers, you need configure the
`indent` component:

...at the global level:
>
require("neo-tree").setup({
default_component_configs = {
indent = {
with_markers = true,
indent_marker = "│",
last_indent_marker = "└",
indent_size = 2,
},
},
})
<

...or in each renderer:
>
require("neo-tree").setup({
filesystem = {
renderers = {
directory = {
{
"indent",
with_markers = true,
indent_marker = "│",
last_indent_marker = "└",
indent_size = 2,
},
-- other components
},
file = {
{
"indent",
with_markers = true,
indent_marker = "│",
last_indent_marker = "└",
indent_size = 2,
-- other components
},
}
}
})
<

You also can change the marker characters. To do this, you need change
`indent_marker` and `last_indent_marker` settings.

To change highlight of indent markers, you need configure `NeoTreeIndentMarker`
highlight group. By default, it refers to `Normal` highlight.


HIGHLIGHTS *neo-tree-highlights*

The following highlight groups are defined by this plugin. If you set any of
Expand All @@ -385,6 +472,8 @@ NeoTreeNormalNC |hi-NormalNC| override in Neo-tree window.
NeoTreeRootName The name of the root node.
NeoTreeTitleBar Used for the title bar of pop-ups, when the border-style
is set to "NC". This is derived from NeoTreeFloatBorder.
NeoTreeIndentMarker The style of indentation markers (guides). By default,
the "Normal" highlight is used.


EVENTS *neo-tree-events*
Expand Down Expand Up @@ -549,7 +638,10 @@ set of components.
Each component function is called with the following args:
`config` The config object defined in the renderer. This is how a component
can be made to be configurable. This is useful if you want different behavior
in a directory renderer vs a file renderer.
in a directory renderer vs a file renderer. The config is a combination of any
options specified in the default_component_configs
(|neo-tree-default-component-configs|), which can be overriden by settings
specified within each renderer config.

`node` The NuiNode object for this node. The properties can vary by source, but
each one will generally have at least id and name properties.
Expand Down
70 changes: 55 additions & 15 deletions lua/neo-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,47 @@ M.set_log_level = function(level)
log.set_level(level)
end

local function merge_global_components_config(components, config)
local indent_exists = false
local merged_components = {}
for _, component in ipairs(components) do
local name = component[1]
if type(name) == "string" then
if name == "indent" then
indent_exists = true
end
local merged = { name }
local global_config = config.default_component_configs[name]
if global_config then
for k, v in pairs(global_config) do
merged[k] = v
end
end
for k, v in pairs(component) do
merged[k] = v
end
table.insert(merged_components, merged)
else
log.error("component name is the wrong type", component)
end
end

-- If the indent component is not specified, then add it.
-- We do this because it used to be implicitly added, so we don't want to
-- break any existing configs.
if not indent_exists then
local indent = { "indent" }
for k, v in pairs(config.default_component_configs.indent or {}) do
indent[k] = v
end
table.insert(merged_components, 1, indent)
end
return merged_components
end

M.setup = function(config)
config = config or {}
local default_config = vim.deepcopy(defaults)
config = vim.deepcopy(config or {})
if config.log_level ~= nil then
M.set_log_level(config.log_level)
end
Expand All @@ -419,13 +458,13 @@ M.setup = function(config)
highlights.setup()

-- setup the default values for all sources
local source_defaults = {}
local merged_source_config = {}
for _, source_name in ipairs(sources) do
local source = utils.table_copy(defaults[source_name] or {})
local default_source_config = default_config[source_name]
local mod_root = "neo-tree.sources." .. source_name
source.components = require(mod_root .. ".components")
source.commands = require(mod_root .. ".commands")
source.name = source_name
default_source_config.components = require(mod_root .. ".components")
default_source_config.commands = require(mod_root .. ".commands")
default_source_config.name = source_name

--validate the window.position
local pos_key = source_name .. ".window.position"
Expand All @@ -444,26 +483,27 @@ M.setup = function(config)
end

-- Make sure all the mappings are normalized so they will merge properly.
normalize_mappings(source)
normalize_mappings(default_source_config)
normalize_mappings(config[source_name])

-- if user sets renderers, completely wipe the default ones
if utils.get_value(config, source_name .. ".renderers.directory") then
source.renderers.directory = {}
end
if utils.get_value(config, source_name .. ".renderers.file") then
source.renderers.file = {}
for name, _ in pairs(default_source_config.renderers) do
local user = utils.get_value(config, source_name .. ".renderers." .. name)
if user then
default_source_config.renderers[name] = nil
end
end
source_defaults[source_name] = source
end
local default_config = utils.table_merge(defaults, source_defaults)

-- apply the users config
M.config = utils.table_merge(default_config, config)

-- setup the sources with the combined config
for _, source_name in ipairs(sources) do
for name, rndr in pairs(M.config[source_name].renderers) do
M.config[source_name].renderers[name] = merge_global_components_config(rndr, M.config)
end
manager.setup(source_name, M.config[source_name], M.config)
manager.redraw(source_name)
end

local event_handler = {
Expand Down
Loading