import * as vscode from "vscode" ; import { commands , Position , Selection , TextEdit , TextEditor , window , workspace , WorkspaceEdit } from "vscode" ; import * as protocolConverter from "vscode-languageclient/lib/common/protocolConverter" ; import * as ls from "vscode-languageserver-protocol" ; const p2c = protocolConverter . createConverter ( undefined , undefined ) ; export async function applyWorkspaceEdit ( edit ) { const workspaceEdit = p2c . asWorkspaceEdit ( edit ) ; if ( workspaceEdit !== undefined ) { await workspace . applyEdit ( workspaceEdit ) ; try { const currentEditor = window . activeTextEditor ; if ( ! currentEditor || currentEditor . document . uri . fsPath !== workspaceEdit . entries ( ) [ 0 ] [ 0 ] . fsPath ) { return ; } const cursorPostion = currentEditor . selection . active ; const changes = workspaceEdit . entries ( ) [ 0 ] [ 1 ] ; let startPosition = new Position ( changes [ 0 ] . range . start . line , changes [ 0 ] . range . start . character ) ; let lineOffsets = changes [ 0 ] . newText . split ( /\r?\n/ ) . length - 1 ; for ( let i = 1 ; i < changes . length ; i += 1 ) { if ( changes [ i ] . range . start . line !== startPosition . line ) { await executeRangeFormat ( currentEditor , startPosition , lineOffsets ) ; startPosition = new Position ( changes [ i ] . range . start . line , changes [ i ] . range . start . character ) ; lineOffsets = 0 ; } lineOffsets += changes [ i ] . newText . split ( /\r?\n/ ) . length - 1 ; } await executeRangeFormat ( currentEditor , startPosition , lineOffsets ) ; currentEditor . selection = new Selection ( cursorPostion , cursorPostion ) ; } catch ( error ) { } } } async function executeRangeFormat ( editor , startPosition , lineOffset ) { const endPosition = editor . document . positionAt ( editor . document . offsetAt ( new Position ( startPosition . line + lineOffset + 1 , 0 ) ) - 1 ) ; editor . selection = new Selection ( startPosition , endPosition ) ; await commands . executeCommand ( "editor.action.formatSelection" ) ; } export function getIndentation ( document , offset ) { const closingTagPosition = document . positionAt ( offset ) ; return document . getText ( new vscode . Range ( new vscode . Position ( closingTagPosition . line , 0 ) , closingTagPosition ) ) ; } export function constructDependencyNode ( options ) { const { gid , aid , version , dtype , classifier , baseIndent , indent , eol } = options ; const builder = [ eol , "<dependency>" , `${ indent }<groupId>${ gid }</groupId>` , `${ indent }<artifactId>${ aid }</artifactId>` , `${ indent }<version>${ version }</version>` ] ; if ( dtype !== undefined && dtype !== "jar" ) builder . push ( `${ indent }<type>${ dtype }</type>` ) ; if ( classifier !== undefined ) builder . push ( `${ indent }<type>${ classifier }</type>` ) ; builder . push ( `</dependency>${ eol }` ) ; return builder . join ( `${ eol }${ baseIndent }${ indent }` ) ; } export function constructDependenciesNode ( options ) { const { gid , aid , version , dtype , classifier , baseIndent , indent , eol } = options ; const dependencyNode = constructDependencyNode ( { gid , aid , version , dtype , baseIndent : ( baseIndent + indent ) , classifier , indent , eol } ) ; return [ eol , "<dependencies>" , dependencyNode , `</dependencies>${ eol }` ] . join ( `${ eol }${ baseIndent }${ indent }` ) ; } export function constructDependencyManagementNode ( options ) { const { gid , aid , version , dtype , classifier , baseIndent , indent , eol } = options ; const dependenciesNode = constructDependenciesNode ( { gid , aid , version , dtype , baseIndent : ( baseIndent + indent ) , classifier , indent , eol } ) ; return [ eol , "<dependencyManagement>" , dependenciesNode , `</dependencyManagement>${ eol }` ] . join ( `${ eol }${ baseIndent }${ indent }` ) ; }