55 lines
1.8 KiB
Bash
Executable File
55 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Kmpl: acme glue for u/complete. Triggers completion and prints result to stdout.
|
|
set -euo pipefail
|
|
|
|
cmd=${0##*/}
|
|
die() { printf '%s: %s\n' "$cmd" "$*" >&2; exit 1; }
|
|
|
|
[ -z "${winid:-}" ] || [ "$winid" = "0" ] && die "must be run from an acme window"
|
|
|
|
# Get filename from tag
|
|
filename=$(9p read acme/$winid/tag | awk '{print $1; exit}')
|
|
|
|
# Read dot position (char offsets)
|
|
addr=$(printf 'addr=dot' | 9p write acme/$winid/ctl | 9p read acme/$winid/addr)
|
|
read -r q0 q1 <<< "$addr"
|
|
[ -z "$q1" ] && q1=$q0
|
|
|
|
# prefix = BOF to q0, suffix = q1 to EOF, selection = rdsel
|
|
prefix=$(printf '#0,#%s' "$q0" | 9p write acme/$winid/addr | 9p read acme/$winid/data)
|
|
suffix=$(printf '#%s,$' "$q1" | 9p write acme/$winid/addr | 9p read acme/$winid/data)
|
|
sel=$(9p read acme/$winid/rdsel 2>/dev/null || true)
|
|
|
|
# Compute cursor line/col for LSP calls
|
|
line=$(printf '%s' "$prefix" | wc -l)
|
|
[ "$line" -eq 0 ] && line=1
|
|
col=$(printf '%s' "$prefix" | tail -1 | wc -m)
|
|
|
|
# Gather LSP context in parallel
|
|
tmpdir=$(mktemp -d)
|
|
trap 'rm -rf "$tmpdir"' EXIT
|
|
|
|
python3 $OLLIE/t/lsp_completion "$filename" "$line" "$col" > "$tmpdir/completions" 2>/dev/null &
|
|
python3 $OLLIE/t/lsp_symbols "$filename" > "$tmpdir/symbols" 2>/dev/null &
|
|
python3 $OLLIE/t/lsp_hover "$filename" "$line" "$col" > "$tmpdir/hover" 2>/dev/null &
|
|
wait
|
|
|
|
context=""
|
|
[ -s "$tmpdir/completions" ] && context="${context}<completions>
|
|
$(cat "$tmpdir/completions")
|
|
</completions>
|
|
"
|
|
[ -s "$tmpdir/symbols" ] && context="${context}<symbols>
|
|
$(cat "$tmpdir/symbols")
|
|
</symbols>
|
|
"
|
|
[ -s "$tmpdir/hover" ] && context="${context}<hover>
|
|
$(cat "$tmpdir/hover")
|
|
</hover>
|
|
"
|
|
|
|
# Send prefix+selection as prefix context; selection is the focal point
|
|
result=$(printf '%s%s' "$prefix" "$sel" | $OLLIE/u/complete -file "$filename" -suffix "$suffix" -context "$context" "$@")
|
|
|
|
printf '%s' "$result"
|