"use strict" ; import * as vscode from "vscode" ; import * as config from "../config/Configuration" ; export class RegionWrapperService { _configService : config . ConfigurationService ; /**
     *
     */ constructor ( configService ) { this . _configService = configService ; } public wrapCurrentWithRegion ( ) { var ate = vscode . window . activeTextEditor ; if ( ! ate ) { return ; } let document = ate . document ; if ( ! document ) { return ; } let currentLanguageConfig = this . _configService . getConfigurationForCurrentLanguage ( document . languageId ) ; if ( ! currentLanguageConfig ) { return ; } if ( ate . selections . length > 1 || ate . selections . length < 1 ) { return ; } var sel = ate . selection ; if ( sel . isEmpty ) { return ; } var linePrefix = ate . document . getText ( new vscode . Range ( new vscode . Position ( sel . start . line , 0 ) , sel . start ) ) ; var addPrefix = "" ; if ( /^\s+$/ . test ( linePrefix ) ) { addPrefix = linePrefix ; } var eol = this . getEOLStr ( ate . document . eol ) ; let regionStartTemplate = currentLanguageConfig . foldStart ; const idx = regionStartTemplate . indexOf ( "[NAME]" ) ; const nameInsertionIndex = idx < 0 ? 0 : regionStartTemplate . length - "[NAME]" . length - idx ; const regionStartText = regionStartTemplate . replace ( "[NAME]" , "" ) ; ate . edit ( edit => { if ( ! currentLanguageConfig ) { return ; } if ( ! ate ) { return ; } edit . insert ( sel . end , eol + addPrefix + currentLanguageConfig . foldEnd ) ; edit . insert ( sel . start , regionStartText + eol + addPrefix ) ; } ) . then ( edit => { if ( ! currentLanguageConfig ) { return ; } if ( ! ate ) { return ; } var sel = ate . selection ; var newLine = sel . start . line - 1 ; var newChar = ate . document . lineAt ( newLine ) . text . length - nameInsertionIndex ; var newStart = sel . start . translate ( newLine - sel . start . line , newChar - sel . start . character ) ; var newSelection = new vscode . Selection ( newStart , newStart ) ; ate . selections = [ newSelection ] ; vscode . commands . executeCommand ( "editor.action.formatDocument" , "editorHasDocumentFormattingProvider && editorTextFocus" , true ) ; } ) ; } private getEOLStr ( eol ) { if ( eol === vscode . EndOfLine . CRLF ) { return "\r\n" ; } return "\n" ; } }