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

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

Open
1 of 2 tasks
olets opened this issue Sep 19, 2020 · 5 comments
Labels
enhancement New feature or request help welcome

Comments

@olets
Copy link
Owner

olets commented Sep 19, 2020

Would be nice to support fast-syntax-highlighting.

Its chroma docs are here.

  • single-word abbreviations
  • multi-word abbreviations
@olets olets added Hacktoberfest enhancement New feature or request labels Sep 24, 2021
@shimajiteppei
Copy link

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
Copy link
Owner Author

olets commented May 13, 2023

@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
Copy link

shimajiteppei commented May 13, 2023

@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
Copy link
Owner Author

olets commented May 21, 2023

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
Copy link

shimajiteppei commented May 22, 2023

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

@olets olets changed the title fast-syntax-highlighting highlighting of abbreviations fast-syntax-highlighting highlighting of abbreviations [progress: single-word abbreviations is solved; multi-word abbreviations isn't] Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help welcome
Projects
None yet
Development

No branches or pull requests

2 participants