Repository: neovim
Issue: LSP: `definition` "Cursor position outside of buffer" error
Labels: bug,has:plan,bug-regression,lsp,has:bisected

### Problem

Following 0083e03d, `vim.lsp.buf.definition` can throw an error:
```
Error executing vim.schedule lua callback: /usr/local/share/nvim/runtime/lua/vim/lsp/buf.lua:102: Cursor position outside buffer
stack traceback:
        [C]: in function 'nvim_win_set_cursor'
        /usr/local/share/nvim/runtime/lua/vim/lsp/buf.lua:102: in function 'on_response'
        /usr/local/share/nvim/runtime/lua/vim/lsp/buf.lua:121: in function 'handler'
        /usr/local/share/nvim/runtime/lua/vim/lsp/client.lua:681: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>
```

This is reproducible with denols and the custom handler for it in nvim-lspconfig. I think this is a neovim core bug considering it was working prior to 0083e03d.


### Steps to reproduce using "nvim -u minimal_init.lua"

Handlers are ripped from nvim-lspconfig.
`minimal_init.lua`:
```lua
--- CHANGE THESE
local pattern = {
  "javascript",
  "javascriptreact",
  "javascript.jsx",
  "typescript",
  "typescriptreact",
  "typescript.tsx",
}
local cmd = { "deno", "lsp" }
-- Add files/folders here that indicate the root of a project
local root_markers = { "deno.json" }
-- Change to table with settings if required
local settings = {
  deno = {
    enable = true,
    suggest = {
      imports = {
        hosts = {
          ["https://deno.land"] = true,
        },
      },
    },
  },
}

local function virtual_text_document_handler(uri, res, client)
  if not res then
    return nil
  end

  local lines = vim.split(res.result, "\n")
  local bufnr = vim.uri_to_bufnr(uri)

  local current_buf = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
  if #current_buf ~= 0 then
    return nil
  end

  vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
  vim.api.nvim_set_option_value("readonly", true, { buf = bufnr })
  vim.api.nvim_set_option_value("modified", false, { buf = bufnr })
  vim.api.nvim_set_option_value("modifiable", false, { buf = bufnr })
  vim.lsp.buf_attach_client(bufnr, client.id)
end

local function virtual_text_document(uri, client)
  local params = {
    textDocument = {
      uri = uri,
    },
  }
  local result = client.request_sync("deno/virtualTextDocument", params)
  virtual_text_document_handler(uri, result, client)
end

local function denols_handler(err, result, ctx, config)
  if not result or vim.tbl_isempty(result) then
    return nil
  end

  local client = vim.lsp.get_client_by_id(ctx.client_id)
  for _, res in pairs(result) do
    local uri = res.uri or res.targetUri
    if uri:match("^deno:") then
      virtual_text_document(uri, client)
      res["uri"] = uri
      res["targetUri"] = uri
    end
  end

  vim.lsp.handlers[ctx.method](err, result, ctx, config)
end

vim.api.nvim_create_autocmd("FileType", {
  pattern = pattern,
  callback = function(args)
    local match = vim.fs.find(root_markers, { path = args.file, upward = true })[1]
    local root_dir = match and vim.fn.fnamemodify(match, ":p:h") or nil
    vim.lsp.start({
      name = "bugged-ls",
      cmd = cmd,
      root_dir = root_dir,
      settings = settings,
      handlers = {
        ["textDocument/definition"] = denols_handler,
        ["textDocument/typeDefinition"] = denols_handler,
        ["textDocument/references"] = denols_handler,
      },
    })
  end,
})

if false then
  vim.lsp.set_log_level("debug")
end
```

### Steps
1. `mkdir /tmp/testing && cd /tmp/testing`
2. `touch deno.json && echo '{}' > deno.json`
3. `touch test.ts && echo 'console.log('hello world')' > test.ts`
4. `nvim -u minimal_init.lua test.ts`
5. put cursor over the `log` of `console.log` and run `:lua vim.lsp.buf.definition()`
6. observe error

### Expected behavior

Should open the buffer/window at the URI and cursor position returned by denols.
This is observable prior to 0083e03d

### Nvim version (nvim -v)

NVIM v0.11.0-dev-1028+g6dad1f9f19

### Language server name/version

denols 2.0.2

### Operating system/version

Linux archlinux 6.11.3-arch1-1

### Log file

https://gist.github.com/jamestrew/d9ad40083357a41308f2075bff962623