Skip to content

Commit 4bfb974

Browse files
committed
[html/css] decorators for embedded css
1 parent 2df11a3 commit 4bfb974

3 files changed

Lines changed: 60 additions & 33 deletions

File tree

extensions/css/client/src/colorDecorators.ts

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
'use strict';
66

77
import { window, workspace, DecorationOptions, DecorationRenderOptions, Disposable, Range, TextDocument, TextEditor } from 'vscode';
8+
import { isEmbeddedContentUri, getHostDocumentUri } from './embeddedContentUri';
89

910
const MAX_DECORATORS = 500;
1011

@@ -49,51 +50,47 @@ export function activateColorDecorations(decoratorProvider: (uri: string) => The
4950
workspace.onDidOpenTextDocument(triggerUpdateDecorations, null, disposables);
5051
workspace.onDidCloseTextDocument(triggerUpdateDecorations, null, disposables);
5152

53+
workspace.textDocuments.forEach(triggerUpdateDecorations);
54+
5255
function triggerUpdateDecorations(document: TextDocument) {
5356
let triggerUpdate = supportedLanguages[document.languageId];
54-
let uri = document.uri.toString();
55-
let timeout = pendingUpdateRequests[uri];
57+
let documentUri = document.uri;
58+
let documentUriStr = documentUri.toString();
59+
let timeout = pendingUpdateRequests[documentUriStr];
5660
if (typeof timeout !== 'undefined') {
5761
clearTimeout(timeout);
5862
triggerUpdate = true; // force update, even if languageId is not supported (anymore)
5963
}
6064
if (triggerUpdate) {
61-
pendingUpdateRequests[uri] = setTimeout(() => {
62-
updateDecorations(uri);
63-
delete pendingUpdateRequests[uri];
65+
pendingUpdateRequests[documentUriStr] = setTimeout(() => {
66+
// check if the document is in use by an active editor
67+
let contentHostUri = isEmbeddedContentUri(documentUri) ? getHostDocumentUri(documentUri) : documentUriStr;
68+
window.visibleTextEditors.forEach(editor => {
69+
if (editor.document && contentHostUri === editor.document.uri.toString()) {
70+
updateDecorationForEditor(editor, documentUriStr);
71+
}
72+
});
73+
delete pendingUpdateRequests[documentUriStr];
6474
}, 500);
6575
}
6676
}
6777

68-
function updateDecorations(uri: string) {
69-
window.visibleTextEditors.forEach(editor => {
70-
let document = editor.document;
71-
if (document && document.uri.toString() === uri) {
72-
updateDecorationForEditor(editor);
73-
}
74-
});
75-
}
76-
77-
function updateDecorationForEditor(editor: TextEditor) {
78+
function updateDecorationForEditor(editor: TextEditor, contentUri: string) {
7879
let document = editor.document;
79-
if (supportedLanguages[document.languageId]) {
80-
decoratorProvider(document.uri.toString()).then(ranges => {
81-
let decorations = ranges.slice(0, MAX_DECORATORS).map(range => {
82-
let color = document.getText(range);
83-
return <DecorationOptions>{
84-
range: range,
85-
renderOptions: {
86-
before: {
87-
backgroundColor: color
88-
}
80+
decoratorProvider(contentUri).then(ranges => {
81+
let decorations = ranges.slice(0, MAX_DECORATORS).map(range => {
82+
let color = document.getText(range);
83+
return <DecorationOptions>{
84+
range: range,
85+
renderOptions: {
86+
before: {
87+
backgroundColor: color
8988
}
90-
};
91-
});
92-
editor.setDecorations(colorsDecorationType, decorations);
89+
}
90+
};
9391
});
94-
} else {
95-
editor.setDecorations(colorsDecorationType, []);
96-
}
92+
editor.setDecorations(colorsDecorationType, decorations);
93+
});
9794
}
9895

9996
return Disposable.from(...disposables);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
'use strict';
6+
7+
import { Uri } from 'vscode';
8+
9+
export const EMBEDDED_CONTENT_SCHEME = 'embedded-content';
10+
11+
export function isEmbeddedContentUri(virtualDocumentUri: Uri): boolean {
12+
return virtualDocumentUri.scheme === EMBEDDED_CONTENT_SCHEME;
13+
}
14+
15+
export function getEmbeddedContentUri(parentDocumentUri: string, embeddedLanguageId: string): Uri {
16+
return Uri.parse(EMBEDDED_CONTENT_SCHEME + '://' + embeddedLanguageId + '/' + encodeURIComponent(parentDocumentUri) + '.' + embeddedLanguageId);
17+
};
18+
19+
export function getHostDocumentUri(virtualDocumentUri: Uri): string {
20+
let languageId = virtualDocumentUri.authority;
21+
let path = virtualDocumentUri.path.substring(1, virtualDocumentUri.path.length - languageId.length - 1); // remove leading '/' and new file extension
22+
return decodeURIComponent(path);
23+
};
24+
25+
export function getEmbeddedLanguageId(virtualDocumentUri: Uri): string {
26+
return virtualDocumentUri.authority;
27+
}

extensions/css/server/src/cssServerMain.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ connection.onCodeAction(codeActionParams => {
176176

177177
connection.onRequest(ColorSymbolRequest.type, uri => {
178178
let document = documents.get(uri);
179-
let stylesheet = stylesheets.get(document);
180-
return getLanguageService(document).findColorSymbols(document, stylesheet);
179+
if (document) {
180+
let stylesheet = stylesheets.get(document);
181+
return getLanguageService(document).findColorSymbols(document, stylesheet);
182+
}
183+
return [];
181184
});
182185

183186
connection.onRenameRequest(renameParameters => {

0 commit comments

Comments
 (0)