mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-27 03:54:24 +01:00
Re #34366. Extensions define color formats.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
import { languages, window, commands, ExtensionContext, TextDocument, ColorRange, ColorFormat, Color } from 'vscode';
|
||||
import { languages, window, commands, ExtensionContext, TextDocument, ColorInformation, ColorPresentation, Color, TextEdit as CodeTextEdit } from 'vscode';
|
||||
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, TextEdit } from 'vscode-languageclient';
|
||||
|
||||
import { ConfigurationFeature } from 'vscode-languageclient/lib/proposed';
|
||||
@@ -60,38 +60,45 @@ export function activate(context: ExtensionContext) {
|
||||
client.onReady().then(_ => {
|
||||
// register color provider
|
||||
context.subscriptions.push(languages.registerColorProvider(documentSelector, {
|
||||
provideDocumentColors(document: TextDocument): Thenable<ColorRange[]> {
|
||||
provideDocumentColors(document: TextDocument): Thenable<ColorInformation[]> {
|
||||
let params = client.code2ProtocolConverter.asDocumentSymbolParams(document);
|
||||
return client.sendRequest(DocumentColorRequest.type, params).then(symbols => {
|
||||
return symbols.map(symbol => {
|
||||
let range = client.protocol2CodeConverter.asRange(symbol.range);
|
||||
let color = new Color(symbol.color.red * 255, symbol.color.green * 255, symbol.color.blue * 255, symbol.color.alpha);
|
||||
return new ColorRange(range, color);
|
||||
return new ColorInformation(range, color);
|
||||
});
|
||||
});
|
||||
},
|
||||
resolveDocumentColor(color: Color, colorFormat: ColorFormat): Thenable<string> | string {
|
||||
switch (colorFormat) {
|
||||
case ColorFormat.RGB:
|
||||
if (color.alpha === 1) {
|
||||
return `rgb(${Math.round(color.red * 255)}, ${Math.round(color.green * 255)}, ${Math.round(color.blue * 255)})`;
|
||||
} else {
|
||||
return `rgba(${Math.round(color.red * 255)}, ${Math.round(color.green * 255)}, ${Math.round(color.blue * 255)}, ${color.alpha})`;
|
||||
}
|
||||
case ColorFormat.HEX:
|
||||
if (color.alpha === 1) {
|
||||
return `#${_toTwoDigitHex(Math.round(color.red * 255))}${_toTwoDigitHex(Math.round(color.green * 255))}${_toTwoDigitHex(Math.round(color.blue * 255))}`;
|
||||
} else {
|
||||
return `#${_toTwoDigitHex(Math.round(color.red * 255))}${_toTwoDigitHex(Math.round(color.green * 255))}${_toTwoDigitHex(Math.round(color.blue * 255))}${_toTwoDigitHex(Math.round(color.alpha * 255))}`;
|
||||
}
|
||||
case ColorFormat.HSL:
|
||||
const hsl = convert.rgb.hsl(Math.round(color.red * 255), Math.round(color.green * 255), Math.round(color.blue * 255));
|
||||
if (color.alpha === 1) {
|
||||
return `hsl(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%)`;
|
||||
} else {
|
||||
return `hsla(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%, ${color.alpha})`;
|
||||
}
|
||||
provideColorPresentations(colorInfo: ColorInformation): ColorPresentation[] | Thenable<ColorPresentation[]> {
|
||||
let result: ColorPresentation[] = [];
|
||||
let color = colorInfo.color;
|
||||
let label;
|
||||
if (color.alpha === 1) {
|
||||
label = `rgb(${Math.round(color.red * 255)}, ${Math.round(color.green * 255)}, ${Math.round(color.blue * 255)})`;
|
||||
} else {
|
||||
label = `rgba(${Math.round(color.red * 255)}, ${Math.round(color.green * 255)}, ${Math.round(color.blue * 255)}, ${color.alpha})`;
|
||||
}
|
||||
|
||||
result.push({ label: label, textEdit: new CodeTextEdit(colorInfo.range, label) });
|
||||
|
||||
if (color.alpha === 1) {
|
||||
label = `#${_toTwoDigitHex(Math.round(color.red * 255))}${_toTwoDigitHex(Math.round(color.green * 255))}${_toTwoDigitHex(Math.round(color.blue * 255))}`;
|
||||
} else {
|
||||
label = `#${_toTwoDigitHex(Math.round(color.red * 255))}${_toTwoDigitHex(Math.round(color.green * 255))}${_toTwoDigitHex(Math.round(color.blue * 255))}${_toTwoDigitHex(Math.round(color.alpha * 255))}`;
|
||||
}
|
||||
|
||||
result.push({ label: label, textEdit: new CodeTextEdit(colorInfo.range, label) });
|
||||
|
||||
const hsl = convert.rgb.hsl(Math.round(color.red * 255), Math.round(color.green * 255), Math.round(color.blue * 255));
|
||||
if (color.alpha === 1) {
|
||||
label = `hsl(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%)`;
|
||||
} else {
|
||||
label = `hsla(${hsl[0]}, ${hsl[1]}%, ${hsl[2]}%, ${color.alpha})`;
|
||||
}
|
||||
|
||||
result.push({ label: label, textEdit: new CodeTextEdit(colorInfo.range, label) });
|
||||
return result;
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user