Skip to content

Commit

Permalink
localize
Browse files Browse the repository at this point in the history
  • Loading branch information
dibarbet committed Aug 17, 2023
1 parent f80420d commit 4737540
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 8 deletions.
10 changes: 7 additions & 3 deletions src/lsptoolshost/optionChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ function handleLanguageServerOptionChanges(changedOptions: OptionChanges, vscode
return;
}

const reloadTitle = 'Reload Window';
const reloadTitle = vscode.l10n.t('Reload Window');
const reloadCommand = 'workbench.action.reloadWindow';
if (changedOptions.changedCommonOptions.find((key) => key === 'useOmnisharpServer')) {
// If the user has changed the useOmnisharpServer flag we need to reload the window.
ShowInformationMessage(
vscode,
'The useOmnisharpServer option has changed. Please reload the window to apply the change',
vscode.l10n.t(
'dotnet.server.useOmnisharp option has changed. Please reload the window to apply the change'
),
{
title: reloadTitle,
command: reloadCommand,
Expand All @@ -51,6 +53,8 @@ function handleLanguageServerOptionChanges(changedOptions: OptionChanges, vscode

// Typically when we have a regular config change, we can just restart the server, but due to
// https://github.com/dotnet/vscode-csharp/issues/5882 we need to reload the window when using devkit.
const message = 'C# configuration has changed. Would you like to reload the window to apply your changes?';
const message = vscode.l10n.t(
'C# configuration has changed. Would you like to reload the window to apply your changes?'
);
ShowInformationMessage(vscode, message, { title: reloadTitle, command: reloadCommand });
}
24 changes: 20 additions & 4 deletions src/omnisharp/omnisharpOptionChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,26 @@ export function registerOmnisharpOptionChanges(vscode: vscode, optionObservable:
changedOmnisharpOptions: OmnisharpOptionsThatTriggerReload,
};
},
handleOptionChanges(_) {
const message =
'C# configuration has changed. Would you like to relaunch the Language Server with your changes?';
const title = 'Restart Language Server';
handleOptionChanges(changedOptions) {
if (changedOptions.changedCommonOptions.find((key) => key === 'useOmnisharpServer')) {
// If the user has changed the useOmnisharpServer flag we need to reload the window.
ShowInformationMessage(
vscode,
vscode.l10n.t(
'dotnet.server.useOmnisharp option has changed. Please reload the window to apply the change'
),
{
title: vscode.l10n.t('Reload Window'),
command: 'workbench.action.reloadWindow',
}
);
return;
}

const message = vscode.l10n.t(
'C# configuration has changed. Would you like to relaunch the Language Server with your changes?'
);
const title = vscode.l10n.t('Restart Language Server');
const commandName = 'o.restart';
ShowInformationMessage(vscode, message, { title: title, command: commandName });
},
Expand Down
12 changes: 12 additions & 0 deletions src/vscodeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -999,4 +999,16 @@ export interface vscode {
sessionId: string;
openExternal(target: Uri): Thenable<boolean>;
};

l10n: {
t(message: string, ...args: Array<string | number | boolean>): string;
t(message: string, args: Record<string, any>): string;
t(options: {
message: string;
args?: Array<string | number | boolean> | Record<string, any>;
comment: string | string[];
}): string;
bundle: { [key: string]: string } | undefined;
uri: Uri | undefined;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ suite('LanguageServerConfigChangeObserver', () => {
});
});

[{ config: 'dotnet', section: 'server.useOmnisharp', value: true }].forEach((elem) => {
suite(`When the ${elem.config} ${elem.section} changes`, () => {
setup(() => {
expect(infoMessage).to.be.undefined;
expect(invokedCommand).to.be.undefined;
updateConfig(vscode, elem.config, elem.section, elem.value);
optionObservable.next(Options.Read(vscode));
});

test(`The information message is shown`, async () => {
expect(infoMessage).to.be.equal(
'dotnet.server.useOmnisharp option has changed. Please reload the window to apply the change'
);
});

test('Given an information message if the user clicks cancel, the command is not executed', async () => {
doClickCancel();
await expect(observableFrom(commandDone!).pipe(timeout(1)).toPromise()).to.be.rejected;
expect(invokedCommand).to.be.undefined;
});

test('Given an information message if the user clicks Reload, the command is executed', async () => {
doClickOk();
await commandDone;
expect(invokedCommand).to.be.equal('workbench.action.reloadWindow');
});
});
});

suite('Information Message is not shown if no change in value', () => {
[
{ config: 'dotnet', section: 'server.documentSelector', value: ['csharp'] },
Expand Down
31 changes: 30 additions & 1 deletion test/unitTests/optionObserver/optionChangeObserver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,43 @@ suite('OmniSharpConfigChangeObserver', () => {
expect(invokedCommand).to.be.undefined;
});

test('Given an information message if the user clicks Restore, the command is executed', async () => {
test('Given an information message if the user clicks Restart, the command is executed', async () => {
doClickOk();
await commandDone;
expect(invokedCommand).to.be.equal('o.restart');
});
});
});

[{ config: 'dotnet', section: 'server.useOmnisharp', value: true }].forEach((elem) => {
suite(`When the ${elem.config} ${elem.section} changes`, () => {
setup(() => {
expect(infoMessage).to.be.undefined;
expect(invokedCommand).to.be.undefined;
updateConfig(vscode, elem.config, elem.section, elem.value);
optionObservable.next(Options.Read(vscode));
});

test(`The information message is shown`, async () => {
expect(infoMessage).to.be.equal(
'dotnet.server.useOmnisharp option has changed. Please reload the window to apply the change'
);
});

test('Given an information message if the user clicks cancel, the command is not executed', async () => {
doClickCancel();
await expect(observableFrom(commandDone!).pipe(timeout(1)).toPromise()).to.be.rejected;
expect(invokedCommand).to.be.undefined;
});

test('Given an information message if the user clicks Reload, the command is executed', async () => {
doClickOk();
await commandDone;
expect(invokedCommand).to.be.equal('workbench.action.reloadWindow');
});
});
});

suite('Information Message is not shown on change in', () => {
[
{ config: 'csharp', section: 'disableCodeActions', value: true },
Expand Down
43 changes: 43 additions & 0 deletions test/unitTests/testAssets/fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
OmnisharpServerUnresolvedDependencies,
WorkspaceInformationUpdated,
} from '../../../src/omnisharp/loggingEvents';
import { format } from 'util';

export const getNullChannel = (): vscode.OutputChannel => {
const returnChannel: vscode.OutputChannel = {
Expand Down Expand Up @@ -221,6 +222,48 @@ export function getFakeVsCode(): vscode.vscode {
throw new Error('Not Implemented');
},
},
l10n: {
t: (
options:
| string
| {
message: string;
args?: Array<string | number | boolean> | Record<string, any>;
comment: string | string[];
},
...args: (string | number | boolean)[] | Record<string, any>[]
) => {
let message = '';
let actualArgs:
| (string | number | boolean)[]
| Record<string, any>[]
| Record<string, any>
| undefined = args;
if (typeof options === 'string') {
message = options;
} else {
message = options.message;
actualArgs = options.args;
}

if (!Array.isArray(actualArgs)) {
throw new Error('Non-array l10n args not implemented');
}

const strArgs: string[] = [];
actualArgs.forEach((arg) => {
if (typeof arg !== 'string') {
throw new Error('Non-string l10n args not implemented');
}

strArgs.push(arg);
});

return format(message, ...strArgs);
},
bundle: undefined,
uri: undefined,
},
};
}

Expand Down

0 comments on commit 4737540

Please sign in to comment.