Skip to content

Commit 07daf4e

Browse files
Merge pull request #721 from DustinCampbell/omnisharp-disable
Add 'omnisharp.autoStart' option that allows users to control whether OmniSharp is started automatically or not
2 parents 941e99b + 276dbdd commit 07daf4e

File tree

6 files changed

+64
-35
lines changed

6 files changed

+64
-35
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@
9999
"verbose"
100100
],
101101
"description": "Specifies the level of logging output from the OmniSharp server."
102+
},
103+
"omnisharp.autoStart": {
104+
"type": "boolean",
105+
"default": true,
106+
"description": "Specifies whether the OmniSharp server will be automatically started or not. If false, OmniSharp can be started with the 'Restart OmniSharp' command"
102107
}
103108
}
104109
},

src/features/commands.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {DotNetAttachItemsProviderFactory, AttachPicker} from './processPicker'
1919
let channel = vscode.window.createOutputChannel('.NET');
2020

2121
export default function registerCommands(server: OmnisharpServer, extensionPath: string) {
22-
let d1 = vscode.commands.registerCommand('o.restart', () => server.restart());
22+
let d1 = vscode.commands.registerCommand('o.restart', () => restartOmniSharp(server));
2323
let d2 = vscode.commands.registerCommand('o.pickProjectAndStart', () => pickProjectAndStart(server));
2424
let d3 = vscode.commands.registerCommand('o.showOutput', () => server.getChannel().show(vscode.ViewColumn.Three));
2525
let d4 = vscode.commands.registerCommand('dotnet.restore', () => dotnetRestoreAllProjects(server));
@@ -40,6 +40,15 @@ export default function registerCommands(server: OmnisharpServer, extensionPath:
4040
return vscode.Disposable.from(d1, d2, d3, d4, d5, d6, d7, d8);
4141
}
4242

43+
function restartOmniSharp(server: OmnisharpServer) {
44+
if (server.isRunning()) {
45+
server.restart();
46+
}
47+
else {
48+
server.autoStart('');
49+
}
50+
}
51+
4352
function pickProjectAndStart(server: OmnisharpServer) {
4453

4554
return findLaunchTargets().then(targets => {

src/main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import reportDiagnostics,{Advisor} from './features/diagnosticsProvider';
2020
import SignatureHelpProvider from './features/signatureHelpProvider';
2121
import registerCommands from './features/commands';
2222
import {StdioOmnisharpServer} from './omnisharp/server';
23+
import {readOptions} from './omnisharp/options';
2324
import forwardChanges from './features/changeForwarding';
2425
import reportStatus from './features/status';
2526
import * as coreclrdebug from './coreclr-debug/activate';
@@ -28,7 +29,6 @@ import * as vscode from 'vscode';
2829
import TelemetryReporter from 'vscode-extension-telemetry';
2930
import {DefinitionMetadataDocumentProvider} from './features/definitionMetadataDocumentProvider';
3031

31-
3232
export function activate(context: vscode.ExtensionContext): any {
3333

3434
const extensionId = 'ms-vscode.csharp';
@@ -88,7 +88,11 @@ export function activate(context: vscode.ExtensionContext): any {
8888

8989
// read and store last solution or folder path
9090
disposables.push(server.onBeforeServerStart(path => context.workspaceState.update('lastSolutionPathOrFolder', path)));
91-
server.autoStart(context.workspaceState.get<string>('lastSolutionPathOrFolder'));
91+
92+
const options = readOptions();
93+
if (options.autoStart) {
94+
server.autoStart(context.workspaceState.get<string>('lastSolutionPathOrFolder'));
95+
}
9296

9397
// stop server on deactivate
9498
disposables.push(new vscode.Disposable(() => {

src/omnisharp/omnisharp.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@
1313
import * as fs from 'fs-extra-promise';
1414
import * as path from 'path';
1515

16-
export interface Options {
17-
path?: string;
18-
useMono?: boolean;
19-
loggingLevel?: string;
20-
}
21-
2216
export enum Flavor {
2317
CoreCLR,
2418
Mono,

src/omnisharp/options.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
8+
import * as vscode from 'vscode';
9+
10+
export interface Options {
11+
path?: string;
12+
useMono?: boolean;
13+
loggingLevel?: string;
14+
autoStart?: boolean;
15+
}
16+
17+
export function readOptions(): Options {
18+
// Extra effort is taken below to ensure that legacy versions of options
19+
// are supported below. In particular, these are:
20+
//
21+
// - "csharp.omnisharp" -> "omnisharp.path"
22+
// - "csharp.omnisharpUsesMono" -> "omnisharp.useMono"
23+
24+
const omnisharpConfig = vscode.workspace.getConfiguration('omnisharp');
25+
const csharpConfig = vscode.workspace.getConfiguration('csharp');
26+
27+
const path = omnisharpConfig.has('path')
28+
? omnisharpConfig.get<string>('path')
29+
: csharpConfig.get<string>('omnisharp');
30+
31+
const useMono = omnisharpConfig.has('useMono')
32+
? omnisharpConfig.get<boolean>('useMono')
33+
: csharpConfig.get<boolean>('omnisharpUsesMono');
34+
35+
const loggingLevel = omnisharpConfig.get<string>('loggingLevel');
36+
const autoStart = omnisharpConfig.get<boolean>('autoStart', true);
37+
38+
return { path, useMono, loggingLevel, autoStart };
39+
}

src/omnisharp/server.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {launchOmniSharp} from './launcher';
1313
import * as protocol from './protocol';
1414
import * as omnisharp from './omnisharp';
1515
import * as download from './download';
16+
import {readOptions} from './options';
1617
import {Logger} from './logger';
1718
import {DelayTracker} from './delayTracker';
1819
import {LaunchTarget, findLaunchTargets, getDefaultFlavor} from './launcher';
@@ -107,29 +108,6 @@ export abstract class OmnisharpServer {
107108
}
108109
}
109110

110-
private _readOptions(): omnisharp.Options {
111-
// Extra effort is taken below to ensure that legacy versions of options
112-
// are supported below. In particular, these are:
113-
//
114-
// - "csharp.omnisharp" -> "omnisharp.path"
115-
// - "csharp.omnisharpUsesMono" -> "omnisharp.useMono"
116-
117-
const omnisharpConfig = vscode.workspace.getConfiguration('omnisharp');
118-
const csharpConfig = vscode.workspace.getConfiguration('csharp');
119-
120-
const path = omnisharpConfig.has('path')
121-
? omnisharpConfig.get<string>('path')
122-
: csharpConfig.get<string>('omnisharp');
123-
124-
const useMono = omnisharpConfig.has('useMono')
125-
? omnisharpConfig.get<boolean>('useMono')
126-
: csharpConfig.get<boolean>('omnisharpUsesMono');
127-
128-
const loggingLevel = omnisharpConfig.get<string>('loggingLevel');
129-
130-
return { path, useMono, loggingLevel };
131-
}
132-
133111
private _recordRequestDelay(requestName: string, elapsedTime: number) {
134112
let tracker = this._delayTrackers[requestName];
135113
if (!tracker) {
@@ -252,7 +230,7 @@ export abstract class OmnisharpServer {
252230
// --- start, stop, and connect
253231

254232
private _start(launchTarget: LaunchTarget): Promise<void> {
255-
const options = this._readOptions();
233+
const options = readOptions();
256234

257235
let flavor: omnisharp.Flavor;
258236
if (options.path !== undefined && options.useMono === true) {
@@ -403,7 +381,7 @@ export abstract class OmnisharpServer {
403381
// If there's more than one launch target, we start the server if one of the targets
404382
// matches the preferred path. Otherwise, we fire the "MultipleLaunchTargets" event,
405383
// which is handled in status.ts to display the launch target selector.
406-
if (launchTargets.length > 1) {
384+
if (launchTargets.length > 1 && preferredPath) {
407385

408386
for (let launchTarget of launchTargets) {
409387
if (launchTarget.target === preferredPath) {
@@ -425,7 +403,7 @@ export abstract class OmnisharpServer {
425403
// Attempt to find launch file path first from options, and then from the default install location.
426404
// If OmniSharp can't be found, download it.
427405

428-
const options = this._readOptions();
406+
const options = readOptions();
429407
const installDirectory = omnisharp.getInstallDirectory(flavor);
430408

431409
return new Promise<string>((resolve, reject) => {

0 commit comments

Comments
 (0)