Skip to content

Commit

Permalink
✨ Add progress indicator on initialization (#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Jul 5, 2024
1 parent 6ac99d2 commit 2a9eddd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
9 changes: 2 additions & 7 deletions packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ connection.onInitialize(async (params) => {
)
}

if (params.workDoneToken) {
progressReporter = connection.window.attachWorkDoneProgress(params.workDoneToken)
progressReporter.begin(locales.localize('server.progress.preparing.title'))
}

try {
await locales.loadLocale(params.locale)
} catch (e) {
Expand Down Expand Up @@ -99,8 +94,8 @@ connection.onInitialize(async (params) => {
} catch (e) {
console.error('[sendDiagnostics]', e)
}
}).on('ready', () => {
progressReporter?.done()
}).on('ready', async () => {
await connection.sendProgress(ls.WorkDoneProgress.type, 'initialize', { kind: 'end' })
})
await service.project.init()
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions packages/locales/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@
"parser.string.illegal-escape": "Unexpected escape character %0%",
"parser.string.illegal-quote": "Only %0% can be used to quote strings here",
"parser.string.illegal-unicode-escape": "Hexadecimal digit expected",
"progress.initializing.title": "Initializing Spyglass…",
"progress.reset-project-cache.title": "Resetting Project Cache…",
"punc.period": ".",
"punc.quote": "“%0%”",
"quote": "a quote (“'” or “\"”)",
Expand Down
53 changes: 37 additions & 16 deletions packages/vscode-extension/src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
* ------------------------------------------------------------------------------------------*/

import type * as server from '@spyglassmc/language-server'
import { localize } from '@spyglassmc/locales'
import path from 'path'
import * as vsc from 'vscode'
import * as lc from 'vscode-languageclient/node.js'

let client: lc.LanguageClient

export function activate(context: vsc.ExtensionContext) {
export async function activate(context: vsc.ExtensionContext) {
if ((vsc.workspace.workspaceFolders ?? []).length === 0) {
// Don't start the language server without a workspace folder
return
Expand Down Expand Up @@ -53,7 +54,6 @@ export function activate(context: vsc.ExtensionContext) {
const clientOptions: lc.LanguageClientOptions = {
documentSelector,
initializationOptions,
progressOnInitialization: true,
}

// Create the language client and start the client.
Expand All @@ -64,14 +64,23 @@ export function activate(context: vsc.ExtensionContext) {
clientOptions,
)

// Start the client. This will also launch the server
client.start().then(() => {
await vsc.window.withProgress({
location: vsc.ProgressLocation.Window,
title: localize('progress.initializing.title'),
}, async (progress) => {
try {
// Start the client. This will also launch the server
await client.start()
} catch (e) {
console.error('[client#start]', e)
}

const customCapabilities: server.CustomServerCapabilities | undefined = client
.initializeResult?.capabilities.experimental?.spyglassmc

if (customCapabilities?.dataHackPubify) {
context.subscriptions.push(
vsc.commands.registerCommand('spyglassmc.dataHackPubify', async (): Promise<void> => {
vsc.commands.registerCommand('spyglassmc.dataHackPubify', async () => {
try {
const initialism = await vsc.window.showInputBox({ placeHolder: 'DHP' })
if (!initialism) {
Expand All @@ -93,16 +102,18 @@ export function activate(context: vsc.ExtensionContext) {

if (customCapabilities?.resetProjectCache) {
context.subscriptions.push(
vsc.commands.registerCommand(
'spyglassmc.resetProjectCache',
async (): Promise<void> => {
try {
vsc.commands.registerCommand('spyglassmc.resetProjectCache', async () => {
try {
await vsc.window.withProgress({
location: vsc.ProgressLocation.Window,
title: localize('progress.reset-project-cache.title'),
}, async () => {
await client.sendRequest('spyglassmc/resetProjectCache')
} catch (e) {
console.error('[client#resetProjectCache]', e)
}
},
),
})
} catch (e) {
console.error('[client#resetProjectCache]', e)
}
}),
)
}

Expand All @@ -117,8 +128,18 @@ export function activate(context: vsc.ExtensionContext) {
}),
)
}
}, (e) => {
console.error('[client#start]', e)

return new Promise<void>((resolve) => {
client.onProgress(lc.WorkDoneProgress.type, 'initialize', (params) => {
if (params.kind === 'begin') {
progress?.report({ increment: 0, message: params.message })
} else if (params.kind === 'report') {
progress?.report({ increment: params.percentage, message: params.message })
} else if (params.kind === 'end') {
resolve()
}
})
})
})
}

Expand Down

0 comments on commit 2a9eddd

Please sign in to comment.