From f5a57c769356b00000056621a5420cfd977dbb9c Mon Sep 17 00:00:00 2001 From: toxicvipa <25963632+rtretter@users.noreply.github.com> Date: Sat, 9 Mar 2024 14:55:13 +0100 Subject: [PATCH 1/2] implemented formatter for selection --- packages/extension/package-lock.json | 4 +- packages/extension/src/lib.rs | 9 ++++- packages/extension/src/main.ts | 58 +++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/packages/extension/package-lock.json b/packages/extension/package-lock.json index dffe931c4..7e7639961 100644 --- a/packages/extension/package-lock.json +++ b/packages/extension/package-lock.json @@ -1,12 +1,12 @@ { "name": "dioxus", - "version": "0.0.2", + "version": "0.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "dioxus", - "version": "0.0.2", + "version": "0.0.3", "license": "MIT", "dependencies": { "dioxus-ext": "./pkg", diff --git a/packages/extension/src/lib.rs b/packages/extension/src/lib.rs index 6581482ca..81e7906cc 100644 --- a/packages/extension/src/lib.rs +++ b/packages/extension/src/lib.rs @@ -22,10 +22,15 @@ pub fn format_rsx(raw: String, use_tabs: bool, indent_size: usize) -> String { } #[wasm_bindgen] -pub fn format_selection(raw: String, use_tabs: bool, indent_size: usize) -> String { +pub fn format_selection( + raw: String, + use_tabs: bool, + indent_size: usize, + base_indent: usize, +) -> String { let block = dioxus_autofmt::fmt_block( &raw, - 0, + base_indent, IndentOptions::new( if use_tabs { IndentType::Tabs diff --git a/packages/extension/src/main.ts b/packages/extension/src/main.ts index 76b7261f1..fe175a9dd 100644 --- a/packages/extension/src/main.ts +++ b/packages/extension/src/main.ts @@ -57,14 +57,68 @@ function fmtSelection() { const editor = vscode.window.activeTextEditor; if (!editor) return; - const unformatted = editor.document.getText(editor.selection); + let end_line = editor.selection.end.line; + + // Select full lines of selection + let selection_range = new vscode.Range( + editor.selection.start.line, + 0, + end_line, + editor.document.lineAt(end_line).range.end.character + ); + + let unformatted = editor.document.getText(selection_range); if (unformatted.length == 0) { vscode.window.showWarningMessage("Please select rsx invoking this command!"); return; } - const fileDir = editor.document.fileName.slice(0, editor.document.fileName.lastIndexOf('\\')); + // If number of closing braces is lower than opening braces, expand selection to end of initial block + while ((unformatted.match(/{/g) || []).length > (unformatted.match(/}/g) || []).length && end_line < editor.document.lineCount - 1) { + end_line += 1; + + selection_range = new vscode.Range( + editor.selection.start.line, + 0, + end_line, + editor.document.lineAt(end_line).range.end.character + ); + + unformatted = editor.document.getText(selection_range); + } + + let tabSize: number; + if (typeof editor.options.tabSize === 'number') { + tabSize = editor.options.tabSize; + } else { + tabSize = 4; + } + + let end_above = Math.max(editor.selection.start.line - 1, 0); + + let lines_above = editor.document.getText( + new vscode.Range( + 0, + 0, + end_above, + editor.document.lineAt(end_above).range.end.character + ) + ); + + // Calculate indent for current selection + let base_indentation = (lines_above.match(/{/g) || []).length - (lines_above.match(/}/g) || []).length - 1; + + try { + const formatted = dioxus.format_selection(unformatted, !editor.options.insertSpaces, tabSize, base_indentation); + if (formatted.length > 0) { + editor.edit(editBuilder => { + editBuilder.replace(selection_range, formatted); + }); + } + } catch (error) { + vscode.window.showErrorMessage(`Selection is not valid rsx! Make sure to only select elements from within the rsx-Macro! \n${error}`); + } } From 664c9c37cc8ff0c71c7be31c117c2b0bf3877dcc Mon Sep 17 00:00:00 2001 From: toxicvipa <25963632+rtretter@users.noreply.github.com> Date: Sat, 9 Mar 2024 19:32:15 +0100 Subject: [PATCH 2/2] removed scheme check for formatter and fixed indent bug --- packages/extension/src/main.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/extension/src/main.ts b/packages/extension/src/main.ts index fe175a9dd..682c56fc4 100644 --- a/packages/extension/src/main.ts +++ b/packages/extension/src/main.ts @@ -57,6 +57,10 @@ function fmtSelection() { const editor = vscode.window.activeTextEditor; if (!editor) return; + if (editor.document.languageId !== "rust") { + return; + } + let end_line = editor.selection.end.line; // Select full lines of selection @@ -69,7 +73,7 @@ function fmtSelection() { let unformatted = editor.document.getText(selection_range); - if (unformatted.length == 0) { + if (unformatted.trim().length == 0) { vscode.window.showWarningMessage("Please select rsx invoking this command!"); return; } @@ -110,14 +114,17 @@ function fmtSelection() { let base_indentation = (lines_above.match(/{/g) || []).length - (lines_above.match(/}/g) || []).length - 1; try { - const formatted = dioxus.format_selection(unformatted, !editor.options.insertSpaces, tabSize, base_indentation); + let formatted = dioxus.format_selection(unformatted, !editor.options.insertSpaces, tabSize, base_indentation); + for(let i = 0; i <= base_indentation; i++) { + formatted = (editor.options.insertSpaces ? " ".repeat(tabSize) : "\t") + formatted; + } if (formatted.length > 0) { editor.edit(editBuilder => { editBuilder.replace(selection_range, formatted); }); } } catch (error) { - vscode.window.showErrorMessage(`Selection is not valid rsx! Make sure to only select elements from within the rsx-Macro! \n${error}`); + vscode.window.showErrorMessage(`Errors occurred while formatting. Make sure you have the most recent Dioxus-CLI installed and you have selected valid rsx with your cursor! \n${error}`); } } @@ -136,7 +143,7 @@ function fmtDocumentOnSave(e: vscode.TextDocumentWillSaveEvent) { function fmtDocument(document: vscode.TextDocument) { try { - if (document.languageId !== "rust" || document.uri.scheme !== "file") { + if (document.languageId !== "rust") { return; }