Skip to content

Commit 5946723

Browse files
authored
Merge pull request #6782 from dibarbet/fix_command_enablement
Fix command enablement by using specific activation contexts for O#, Roslyn standalone, and Roslyn devkit
2 parents 4659bf2 + 4286765 commit 5946723

File tree

6 files changed

+133
-25
lines changed

6 files changed

+133
-25
lines changed
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+
import { expect, test, beforeAll, afterAll, describe } from '@jest/globals';
7+
import * as vscode from 'vscode';
8+
import { activateCSharpExtension } from './integrationHelpers';
9+
import testAssetWorkspace from './testAssets/activeTestAssetWorkspace';
10+
import { CommonCommands, OmniSharpCommands, RoslynCommands } from '../../test/integrationTests/expectedCommands';
11+
12+
describe(`Command Enablement: ${testAssetWorkspace.description}`, function () {
13+
beforeAll(async function () {
14+
const activation = await activateCSharpExtension();
15+
await testAssetWorkspace.restore();
16+
await testAssetWorkspace.waitForIdle(activation.eventStream);
17+
});
18+
19+
afterAll(async () => {
20+
await testAssetWorkspace.cleanupWorkspace();
21+
});
22+
23+
test('Omnisharp commands are available', async function () {
24+
const commands = await vscode.commands.getCommands(true);
25+
26+
// Ensure O# commands are available.
27+
OmniSharpCommands.forEach((command) => {
28+
expect(commands).toContain(command);
29+
});
30+
CommonCommands.forEach((command) => {
31+
expect(commands).toContain(command);
32+
});
33+
34+
// Ensure Roslyn standalone commands are not available.
35+
RoslynCommands.forEach((command) => {
36+
expect(commands).not.toContain(command);
37+
});
38+
});
39+
});

