Gelio / cmp-natdat

nvim-cmp source to autocomplete natural dates and turm them into ISO dates
MIT License
24 stars 1 forks source link
nvim-cmp

cmp-natdat

A nvim-cmp source for turning natural dates into ISO dates.

Demo

https://github.com/Gelio/cmp-natdat/assets/889383/1d6d388d-2a10-4923-9156-b99764c5a342

Examples

Features

Setup

  1. Install the plugin

    Using lazy.nvim:

    { "Gelio/cmp-natdat", config = true }

    config = true is necessary so lazy.nvim calls require("cmp_natdat").setup() to register the source.

    Using packer.nvim:

    use {
       "Gelio/cmp-natdat",
       config = function()
            require("cmp_natdat").setup()
       end
    }
  2. Add the source to the list in your nvim-cmp configuration

    sources = {
       { name = "natdat" },
       --- other sources...
    }

Configuration (optional)

cmp-natdat accepts the following optional configuration, passed as a table to the setup() method:

Example:

{
    "Gelio/cmp-natdat",
    config = function()
        require("cmp_natdat").setup({
            cmp_kind_text = "NatDat",
            highlight_group = "Red",
        })
    end,
}

cmp-natdat completions in the nvim-cmp popup are labeled "NatDat" in red

To get the most out of the custom cmp kind text, you can also use lspkind.nvim to show the calendar icon (📆) for cmp-natdat completions:

require("lspkind").init({
    symbol_map = {
        NatDat = "📅",
    },
})

cmp-natdat completions use the calendar icon

WARNING: cool tech inside

Parsing the dates is done using pcomb, a Lua parser combinator library. Its API is inspired by Rust's nom crate.

pcomb can also be used in other plugins to parse other text input into a more structured format. It is flexible and makes it easy to build parsers from the bottom-up:

---@type pcomb.Parser<{ [1]: integer, [2]: pcomb.NIL | integer }>
local day_of_month_and_opt_year = psequence.sequence({
    -- Day of month
    pcharacter.integer,
    pcombinator.opt(psequence.preceded(
        pcharacter.multispace1,
        -- Year
        pcharacter.integer,
    ))
})

Contributing

See CONTRIBUTING.md.