Skip to content

Instantly share code, notes, and snippets.

@henriquegogo
Created May 19, 2025 20:59
Show Gist options
  • Save henriquegogo/719b5947821cf96b3be93ad5542fad98 to your computer and use it in GitHub Desktop.
Save henriquegogo/719b5947821cf96b3be93ad5542fad98 to your computer and use it in GitHub Desktop.
Invoke code completion using only the terminal
#!/bin/bash
CODE='console.'
INIT='{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"processId":1,"rootUri":null,"capabilities":{}}}'
INITIALIZED='{"jsonrpc":"2.0","method":"initialized","params":{}}'
DIDOPEN="{\"jsonrpc\":\"2.0\",\"method\":\"textDocument/didOpen\",\"params\":{\"textDocument\":{\"uri\":\"file:///dev/null\",\"languageId\":\"typescript\",\"version\":1,\"text\":\"$CODE\"}}}"
COMPLETION="{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"textDocument/completion\",\"params\":{\"textDocument\":{\"uri\":\"file:///dev/null\"},\"position\":{\"line\":0,\"character\":${#CODE}}}}"
{
printf 'Content-Length: %d\r\n\r\n%s' $(echo -n "$INIT" | wc -c) "$INIT"
printf 'Content-Length: %d\r\n\r\n%s' $(echo -n "$INITIALIZED" | wc -c) "$INITIALIZED"
printf 'Content-Length: %d\r\n\r\n%s' $(echo -n "$DIDOPEN" | wc -c) "$DIDOPEN"; sleep 0.3
printf 'Content-Length: %d\r\n\r\n%s' $(echo -n "$COMPLETION" | wc -c) "$COMPLETION"; sleep 0.3
} | typescript-language-server --stdio | tail -n1 | jq -r '.result.items[].label'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment