Replace cformat() with a clang-format wrapper
Replace the function with extra functionality with a wrapper for clang-format that looks for the style flag and substitutes a given filename with its parsed content.
This commit is contained in:
@@ -308,23 +308,39 @@ function histgrep() {
|
|||||||
grep "$@" "${HISTFILE:-$HOME/.zsh_history}"
|
grep "$@" "${HISTFILE:-$HOME/.zsh_history}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function cformat() {
|
function clang-format() {
|
||||||
# TODO: respect manual changes made in meld
|
local idx=${@[(I)-style*]}
|
||||||
CLANG_FORMAT_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/clang/config"
|
if (( ! idx )); then
|
||||||
FORMAT="$(
|
# No style flag given
|
||||||
sed -E '/^\s*($|#)/d' "$CLANG_FORMAT_FILE" \
|
command clang-format "$@"
|
||||||
| tr '\n' ',' \
|
return
|
||||||
| sed 's/,$//'
|
|
||||||
)"
|
|
||||||
FORMAT="{${FORMAT}}"
|
|
||||||
if [ $# -eq 1 ]; then
|
|
||||||
meld <(clang-format -style="$FORMAT" $1) $1
|
|
||||||
fi
|
fi
|
||||||
echo -n "Are you happy? [yn] "
|
|
||||||
read yn
|
local style="${@[$idx]#-style}" prefix=""
|
||||||
if [ $yn = "y" ]; then
|
if [ -n "$style" ]; then
|
||||||
clang-format -i -style="$FORMAT" $1
|
# Flag was given in form -style=<style>
|
||||||
|
style="${style#=}"
|
||||||
|
prefix="-style="
|
||||||
|
else
|
||||||
|
# Flag was given in form -style <style>
|
||||||
|
(( idx++ ))
|
||||||
|
style="${@[$idx]}"
|
||||||
fi
|
fi
|
||||||
|
if [ ! -e "$style" ]; then
|
||||||
|
# Argument is not a file and thus probably a valid style string that can
|
||||||
|
# be passes to clang-format
|
||||||
|
command clang-format "$@"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Delete all empty lines (not counting whitespace) and comments and join all
|
||||||
|
# lines with commas.
|
||||||
|
style="$(sed -E '/^\s*($|#)/d' "$style" | tr '\n' ',')"
|
||||||
|
style="{${style%,}}"
|
||||||
|
|
||||||
|
# Overwrite the argument of the style flag with the parsed format file.
|
||||||
|
set -- "${@[1, $idx - 1]}" "${prefix}${style}" "${@[$idx + 1, -1]}"
|
||||||
|
command clang-format "$@"
|
||||||
}
|
}
|
||||||
|
|
||||||
function urlenc() {
|
function urlenc() {
|
||||||
|
|||||||
Reference in New Issue
Block a user