Skip to content

Instantly share code, notes, and snippets.

@hahdookin
Created March 31, 2022 05:50
Show Gist options
  • Save hahdookin/aa0c60c9c0b0bf872d7eb19aa11a515b to your computer and use it in GitHub Desktop.
Save hahdookin/aa0c60c9c0b0bf872d7eb19aa11a515b to your computer and use it in GitHub Desktop.
Example vim9script with jobs
" vim9script that uses jobs to find a word's defintions
" and post them to the quickfix list
" Last Change: 2022 Mar 31
" Maintainer: Christopher Pane <[email protected]>
vim9script
const api = "https://api.dictionaryapi.dev/api/v2/entries/en/"
def OnResponse(ch: channel, msg: string)
const json = json_decode(msg)
# If a response is received and isn't a list, no def was found
if type(json) == v:t_dict
echo "No definition found"
return
endif
# Grab all definitions and parts of speech
const meanings: list<any> = json[0].meanings
final results: list<string> = [json[0].word]
for meaning in meanings
const pos = "(" .. meaning.partOfSpeech .. ")"
for definition in meaning.definitions
results->add(pos .. " " .. definition.definition)
endfor
endfor
# Set the quickfix list
if setqflist([], ' ', {'lines': results}) == -1
echoe "Could not set quickfix list"
else
copen
endif
enddef
def OnError(ch: channel, msg: string)
echoe "ERROR: " .. msg
enddef
def FindWord(word: string)
const cmd = ["curl", "--silent", api .. word]
const opts = {
"out_cb": OnResponse,
"err_cb": OnError
}
const job = job_start(cmd, opts)
echom "Searching..."
enddef
defcompile
command DefineWord :exec "call FindWord('" .. expand("<cword>") .. "')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment