mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-27 20:13:32 +01:00
[json] adopt colorProvider.proposed protocol
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import { Range, TextDocument, DocumentColorProvider, Color, ColorRange } from 'vscode';
|
||||
|
||||
const ColorFormat_HEX = {
|
||||
opaque: '"#{red:X}{green:X}{blue:X}"',
|
||||
transparent: '"#{red:X}{green:X}{blue:X}{alpha:X}"'
|
||||
};
|
||||
|
||||
export class ColorProvider implements DocumentColorProvider {
|
||||
constructor(private decoratorProvider: (uri: string) => Thenable<Range[]>) { }
|
||||
|
||||
async provideDocumentColors(document: TextDocument): Promise<ColorRange[]> {
|
||||
const ranges = await this.decoratorProvider(document.uri.toString());
|
||||
const result = [];
|
||||
for (let range of ranges) {
|
||||
let color = parseColorFromRange(document, range);
|
||||
if (color) {
|
||||
let r = new Range(range.start.line, range.start.character, range.end.line, range.end.character);
|
||||
result.push(new ColorRange(r, color, [ColorFormat_HEX]));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
function parseColorFromRange(document: TextDocument, range: Range) {
|
||||
let text = document.getText(range);
|
||||
try {
|
||||
let value = <string>JSON.parse(text);
|
||||
if (typeof value === 'string') {
|
||||
return Color.fromHex(value);
|
||||
}
|
||||
} catch (e) {
|
||||
// ignore JSON parse error
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -6,11 +6,13 @@
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
import { workspace, languages, ExtensionContext, extensions, Uri, Range } from 'vscode';
|
||||
import { workspace, languages, ExtensionContext, extensions, Uri, Range, TextDocument, ColorRange, Color } from 'vscode';
|
||||
import { LanguageClient, LanguageClientOptions, RequestType, ServerOptions, TransportKind, NotificationType, DidChangeConfigurationNotification } from 'vscode-languageclient';
|
||||
import TelemetryReporter from 'vscode-extension-telemetry';
|
||||
import { ConfigurationFeature } from 'vscode-languageclient/lib/proposed';
|
||||
import { ColorProvider } from './colorDecorators';
|
||||
|
||||
import { DocumentColorRequest } from 'vscode-languageserver-protocol/lib/protocol.colorProvider.proposed';
|
||||
|
||||
|
||||
import * as nls from 'vscode-nls';
|
||||
let localize = nls.loadMessageBundle();
|
||||
@@ -58,6 +60,11 @@ interface JSONSchemaSettings {
|
||||
schema?: any;
|
||||
}
|
||||
|
||||
const ColorFormat_HEX = {
|
||||
opaque: '"#{red:X}{green:X}{blue:X}"',
|
||||
transparent: '"#{red:X}{green:X}{blue:X}{alpha:X}"'
|
||||
};
|
||||
|
||||
export function activate(context: ExtensionContext) {
|
||||
|
||||
let packageInfo = getPackageInfo(context);
|
||||
@@ -116,11 +123,18 @@ export function activate(context: ExtensionContext) {
|
||||
|
||||
client.sendNotification(SchemaAssociationNotification.type, getSchemaAssociation(context));
|
||||
|
||||
let colorRequestor = (uri: string) => {
|
||||
return client.sendRequest(ColorSymbolRequest.type, uri).then(ranges => ranges.map(client.protocol2CodeConverter.asRange));
|
||||
};
|
||||
|
||||
context.subscriptions.push(languages.registerColorProvider('json', new ColorProvider(colorRequestor)));
|
||||
context.subscriptions.push(languages.registerColorProvider('json', {
|
||||
provideDocumentColors(document: TextDocument): Thenable<ColorRange[]> {
|
||||
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, [ColorFormat_HEX]);
|
||||
});
|
||||
});
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
// Push the disposable to the context's subscriptions so that the
|
||||
|
||||
Reference in New Issue
Block a user