@tomjs/vscode-utils
Version:
Some utilities to simplify the development of VSCode Extensions
2 lines (1 loc) • 2.16 kB
JavaScript
var _ctx;function setExtensionContext(ctx){_ctx=ctx}function getExtensionContext(){return _ctx}function getCtx(){return _ctx}import fs from"fs";import path from"path";import{readJsonSync}from"@tomjs/node";import{env}from"vscode";var DEFAULT_LANGUAGE="en";var DEFAULT_NLS="package.nls.json";var messages={};function loadI18n(){const ctx=getCtx();const language=env.language.toLocaleLowerCase();let name=language===DEFAULT_LANGUAGE?DEFAULT_NLS:`package.nls.${language}.json`;const nlsPath=path.join(ctx.extensionPath,name);if(!fs.existsSync(nlsPath)){name=DEFAULT_NLS}messages=Object.assign({},readJsonSync(path.join(ctx.extensionPath,DEFAULT_NLS)),readJsonSync(nlsPath))}var I18n=class{t(...params){if(params.length===0){return""}const key=params[0];const values=params[1]??{};const text=messages[key]||"";if(Object.keys(values).length===0){return text}return text.replace(/{([^}]+)}/g,(match,group)=>values[group]??match)}};var i18n=new I18n;import{window,workspace}from"vscode";function getActiveWorkspaceFolder(){let activeWorkspace;const editor=window.activeTextEditor;if(editor){activeWorkspace=workspace.getWorkspaceFolder(editor.document.uri)}else{if(workspace.workspaceFolders&&workspace.workspaceFolders.length>0){activeWorkspace=workspace.workspaceFolders[0]}}return activeWorkspace}function getActiveWorkspaceFolderUri(){var _a;return(_a=getActiveWorkspaceFolder())==null?void 0:_a.uri}function getActiveWorkspaceFolderPath(){var _a;return(_a=getActiveWorkspaceFolder())==null?void 0:_a.uri.fsPath}function getAllWorkspaceFolders(){const folders=[];if(workspace.workspaceFolders){folders.push(...workspace.workspaceFolders.map(s=>({...s,current:false})))}if(folders.length===0){return[]}const activePath=getActiveWorkspaceFolderPath();const current=folders.find(s=>s.uri.fsPath===activePath);if(current){current.active=true}return folders}function initExtension(ctx){setExtensionContext(ctx);loadI18n()}var src_default={init:initExtension};export{src_default as default,getActiveWorkspaceFolder,getActiveWorkspaceFolderPath,getActiveWorkspaceFolderUri,getAllWorkspaceFolders,getCtx,getExtensionContext,i18n,initExtension,loadI18n,setExtensionContext};