/**
 *****************************************
 * Created by edonet@163.com
 * Created on 2020-08-22 16:40:26
 *****************************************
 */ 'use strict' ; /**
 *****************************************
 * 加载依赖
 *****************************************
 */ import * as vscode from 'vscode' ; import * as path from 'path' ; import * as fs from 'fs' ; import * as os from 'os' ; import cache from './cache' ; /**
 *****************************************
 * 生成变量缓存
 *****************************************
 */ export default ( ) => cache ( { config ( ) { const document = this . document ( ) ; if ( document ) { return vscode . workspace . getConfiguration ( undefined , document . uri ) ; } return vscode . workspace . getConfiguration ( ) ; } , package ( ) { const workspaceFolder = this . workspaceFolder ( ) ; const packageFile = workspaceFolder ? path . join ( workspaceFolder , 'package.json' ) : '' ; if ( packageFile ) { try { return JSON . parse ( fs . readFileSync ( packageFile , 'utf8' ) ) as Record < string , unknown > ; } catch ( err ) { } } return { } as Record < string , unknown > ; } , env ( ) { const env = process . env ; const map = Object . create ( null ) ; Object . keys ( env ) . forEach ( key => map [ key . toUpperCase ( ) ] = env [ key ] || '' ) ; return map ; } , workspace ( ) { return vscode . workspace ; } , window ( ) { return vscode . window ; } , editor ( ) { return this . window ( ) . activeTextEditor ; } , document ( ) { return this . editor ( ) ?. document ; } , selections ( ) { return this . editor ( ) ?. selections . sort ( ( sl1 , sl2 ) => sl1 . active . line > sl2 . active . line ? 1 : - 1 ) ; } , file ( ) { return this . document ( ) ?. uri . fsPath || '' ; } , fileBasename ( ) { return this . file ( ) && path . basename ( this . file ( ) ) ; } , fileBasenameNoExtension ( ) { return this . file ( ) && path . basename ( this . file ( ) , this . fileExtname ( ) ) ; } , fileDirname ( ) { return this . file ( ) && path . dirname ( this . file ( ) ) ; } , fileExtname ( ) { return this . file ( ) && path . extname ( this . file ( ) ) ; } , relativeFile ( ) { return vscode . workspace . asRelativePath ( this . file ( ) ) ; } , lineNumber ( ) { const [ sl ] = this . selections ( ) || [ ] ; if ( sl ) { return sl . active . line + 1 + '' ; } return '' ; } , lineNumbers ( ) { const selections = this . selections ( ) ; if ( selections && selections . length ) { return selections . map ( sl => sl . active . line + 1 ) . join ( ) ; } return '' ; } , columnNumber ( ) { const [ sl ] = this . selections ( ) || [ ] ; if ( sl ) { return sl . active . character + '' ; } return '' ; } , columnNumbers ( ) { const [ sl ] = this . selections ( ) || [ ] ; if ( sl ) { return sl . active . character + '' ; } return '' ; } , selectedPosition ( ) { const selections = this . selections ( ) ; if ( selections && selections . length ) { return selections . map ( sl => sl . active . character ) . join ( ' ' ) ; } return '' ; } , selectedPositionList ( ) { const selections = this . selections ( ) ; if ( selections && selections . length ) { return selections . map ( sl => [ sl . active . line + 1 , sl . active . character ] . join ( ',' ) ) . join ( ' ' ) ; } return '' ; } , selectedText ( ) { const [ sl ] = this . selections ( ) || [ ] ; if ( sl && ! sl . isEmpty ) { return this . document ( ) ?. getText ( sl ) || '' ; } return '' ; } , selectedTextList ( ) { const document = this . document ( ) ; const selections = this . selections ( ) ; if ( selections && selections . length && document ) { return selections . map ( sl => document . getText ( sl ) ) . join ( ' ' ) ; } return '' ; } , selectedTextSection ( ) { const document = this . document ( ) ; const selections = this . selections ( ) ; if ( selections && selections . length && document ) { return selections . map ( sl => document . getText ( sl ) ) . join ( '\n' ) . trim ( ) ; } return '' ; } , selectedLocation ( ) { const selections = this . selections ( ) ; if ( selections && selections . length ) { const [ sl ] = selections ; return [ sl . start . line + 1 , sl . start . character , sl . anchor . line + 1 , sl . end . character ] . join ( ',' ) ; } return '' ; } , selectedLocationList ( ) { const selections = this . selections ( ) ; if ( selections && selections . length ) { return selections . map ( sl => [ sl . start . line + 1 , sl . start . character , sl . anchor . line + 1 , sl . end . character ] . join ( ',' ) ) . join ( ' ' ) ; } return '' ; } , workspaceFolder ( ) { const workspace = this . workspace ( ) ; const document = this . document ( ) ; const workspaceFolder = ( document ? workspace . getWorkspaceFolder ( document . uri ) : workspace . workspaceFolders ? workspace . workspaceFolders [ 0 ] : undefined ) ; return workspaceFolder ? workspaceFolder . uri . fsPath : '' ; } , workspaceFolderBasename ( ) { return this . workspaceFolder ( ) && path . basename ( this . workspaceFolder ( ) ) ; } , homedir ( ) { return os . homedir ( ) ; } , tmpdir ( ) { return os . tmpdir ( ) ; } , platform ( ) { return os . platform ( ) ; } , commands ( ) { return { ... this . config ( ) . get ( 'command-runner.commands' ) , ... this . package ( ) [ 'commands' ] as object , } ; } , } ) ;