Skip to content

Commit 2db499b

Browse files
authored
Add cohost option to Razor (#8189)
2 parents d07c2de + 6831864 commit 2db499b

8 files changed

+48
-13
lines changed

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,11 @@
15361536
"type": "boolean",
15371537
"default": true,
15381538
"description": "%configuration.razor.languageServer.useNewFormattingEngine%"
1539+
},
1540+
"razor.languageServer.cohostingEnabled": {
1541+
"type": "boolean",
1542+
"default": false,
1543+
"description": "%configuration.razor.languageServer.cohostingEnabled%"
15391544
}
15401545
}
15411546
},
@@ -5596,4 +5601,4 @@
55965601
}
55975602
}
55985603
}
5599-
}
5604+
}

package.nls.json

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
"configuration.razor.languageServer.forceRuntimeCodeGeneration": "(EXPERIMENTAL) Enable combined design time/runtime code generation for Razor files",
132132
"configuration.razor.languageServer.suppressLspErrorToasts": "Suppresses error toasts from showing up if the server encounters a recoverable error.",
133133
"configuration.razor.languageServer.useNewFormattingEngine": "Use the new Razor formatting engine.",
134+
"configuration.razor.languageServer.cohostingEnabled": "(EXPERIMENTAL) Enable Razor cohosting.",
134135
"debuggers.coreclr.configurationSnippets.label.console-local": ".NET: Launch Executable file (Console)",
135136
"debuggers.coreclr.configurationSnippets.label.web-local": ".NET: Launch Executable file (Web)",
136137
"debuggers.coreclr.configurationSnippets.label.attach-local": ".NET: Attach to a .NET process",

src/lsptoolshost/server/roslynLanguageServer.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,23 @@ export class RoslynLanguageServer {
617617
? path.join(context.extension.extensionPath, '.razor')
618618
: razorOptions.razorServerPath;
619619

620-
args.push('--razorSourceGenerator', path.join(razorPath, 'Microsoft.CodeAnalysis.Razor.Compiler.dll'));
620+
let razorComponentPath = '';
621+
getComponentPaths('razorExtension', languageServerOptions).forEach((extPath) => {
622+
additionalExtensionPaths.push(extPath);
623+
razorComponentPath = path.dirname(extPath);
624+
});
625+
626+
// If cohosting is enabled we get the source generator from the razor component path
627+
const razorSourceGeneratorPath = razorOptions.cohostingEnabled ? razorComponentPath : razorPath;
621628

622629
args.push(
623-
'--razorDesignTimePath',
624-
path.join(razorPath, 'Targets', 'Microsoft.NET.Sdk.Razor.DesignTime.targets')
630+
'--razorSourceGenerator',
631+
path.join(razorSourceGeneratorPath, 'Microsoft.CodeAnalysis.Razor.Compiler.dll')
625632
);
626633

627-
getComponentPaths('razorExtension', languageServerOptions).forEach((path) =>
628-
additionalExtensionPaths.push(path)
634+
args.push(
635+
'--razorDesignTimePath',
636+
path.join(razorPath, 'Targets', 'Microsoft.NET.Sdk.Razor.DesignTime.targets')
629637
);
630638

631639
// Get the brokered service pipe name from C# Dev Kit (if installed).

src/razor/src/document/razorDocumentManager.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class RazorDocumentManager implements IRazorDocumentManager {
3333
public razorDocumentGenerationInitialized = false;
3434

3535
constructor(
36-
private readonly serverClient: RazorLanguageServerClient,
36+
private readonly serverClient: RazorLanguageServerClient | undefined,
3737
private readonly logger: RazorLogger,
3838
private readonly telemetryReporter: TelemetryReporter,
3939
private readonly platformInfo: PlatformInformation
@@ -128,13 +128,16 @@ export class RazorDocumentManager implements IRazorDocumentManager {
128128

129129
this.closeDocument(document.uri);
130130
});
131-
this.serverClient.onNotification('razor/updateCSharpBuffer', async (updateBufferRequest) =>
132-
this.updateCSharpBuffer(updateBufferRequest)
133-
);
134131

135-
this.serverClient.onNotification('razor/updateHtmlBuffer', async (updateBufferRequest) =>
136-
this.updateHtmlBuffer(updateBufferRequest)
137-
);
132+
if (this.serverClient !== undefined) {
133+
this.serverClient.onNotification('razor/updateCSharpBuffer', async (updateBufferRequest) =>
134+
this.updateCSharpBuffer(updateBufferRequest)
135+
);
136+
137+
this.serverClient.onNotification('razor/updateHtmlBuffer', async (updateBufferRequest) =>
138+
this.updateHtmlBuffer(updateBufferRequest)
139+
);
140+
}
138141

139142
return vscode.Disposable.from(watcher, didCreateRegistration, didOpenRegistration, didCloseRegistration);
140143
}
@@ -163,6 +166,10 @@ export class RazorDocumentManager implements IRazorDocumentManager {
163166
}
164167

165168
public async ensureRazorInitialized() {
169+
if (this.serverClient === undefined) {
170+
return;
171+
}
172+
166173
// Kick off the generation of all Razor documents so that components are
167174
// discovered correctly. We need to do this even if a Razor file isn't
168175
// open yet to handle the scenario where the user opens a C# file before

src/razor/src/extension.ts

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ export async function activate(
7878
logger
7979
);
8080

81+
if (razorOptions.cohostingEnabled) {
82+
// TODO: We still need a document manager for Html, so need to do _some_ of the below, just not sure what yet,
83+
// and it needs to be able to take a roslynLanguageServerClient instead of a razorLanguageServerClient I guess.
84+
85+
return;
86+
}
87+
8188
const hostExecutableResolver = new DotnetRuntimeExtensionResolver(
8289
platformInfo,
8390
() => razorOptions.serverPath,

src/razor/src/razorLanguageServerOptions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export interface RazorLanguageServerOptions {
1313
forceRuntimeCodeGeneration: boolean;
1414
suppressErrorToasts: boolean;
1515
useNewFormattingEngine: boolean;
16+
cohostingEnabled: boolean;
1617
}

src/razor/src/razorLanguageServerOptionsResolver.ts

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function resolveRazorLanguageServerOptions(
3737

3838
const suppressErrorToasts = serverConfig.get<boolean>('suppressLspErrorToasts');
3939
const useNewFormattingEngine = serverConfig.get<boolean>('useNewFormattingEngine');
40+
const cohostingEnabled = serverConfig.get<boolean>('cohostingEnabled');
4041

4142
return {
4243
serverPath: languageServerExecutablePath,
@@ -46,6 +47,7 @@ export function resolveRazorLanguageServerOptions(
4647
forceRuntimeCodeGeneration,
4748
suppressErrorToasts,
4849
useNewFormattingEngine,
50+
cohostingEnabled,
4951
} as RazorLanguageServerOptions;
5052
}
5153

src/shared/options.ts

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export interface RazorOptions {
8888
readonly razorDevMode: boolean;
8989
readonly razorPluginPath: string;
9090
readonly razorServerPath: string;
91+
readonly cohostingEnabled: boolean;
9192
}
9293

9394
class CommonOptionsImpl implements CommonOptions {
@@ -430,6 +431,9 @@ class RazorOptionsImpl implements RazorOptions {
430431
public get razorServerPath() {
431432
return readOption<string>('razor.languageServer.directory', '');
432433
}
434+
public get cohostingEnabled() {
435+
return readOption<boolean>('razor.languageServer.cohostingEnabled', false);
436+
}
433437
}
434438

435439
export const commonOptions: CommonOptions = new CommonOptionsImpl();

0 commit comments

Comments
 (0)