Skip to content

Commit

Permalink
Fix command enablement by using specific activation contexts for O#, …
Browse files Browse the repository at this point in the history
…Roslyn standalone, and Roslyn devkit
  • Loading branch information
dibarbet committed Jan 4, 2024
1 parent c372c4d commit 8ba0c49
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { expect, test, beforeAll, afterAll, describe } from '@jest/globals';
import * as vscode from 'vscode';
import { activateCSharpExtension } from './integrationHelpers';
import testAssetWorkspace from './testAssets/activeTestAssetWorkspace';

describe(`Command Enablement: ${testAssetWorkspace.description}`, function () {
beforeAll(async function () {
const activation = await activateCSharpExtension();
await testAssetWorkspace.restore();
await testAssetWorkspace.waitForIdle(activation.eventStream);
});

afterAll(async () => {
await testAssetWorkspace.cleanupWorkspace();
});

test('Only expected commands are available', async function () {
const commands = await vscode.commands.getCommands(true);

// Ensure the O# commands are available.
expect(commands).toContain('o.restart');
expect(commands).toContain('o.pickProjectAndStart');
expect(commands).toContain('o.fixAll.solution');
expect(commands).toContain('o.fixAll.project');
expect(commands).toContain('o.fixAll.document');
expect(commands).toContain('o.reanalyze.allProjects');
expect(commands).toContain('o.reanalyze.currentProject');
expect(commands).toContain('dotnet.generateAssets');
expect(commands).toContain('dotnet.restore.project');
expect(commands).toContain('dotnet.restore.all');
expect(commands).toContain('dotnet.test.runTestsInContext');
expect(commands).toContain('dotnet.test.debugTestsInContext');
expect(commands).toContain('csharp.listProcess');
expect(commands).toContain('csharp.listRemoteProcess');
expect(commands).toContain('csharp.listRemoteDockerProcess');
expect(commands).toContain('csharp.attachToProcess');
expect(commands).toContain('csharp.reportIssue');
expect(commands).toContain('csharp.showDecompilationTerms');

// Ensure the non-O# commands are not available.
expect(commands).not.toContain('dotnet.openSolution');
expect(commands).not.toContain('dotnet.restartServer');
});
});
38 changes: 19 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2008,49 +2008,49 @@
"command": "o.restart",
"title": "%command.o.restart%",
"category": "OmniSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "o.pickProjectAndStart",
"title": "%command.o.pickProjectAndStart%",
"category": "OmniSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "dotnet.openSolution",
"title": "%command.dotnet.openSolution%",
"category": ".NET",
"enablement": "!config.dotnet.server.useOmnisharp && dotnet.server.activatedStandalone"
"enablement": "dotnet.server.activationContext == 'Roslyn'"
},
{
"command": "o.fixAll.solution",
"title": "%command.o.fixAll.solution%",
"category": "OmniSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "o.fixAll.project",
"title": "%command.o.fixAll.project%",
"category": "OmniSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "o.fixAll.document",
"title": "%command.o.fixAll.document%",
"category": "OmniSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "o.reanalyze.allProjects",
"title": "%command.o.reanalyze.allProjects%",
"category": "OmniSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "o.reanalyze.currentProject",
"title": "%command.o.reanalyze.currentProject%",
"category": "OmniSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "dotnet.generateAssets",
Expand All @@ -2061,13 +2061,13 @@
"command": "dotnet.restore.project",
"title": "%command.dotnet.restore.project%",
"category": ".NET",
"enablement": "dotnet.server.activatedStandalone"
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "dotnet.restore.all",
"title": "%command.dotnet.restore.all%",
"category": ".NET",
"enablement": "dotnet.server.activatedStandalone"
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "csharp.downloadDebugger",
Expand Down Expand Up @@ -2103,7 +2103,7 @@
"command": "csharp.showDecompilationTerms",
"title": "%command.csharp.showDecompilationTerms%",
"category": "CSharp",
"enablement": "config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "extension.showRazorCSharpWindow",
Expand All @@ -2124,19 +2124,19 @@
"command": "dotnet.test.runTestsInContext",
"title": "%command.dotnet.test.runTestsInContext%",
"category": ".NET",
"enablement": "dotnet.server.activatedStandalone"
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "dotnet.test.debugTestsInContext",
"title": "%command.dotnet.test.debugTestsInContext%",
"category": ".NET",
"enablement": "dotnet.server.activatedStandalone"
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "dotnet.restartServer",
"title": "%command.dotnet.restartServer%",
"category": ".NET",
"enablement": "!config.dotnet.server.useOmnisharp"
"enablement": "dotnet.server.activationContext != 'OmniSharp'"
}
],
"keybindings": [
Expand Down Expand Up @@ -5638,15 +5638,15 @@
"commandPalette": [
{
"command": "dotnet.test.runTestsInContext",
"when": "editorLangId == csharp && dotnet.server.activatedStandalone"
"when": "editorLangId == csharp && dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "dotnet.test.debugTestsInContext",
"when": "editorLangId == csharp && dotnet.server.activatedStandalone"
"when": "editorLangId == csharp && dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "o.restart",
"when": "config.dotnet.server.useOmnisharp"
"when": "dotnet.server.activationContext == 'OmniSharp'"
},
{
"command": "csharp.listProcess",
Expand Down Expand Up @@ -5678,12 +5678,12 @@
"editor/context": [
{
"command": "dotnet.test.runTestsInContext",
"when": "editorLangId == csharp && dotnet.server.activatedStandalone",
"when": "editorLangId == csharp && dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'",
"group": "2_dotnet@1"
},
{
"command": "dotnet.test.debugTestsInContext",
"when": "editorLangId == csharp && dotnet.server.activatedStandalone",
"when": "editorLangId == csharp && dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'",
"group": "2_dotnet@2"
}
]
Expand Down
7 changes: 6 additions & 1 deletion src/lsptoolshost/roslynLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,19 @@ export class RoslynLanguageServer {
_channel.appendLine('Activating C# + C# Dev Kit...');
}

// Set command enablement to use DevKit commands.
vscode.commands.executeCommand('setContext', 'dotnet.server.activationContext', 'RoslynDevKit');

const csharpDevKitArgs = this.getCSharpDevKitExportArgs();
args = args.concat(csharpDevKitArgs);

await this.setupDevKitEnvironment(env, csharpDevkitExtension);
} else {
// C# Dev Kit is not installed - continue C#-only activation.
_channel.appendLine('Activating C# standalone...');
vscode.commands.executeCommand('setContext', 'dotnet.server.activatedStandalone', true);

// Set command enablement to use roslyn standalone commands.
vscode.commands.executeCommand('setContext', 'dotnet.server.activationContext', 'Roslyn');
_wasActivatedWithCSharpDevkit = false;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ export async function activate(
roslynLanguageServerEvents
);
} else {
// Set command enablement to use O# commands.
vscode.commands.executeCommand('setContext', 'dotnet.server.activationContext', 'OmniSharp');

const dotnetChannelObserver = new DotNetChannelObserver(dotnetChannel);
const dotnetLoggerObserver = new DotnetLoggerObserver(dotnetChannel);
eventStream.subscribe(dotnetChannelObserver.post);
Expand Down
46 changes: 46 additions & 0 deletions test/integrationTests/commandEnablement.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { expect, test, beforeAll, afterAll, describe } from '@jest/globals';
import * as vscode from 'vscode';
import { activateCSharpExtension } from './integrationHelpers';
import testAssetWorkspace from './testAssets/testAssetWorkspace';

describe(`Command Enablement: ${testAssetWorkspace.description}`, function () {
beforeAll(async function () {
await activateCSharpExtension();
});

afterAll(async () => {
await testAssetWorkspace.cleanupWorkspace();
});

test('Only expected commands are available', async function () {
const commands = await vscode.commands.getCommands(true);

// Ensure the standalone Roslyn commands are available.
expect(commands).toContain('dotnet.openSolution');
expect(commands).toContain('dotnet.restartServer');
expect(commands).toContain('dotnet.generateAssets');
expect(commands).toContain('dotnet.restore.project');
expect(commands).toContain('dotnet.restore.all');
expect(commands).toContain('dotnet.test.runTestsInContext');
expect(commands).toContain('dotnet.test.debugTestsInContext');
expect(commands).toContain('csharp.listProcess');
expect(commands).toContain('csharp.listRemoteProcess');
expect(commands).toContain('csharp.listRemoteDockerProcess');
expect(commands).toContain('csharp.attachToProcess');
expect(commands).toContain('csharp.reportIssue');

// Ensure the O#-only commands are not available.
expect(commands).not.toContain('o.restart');
expect(commands).not.toContain('o.pickProjectAndStart');
expect(commands).not.toContain('o.fixAll.solution');
expect(commands).not.toContain('o.fixAll.project');
expect(commands).not.toContain('o.fixAll.document');
expect(commands).not.toContain('o.reanalyze.allProjects');
expect(commands).not.toContain('o.reanalyze.currentProject');
});
});

0 comments on commit 8ba0c49

Please sign in to comment.