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

main-highlighter can be run as command, with profiling equipped #371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions highlighters/main/mh-parse.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/sh

#
# This file runs main highlighter on a specified file
# i.e. parses the file with the highlighter. Outputs
# running time (stderr) and resulting region_highlight
# (stdout).
#

# Strive for -f
# .. occuring problems (no q flag), but at least try to quote $0 and $@
[[ -z "$ZSH_VERSION" ]] && exec /usr/bin/env zsh -f -c "source \"$0\" \"$1\" \"$2\" \"$3\""
Copy link
Member

@danielshahaf danielshahaf Sep 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sh scripts can't use [[; change it to [.

Copy link
Member

@danielshahaf danielshahaf Sep 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About quoting: /usr/bin/env zsh -fc 'source "$@"' - "$0" "$@" should do the trick.


ZERO="${(%):-%N}"
ZERO_RESOLVED="${ZERO:A:h}"
Copy link
Member

@danielshahaf danielshahaf Sep 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't actually need $ZERO_RESOLVED; it'd be fine to replace all uses with $ZERO.

Yes, I know I said ${0:A:h} originally. I think I mislead you; sorry for that.


#
# Load Z-SY-H
#

if [[ -e "${ZERO_RESOLVED}/main-highlighter.zsh" ]]; then
source "${ZERO_RESOLVED}/../../zsh-syntax-highlighting.zsh"
elif [[ -e "${0:A:h}/main-highlighter.zsh" ]]; then
source "${0:A:h}/../../zsh-syntax-highlighting.zsh"
elif [[ -e "../../main-highlighter.zsh" ]]; then
source "../../zsh-syntax-highlighting.zsh"
else
print "Could not find zsh-syntax-highlighting.zsh, aborting"
exit 1
fi

#
# Call _zsh_highlight_highlighter_main_paint
#

if [[ -r "$1" ]]; then
# Load from given file
PREBUFFER=""
BUFFER="$(<$1)"

typeset -F SECONDS
SECONDS=0

_zsh_highlight_highlighter_main_paint
Copy link
Member

@danielshahaf danielshahaf Sep 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is in no way specific to the main highlighter; I think it would be better suited in tools/ and taking a highlighter name as an argument. WDYT?

Like the existing test scripts, I think this script would benefit from the stub _zsh_highlight_add_highlight implementation used by various files in tests/. (It'd cause the output to print style names rather than color settings derived from them.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need to setopt interactivecomments here, so that any comments in the file would be highlighted correctly?


print -u2 "Main highlighter's running time: $SECONDS"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather send this to stdout, since it's not an error. Perhaps make it a # comment to ease parsing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A quick reply: this allows to redirect region_highlight output to a file, and have running time on terminal


# This output can be diffed to detect changes in operation
print -rl -- "${region_highlight[@]}"
else
if [[ -z "$1" ]]; then
print -u2 "Usage: ./mh-parse.zsh {to-parse file}"
Copy link
Member

@danielshahaf danielshahaf Sep 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather the argument read {file to parse}; I think that scans better in English.

exit 1
else
print -u2 "Unreadable to-parse file \`$1', aborting"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two minor issues:

  • Prefix $0 to the error message.
  • Use ${(qq)1} to escape the filename.

exit 2
fi
fi

exit 0

# vim:ft=zsh