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

Display prompt [Information Message] to restart the server when the omnisharp configuration changes #2316

Merged
merged 5 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import { ProjectStatusBarObserver } from './observers/ProjectStatusBarObserver';
import CSharpExtensionExports from './CSharpExtensionExports';
import { vscodeNetworkSettingsProvider, NetworkSettingsProvider } from './NetworkSettings';
import { ErrorMessageObserver } from './observers/ErrorMessageObserver';
import OptionStream from './observables/OptionStream';
import OptionProvider from './observers/OptionProvider';
import DotNetTestChannelObserver from './observers/DotnetTestChannelObserver';
import DotNetTestLoggerObserver from './observers/DotnetTestLoggerObserver';
import { ShowOmniSharpConfigChangePrompt } from './observers/OptionChangeObserver';
import createOptionStream from './observables/CreateOptionStream';

export async function activate(context: vscode.ExtensionContext): Promise<CSharpExtensionExports> {

Expand All @@ -46,7 +47,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<CSharp
util.setExtensionPath(extension.extensionPath);

const eventStream = new EventStream();
const optionStream = new OptionStream(vscode);
const optionStream = createOptionStream(vscode);
let optionProvider = new OptionProvider(optionStream);

let dotnetChannel = vscode.window.createOutputChannel('.NET');
Expand Down Expand Up @@ -119,7 +120,8 @@ export async function activate(context: vscode.ExtensionContext): Promise<CSharp
eventStream.post(new ActiveTextEditorChanged());
}));

context.subscriptions.push(optionStream);
context.subscriptions.push(optionProvider);
context.subscriptions.push(ShowOmniSharpConfigChangePrompt(optionStream, vscode));

let coreClrDebugPromise = Promise.resolve();
if (runtimeDependenciesExist) {
Expand Down
24 changes: 24 additions & 0 deletions src/observables/CreateOptionStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Options } from "../omnisharp/options";
import { vscode } from "../vscodeAdapter";
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/publishBehavior';
import { Observable } from "rxjs/Observable";
import { Observer } from "rxjs/Observer";

export default function createOptionStream(vscode: vscode): Observable<Options> {
return Observable.create((observer: Observer<Options>) => {
let disposable = vscode.workspace.onDidChangeConfiguration(e => {
//if the omnisharp or csharp configuration are affected only then read the options
if (e.affectsConfiguration('omnisharp') || e.affectsConfiguration('csharp')) {
observer.next(Options.Read(vscode));
}
});

return () => disposable.dispose();
Copy link

Choose a reason for hiding this comment

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

This and the line below are beyond my rx-fu, can you explain them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

publishBehavior is used to create a behavior observable which has the initial value as Option.Read(). refCount() is used so that the dispose function on the observable is called only when all the observers who susbcribed to this observable invoke unsubscribe. This test verifies this behavior https://github.com/OmniSharp/omnisharp-vscode/pull/2316/files#diff-dd9d0618b9edb503c00be5f531debc27R78

}).publishBehavior(Options.Read(vscode)).refCount();
}
34 changes: 0 additions & 34 deletions src/observables/OptionStream.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/observers/OptionChangeObserver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { vscode } from "../vscodeAdapter";
import { Options } from "../omnisharp/options";
import ShowInformationMessage from "./utils/ShowInformationMessage";
import { Observable } from "rxjs/Observable";
import Disposable from "../Disposable";
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/distinctUntilChanged';

function ConfigChangeObservable(optionObservable: Observable<Options>): Observable<Options> {
let options: Options;
return optionObservable. filter(newOptions => {
let changed = (options && hasChanged(options, newOptions));
options = newOptions;
return changed;
});
}

export function ShowOmniSharpConfigChangePrompt(optionObservable: Observable<Options>, vscode: vscode): Disposable {
let subscription = ConfigChangeObservable(optionObservable)
.subscribe(_ => {
let message = "OmniSharp configuration has changed. Would you like to relaunch the OmniSharp server with your changes?";
ShowInformationMessage(vscode, message, { title: "Restart OmniSharp", command: 'o.restart' });
});

return new Disposable(subscription);
}

