extension.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const vscode = require('vscode');
  2. const path = require('path');
  3. const { exec } = require('child_process');
  4. // 组件列表
  5. // components list
  6. const componentListOrgin = require('./menuList');
  7. const componentList = ArrChildFun(componentListOrgin);
  8. async function activate() {
  9. // 如果没有设置,返回undefined
  10. // If not set, return undefined
  11. const result = vscode.workspace.getConfiguration().get('STDF');
  12. // 获取插件语言
  13. // Get plugin language
  14. const isZh = result.English ? false : true;
  15. // 读取当前工程的 package.json 文件
  16. // Read the package.json file of the current project
  17. const packageJson = require(path.join(vscode.workspace.rootPath, 'package.json'));
  18. // 判断当前工程是否引入了 stdf 组件库
  19. // Determine whether the current project has introduced the stdf component library
  20. const isImportStdf = packageJson?.dependencies?.stdf || packageJson?.devDependencies?.stdf ? true : false;
  21. // 如果 isImportStdf 为 true 时,读取 stdf 组件库的版本号做为当前版本号
  22. // If isImportStdf is true, read the version number of the stdf component library as the current version number
  23. let currentVersion = isImportStdf ? packageJson.dependencies?.stdf || packageJson.devDependencies?.stdf : '';
  24. // 如果 currentVersion 开头为 ^,则去掉 ^
  25. // If currentVersion starts with ^, remove ^
  26. if (currentVersion.startsWith('^')) {
  27. currentVersion = currentVersion.slice(1);
  28. }
  29. // stdf 组件库的最新版本号
  30. // The latest version number of the stdf component library
  31. const latestVersion = (await getLatestVersion('stdf')) || (isZh ? '获取失败' : 'Failed to get');
  32. // 当前文件是 .svelte 文件时且 isImportStdf 为 true 时,才注册悬浮提示
  33. // When the current file is a .svelte file and isImportStdf is true, register the hover prompt
  34. if (vscode.window.activeTextEditor.document.languageId === 'svelte' && isImportStdf) {
  35. vscode.languages.registerHoverProvider('svelte', {
  36. provideHover(document, position) {
  37. // 获取当前悬浮的单词
  38. // Get the word currently hovering
  39. const word = document.getText(document.getWordRangeAtPosition(position));
  40. // 将单词首字母小写
  41. // Lowercase the first letter of the word
  42. const wordLower = word.charAt(0).toLowerCase() + word.slice(1);
  43. // 如果单词为 components 数组中的一项,则显示提示信息,内容为 ./doc/components/${wordLower}/api.md 文件的内容
  44. // If the word is an item in the components array, display the prompt information, and the content is the content of the ./doc/components/${wordLower}/api.md file
  45. if (componentList.includes(word)) {
  46. // 组合当前 STDF 版本和最新版本
  47. // Combine the current STDF version and the latest version
  48. const versionContent = `STDF ${isZh ? '当前' : 'Current'}:${currentVersion}   ${
  49. isZh ? '最新' : 'Latest'
  50. }:${latestVersion}   [${isZh ? '查看更新日志' : 'See changelog'}](https://stdf.design/#/guide/changelog)`;
  51. // 获取对应组件的 api.md 文件的路径
  52. // Get the path of the api.md file of the corresponding component
  53. const apiPath = path.join(__dirname, `./doc/components/${wordLower}/api${isZh ? '' : '_en'}.md`);
  54. // 读取 api.md 文件的内容
  55. // Read the contents of the api.md file
  56. const apiContent = require('fs').readFileSync(apiPath, 'utf-8');
  57. // 在 apiContent 后面添加示例、API、指南、组件源码的链接
  58. // Add links to examples, API, guides, and component source code after apiContent
  59. const baseUrl = 'https://stdf.design/#/components?nav=';
  60. const baseUrlCode = 'https://github.com/any-tdf/stdf/blob/main/packages/stdf/components/';
  61. const baseUrlCodeGitee = 'https://gitee.com/dufu1991/stdf/blob/main/packages/stdf/components/';
  62. const addContent = `**[${isZh ? '示例' : 'Demo'}](${baseUrl}${wordLower}&tab=0&lang=${
  63. isZh ? 'zh_CN' : 'en_US'
  64. })   [API](${baseUrl}${wordLower}&tab=1&lang=${isZh ? 'zh_CN' : 'en_US'})   [${
  65. isZh ? '指南' : 'Guide'
  66. }](${baseUrl}${wordLower}&tab=2&lang=${isZh ? 'zh_CN' : 'en_US'})   [${
  67. isZh ? '更新日志' : 'Changelog'
  68. }](${baseUrl}${wordLower}&tab=4&lang=${isZh ? 'zh_CN' : 'en_US'})   [${
  69. isZh ? '源码(GitHub)' : 'Source code(GitHub)'
  70. }](${baseUrlCode}${wordLower}/${word}.svelte)   [${
  71. isZh ? '源码(Gitee)' : 'Source code(Gitee)'
  72. }](${baseUrlCodeGitee}${wordLower}/${word}.svelte)**`;
  73. const allContent =
  74. versionContent + '\n\n' + '---' + '\n\n' + apiContent + '\n\n' + '---' + '\n\n' + addContent + '\n\n' + '\n\n' + '---';
  75. // 返回提示信息
  76. // Return prompt information
  77. return new vscode.Hover(allContent);
  78. }
  79. },
  80. });
  81. }
  82. }
  83. //数组处理
  84. //Array processing
  85. function ArrChildFun(arr) {
  86. let newArr = [];
  87. for (let e = 0; e < arr.length; e++) {
  88. newArr.push(...arr[e].childs);
  89. }
  90. //再将 newArr 内的 title_en 组成一维数组
  91. //Then the title_en in newArr is composed of one-dimensional array
  92. let newArr2 = [];
  93. for (let e = 0; e < newArr.length; e++) {
  94. newArr2.push(newArr[e].title_en);
  95. }
  96. return newArr2;
  97. }
  98. // 获取指定 npm 包的最新版本号
  99. // Get the latest version number of the specified npm package
  100. async function getLatestVersion(packageName) {
  101. return new Promise(resolve => {
  102. exec(`npm show ${packageName} version`, (error, stdout, stderr) => {
  103. if (error || stderr) {
  104. resolve(null);
  105. } else {
  106. resolve(stdout.trim());
  107. }
  108. });
  109. });
  110. }
  111. function deactivate() {}
  112. module.exports = {
  113. activate,
  114. deactivate,
  115. };