'use strict' ; import { window , workspace , DecorationOptions , DecorationRenderOptions , Disposable , Range , TextDocument , TextEditor } from 'vscode' ; let decorationType = { before : { contentText : ' ' , border : 'solid 0.1em #000' , margin : '0.1em 0.2em 0 0.2em' , width : '0.8em' , height : '0.8em' } , dark : { before : { border : 'solid 0.1em #eee' } } } ; export function activateColorDecorations ( decoratorProvider , supportedLanguages ) { let disposables = [ ] ; let colorsDecorationType = window . createTextEditorDecorationType ( decorationType ) ; disposables . push ( colorsDecorationType ) ; let pendingUpdateRequests = { } ; window . visibleTextEditors . forEach ( editor => { if ( editor . document ) { triggerUpdateDecorations ( editor . document ) ; } } ) ; window . onDidChangeActiveTextEditor ( editor => { if ( editor ) { triggerUpdateDecorations ( editor . document ) ; } } , null , disposables ) ; workspace . onDidChangeTextDocument ( event => triggerUpdateDecorations ( event . document ) , null , disposables ) ; workspace . onDidOpenTextDocument ( triggerUpdateDecorations , null , disposables ) ; workspace . onDidCloseTextDocument ( triggerUpdateDecorations , null , disposables ) ; function triggerUpdateDecorations ( document ) { let triggerUpdate = supportedLanguages [ document . languageId ] ; let uri = document . uri . toString ( ) ; let timeout = pendingUpdateRequests [ uri ] ; if ( typeof timeout !== 'undefined' ) { clearTimeout ( timeout ) ; triggerUpdate = true ; } if ( triggerUpdate ) { pendingUpdateRequests [ uri ] = setTimeout ( ( ) => { updateDecorations ( uri ) ; delete pendingUpdateRequests [ uri ] ; } , 500 ) ; } } function updateDecorations ( uri ) { window . visibleTextEditors . forEach ( editor => { let document = editor . document ; if ( document && document . uri . toString ( ) === uri ) { updateDecorationForEditor ( editor ) ; } } ) ; } function updateDecorationForEditor ( editor ) { let document = editor . document ; if ( supportedLanguages [ document . languageId ] ) { decoratorProvider ( document . uri . toString ( ) ) . then ( ranges => { let decorations = ranges . map ( range => { let color = document . getText ( range ) ; return < DecorationOptions > { range : range , renderOptions : { before : { backgroundColor : color } } } ; } ) ; editor . setDecorations ( colorsDecorationType , decorations ) ; } ) ; } else { editor . setDecorations ( colorsDecorationType , [ ] ) ; } } return Disposable . from ( ... disposables ) ; }