package.json

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,49 +2007,49 @@
20072007
"command": "o.restart",
20082008
"title": "%command.o.restart%",
20092009
"category": "OmniSharp",
2010-
"enablement": "config.dotnet.server.useOmnisharp"
2010+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
20112011
},
20122012
{
20132013
"command": "o.pickProjectAndStart",
20142014
"title": "%command.o.pickProjectAndStart%",
20152015
"category": "OmniSharp",
2016-
"enablement": "config.dotnet.server.useOmnisharp"
2016+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
20172017
},
20182018
{
20192019
"command": "dotnet.openSolution",
20202020
"title": "%command.dotnet.openSolution%",
20212021
"category": ".NET",
2022-
"enablement": "!config.dotnet.server.useOmnisharp && dotnet.server.activatedStandalone"
2022+
"enablement": "dotnet.server.activationContext == 'Roslyn'"
20232023
},
20242024
{
20252025
"command": "o.fixAll.solution",
20262026
"title": "%command.o.fixAll.solution%",
20272027
"category": "OmniSharp",
2028-
"enablement": "config.dotnet.server.useOmnisharp"
2028+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
20292029
},
20302030
{
20312031
"command": "o.fixAll.project",
20322032
"title": "%command.o.fixAll.project%",
20332033
"category": "OmniSharp",
2034-
"enablement": "config.dotnet.server.useOmnisharp"
2034+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
20352035
},
20362036
{
20372037
"command": "o.fixAll.document",
20382038
"title": "%command.o.fixAll.document%",
20392039
"category": "OmniSharp",
2040-
"enablement": "config.dotnet.server.useOmnisharp"
2040+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
20412041
},
20422042
{
20432043
"command": "o.reanalyze.allProjects",
20442044
"title": "%command.o.reanalyze.allProjects%",
20452045
"category": "OmniSharp",
2046-
"enablement": "config.dotnet.server.useOmnisharp"
2046+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
20472047
},
20482048
{
20492049
"command": "o.reanalyze.currentProject",
20502050
"title": "%command.o.reanalyze.currentProject%",
20512051
"category": "OmniSharp",
2052-
"enablement": "config.dotnet.server.useOmnisharp"
2052+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
20532053
},
20542054
{
20552055
"command": "dotnet.generateAssets",
@@ -2060,18 +2060,13 @@
20602060
"command": "dotnet.restore.project",
20612061
"title": "%command.dotnet.restore.project%",
20622062
"category": ".NET",
2063-
"enablement": "dotnet.server.activatedStandalone"
2063+
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
20642064
},
20652065
{
20662066
"command": "dotnet.restore.all",
20672067
"title": "%command.dotnet.restore.all%",
20682068
"category": ".NET",
2069-
"enablement": "dotnet.server.activatedStandalone"
2070-
},
2071-
{
2072-
"command": "csharp.downloadDebugger",
2073-
"title": "%command.csharp.downloadDebugger%",
2074-
"category": "Debug"
2069+
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
20752070
},
20762071
{
20772072
"command": "csharp.listProcess",
@@ -2102,7 +2097,7 @@
21022097
"command": "csharp.showDecompilationTerms",
21032098
"title": "%command.csharp.showDecompilationTerms%",
21042099
"category": "CSharp",
2105-
"enablement": "config.dotnet.server.useOmnisharp"
2100+
"enablement": "dotnet.server.activationContext == 'OmniSharp'"
21062101
},
21072102
{
21082103
"command": "extension.showRazorCSharpWindow",
@@ -2123,19 +2118,19 @@
21232118
"command": "dotnet.test.runTestsInContext",
21242119
"title": "%command.dotnet.test.runTestsInContext%",
21252120
"category": ".NET",
2126-
"enablement": "dotnet.server.activatedStandalone"
2121+
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
21272122
},
21282123
{
21292124
"command": "dotnet.test.debugTestsInContext",
21302125
"title": "%command.dotnet.test.debugTestsInContext%",
21312126
"category": ".NET",
2132-
"enablement": "dotnet.server.activatedStandalone"
2127+
"enablement": "dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
21332128
},
21342129
{
21352130
"command": "dotnet.restartServer",
21362131
"title": "%command.dotnet.restartServer%",
21372132
"category": ".NET",
2138-
"enablement": "!config.dotnet.server.useOmnisharp"
2133+
"enablement": "dotnet.server.activationContext != 'OmniSharp'"
21392134
}
21402135
],
21412136
"keybindings": [
@@ -5637,15 +5632,15 @@
56375632
"commandPalette": [
56385633
{
56395634
"command": "dotnet.test.runTestsInContext",
5640-
"when": "editorLangId == csharp && dotnet.server.activatedStandalone"
5635+
"when": "editorLangId == csharp && dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
56415636
},
56425637
{
56435638
"command": "dotnet.test.debugTestsInContext",
5644-
"when": "editorLangId == csharp && dotnet.server.activatedStandalone"
5639+
"when": "editorLangId == csharp && dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp'"
56455640
},
56465641
{
56475642
"command": "o.restart",
5648-
"when": "config.dotnet.server.useOmnisharp"
5643+
"when": "dotnet.server.activationContext == 'OmniSharp'"
56495644
},
56505645
{
56515646
"command": "csharp.listProcess",
@@ -5677,12 +5672,12 @@
56775672
"editor/context": [
56785673
{
56795674
"command": "dotnet.test.runTestsInContext",
5680-
"when": "editorLangId == csharp && dotnet.server.activatedStandalone",
5675+
"when": "editorLangId == csharp && (dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp')",
56815676
"group": "2_dotnet@1"
56825677
},
56835678
{
56845679
"command": "dotnet.test.debugTestsInContext",
5685-
"when": "editorLangId == csharp && dotnet.server.activatedStandalone",
5680+
"when": "editorLangId == csharp && (dotnet.server.activationContext == 'Roslyn' || dotnet.server.activationContext == 'OmniSharp')",
56865681
"group": "2_dotnet@2"
56875682
}
56885683
]

src/lsptoolshost/roslynLanguageServer.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,19 @@ export class RoslynLanguageServer {
518518
_channel.appendLine('Activating C# + C# Dev Kit...');
519519
}
520520

521+
// Set command enablement as soon as we know devkit is available.
522+
vscode.commands.executeCommand('setContext', 'dotnet.server.activationContext', 'RoslynDevKit');
523+
521524
const csharpDevKitArgs = this.getCSharpDevKitExportArgs();
522525
args = args.concat(csharpDevKitArgs);
523526

524527
await this.setupDevKitEnvironment(env, csharpDevkitExtension);
525528
} else {
526529
// C# Dev Kit is not installed - continue C#-only activation.
527530
_channel.appendLine('Activating C# standalone...');
528-
vscode.commands.executeCommand('setContext', 'dotnet.server.activatedStandalone', true);
531+
532+
// Set command enablement to use roslyn standalone commands.
533+
vscode.commands.executeCommand('setContext', 'dotnet.server.activationContext', 'Roslyn');
529534
_wasActivatedWithCSharpDevkit = false;
530535
}
531536

src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ export async function activate(
184184
roslynLanguageServerEvents
185185
);
186186
} else {
187+
// Set command enablement to use O# commands.
188+
vscode.commands.executeCommand('setContext', 'dotnet.server.activationContext', 'OmniSharp');
189+
187190
const dotnetChannelObserver = new DotNetChannelObserver(dotnetChannel);
188191
const dotnetLoggerObserver = new DotnetLoggerObserver(dotnetChannel);
189192
eventStream.subscribe(dotnetChannelObserver.post);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
import { expect, test, beforeAll, afterAll, describe } from '@jest/globals';
7+
import * as vscode from 'vscode';
8+
import { activateCSharpExtension } from './integrationHelpers';
9+
import testAssetWorkspace from './testAssets/testAssetWorkspace';
10+
import { CommonCommands, OmniSharpCommands, RoslynCommands } from './expectedCommands';
11+
12+
describe(`Command Enablement: ${testAssetWorkspace.description}`, function () {
13+
beforeAll(async function () {
14+
await activateCSharpExtension();
15+
});
16+
17+
afterAll(async () => {
18+
await testAssetWorkspace.cleanupWorkspace();
19+
});
20+
21+
test('Roslyn commands are available', async function () {
22+
const commands = await vscode.commands.getCommands(true);
23+
24+
// Ensure the standalone Roslyn commands are available.
25+
RoslynCommands.forEach((command) => {
26+
expect(commands).toContain(command);
27+
});
28+
CommonCommands.forEach((command) => {
29+
expect(commands).toContain(command);
30+
});
31+
32+
// Ensure O# commands are not available.
33+
OmniSharpCommands.forEach((command) => {
34+
expect(commands).not.toContain(command);
35+
});
36+
});
37+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
export const OmniSharpCommands = [
7+
'o.restart',
8+
'o.pickProjectAndStart',
9+
'o.fixAll.solution',
10+
'o.fixAll.project',
11+
'o.fixAll.document',
12+
'o.reanalyze.allProjects',
13+
'o.reanalyze.currentProject',
14+
];
15+
16+
export const RoslynCommands = ['dotnet.openSolution', 'dotnet.restartServer'];
17+
18+
export const CommonCommands = [
19+
'dotnet.generateAssets',
20+
'dotnet.restore.project',
21+
'dotnet.restore.all',
22+
'dotnet.test.runTestsInContext',
23+
'dotnet.test.debugTestsInContext',
24+
'csharp.listProcess',
25+
'csharp.listRemoteProcess',
26+
'csharp.listRemoteDockerProcess',
27+
'csharp.attachToProcess',
28+
'csharp.reportIssue',
29+
];

0 commit comments

Comments
 (0)