diff --git a/package.json b/package.json index 7b1a7f242..fb169e016 100644 --- a/package.json +++ b/package.json @@ -99,6 +99,11 @@ "verbose" ], "description": "Specifies the level of logging output from the OmniSharp server." + }, + "omnisharp.autoStart": { + "type": "boolean", + "default": true, + "description": "Specifies whether the OmniSharp server will be automatically started or not. If false, OmniSharp can be started with the 'Restart OmniSharp' command" } } }, diff --git a/src/features/commands.ts b/src/features/commands.ts index 3da9cd271..c0facef07 100644 --- a/src/features/commands.ts +++ b/src/features/commands.ts @@ -19,7 +19,7 @@ import {DotNetAttachItemsProviderFactory, AttachPicker} from './processPicker' let channel = vscode.window.createOutputChannel('.NET'); export default function registerCommands(server: OmnisharpServer, extensionPath: string) { - let d1 = vscode.commands.registerCommand('o.restart', () => server.restart()); + let d1 = vscode.commands.registerCommand('o.restart', () => restartOmniSharp(server)); let d2 = vscode.commands.registerCommand('o.pickProjectAndStart', () => pickProjectAndStart(server)); let d3 = vscode.commands.registerCommand('o.showOutput', () => server.getChannel().show(vscode.ViewColumn.Three)); let d4 = vscode.commands.registerCommand('dotnet.restore', () => dotnetRestoreAllProjects(server)); @@ -40,6 +40,15 @@ export default function registerCommands(server: OmnisharpServer, extensionPath: return vscode.Disposable.from(d1, d2, d3, d4, d5, d6, d7, d8); } +function restartOmniSharp(server: OmnisharpServer) { + if (server.isRunning()) { + server.restart(); + } + else { + server.autoStart(''); + } +} + function pickProjectAndStart(server: OmnisharpServer) { return findLaunchTargets().then(targets => { diff --git a/src/main.ts b/src/main.ts index 7f57908e3..11f1ca80b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,6 +20,7 @@ import reportDiagnostics,{Advisor} from './features/diagnosticsProvider'; import SignatureHelpProvider from './features/signatureHelpProvider'; import registerCommands from './features/commands'; import {StdioOmnisharpServer} from './omnisharp/server'; +import {readOptions} from './omnisharp/options'; import forwardChanges from './features/changeForwarding'; import reportStatus from './features/status'; import * as coreclrdebug from './coreclr-debug/activate'; @@ -28,7 +29,6 @@ import * as vscode from 'vscode'; import TelemetryReporter from 'vscode-extension-telemetry'; import {DefinitionMetadataDocumentProvider} from './features/definitionMetadataDocumentProvider'; - export function activate(context: vscode.ExtensionContext): any { const extensionId = 'ms-vscode.csharp'; @@ -88,7 +88,11 @@ export function activate(context: vscode.ExtensionContext): any { // read and store last solution or folder path disposables.push(server.onBeforeServerStart(path => context.workspaceState.update('lastSolutionPathOrFolder', path))); - server.autoStart(context.workspaceState.get('lastSolutionPathOrFolder')); + + const options = readOptions(); + if (options.autoStart) { + server.autoStart(context.workspaceState.get('lastSolutionPathOrFolder')); + } // stop server on deactivate disposables.push(new vscode.Disposable(() => { diff --git a/src/omnisharp/omnisharp.ts b/src/omnisharp/omnisharp.ts index d5794d86e..f4362ba35 100644 --- a/src/omnisharp/omnisharp.ts +++ b/src/omnisharp/omnisharp.ts @@ -13,12 +13,6 @@ import * as fs from 'fs-extra-promise'; import * as path from 'path'; -export interface Options { - path?: string; - useMono?: boolean; - loggingLevel?: string; -} - export enum Flavor { CoreCLR, Mono, diff --git a/src/omnisharp/options.ts b/src/omnisharp/options.ts new file mode 100644 index 000000000..05fe0e757 --- /dev/null +++ b/src/omnisharp/options.ts @@ -0,0 +1,39 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import * as vscode from 'vscode'; + +export interface Options { + path?: string; + useMono?: boolean; + loggingLevel?: string; + autoStart?: boolean; +} + +export function readOptions(): Options { + // Extra effort is taken below to ensure that legacy versions of options + // are supported below. In particular, these are: + // + // - "csharp.omnisharp" -> "omnisharp.path" + // - "csharp.omnisharpUsesMono" -> "omnisharp.useMono" + + const omnisharpConfig = vscode.workspace.getConfiguration('omnisharp'); + const csharpConfig = vscode.workspace.getConfiguration('csharp'); + + const path = omnisharpConfig.has('path') + ? omnisharpConfig.get('path') + : csharpConfig.get('omnisharp'); + + const useMono = omnisharpConfig.has('useMono') + ? omnisharpConfig.get('useMono') + : csharpConfig.get('omnisharpUsesMono'); + + const loggingLevel = omnisharpConfig.get('loggingLevel'); + const autoStart = omnisharpConfig.get('autoStart', true); + + return { path, useMono, loggingLevel, autoStart }; +} \ No newline at end of file diff --git a/src/omnisharp/server.ts b/src/omnisharp/server.ts index 885dcc3e8..72b63181f 100644 --- a/src/omnisharp/server.ts +++ b/src/omnisharp/server.ts @@ -13,6 +13,7 @@ import {launchOmniSharp} from './launcher'; import * as protocol from './protocol'; import * as omnisharp from './omnisharp'; import * as download from './download'; +import {readOptions} from './options'; import {Logger} from './logger'; import {DelayTracker} from './delayTracker'; import {LaunchTarget, findLaunchTargets, getDefaultFlavor} from './launcher'; @@ -107,29 +108,6 @@ export abstract class OmnisharpServer { } } - private _readOptions(): omnisharp.Options { - // Extra effort is taken below to ensure that legacy versions of options - // are supported below. In particular, these are: - // - // - "csharp.omnisharp" -> "omnisharp.path" - // - "csharp.omnisharpUsesMono" -> "omnisharp.useMono" - - const omnisharpConfig = vscode.workspace.getConfiguration('omnisharp'); - const csharpConfig = vscode.workspace.getConfiguration('csharp'); - - const path = omnisharpConfig.has('path') - ? omnisharpConfig.get('path') - : csharpConfig.get('omnisharp'); - - const useMono = omnisharpConfig.has('useMono') - ? omnisharpConfig.get('useMono') - : csharpConfig.get('omnisharpUsesMono'); - - const loggingLevel = omnisharpConfig.get('loggingLevel'); - - return { path, useMono, loggingLevel }; - } - private _recordRequestDelay(requestName: string, elapsedTime: number) { let tracker = this._delayTrackers[requestName]; if (!tracker) { @@ -252,7 +230,7 @@ export abstract class OmnisharpServer { // --- start, stop, and connect private _start(launchTarget: LaunchTarget): Promise { - const options = this._readOptions(); + const options = readOptions(); let flavor: omnisharp.Flavor; if (options.path !== undefined && options.useMono === true) { @@ -403,7 +381,7 @@ export abstract class OmnisharpServer { // If there's more than one launch target, we start the server if one of the targets // matches the preferred path. Otherwise, we fire the "MultipleLaunchTargets" event, // which is handled in status.ts to display the launch target selector. - if (launchTargets.length > 1) { + if (launchTargets.length > 1 && preferredPath) { for (let launchTarget of launchTargets) { if (launchTarget.target === preferredPath) { @@ -425,7 +403,7 @@ export abstract class OmnisharpServer { // Attempt to find launch file path first from options, and then from the default install location. // If OmniSharp can't be found, download it. - const options = this._readOptions(); + const options = readOptions(); const installDirectory = omnisharp.getInstallDirectory(flavor); return new Promise((resolve, reject) => {