main.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import * as vscode from 'vscode';
  2. import init, * as dioxus from 'dioxus-ext';
  3. export async function activate(context: vscode.ExtensionContext) {
  4. // Load the wasm from the file system
  5. const wasmSourceCode = await vscode.workspace.fs.readFile(vscode.Uri.joinPath(context.extensionUri, "./pkg/dioxus_ext_bg.wasm"));
  6. // Wait for the initialization to finish
  7. // This is using the byte buffer directly which won't go through the "fetch" machinery
  8. //
  9. // For whatever reason, wasm-bindgen generates `fetch` when we don't want it to
  10. // VSCode doesn't have a `fetch` implementation, but we don't really care about polyfilling it
  11. await init(wasmSourceCode);
  12. // Todo:
  13. // I want a paste-handler that translates HTML to RSX whenever HTML is pasted into an Rsx block
  14. // Or, a little tooltip that pops up and asks if you want to translate the HTML to RSX
  15. context.subscriptions.push(
  16. vscode.commands.registerCommand('extension.htmlToDioxusRsx', () => translate(false)),
  17. vscode.commands.registerCommand('extension.htmlToDioxusComponent', () => translate(true)),
  18. vscode.commands.registerCommand('extension.formatRsx', fmtSelection),
  19. vscode.commands.registerCommand('extension.formatRsxDocument', formatRsxDocument),
  20. vscode.workspace.onWillSaveTextDocument(fmtDocumentOnSave)
  21. );
  22. }
  23. function translate(component: boolean) {
  24. // Load the activate editor
  25. const editor = vscode.window.activeTextEditor;
  26. if (!editor) return;
  27. // Get the selected text
  28. const html = editor.document.getText(editor.selection);
  29. if (html.length == 0) {
  30. vscode.window.showWarningMessage("Please select HTML fragment before invoking this command!");
  31. return;
  32. }
  33. // Translate the HTML to RSX
  34. const out = dioxus.translate_rsx(html, component);
  35. if (out.length > 0) {
  36. editor.edit(editBuilder => editBuilder.replace(editor.selection, out));
  37. } else {
  38. vscode.window.showWarningMessage(`Errors occurred while translating, make sure this block of HTML is valid`);
  39. }
  40. }
  41. function formatRsxDocument() {
  42. const editor = vscode.window.activeTextEditor;
  43. if (!editor) return;
  44. fmtDocument(editor.document);
  45. }
  46. function fmtSelection() {
  47. const editor = vscode.window.activeTextEditor;
  48. if (!editor) return;
  49. const unformatted = editor.document.getText(editor.selection);
  50. if (unformatted.length == 0) {
  51. vscode.window.showWarningMessage("Please select rsx invoking this command!");
  52. return;
  53. }
  54. const fileDir = editor.document.fileName.slice(0, editor.document.fileName.lastIndexOf('\\'));
  55. }
  56. function fmtDocumentOnSave(e: vscode.TextDocumentWillSaveEvent) {
  57. // check the settings to make sure format on save is configured
  58. const dioxusConfig = vscode.workspace.getConfiguration('dioxus', e.document).get('formatOnSave');
  59. const globalConfig = vscode.workspace.getConfiguration('editor', e.document).get('formatOnSave');
  60. if (
  61. (dioxusConfig === 'enabled') ||
  62. (dioxusConfig !== 'disabled' && globalConfig)
  63. ) {
  64. fmtDocument(e.document);
  65. }
  66. }
  67. function fmtDocument(document: vscode.TextDocument) {
  68. try {
  69. if (document.languageId !== "rust" || document.uri.scheme !== "file") {
  70. return;
  71. }
  72. const [editor,] = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName === document.fileName);
  73. if (!editor) return; // Need an editor to apply text edits.
  74. const contents = editor.document.getText();
  75. let tabSize: number;
  76. if (typeof editor.options.tabSize === 'number') {
  77. tabSize = editor.options.tabSize;
  78. } else {
  79. tabSize = 4;
  80. }
  81. const formatted = dioxus.format_file(contents, !editor.options.insertSpaces, tabSize);
  82. // Replace the entire text document
  83. // Yes, this is a bit heavy handed, but the dioxus side doesn't know the line/col scheme that vscode is using
  84. if (formatted.length() > 0) {
  85. editor.edit(editBuilder => {
  86. const range = new vscode.Range(0, 0, document.lineCount, 0);
  87. editBuilder.replace(range, formatted.formatted());
  88. });
  89. }
  90. } catch (error) {
  91. vscode.window.showWarningMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed! \n${error}`);
  92. }
  93. }