import { Range , TextEditorDecorationType , Position , } from 'vscode' ; import { IDecoration } from '../util/color-util' ; import Variable from './variable' ; import VariablesManager from './variables-manager' ; import Color from '../colors/color' ; class VariableDecoration implements IDecoration { type { Variable } public memberOf public variable : Variable ; type { boolean } public memberOf public disposed = false ; private hidden = false ; private decorationFn : ( Color ) => TextEditorDecorationType ; public currentRange : Range ; private _decoration : TextEditorDecorationType ; type { TextEditorDecorationType } memberOf get decoration ( ) : TextEditorDecorationType { this . _generateDecorator ( ) ; return this . _decoration ; } set decoration ( deco ) { this . _decoration = deco ; } get rgb ( ) : [ number , number , number ] { return this . variable . color . rgb ; } public constructor ( variable , line , decorationFn ) { this . variable = variable ; this . decorationFn = decorationFn ; if ( this . variable . color ) { this . generateRange ( line ) ; } else { this . currentRange = new Range ( new Position ( line , 0 ) , new Position ( line , 0 ) ) ; } } public memberOf public dispose ( ) { try { this . _decoration . dispose ( ) ; this . variable . color . rgb = null ; } catch ( error ) { } this . disposed = true ; } public hide ( ) { if ( this . _decoration ) { this . _decoration . dispose ( ) ; } this . hidden = true ; } param { number } line returns { Range } memberOf public generateRange ( line ) { const range = new Range ( new Position ( line , this . variable . color . positionInText ) , new Position ( line , this . variable . color . positionInText + this . variable . color . value . length ) ) ; this . currentRange = range ; return range ; } public shouldGenerateDecoration ( ) { const color = VariablesManager . findVariable ( this . variable ) ; if ( this . disposed === true || color === null ) { return false ; } return ( this . _decoration === null || this . _decoration === undefined || this . hidden ) ; } private _generateDecorator ( ) { const color = VariablesManager . findVariable ( this . variable ) ; if ( color && this . variable . color !== color ) { this . variable . color = color ; } if ( this . variable . color && this . variable . color . rgb ) { this . _decoration = this . decorationFn ( this . variable . color ) ; } } } export default VariableDecoration ;