zsh-abbr icon indicating copy to clipboard operation
zsh-abbr copied to clipboard

fast-syntax-highlighting highlighting of abbreviations [progress: single-word abbreviations is solved; multi-word abbreviations isn't]

Open olets opened this issue 4 years ago • 7 comments

Would be nice to support fast-syntax-highlighting.

Its chroma docs are here.

  • [x] single-word abbreviations
  • ~multi-word abbreviations~ tracking in #120

olets avatar Sep 19 '20 20:09 olets

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

shimajiteppei avatar May 13 '23 01:05 shimajiteppei

@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?

olets avatar May 13 '23 03:05 olets

@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

shimajiteppei avatar May 13 '23 10:05 shimajiteppei

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!

olets avatar May 21 '23 18:05 olets

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

shimajiteppei avatar May 22 '23 00:05 shimajiteppei