function hasChanged(oldOptions: Options, newOptions: Options): boolean {
return (oldOptions.path != newOptions.path ||
oldOptions.useGlobalMono != newOptions.useGlobalMono ||
oldOptions.waitForDebugger != newOptions.waitForDebugger);
}
13 changes: 10 additions & 3 deletions src/observers/OptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
*--------------------------------------------------------------------------------------------*/

import { Options } from "../omnisharp/options";
import OptionStream from "../observables/OptionStream";
import { Subscription } from "rxjs/Subscription";
import { Observable } from "rxjs/Observable";

export default class OptionProvider {
private options: Options;
private subscription: Subscription;

constructor(optionStream: OptionStream) {
optionStream.subscribe(options => this.options = options);
constructor(optionObservable: Observable<Options>) {
this.subscription = optionObservable.subscribe(options => this.options = options);
}

public GetLatestOptions(): Options {
if (!this.options) {
throw new Error("Error reading OmniSharp options");
}

return this.options;
}

public dispose = () => {
this.subscription.unsubscribe();
}
}
127 changes: 127 additions & 0 deletions test/unitTests/OptionObserver/OptionChangeObserver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { use as chaiUse, expect, should } from 'chai';
import { updateConfig, getVSCodeWithConfig } from '../testAssets/Fakes';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/timeout';
import { vscode } from '../../../src/vscodeAdapter';
import { ShowOmniSharpConfigChangePrompt } from '../../../src/observers/OptionChangeObserver';
import { Subject } from 'rxjs/Subject';
import { Options } from '../../../src/omnisharp/options';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';

chaiUse(require('chai-as-promised'));
chaiUse(require('chai-string'));

suite("OmniSharpConfigChangeObserver", () => {
suiteSetup(() => should());

let doClickOk: () => void;
let doClickCancel: () => void;
let signalCommandDone: () => void;
let commandDone: Promise<void>;
let vscode: vscode;
let infoMessage: string;
let invokedCommand: string;
let optionObservable: Subject<Options>;

setup(() => {
vscode = getVSCode();
optionObservable = new BehaviorSubject<Options>(Options.Read(vscode));
ShowOmniSharpConfigChangePrompt(optionObservable, vscode);
commandDone = new Promise<void>(resolve => {
signalCommandDone = () => { resolve(); };
});
});

[
{ config: "omnisharp", section: "path", value: "somePath" },
{ config: "omnisharp", section: "waitForDebugger", value: true },
{ config: "omnisharp", section: "useGlobalMono", value: "always" }

].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("OmniSharp configuration has changed. Would you like to relaunch the OmniSharp server with your changes?");
});

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

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

suite('Information Message is not shown on change in',() => {
[
{ config: "csharp", section: 'disableCodeActions', value: true },
{ config: "csharp", section: 'testsCodeLens.enabled', value: false },
{ config: "omnisharp", section: 'referencesCodeLens.enabled', value: false },
{ config: "csharp", section: 'format.enable', value: false },
{ config: "omnisharp", section: 'useEditorFormattingSettings', value: false },
{ config: "omnisharp", section: 'maxProjectResults', value: 1000 },
{ config: "omnisharp", section: 'projectLoadTimeout', value: 1000 },
{ config: "omnisharp", section: 'autoStart', value: false },
{ config: "omnisharp", section: 'loggingLevel', value: 'verbose' }
].forEach(elem => {
test(`${elem.config} ${elem.section}`, async () => {
expect(infoMessage).to.be.undefined;
expect(invokedCommand).to.be.undefined;
updateConfig(vscode, elem.config, elem.section, elem.value);
optionObservable.next(Options.Read(vscode));
expect(infoMessage).to.be.undefined;
});
});
});

teardown(() => {
infoMessage = undefined;
invokedCommand = undefined;
doClickCancel = undefined;
doClickOk = undefined;
signalCommandDone = undefined;
commandDone = undefined;
});

function getVSCode(): vscode {
let vscode = getVSCodeWithConfig();
vscode.window.showInformationMessage = async <T>(message: string, ...items: T[]) => {
infoMessage = message;
return new Promise<T>(resolve => {
doClickCancel = () => {
resolve(undefined);
};

doClickOk = () => {
resolve(...items);
};
});
};

vscode.commands.executeCommand = (command: string, ...rest: any[]) => {
invokedCommand = command;
signalCommandDone();
return undefined;
};

return vscode;
}
});
66 changes: 15 additions & 51 deletions test/unitTests/OptionObserver/OptionProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,35 @@

import { should, expect } from 'chai';
import { getVSCodeWithConfig, updateConfig } from "../testAssets/Fakes";
import OptionStream from "../../../src/observables/OptionStream";
import { vscode, ConfigurationChangeEvent } from "../../../src/vscodeAdapter";
import Disposable from "../../../src/Disposable";
import { vscode } from "../../../src/vscodeAdapter";
import OptionProvider from '../../../src/observers/OptionProvider';
import { Subject } from 'rxjs/Subject';
import { Options } from '../../../src/omnisharp/options';

suite('OptionProvider', () => {
suiteSetup(() => should());

let vscode: vscode;
let listenerFunction: Array<(e: ConfigurationChangeEvent) => any>;
let optionProvider: OptionProvider;
let optionObservable: Subject<Options>;

setup(() => {
listenerFunction = new Array<(e: ConfigurationChangeEvent) => any>();
vscode = getVSCode(listenerFunction);
let optionStream = new OptionStream(vscode);
optionProvider = new OptionProvider(optionStream);
vscode = getVSCodeWithConfig();
optionObservable = new Subject<Options>();
optionProvider = new OptionProvider(optionObservable);
});

test("Gives the default options if there is no change", () => {
let options = optionProvider.GetLatestOptions();
expect(options.path).to.be.null;
options.useGlobalMono.should.equal("auto");
options.waitForDebugger.should.equal(false);
options.loggingLevel.should.equal("information");
options.autoStart.should.equal(true);
options.projectLoadTimeout.should.equal(60);
options.maxProjectResults.should.equal(250);
options.useEditorFormattingSettings.should.equal(true);
options.useFormatting.should.equal(true);
options.showReferencesCodeLens.should.equal(true);
options.showTestsCodeLens.should.equal(true);
options.disableCodeActions.should.equal(false);
options.disableCodeActions.should.equal(false);
test("Throws exception when no options are pushed", () => {
expect(optionProvider.GetLatestOptions).to.throw();
});

test("Gives the latest options if there are changes in omnisharp config", () => {
test("Gives the latest options when options are changed", () => {
let changingConfig = "omnisharp";
updateConfig(vscode, changingConfig, 'path', "somePath");
listenerFunction.forEach(listener => listener(getConfigChangeEvent(changingConfig)));
let options = optionProvider.GetLatestOptions();
expect(options.path).to.be.equal("somePath");
});

test("Gives the latest options if there are changes in csharp config", () => {
let changingConfig = 'csharp';
updateConfig(vscode, changingConfig, 'disableCodeActions', true);
listenerFunction.forEach(listener => listener(getConfigChangeEvent(changingConfig)));
optionObservable.next(Options.Read(vscode));
updateConfig(vscode, changingConfig, 'path', "anotherPath");
optionObservable.next(Options.Read(vscode));
let options = optionProvider.GetLatestOptions();
expect(options.disableCodeActions).to.be.equal(true);
expect(options.path).to.be.equal("anotherPath");
});
});

function getVSCode(listenerFunction: Array<(e: ConfigurationChangeEvent) => any>): vscode {
let vscode = getVSCodeWithConfig();
vscode.workspace.onDidChangeConfiguration = (listener: (e: ConfigurationChangeEvent) => any, thisArgs?: any, disposables?: Disposable[]) => {
listenerFunction.push(listener);
return new Disposable(() => { });
};

return vscode;
}

function getConfigChangeEvent(changingConfig: string): ConfigurationChangeEvent {
return {
affectsConfiguration: (section: string) => section == changingConfig
};
}
});
Loading