import * as vscode from 'vscode' ; function getTabString ( editor ) { let spacesUsed = < boolean > editor . options . insertSpaces ; if ( spacesUsed ) { let numOfUsedSpaces = < number > editor . options . tabSize ; return ' ' . repeat ( numOfUsedSpaces ) ; } return '\t' ; } export function activate ( ) { console . log ( 'Congratulations, your extension "copythis" is now active!' ) ; vscode . commands . registerCommand ( 'extension.htmlTagWrap' , ( ) => { var editor = vscode . window . activeTextEditor ; if ( editor != undefined ) { var selection = editor . selection ; var selectedText = editor . document . getText ( selection ) ; var firstIndex = 1 ; var lastIndex = selectedText . length ; console . log ( 'selection is: ' + selectedText ) ; console . log ( 'length is: ' + lastIndex ) ; console . log ( 'selection.start.character: ' + selection . start . character ) ; console . log ( 'selection.end.character: ' + selection . end . character ) ; var selectionStart = selection . start ; var selectionEnd = selection . end ; if ( selectionEnd . line > selectionStart . line ) { var lineAbove = selectionStart . line - 1 ; var lineBelow = selectionEnd . line + 1 ; let tabSizeSpace = getTabString ( editor ) ; var selectionStart_spaces = editor . document . lineAt ( selectionStart . line ) . text . substring ( 0 , selectionStart . character ) ; editor . edit ( ( editBuilder ) => { editBuilder . insert ( new vscode . Position ( selectionEnd . line , selectionEnd . character ) , '\n' + selectionStart_spaces + '</p>' ) ; editBuilder . insert ( new vscode . Position ( selectionEnd . line , 0 ) , tabSizeSpace ) ; console . log ( 'End line done.  Line #: ' + selectionEnd . line ) ; for ( let lineNumber = selectionEnd . line - 1 ; lineNumber > selectionStart . line ; lineNumber -- ) { console . log ( 'FOR Loop line #: ' + lineNumber ) ; editBuilder . insert ( new vscode . Position ( lineNumber , 0 ) , tabSizeSpace ) ; } editBuilder . insert ( new vscode . Position ( selectionStart . line , selectionStart . character ) , '<p>\n' + selectionStart_spaces + tabSizeSpace ) ; console . log ( 'Start Line done.  Line #: ' + selectionStart . line ) ; } ) . then ( ( ) => { console . log ( 'Edit applied!' ) ; var bottomTagLine = lineBelow + 1 ; var firstTagSelectionSelection = new vscode . Selection ( selectionStart . line , selectionStart . character + 1 , selectionStart . line , selectionStart . character + 2 ) ; var lastTagSelectionSelection = new vscode . Selection ( bottomTagLine , selectionStart . character + 2 , bottomTagLine , selectionStart . character + 3 ) ; var tagSelections = [ firstTagSelectionSelection , lastTagSelectionSelection ] ; editor . selections = tagSelections ; } , ( err ) => { console . log ( 'Edit rejected!' ) ; console . error ( err ) ; } ) ; } else { editor . edit ( ( editBuilder ) => { editBuilder . insert ( new vscode . Position ( selectionEnd . line , selectionEnd . character ) , '</p>' ) ; editBuilder . insert ( new vscode . Position ( selectionEnd . line , selectionStart . character ) , '<p>' ) ; } ) . then ( ( ) => { console . log ( 'Edit applied!' ) ; var firstTagSelectionSelection = new vscode . Selection ( selectionStart . line , selectionStart . character + 1 , selectionStart . line , selectionStart . character + 2 ) ; var lastTagSelectionSelection = new vscode . Selection ( selectionEnd . line , selectionEnd . character + 3 + 2 , selectionEnd . line , selectionEnd . character + 3 + 3 ) ; var tagSelections = [ firstTagSelectionSelection , lastTagSelectionSelection ] ; editor . selections = tagSelections ; } , ( err ) => { console . log ( 'Edit rejected!' ) ; console . error ( err ) ; } ) ; } } ; } ) ; }