Skip to content
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

Customizable formatters #1

Open
Gelio opened this issue Oct 13, 2023 · 7 comments
Open

Customizable formatters #1

Gelio opened this issue Oct 13, 2023 · 7 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@Gelio
Copy link
Owner

Gelio commented Oct 13, 2023

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 Gelio added enhancement New feature or request good first issue Good for newcomers labels Oct 13, 2023
@chuan2984
Copy link

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

@Gelio
Copy link
Owner Author

Gelio commented Mar 18, 2024

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 😄

@chuan2984
Copy link

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

@Gelio
Copy link
Owner Author

Gelio commented Mar 18, 2024

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 🙂

@chuan2984
Copy link

chuan2984 commented Mar 18, 2024

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

@Gelio
Copy link
Owner Author

Gelio commented Mar 19, 2024

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 😅

@chuan2984
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants