olets / zsh-abbr

The zsh manager for auto-expanding abbreviations, inspired by fish. ~18,000 clones by ~12,000 unique cloners as of Sept '24, and averaging ~60 Homebrew installs monthly since June 2023
https://zsh-abbr.olets.dev
Other
555 stars 19 forks source link

fast-syntax-highlighting highlighting of single-word abbreviations #24

Closed olets closed 4 months ago

olets commented 4 years ago

Would be nice to support fast-syntax-highlighting.

Its chroma docs are here.

shimajiteppei commented 1 year ago

As a workaround solution, I use this code snippet at the end of .zshrc file to highlight for single word abbreviations.

chroma_single_word() {
  (( next_word = 2 | 8192 ))

  local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
  local __style

  (( __first_call )) && { __style=${FAST_THEME_NAME}command }
  [[ -n "$__style" ]] && (( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")

  (( this_word = next_word ))
  _start_pos=$_end_pos

  return 0
}

# register single word command execpt for the followings:
# - already in PATH
# - already in fast-syntax-highlighting chroma map
register_single_word_chroma() {
  local word=$1
  if [[ -x $(command -v $word) ]] || [[ -n $FAST_HIGHLIGHT["chroma-$word"] ]]; then
    return 1
  fi

  FAST_HIGHLIGHT+=( "chroma-$word" chroma_single_word )
  return 0
}

if [[ -n $FAST_HIGHLIGHT ]]; then

  for abbr in ${(f)"$(abbr list-abbreviations)"}; do
    if [[ $abbr != *' '* ]]; then
      register_single_word_chroma ${(Q)abbr}
    fi
  done

fi
olets commented 1 year ago

@tomoesaturn what an exciting notification to receive! Thanks for sharing. Looks like it works.

My intuition would be to use ${FAST_THEME_NAME}alias not ${FAST_THEME_NAME}command. If you considered that, what made you choose ${FAST_THEME_NAME}command?

How hard do you think it would be to support multi-word abbreviations? Is that possible with F-Sy-H?

shimajiteppei commented 1 year ago

@olets

My intuition would be to use ${FAST_THEME_NAME}alias not ${FAST_THEME_NAME}command. If you considered that, what made you choose ${FAST_THEME_NAME}command?

Oops, I didn't care about highlight color. I agree with you to use ${FAST_THEME_NAME}alias!

How hard do you think it would be to support multi-word abbreviations? Is that possible with F-Sy-H?

In my opinion, it would be slightly difficult because F-Sy-H chroma function seems to be triggered by a single word. To support multi-word abbreviations, we need to create a chroma function for the first word only, and then the second and subsequent words should be treated like arguments of the first word.

I think therefore that it can be implemented roughly in the following flow.

  1. list all abbreviations
  2. group by the first word
  3. generate chroma function for each first word dynamically using eval or something
  4. register chroma function for each first word
olets commented 1 year ago

Sounds tricky enough that it's not worth letting multi-word highlighting be a blocker for documenting single-word highlighting. For now, let's put single-word out into the world.

If you're interested in contributing to the documentation, please open a PR for https://zsh-abbr.olets.dev/advanced.html#syntax-highlighting. To speed up any PR back-and-forth, I'm picturing something like

#### fast-syntax-highlighting

[fast-syntax-highlighting](https://github.com/zdharma/fast-syntax-highlighting) users…

…

Want highlighting for multi-word aliases? See [zsh-abbr#24](https://github.com/olets/zsh-abbr/issues/24).

#### zsh-syntax-highlighting

<existing zsh-syntax-highlighting docs>

If you're not interested in making the change, lmk and I'll make an update!

shimajiteppei commented 1 year ago

@olets Thanks! I'll try to contribute to the documentation!

olets commented 4 months ago

@shimajiteppei would you like to be added as a contributor in the GitHub readme and https://zsh-abbr.olets.dev/community/ * ?

 

* the docs site's GitHub-hosted images don't always load on the first try

shimajiteppei commented 4 months ago

@olets Thank you for your offer. I would be happy to be added.

olets commented 4 months ago

@all-contributors please add @shimajiteppei for code and doc

allcontributors[bot] commented 4 months ago

@olets

I've put up a pull request to add @shimajiteppei! :tada:

olets commented 4 months ago

I've opened #120 to track multi-word abbreviations. Closing