Skip to content

Commit

Permalink
fix: Add a restart server command. (#3253)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed May 11, 2024
1 parent db0f078 commit 9c1a345
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@
"title": "Open Spell Checker Issues Panel",
"shortTitle": "Open Spelling Issues",
"icon": "$(eye)"
},
{
"command": "cSpell.restart",
"category": "Spell",
"title": "Restart Spell Checker Server",
"shortTitle": "Restart Spell Checker",
"icon": "$(sync)"
}
],
"languages": [
Expand Down
13 changes: 12 additions & 1 deletion packages/client/src/client/client.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logger } from '@internal/common-utils/log';
import { setOfSupportedSchemes, supportedSchemes } from '@internal/common-utils/uriHelper';
import type { SpellingSuggestionsResult, WorkspaceConfigForDocument } from 'code-spell-checker-server/api';
import { extractEnabledSchemeList, extractKnownFileTypeIds } from 'code-spell-checker-server/lib';
Expand All @@ -13,7 +14,7 @@ import { ConfigFields, inspectConfigKeys, sectionCSpell } from '../settings/inde
import * as LanguageIds from '../settings/languageIds.js';
import { createBroadcaster } from '../util/broadcaster.js';
import { extractUriFromConfigurationScope, findConicalDocumentScope } from '../util/documentUri.js';
import { logErrors, silenceErrors } from '../util/errors.js';
import { logErrors, silenceErrors, toError } from '../util/errors.js';
import type { Maybe } from '../util/index.js';
import { Resolvable } from './Resolvable.js';
import type {
Expand Down Expand Up @@ -132,6 +133,16 @@ export class CSpellClient implements Disposable {
return this.ready.promise;
}

public async restart(): Promise<void> {
try {
logger.log('Restarting the server');
await this.client.restart();
logger.log('Server restarted');
} catch (e) {
logger.error(`Failed to restart the server: ${toError(e).message}`);
}
}

public async isSpellCheckEnabled(document: TextDocument): Promise<ServerResponseIsSpellCheckEnabledForFile> {
const { uri, languageId = '' } = document;

Expand Down
6 changes: 6 additions & 0 deletions packages/client/src/commands.mts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ export const commandHandlers = {
'cSpell.createCSpellTerminal': handlerResolvedLater,

'cSpell.openIssuesPanel': callCommand('cspell.issuesViewByFile.focus'),

'cSpell.restart': handleRestart,
} as const satisfies CommandHandler;

type ImplementedCommandHandlers = typeof commandHandlers;
Expand Down Expand Up @@ -648,3 +650,7 @@ function handleInsertWordsDirective(textEditor?: TextEditor, _edit?: TextEditorE
function callCommand(command: string): () => void {
return () => commands.executeCommand(command);
}

function handleRestart() {
return handleErrors(di.getClient().restart(), 'handle restart server');
}
14 changes: 12 additions & 2 deletions packages/client/src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { activate as activateWebview } from './webview/index.mjs';
performance.mark('cspell_done_import');

const debugMode = false;
let currLogLevel: CSpellSettings['logLevel'] = undefined;

modules.init();

Expand Down Expand Up @@ -77,6 +78,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionApi>

function triggerConfigChange(uri: vscode.Uri) {
logger.log(`Configuration Change: ${uri.toString()}`);
currLogLevel = undefined;
triggerGetSettings();
}

Expand Down Expand Up @@ -239,8 +241,10 @@ export async function activate(context: ExtensionContext): Promise<ExtensionApi>

function bindLoggerToOutput(logOutput: vscode.LogOutputChannel): vscode.Disposable {
const disposableList = createDisposableList();
const logLevel = getLogLevel();
const console = {
log: debugMode ? logOutput.info.bind(logOutput) : logOutput.debug.bind(logOutput),
log: (...args: Parameters<typeof logOutput.info>) =>
debugMode || logLevel === 'Debug' ? logOutput.info(...args) : logOutput.debug(...args),
error: logOutput.error.bind(logOutput),
info: logOutput.info.bind(logOutput),
warn: logOutput.warn.bind(logOutput),
Expand All @@ -253,7 +257,13 @@ function bindLoggerToOutput(logOutput: vscode.LogOutputChannel): vscode.Disposab

performance.mark('cspell_done_load');

function getLogLevel() {
if (currLogLevel) return currLogLevel;
currLogLevel = settings.getSettingFromVSConfig('logLevel', undefined) || 'Error';
return currLogLevel;
}

function setOutputChannelLogLevel(level?: CSpellSettings['logLevel']) {
const logLevel = level ?? (settings.getSettingFromVSConfig('logLevel', undefined) || 'Error');
const logLevel = level ?? getLogLevel();
logger.level = logLevel;
}

0 comments on commit 9c1a345

Please sign in to comment.