Coder Social home page Coder Social logo

Comments (5)

Bodleum avatar Bodleum commented on September 26, 2024

I think I can tackle this next week.
The only hurdle I can think of right now is we would want event to be null or string or list of string just like autoCmd.*.event, and in the list of string case we would want the keymap to be registered in both groupings of event and I'm not sure how to implement that.

The basic string case I'm pretty sure I can get going though!

from nixvim.

MattSturgeon avatar MattSturgeon commented on September 26, 2024

I think I can tackle this next week.

👀 If you run into issues or loose interest, I'm also tempted to work on this

in the list of string case we would want the keymap to be registered in both groupings of event and I'm not sure how to implement that.

I hadn't thought about lists of events, that makes sense...

Would something like this work?

Draft one
eventKeys = let
  # The mapping function converts a keymap
  # with a _list of events_ into a _list
  # of keymaps_, each with a _single_ event.
  mapper = keymap:
    if isList keymap.event
    then map (event: keymap // {inherit event;}) keymap.event
    else [keymap];

  # Filter for keymaps with events
  # this could be made more efficient by
  # making the mapper handle null events
  matched = filter (k: k ? "event" && k.event != null) cfg.keymaps;

  # Map using mapper, then flatten the result
  # this is a flat list of keymaps,
  # each with a str-type event
  flattened = concatMap mapper matched;

  # Group by event
  grouped = groupBy (k: k.event) flattened;
  
in grouped;

# keymaps without event
globalKeys = filter (k: !(k ? "event") || k.event == null) cfg.keymaps;

EDIT: A better approach might use foldAttrs instead of groupBy, since we probably want to also filter out the event attribute:

eventKeymaps = let
  inherit (builtins) concatMap isList isString map removeAttrs;
  inherit (lib) foldAttrs;

  # map a keymap into a list of pairs.
  # only include keymaps that are associated with an event
  # flatten keymaps associated with multiple events
  # i.e. [{event=keymap}]
  toPairs = attrs@{ event ? null, ... }: let
      # TODO could convert to lua table at this point...
      keymap = removeAttrs attrs ["event"];
    in
      if event == null then []
      else if isString event then [{ ${event} = keymap; }]
      else map (e: { ${e} = keymap; }) event;

  # a list of pairs
  # i.e. [{event=keymap}]
  pairs = concatMap toPairs cfg.keymaps;

  # group pairs into a single set
  # i.e. {event=[keymap]}
  grouped = foldAttrs (val: acc: acc ++ [val]) [] pairs;
in grouped

I threw this together on my phone so forgive any mistakes, it's mostly to demonstrate the concept.

from nixvim.

MattSturgeon avatar MattSturgeon commented on September 26, 2024

I think the general solution of adding event to keymaps that I proposed above is probably not the right approach... Similar to #1214; it adds too much magic, on top of how neovim does things.

For LSPs we could instead make use of the existing onAttach option (__lspOnAttach func).

For a general solution, perhaps auto commands could support recieving a NixVim config in a similar way to the files option? Maybe this should have its own issue...

from nixvim.

Bodleum avatar Bodleum commented on September 26, 2024

I've got some working code for this already which I was going to tidy up today, but I hear what you're saying.
What do the maintainers think @pta2002 , @traxys, @GaetanLepage?

from nixvim.

traxys avatar traxys commented on September 26, 2024

I think it would depend on how much it is applicable to different kind of plugins. If you have something maybe you can try to open a PR, but I won't guarantee that we would integrate this in nixvim.

from nixvim.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.