extension.js 5.1 KB

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