|
| 1 | +import { env, window, Position, Selection } from "vscode"; |
| 2 | + |
| 3 | +import { buildInsertionText } from "./paste_as_rescript_json"; |
| 4 | +import { transformJsx } from "./transform-jsx"; |
| 5 | + |
| 6 | +export type JsxConversionResult = |
| 7 | + | { kind: "success"; formatted: string } |
| 8 | + | { kind: "empty" } |
| 9 | + | { kind: "error"; errorMessage: string }; |
| 10 | + |
| 11 | +export const convertPlainTextToRescriptJsx = ( |
| 12 | + text: string, |
| 13 | +): JsxConversionResult => { |
| 14 | + if (text.trim().length === 0) { |
| 15 | + return { kind: "empty" }; |
| 16 | + } |
| 17 | + |
| 18 | + try { |
| 19 | + const formatted = transformJsx(text); |
| 20 | + return { kind: "success", formatted }; |
| 21 | + } catch (error) { |
| 22 | + const errorMessage = |
| 23 | + error instanceof Error ? error.message : "Unknown conversion error."; |
| 24 | + return { |
| 25 | + kind: "error", |
| 26 | + errorMessage, |
| 27 | + }; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +const computeEndPosition = ( |
| 32 | + insertionStart: Position, |
| 33 | + indentedText: string, |
| 34 | +): Position => { |
| 35 | + const lines = indentedText.split("\n"); |
| 36 | + if (lines.length === 1) { |
| 37 | + return insertionStart.translate(0, lines[0].length); |
| 38 | + } |
| 39 | + return new Position( |
| 40 | + insertionStart.line + lines.length - 1, |
| 41 | + lines[lines.length - 1].length, |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +export const pasteAsRescriptJsx = async () => { |
| 46 | + const editor = window.activeTextEditor; |
| 47 | + if (!editor) { |
| 48 | + window.showInformationMessage( |
| 49 | + "No active editor to paste the ReScript JSX into.", |
| 50 | + ); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const clipboardText = await env.clipboard.readText(); |
| 55 | + const conversion = convertPlainTextToRescriptJsx(clipboardText); |
| 56 | + |
| 57 | + if (conversion.kind === "empty") { |
| 58 | + window.showInformationMessage( |
| 59 | + "Clipboard does not appear to contain any JSX content.", |
| 60 | + ); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (conversion.kind === "error") { |
| 65 | + window.showErrorMessage( |
| 66 | + `Clipboard JSX could not be transformed: ${conversion.errorMessage}`, |
| 67 | + ); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const formatted = conversion.formatted; |
| 72 | + const selection = editor.selection; |
| 73 | + const indentedText = buildInsertionText( |
| 74 | + editor.document, |
| 75 | + selection.start, |
| 76 | + formatted, |
| 77 | + ); |
| 78 | + const insertionStart = selection.start; |
| 79 | + const didEdit = await editor.edit((editBuilder) => { |
| 80 | + editBuilder.replace(selection, indentedText); |
| 81 | + }); |
| 82 | + |
| 83 | + if (didEdit) { |
| 84 | + const endPosition = computeEndPosition(insertionStart, indentedText); |
| 85 | + editor.selection = new Selection(endPosition, endPosition); |
| 86 | + } |
| 87 | +}; |
0 commit comments