Skip to content

Commit

Permalink
Merge pull request #7333 from dotnet/dev/jorobich/razor-project-context
Browse files Browse the repository at this point in the history
Enable Project Context status item for Razor files
  • Loading branch information
JoeRobich committed Jul 12, 2024
2 parents b1a2e1d + d79b0ca commit a3d678e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/lsptoolshost/languageStatusBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ class WorkspaceStatus {

class ProjectContextStatus {
static createStatusItem(context: vscode.ExtensionContext, languageServer: RoslynLanguageServer) {
const documentSelector = combineDocumentSelectors(
languageServerOptions.documentSelector,
RazorLanguage.documentSelector
);
const projectContextService = languageServer._projectContextService;

const item = vscode.languages.createLanguageStatusItem(
'csharp.projectContextStatus',
languageServerOptions.documentSelector
);
const item = vscode.languages.createLanguageStatusItem('csharp.projectContextStatus', documentSelector);
item.name = vscode.l10n.t('C# Project Context Status');
item.detail = vscode.l10n.t('Active File Context');
context.subscriptions.push(item);
Expand Down
32 changes: 30 additions & 2 deletions src/lsptoolshost/services/projectContextService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { TextDocumentIdentifier } from 'vscode-languageserver-protocol';
import { UriConverter } from '../uriConverter';
import { LanguageServerEvents } from '../languageServerEvents';
import { ServerState } from '../serverStateChange';
import { DynamicFileInfoHandler } from '../../razor/src/dynamicFile/dynamicFileInfoHandler';
import { ProvideDynamicFileResponse } from '../../razor/src/dynamicFile/provideDynamicFileResponse';
import { ProvideDynamicFileParams } from '../../razor/src/dynamicFile/provideDynamicFileParams';

export interface ProjectContextChangeEvent {
uri: vscode.Uri;
Expand Down Expand Up @@ -39,11 +42,22 @@ export class ProjectContextService {

public async refresh() {
const textEditor = vscode.window.activeTextEditor;
if (textEditor?.document?.languageId !== 'csharp') {
const languageId = textEditor?.document?.languageId;
if (languageId !== 'csharp' && languageId !== 'aspnetcorerazor') {
return;
}

const uri = textEditor.document.uri;
let uri = textEditor!.document.uri;

// If the active document is a Razor file, we need to map it back to a C# file.
if (languageId === 'aspnetcorerazor') {
const virtualUri = await this.getVirtualCSharpUri(uri);
if (!virtualUri) {
return;
}

uri = virtualUri;
}

// If we have an open request, cancel it.
this._source.cancel();
Expand All @@ -58,6 +72,20 @@ export class ProjectContextService {
this._contextChangeEmitter.fire({ uri, context });
}

private async getVirtualCSharpUri(uri: vscode.Uri): Promise<vscode.Uri | undefined> {
const response = await vscode.commands.executeCommand<ProvideDynamicFileResponse>(
DynamicFileInfoHandler.provideDynamicFileInfoCommand,
new ProvideDynamicFileParams([uri.fsPath])
);

const responseUri = response.generatedFiles[0];
if (!responseUri) {
return undefined;
}

return UriConverter.deserialize(responseUri);
}

private async getProjectContexts(
uri: vscode.Uri,
token: vscode.CancellationToken
Expand Down
8 changes: 8 additions & 0 deletions src/razor/src/document/razorDocumentSynchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export class RazorDocumentSynchronizer {
}

private removeSynchronization(context: SynchronizationContext) {
context.dispose();

const documentKey = getUriPath(context.projectedDocument.uri);
const synchronizations = this.synchronizations[documentKey];
clearTimeout(context.timeoutId);
Expand Down Expand Up @@ -167,6 +169,11 @@ export class RazorDocumentSynchronizer {
reject(reason);
}
},
dispose: () => {
while (rejectionsForCancel.length > 0) {
rejectionsForCancel.pop();
}
},
projectedDocumentSynchronized,
onProjectedDocumentSynchronized,
projectedTextDocumentSynchronized,
Expand Down Expand Up @@ -271,4 +278,5 @@ interface SynchronizationContext {
readonly projectedTextDocumentSynchronized: () => void;
readonly onProjectedTextDocumentSynchronized: Promise<void>;
readonly cancel: (reason: string) => void;
readonly dispose: () => void;
}
7 changes: 5 additions & 2 deletions test/razorIntegrationTests/formatting.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

Expand All @@ -20,10 +20,13 @@ describe(`Razor Formatting ${testAssetWorkspace.description}`, function () {
const htmlConfig = vscode.workspace.getConfiguration('html');
await htmlConfig.update('format.enable', true);

await integrationHelpers.openFileInWorkspaceAsync(path.join('Pages', 'BadlyFormatted.razor'));
await integrationHelpers.activateCSharpExtension();
});

beforeEach(async function () {
await integrationHelpers.openFileInWorkspaceAsync(path.join('Pages', 'BadlyFormatted.razor'));
});

afterAll(async () => {
await testAssetWorkspace.cleanupWorkspace();
});
Expand Down
7 changes: 5 additions & 2 deletions test/razorIntegrationTests/hover.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

Expand All @@ -15,10 +15,13 @@ describe(`Razor Hover ${testAssetWorkspace.description}`, function () {
return;
}

await integrationHelpers.openFileInWorkspaceAsync(path.join('Pages', 'Index.cshtml'));
await integrationHelpers.activateCSharpExtension();
});

beforeEach(async function () {
await integrationHelpers.openFileInWorkspaceAsync(path.join('Pages', 'Index.cshtml'));
});

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

0 comments on commit a3d678e

Please sign in to comment.