@@ -86,6 +86,7 @@ import {
8686 LineAndCharacter ,
8787 LinkedEditingInfo ,
8888 map ,
89+ MapCodeDocumentMapping ,
8990 mapDefined ,
9091 mapDefinedIterator ,
9192 mapIterator ,
@@ -1905,6 +1906,71 @@ export class Session<TMessage = string> implements EventSender {
19051906 } ) ;
19061907 }
19071908
1909+ private mapCode ( args : protocol . MapCodeRequestArgs ) : protocol . FileCodeEdits [ ] {
1910+ const formatOptions = this . getHostFormatOptions ( ) ;
1911+ const preferences = this . getHostPreferences ( ) ;
1912+ const projects = new Map < Project , MapCodeDocumentMapping [ ] > ( ) ;
1913+ args . mappings . forEach ( mapping => {
1914+ if ( ! mapping . file ) {
1915+ return { contents : mapping . contents } ;
1916+ }
1917+ const { file, project } = this . getFileAndProjectWorker ( mapping . file , mapping . projectFileName ) ;
1918+ const scriptInfo = this . projectService . getScriptInfoForNormalizedPath ( file ) ! ;
1919+ const focusLocations = mapping . focusLocations ?. map ( spans => {
1920+ return spans . map ( loc => {
1921+ const start = scriptInfo . lineOffsetToPosition ( loc . start . line , loc . start . offset ) ;
1922+ const end = scriptInfo . lineOffsetToPosition ( loc . end . line , loc . end . offset ) ;
1923+ return {
1924+ start,
1925+ length : end - start ,
1926+ } ;
1927+ } ) ;
1928+ } ) ;
1929+ if ( ! projects . has ( project ) ) {
1930+ projects . set ( project , [ ] ) ;
1931+ }
1932+ projects . get ( project ) ! . push ( {
1933+ contents : mapping . contents ,
1934+ fileName : file ,
1935+ focusLocations
1936+ } ) ;
1937+ } ) ;
1938+ const updates = args . updates ?. map ( edit => {
1939+ const file = this . getFileAndProjectWorker ( edit . fileName , /*projectFileName*/ undefined ) . file ;
1940+ const scriptInfo = this . projectService . getScriptInfoForNormalizedPath ( file ) ! ;
1941+ return {
1942+ fileName : edit . fileName ,
1943+ textChanges : edit . textChanges . map ( ( { start, end, newText } ) => {
1944+ const newStart = scriptInfo . lineOffsetToPosition ( start . line , start . offset ) ;
1945+ const newEnd = scriptInfo . lineOffsetToPosition ( end . line , end . offset ) ;
1946+ return {
1947+ span : { start : newStart , length : newEnd - newStart } ,
1948+ newText,
1949+ } ;
1950+ } ) ,
1951+ } ;
1952+ } ) ;
1953+
1954+ return [ ...projects . entries ( ) ] . map ( ( [ project , mappings ] ) => {
1955+ return project . getLanguageService ( ) . mapCode ( mappings , formatOptions , preferences , updates ) ;
1956+ } ) . filter ( x => x ) . flatMap ( ( changes ) => {
1957+ return changes . map ( change => {
1958+ const scriptInfo = this . projectService . getScriptInfoForNormalizedPath ( toNormalizedPath ( change . fileName ) ) ! ;
1959+ return {
1960+ fileName : change . fileName ,
1961+ textChanges : change . textChanges . map ( ( { span, newText } ) => {
1962+ const newSpan = toProtocolTextSpan ( span , scriptInfo ) ;
1963+ return {
1964+ start : newSpan . start ,
1965+ end : newSpan . end ,
1966+ newText
1967+ } ;
1968+ } ) ,
1969+ } ;
1970+ } ) ;
1971+ } ) ;
1972+ }
1973+
19081974 private setCompilerOptionsForInferredProjects ( args : protocol . SetCompilerOptionsForInferredProjectsArgs ) : void {
19091975 this . projectService . setCompilerOptionsForInferredProjects ( args . options , args . projectRootPath ) ;
19101976 }
@@ -3583,6 +3649,9 @@ export class Session<TMessage = string> implements EventSender {
35833649 [ protocol . CommandTypes . ProvideInlayHints ] : ( request : protocol . InlayHintsRequest ) => {
35843650 return this . requiredResponse ( this . provideInlayHints ( request . arguments ) ) ;
35853651 } ,
3652+ [ protocol . CommandTypes . MapCode ] : ( request : protocol . MapCodeRequest ) => {
3653+ return this . requiredResponse ( this . mapCode ( request . arguments ) ) ;
3654+ } ,
35863655 } ) ) ;
35873656
35883657 public addProtocolHandler ( command : string , handler : ( request : protocol . Request ) => HandlerResponse ) {
0 commit comments