|
5 | 5 | 'use strict'; |
6 | 6 |
|
7 | 7 | import { window, workspace, DecorationOptions, DecorationRenderOptions, Disposable, Range, TextDocument, TextEditor } from 'vscode'; |
| 8 | +import { isEmbeddedContentUri, getHostDocumentUri } from './embeddedContentUri'; |
8 | 9 |
|
9 | 10 | const MAX_DECORATORS = 500; |
10 | 11 |
|
@@ -49,51 +50,47 @@ export function activateColorDecorations(decoratorProvider: (uri: string) => The |
49 | 50 | workspace.onDidOpenTextDocument(triggerUpdateDecorations, null, disposables); |
50 | 51 | workspace.onDidCloseTextDocument(triggerUpdateDecorations, null, disposables); |
51 | 52 |
|
| 53 | + workspace.textDocuments.forEach(triggerUpdateDecorations); |
| 54 | + |
52 | 55 | function triggerUpdateDecorations(document: TextDocument) { |
53 | 56 | 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]; |
56 | 60 | if (typeof timeout !== 'undefined') { |
57 | 61 | clearTimeout(timeout); |
58 | 62 | triggerUpdate = true; // force update, even if languageId is not supported (anymore) |
59 | 63 | } |
60 | 64 | 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]; |
64 | 74 | }, 500); |
65 | 75 | } |
66 | 76 | } |
67 | 77 |
|
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) { |
78 | 79 | 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 |
89 | 88 | } |
90 | | - }; |
91 | | - }); |
92 | | - editor.setDecorations(colorsDecorationType, decorations); |
| 89 | + } |
| 90 | + }; |
93 | 91 | }); |
94 | | - } else { |
95 | | - editor.setDecorations(colorsDecorationType, []); |
96 | | - } |
| 92 | + editor.setDecorations(colorsDecorationType, decorations); |
| 93 | + }); |
97 | 94 | } |
98 | 95 |
|
99 | 96 | return Disposable.from(...disposables); |
|
0 commit comments