Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tokens for timeout in dynamic completions #2434

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/canceltokenutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CancellationToken, CancellationTokenSource } from 'vscode-jsonrpc'

export function createTimeoutCancellation(millis: number): CancellationToken {
const source = new CancellationTokenSource()
setTimeout(() => source.cancel(), millis)
return source.token
}

export function combineCancellationTokens(a: CancellationToken, b: CancellationToken): CancellationToken {
if (a.isCancellationRequested || b.isCancellationRequested) {
return CancellationToken.Cancelled
}
const source = new CancellationTokenSource()
a.onCancellationRequested(() => source.cancel())
b.onCancellationRequested(() => source.cancel())
return source.token
}
49 changes: 21 additions & 28 deletions src/interactive/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as vscode from 'vscode'
import * as rpc from 'vscode-jsonrpc'
import { Disposable, MessageConnection } from 'vscode-jsonrpc'
import { combineCancellationTokens, createTimeoutCancellation } from '../canceltokenutils'
import { getModuleForEditor } from './modules'
import { onExit, onInit } from './repl'

Expand Down Expand Up @@ -36,42 +37,34 @@ const requestTypeGetCompletionItems = new rpc.RequestType<
void
>('repl/getcompletions')


function completionItemProvider(conn: MessageConnection): vscode.CompletionItemProvider {
return {
provideCompletionItems: async (document, position, token, context) => {
if (!vscode.workspace.getConfiguration('julia.runtimeCompletions')) {
return
}
const completionPromise = (async () => {
const startPosition = new vscode.Position(position.line, 0)
const lineRange = new vscode.Range(startPosition, position)
const line = document.getText(lineRange)
const mod: string = await getModuleForEditor(document, position)
return {
items: await conn.sendRequest(requestTypeGetCompletionItems, { line, mod }),
isIncomplete: true
}
})()

const cancelPromise: Promise<vscode.CompletionList> = new Promise(resolve => {
token.onCancellationRequested(() => resolve({
items: [],
isIncomplete: true
}))
setTimeout(() => {
if (!token.isCancellationRequested) {
resolve({
items: [],
isIncomplete: true
})
}
}, 500)
})
const newToken = combineCancellationTokens(
token,
createTimeoutCancellation(500)
)

const startPosition = new vscode.Position(position.line, 0)
const lineRange = new vscode.Range(startPosition, position)
const line = document.getText(lineRange)
const mod: string = await getModuleForEditor(document, position) // TODO pass newToken once we support tokens in JSONRPC.jl

if (newToken.isCancellationRequested) { return }

return Promise.race([
completionPromise,
cancelPromise
])
const items = await conn.sendRequest(requestTypeGetCompletionItems, { line, mod }) // TODO pass newToken once we support tokens in JSONRPC.jl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean that we always wait for this to go through before returning, unless cancellation got requested before we dispatch this request?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, you're right. I don't know actually what the semantics are if we were to pass the token to the sendRequest call: would this await here finish right away, irrespective of what is happening on the server? In that case we could just pass the token here, even if the server doesn't yet implement cancel notifications. Or would it send the cancel notification message to the server and then wait until the request returns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter, I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, seems more likely...


if (newToken.isCancellationRequested) { return }

return {
items: items,
isIncomplete: true
}
}
}
}