Skip to content

Commit 0fe3547

Browse files
committed
Add document color endpoints
1 parent dca6632 commit 0fe3547

File tree

3 files changed

+70
-30
lines changed

3 files changed

+70
-30
lines changed

src/lsptoolshost/razor/razorEndpoints.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@
55

66
import { RoslynLanguageServer } from '../server/roslynLanguageServer';
77
import * as vscode from 'vscode';
8-
import { LogMessageParams, NotificationType, RequestType } from 'vscode-languageclient';
8+
import {
9+
ColorInformation,
10+
ColorPresentationParams,
11+
ColorPresentationRequest,
12+
DocumentColorParams,
13+
DocumentColorRequest,
14+
LogMessageParams,
15+
NotificationType,
16+
RequestType,
17+
} from 'vscode-languageclient';
918
import { RazorLogger } from '../../razor/src/razorLogger';
1019
import { HtmlUpdateParameters } from './htmlUpdateParameters';
1120
import { UriConverter } from '../utils/uriConverter';
1221
import { PlatformInformation } from '../../shared/platform';
1322
import { HtmlDocumentManager } from './htmlDocumentManager';
23+
import { DocumentColorHandler } from '../../razor/src/documentColor/documentColorHandler';
1424
import { razorOptions } from '../../shared/options';
25+
import { ColorPresentationHandler } from '../../razor/src/colorPresentation/colorPresentationHandler';
26+
import { ColorPresentation } from 'vscode-html-languageservice';
1527

1628
export function registerRazorEndpoints(
1729
context: vscode.ExtensionContext,
@@ -36,6 +48,23 @@ export function registerRazorEndpoints(
3648
await documentManager.updateDocumentText(uri, params.text);
3749
});
3850

51+
registerRequestHandler<DocumentColorParams, ColorInformation[]>(DocumentColorRequest.method, async (params) => {
52+
const uri = UriConverter.deserialize(params.textDocument.uri);
53+
const document = await documentManager.getDocument(uri);
54+
55+
return await DocumentColorHandler.doDocumentColorRequest(document.uri);
56+
});
57+
58+
registerRequestHandler<ColorPresentationParams, ColorPresentation[]>(
59+
ColorPresentationRequest.method,
60+
async (params) => {
61+
const uri = UriConverter.deserialize(params.textDocument.uri);
62+
const document = await documentManager.getDocument(uri);
63+
64+
return await ColorPresentationHandler.doColorPresentationRequest(document.uri, params);
65+
}
66+
);
67+
3968
// Helper method that registers a request handler, and logs errors to the Razor logger.
4069
function registerRequestHandler<Params, Result>(method: string, invocation: (params: Params) => Promise<Result>) {
4170
const requestType = new RequestType<Params, Result, Error>(method);

src/razor/src/colorPresentation/colorPresentationHandler.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,38 @@ export class ColorPresentationHandler {
5454
return this.emptyColorInformationResponse;
5555
}
5656

57-
const color = new vscode.Color(
58-
colorPresentationParams.color.red,
59-
colorPresentationParams.color.green,
60-
colorPresentationParams.color.blue,
61-
colorPresentationParams.color.alpha
62-
);
6357
const virtualHtmlUri = razorDocument.htmlDocument.uri;
6458

65-
const colorPresentations = await vscode.commands.executeCommand<vscode.ColorPresentation[]>(
66-
'vscode.executeColorPresentationProvider',
67-
color,
68-
new ColorPresentationContext(virtualHtmlUri, colorPresentationParams.range)
69-
);
70-
71-
const serializableColorPresentations = this.SerializeColorPresentations(colorPresentations);
72-
return serializableColorPresentations;
59+
return await ColorPresentationHandler.doColorPresentationRequest(virtualHtmlUri, colorPresentationParams);
7360
} catch (error) {
7461
this.logger.logWarning(`${ColorPresentationHandler.provideHtmlColorPresentation} failed with ${error}`);
7562
}
7663

7764
return this.emptyColorInformationResponse;
7865
}
7966

80-
private SerializeColorPresentations(colorPresentations: vscode.ColorPresentation[]) {
67+
public static async doColorPresentationRequest(
68+
virtualHtmlUri: vscode.Uri,
69+
colorPresentationParams: SerializableColorPresentationParams
70+
) {
71+
const color = new vscode.Color(
72+
colorPresentationParams.color.red,
73+
colorPresentationParams.color.green,
74+
colorPresentationParams.color.blue,
75+
colorPresentationParams.color.alpha
76+
);
77+
78+
const colorPresentations = await vscode.commands.executeCommand<vscode.ColorPresentation[]>(
79+
'vscode.executeColorPresentationProvider',
80+
color,
81+
new ColorPresentationContext(virtualHtmlUri, colorPresentationParams.range)
82+
);
83+
84+
const serializableColorPresentations = ColorPresentationHandler.SerializeColorPresentations(colorPresentations);
85+
return serializableColorPresentations;
86+
}
87+
88+
private static SerializeColorPresentations(colorPresentations: vscode.ColorPresentation[]) {
8189
const serializableColorPresentations = new Array<SerializableColorPresentation>();
8290
for (const colorPresentation of colorPresentations) {
8391
let serializedTextEdit: any = null;

src/razor/src/documentColor/documentColorHandler.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,27 @@ export class DocumentColorHandler {
6767
}
6868

6969
const virtualHtmlUri = razorDocument.htmlDocument.uri;
70-
71-
const colorInformation = await vscode.commands.executeCommand<vscode.ColorInformation[]>(
72-
'vscode.executeDocumentColorProvider',
73-
virtualHtmlUri
74-
);
75-
76-
const serializableColorInformation = new Array<SerializableColorInformation>();
77-
for (const color of colorInformation) {
78-
const serializableRange = convertRangeToSerializable(color.range);
79-
const serializableColor = new SerializableColorInformation(serializableRange, color.color);
80-
serializableColorInformation.push(serializableColor);
81-
}
82-
83-
return serializableColorInformation;
70+
return await DocumentColorHandler.doDocumentColorRequest(virtualHtmlUri);
8471
} catch (error) {
8572
this.logger.logWarning(`${DocumentColorHandler.provideHtmlDocumentColorEndpoint} failed with ${error}`);
8673
}
8774

8875
return this.emptyColorInformationResponse;
8976
}
77+
78+
public static async doDocumentColorRequest(virtualHtmlUri: vscode.Uri) {
79+
const colorInformation = await vscode.commands.executeCommand<vscode.ColorInformation[]>(
80+
'vscode.executeDocumentColorProvider',
81+
virtualHtmlUri
82+
);
83+
84+
const serializableColorInformation = new Array<SerializableColorInformation>();
85+
for (const color of colorInformation) {
86+
const serializableRange = convertRangeToSerializable(color.range);
87+
const serializableColor = new SerializableColorInformation(serializableRange, color.color);
88+
serializableColorInformation.push(serializableColor);
89+
}
90+
91+
return serializableColorInformation;
92+
}
9093
}

0 commit comments

Comments
 (0)