cmp-natdat icon indicating copy to clipboard operation
cmp-natdat copied to clipboard

Customizable formatters

Open Gelio opened this issue 2 years ago • 7 comments

Let's refactor the way formatting is done to allow users to plug in their own formatters if they do not like the default formatting.

This would involve:

  1. Creating a natdat.Formatter type with methods for formatting different structs

    ---@class natdat.Formatter
    ---@field format_month fun(month: natdat.Month): string
    ---@field format_absolute_date fun(absolute_date: natdat.AbsoluteDate): string
    ---...
    
  2. Creating 2 formatters:

    • ISOFormatter with methods from existing format_iso
    • LabelFormatter with methods from existing format_original
  3. Replacing the format_iso and format_original methods on structures with a single format fun(formatter: natdat.Formatter): string. Each such method would call a corresponding formatter method.

  4. Accepting a formatter for the insertText in the cmp_natdat source. By default, it would be the ISOFormatter

Gelio avatar Oct 13 '23 11:10 Gelio

Hello @Gelio, is this still being worked on? I really need a way to auto-complete dates in a specific format for me since im using neovim with obsidian. I may be able to contribute since this seems entry level enough for me to learn when i get a chance

chuan2984 avatar Mar 17 '24 23:03 chuan2984

Hey hey, thanks for reaching out! I didn't plan on working on it since the existing features cover my use-cases (including Obsidian, which I am also using).

Feel free to create a PR if you manage to get it working. I can also fill in some bits that you are unsure about. Don't hesitate to ask questions.

By the way, what date format are you looking for when working with Obsidian? I'm asking since I'm curious and maybe I am missing some cool plugin 😄

Gelio avatar Mar 18 '24 08:03 Gelio

Sure, once i put some time together this week or the next, ill see if i can put in the forrmatter. Thanks for being so responsive! Im not using any cool plugin. If anything im trying to keep it as the bare minimum since Im trying to use nvim to write those daily notes, already modified it to hide all the dataview stuff. Im just using YYYY-MM-DD-dddd as the daily title since i want to see what day of the week, guess i could also just alias my daily notes too. I also need the weekly dates like 2024-03-W10

chuan2984 avatar Mar 18 '24 16:03 chuan2984

I see. What are you looking to get from custom formatters in cmp-natdat?

I guess you could configure the day formatter to also output the dddd part, but I am not sure how you would get 2024-03-W10 from one of the existing completions.

I am asking to understand the problem you are having and to also understand whether adding custom formatters will help you solve it.

If you could add an example Markdown file and places where you expect these completions (whether they are inside [[ links, or inside headings), that would make it easier.

I wouldn't want to you spend time contributing a feature that ends up not solving your problem 🙂

Gelio avatar Mar 18 '24 19:03 Gelio

Yes, the dddd part for sure. Im unsure how the timestamp is served by cmp-natdat, but if it follows ISO 8601, i should just as easily be able to format a time into the YYYY-ww format, right? This format is not going to be super helpful to me anyway, was just trying to get my feet wet into lua plugins for neovim, and this seems like a good opportunity thats not very overwhelming.

Can you give me an example for the markdown solution? Thanks

chuan2984 avatar Mar 18 '24 21:03 chuan2984

Im unsure how the timestamp is served by cmp-natdat, but if it follows ISO 8601, i should just as easily be able to format a time into the YYYY-ww format, right?

Yeah, you could change it so that any date will turn to YYYY-{W}ww, so e.g. typing @today will give you the 2024-W12, but so would @yesterday, since they are both in the same week. I am not sure that it is too useful as an nvim-cmp source. If I were you and I needed a way to type the current YYYY-{W}ww quickly, I would probably add some snippet (e.g. using https://github.com/L3MON4D3/LuaSnip). I already have snippets for some date formats, e.g.:

image

Besides, if you wanted to have cmp-natdat print out dates in the YYYY-{W}ww format, it would be doing that all the time, until you call .setup again with a different formatter. If you wanted to hot-swap the formatter between ISO dates (YYYY-MM-DD) and ISO weeks (YYYY-{W}ww), you would have to re-register the source, which I am not sure is convenient.

All in all, I am not sure implementing this feature (#1) will solve your problems, but I don't mind you trying anyway 😄

Can you give me an example for the markdown solution? Thanks

I'm not following what you are asking for here 😅

Gelio avatar Mar 19 '24 09:03 Gelio

Hey, thanks again for being so helpful. Your lua-snip suggestion makes a lot more sense. I didn't think about the snippet engine. And you are absolutely right about the formatter that once its set, you need to reload it. Dont worry about the followup, you answered it with luasnip. Thank you very much sir!

chuan2984 avatar Mar 19 '24 16:03 chuan2984