David-Kunz / markid

A Neovim extension to highlight same-name identifiers with the same color.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

markid

A Neovim extension to highlight same-name identifiers with the same color.

Motivation

Syntax highlighting is mostly based on element kinds of the abstract syntax tree. This sometimes leads to different visual representations of the same variable, consider this example:

Here, myParam has the colors yellow and white, making it hard for the developer to recognise that both represent the same thing.

Now with markid, it's ensured that same-name identifiers are represented with the same color:

Here's a slightly more complicated example, try to track the flow of qux:

Installation

Requirements: nvim-treesitter including a parser for your language

For vim-plug:

Plug 'David-Kunz/markid'

For packer:

use 'David-Kunz/markid'

Enable the nvim-treesitter plugin:

require'nvim-treesitter.configs'.setup {
  markid = { enable = true }
}

Options

These are the configuration options (with defaults):

local m = require'markid'
require'nvim-treesitter.configs'.setup {
  markid = {
    enable = true,
    colors = m.colors.medium,
    queries = m.queries,
    is_supported = function(lang)
      local queries = configs.get_module("markid").queries
      return pcall(vim.treesitter.parse_query, lang, queries[lang] or queries['default'])
    end
  }
}

M.colors = {
    dark = { "#619e9d", "#9E6162", "#81A35C", "#7E5CA3", "#9E9261", "#616D9E", "#97687B", "#689784", "#999C63", "#66639C" },
    bright = {"#f5c0c0", "#f5d3c0", "#f5eac0", "#dff5c0", "#c0f5c8", "#c0f5f1", "#c0dbf5", "#ccc0f5", "#f2c0f5", "#98fc03" },
    medium = { "#c99d9d", "#c9a99d", "#c9b79d", "#c9c39d", "#bdc99d", "#a9c99d", "#9dc9b6", "#9dc2c9", "#9da9c9", "#b29dc9" }
}

m.queries = {
  default = '(identifier) @markid',
  javascript = [[
          (identifier) @markid
          (property_identifier) @markid
          (shorthand_property_identifier_pattern) @markid
        ]]
}
m.queries.typescript = m.queries.javascript

The m.queries table above can be used to define language-specific highlighting rules via custom Treesitter queries. Alternatively, markid can also source queries from standalone files located in your local runtime queries/ directory. Simply create a new directory in your nvim config folder for your language of choice, e.g. $HOME/.config/nvim/queries/python, and write your query in a file called markid.scm

Custom Highlight Groups

For more control, you can define the highlight groups markid1, markid2, ..., markid10, this is especially useful for theme designers.

Example:

vim.api.nvim_set_hl(0, 'markid1', { fg = '#c99d9d', bg = '#003844', underline = true })
vim.api.nvim_set_hl(0, 'markid2', { fg = '#c9a99d', bg = '#003844', underline = true })
-- ...
vim.api.nvim_set_hl(0, 'markid10', { fg = '#c9b79d', bg = '#003844', underline = true })

About

A Neovim extension to highlight same-name identifiers with the same color.

License:MIT License


Languages

Language:Lua 100.0%