import type * as lspTypes from "vscode-languageserver-protocol" ; import { wrapCommand } from "../novaUtils" ; export function registerOrganizeImports ( client ) { return nova . commands . register ( "apexskier.typescript.commands.organizeImports" , wrapCommand ( organizeImports ) ) ; async function organizeImports ( editor ) ; async function organizeImports ( workspace , editor ) ; async function organizeImports ( editorOrWorkspace , maybeEditor ? ) { const editor = maybeEditor ?? ( editorOrWorkspace as TextEditor ) ; const originalSelections = editor . selectedRanges ; const originalLength = editor . document . length ; if ( ! editor . document . path ) { nova . workspace . showWarningMessage ( "Please save this document before organizing imports." ) ; return ; } const documentFormatting = { textDocument : { uri : editor . document . uri } , options : { insertSpaces : editor . softTabs , tabSize : editor . tabLength , } , } ; await client . sendRequest ( "textDocument/formatting" , documentFormatting ) ; const organizeImportsCommand = { command : "_typescript.organizeImports" , arguments : [ editor . document . path ] , } ; await client . sendRequest ( "workspace/executeCommand" , organizeImportsCommand ) ; const newLength = editor . document . length ; const lengthChange = originalLength - newLength ; editor . selectedRanges = originalSelections . map ( ( r ) => new Range ( Math . max ( 0 , r . start - lengthChange ) , Math . max ( 0 , r . end - lengthChange ) ) ) ; editor . scrollToCursorPosition ( ) ; } }