'use strict' ; import * as _ from 'lodash' ; import { window , workspace , DecorationRenderOptions , Disposable , Range , TextEditor } from 'vscode' ; import { LanguageClient } from 'vscode-languageclient' ; import { DiagnosticSeverity , Suggestion } from './types' ; const decorationType = { isWholeLine : true , light : { after : { color : '#793600' } } , dark : { after : { color : '#ff9527' } } } ; export function activateDecorations ( client ) { let disposables = [ ] ; let deepscanDecorationType = window . createTextEditorDecorationType ( decorationType ) ; disposables . push ( deepscanDecorationType ) ; let showDecorators = workspace . getConfiguration ( 'deepscan' ) . get ( 'showDecorators' ) ; let timeout = null ; let activeEditor = window . activeTextEditor ; window . onDidChangeActiveTextEditor ( editor => { activeEditor = editor ; if ( editor ) { triggerReviveDecorations ( ) ; } } , null , disposables ) ; workspace . onDidChangeConfiguration ( ( ) => { showDecorators = workspace . getConfiguration ( 'deepscan' ) . get ( 'showDecorators' ) ; } ) ; function triggerReviveDecorations ( ) { if ( timeout ) { clearTimeout ( timeout ) ; } timeout = setTimeout ( reviveDecorations , 500 ) ; } function reviveDecorations ( ) { if ( ! activeEditor ) { return ; } updateDecorationForEditor ( activeEditor , client . diagnostics . get ( activeEditor . document . uri ) ) ; } function clearDecorations ( uri ) { window . visibleTextEditors . forEach ( ( editor ) => { if ( editor . document && uri === editor . document . uri . toString ( ) ) { editor . setDecorations ( deepscanDecorationType , [ ] ) ; } } ) ; } function updateDecorations ( uri ) { window . visibleTextEditors . forEach ( editor => { if ( editor . document && uri === editor . document . uri . toString ( ) ) { updateDecorationForEditor ( editor , client . diagnostics . get ( editor . document . uri ) ) ; } } ) ; } function updateDecorationForEditor ( editor , diagnostics ) { if ( ! showDecorators ) { clearDecorations ( editor . document . uri . toString ( ) ) ; return ; } const suggestions = getSuggestions ( diagnostics ) ; let result = _ . sortBy ( suggestions , ( { severity } ) => severity ) ; result = _ . filter ( result , ( { severity } ) => severity === DiagnosticSeverity . Error . valueOf ( ) ) ; let decorations = result . map ( ( { startLine , startChar , endLine , endChar , message } ) => ( { range : new Range ( startLine , 0 , endLine , 1000 ) , renderOptions : { after : { contentText : `  ← ${ _ . unescape ( message ) }` } } } ) ) ; editor . setDecorations ( deepscanDecorationType , decorations ) ; } function getSuggestions ( diagnostics ) { if ( ! diagnostics ) { return [ ] ; } let suggestions = [ ] ; diagnostics . forEach ( ( { range , message , severity } ) => { const suggestion = { startLine : range . start . line , startChar : range . start . character , endLine : range . end . line , endChar : range . end . character , message , severity } ; suggestions . push ( suggestion ) ; } ) ; return suggestions ; } return { clearDecorations , updateDecorations , disposables : Disposable . from ( ... disposables ) } ; }