This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheditorconfig
executable file
·218 lines (171 loc) · 5.6 KB
/
editorconfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/bash
# editorconfig: Editorconfig Vimscript core CLI
# Copyright (c) 2018--2019 Chris White. CC-BY-SA 3.0+.
# Documentation {{{1
helpstr=$(cat<<'EOF'
editorconfig: command-line invoker for the Vimscript editorconfig core
Normal usage:
editorconfig [-f <config-file name>] [-b <version>]
[-x <extra information>] <filenames...>
The default <config-file name> is ".editorconfig".
If -b is given, behave as <version>.
If -x is given, the <extra information> is included in the debug-output file.
Other options:
editorconfig -h, --help Show this help
editorconfig -v, --version Show version information
Environment variables:
VIM_EXE File/path of vim (default "vim")
EDITORCONFIG_DEBUG File/path to which to append debug output
EOF
)
# }}}1
# Get the directory of this script into $this_script_dir. {{{1
# From https://stackoverflow.com/a/246128/2877364 by
# https://stackoverflow.com/users/407731 et al.
this_script_dir=
function get_dir()
{
local script_source_path="${BASH_SOURCE[0]}"
while [ -h "$script_source_path" ]; do
# resolve $script_source_path until the file is no longer a symlink
this_script_dir="$( cd -P "$( dirname "$script_source_path" )" >/dev/null && pwd )"
script_source_path="$(readlink "$script_source_path")"
[[ $script_source_path != /* ]] && script_source_path="$this_script_dir/$script_source_path"
# if $script_source_path was a relative symlink, we need to resolve
# it relative to the path where the symlink file was located
done
this_script_dir="$( cd -P "$( dirname "$script_source_path" )" >/dev/null && pwd )"
} #get_dir()
get_dir
# }}}1
# Setup debug output, if $EDITORCONFIG_DEBUG is given {{{1
debug="${EDITORCONFIG_DEBUG}" # Debug filename
if [[ $debug && $debug != /* ]]; then # Relative to this script unless it
debug="${this_script_dir}/${debug}" # starts with a slash. This is because
fi # cwd is usually not $this_script_dir when testing.
if [[ $debug ]] && ! touch "$debug"; then
echo "Could not write file '$debug' - aborting" 1>&2
exit 1
fi
[[ $debug ]] && echo "$(date) ==================================" >> "$debug"
# }}}1
# Option processing {{{1
# Use a manually-specified Vim, if any
if [[ $VIM_EXE ]]; then
vim_pgm="$VIM_EXE"
else
vim_pgm="vim"
fi
# Command-line options
confname=
ver=
print_ver=
extra_info=
while getopts 'hvf:b:-:x:' opt ; do
case "$opt" in
(v) print_ver=1
;;
(f) confname="$OPTARG"
;;
(b) ver="$OPTARG"
;;
(-) case "$OPTARG" in # hacky long-option processing
version) print_ver=1
;;
dummy) # A dummy option so that I can test
# list-valued EDITORCONFIG_CMD
;;
help) echo "$helpstr"
exit 0
;;
esac
;;
(h) echo "$helpstr"
exit 0
;;
# A way to put the test name into the log
(x) extra_info="$OPTARG"
;;
esac
done
shift $(( $OPTIND - 1 ))
if [[ $print_ver ]]; then
echo "EditorConfig VimScript Core Version 0.12.2"
exit 0
fi
if (( "$#" < 1 )); then
exit 1
fi
if [[ $1 = '-' ]]; then
echo "Reading filenames from stdin not yet supported" 1>&2 # TODO
exit 1
fi
# }}}1
# Build the Vim command line {{{1
fn="$(mktemp)" # Vim will write the settings into here. ~stdout.
script_output_fn="${debug:+$(mktemp)}" # Vim's :messages. ~stderr.
cmd="call editorconfig_core#currbuf_cli({"
# Names
cmd+="'output':'${fn//\'/\'\'}', "
# filename to put the settings in
[[ $debug ]] && cmd+=" 'dump':'${script_output_fn//\'/\'\'}', "
# where to put debug info
# Filenames to get the settings for
cmd+="'target':["
for f in "$@" ; do
cmd+="'${f//\'/\'\'}', "
done
cmd+="],"
# filename to get the settings for
# Job
cmd+="}, {"
[[ $confname ]] && cmd+="'config':'${confname//\'/\'\'}', "
# config name (e.g., .editorconfig)
[[ $ver ]] && cmd+="'version':'${ver//\'/\'\'}', "
# version number we should behave as
cmd+="})"
vim_args=(
-c "set runtimepath+=$this_script_dir"
-c "$cmd"
)
# }}}1
# Run the editorconfig core through Vim {{{1
# Thanks for options to
# http://vim.wikia.com/wiki/Vim_as_a_system_interpreter_for_vimscript .
# Add -V1 to the below for debugging output.
# Do not output anything to stdout or stderr,
# since it messes up ctest's interpretation
# of the results.
"$vim_pgm" -nNes -i NONE -u NONE -U NONE \
"${vim_args[@]}" \
</dev/null &>> "${debug:-/dev/null}"
vimstatus="$?"
if [[ $vimstatus -eq 0 ]]; then
cat "$fn"
fi
# }}}1
# Produce debug output {{{1
# Debug output cannot be included on stdout or stderr, because
# ctest's regex check looks both of those places. Therefore, dump to a
# separate debugging file.
if [[ $debug ]]
then
[[ $extra_info ]] && echo "--- $extra_info ---" >> "$debug"
echo "Vim in $vim_pgm" >> "$debug"
echo "Current directory: $(pwd)" >> "$debug"
echo "Script directory: $this_script_dir" >> "$debug"
echo Vim args: "${vim_args[@]}" >> "$debug"
#od -c <<<"${vim_args[@]}" >> "$debug"
echo "Vim returned $vimstatus" >> "$debug"
echo "Vim messages were: " >> "$debug"
cat "$script_output_fn" >> "$debug"
echo "Output was:" >> "$debug"
od -c "$fn" >> "$debug"
rm -f "$script_output_fn"
fi
# }}}1
# Cleanup {{{1
rm -f "$fn"
# }}}1
exit "$vimstatus" # forward the Vim exit status to the caller
# vi: set ft=sh fdm=marker: