Skip to content

nvim-cmp source to autocomplete natural dates and turm them into ISO dates

License

Notifications You must be signed in to change notification settings

Gelio/cmp-natdat

Repository files navigation

cmp-natdat

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

Demo

2023-10-13.13-05-51.mp4

Examples

  • @now -> 2023-10-13 12:38
  • @tomorrow -> 2023-10-14
  • @last Friday 2pm -> 2023-10-06 14:00
  • @Oct 8 2021 -> 2021-10-08
  • @today 14:20 -> 2023-10-13 14:20

Features

  • autocomplete for partially typed month names, relative dates
  • supported formats:
    • now
    • yesterday, today, tomorrow with optional time
    • days of week (Monday -> Sunday), with optional last/next modifier and time
    • full dates: month, day, optional year, optional time
    • time: am/pm, or 24h format

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:

  • cmp_kind_text - the text to use as the completion item's label in the nvim-cmp completions popup.

    Default: Text

  • highlight_group - the name of an existing highlight group to use for that completion item's label in the nvim-cmp completions popup.

    Default: CmpItemKindText

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.

About

nvim-cmp source to autocomplete natural dates and turm them into ISO dates

Topics

Resources

License

Stars

Watchers

Forks