Skip to content

Commit

Permalink
Use #line directive to fix number of lines and file names in C/C++ files
Browse files Browse the repository at this point in the history
  • Loading branch information
whoan committed Nov 28, 2019
1 parent 564fc46 commit 0254984
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions snip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ __snip__replace_snips() {
tmp_file=$(command -p mktemp -t snip.XXXXXX) || return 1
rm "${tmp_file%/*}"/snip.*

# show proper number of line and filename on c/c++ compilation errors
cp "$source_file" "$tmp_file"
if __snip__is_c_or_cpp_file "$source_file"; then
__snip__process_c_or_cpp_file "$source_file" "$tmp_file" "${snippets[@]}" || return 1
fi
source_file=$tmp_file

local cache_dir=~/.cache/snip
mkdir -p "$cache_dir"/

Expand Down Expand Up @@ -173,6 +180,36 @@ __snip__is_text_file() {
}


__snip__is_c_or_cpp_file() {
local filename
filename=${1:?Missing filename by param}
[[ $(file -i -- "$filename" 2> /dev/null) =~ text/x-c[\+\;] ]]
}


__snip__process_c_or_cpp_file() {
local source_file
source_file=${1:?Missing source file as param}
shift
local tmp_file
tmp_file=${1:?Missing tmp file as param}
shift
: ${1:?Missing snippets as params}

sed -i.bak \
"1i #line 1 \"$source_file\"" \
"$tmp_file" || return 1

local snippet
for snippet in "$@"; do
sed -i.bak \
-e "${snippet/:*/}i #line 1 \"${snippet/*:/}\"" \
-e "${snippet/:*/}a #line $((${snippet/:*/} + 1)) \"$source_file\"" \
"$tmp_file" || return 1
done
}


__snip__can_run() {

if (( ${#@} == 0 )) || [[ $1 == '-h' ]] || [[ $1 == '--help' ]]; then
Expand Down

0 comments on commit 0254984

Please sign in to comment.