@@ -7,6 +7,7 @@ namespace ts.projectSystem {
77 const session = createSession ( createServerHost ( [ aTs , bTs ] ) ) ;
88 openFilesForSession ( [ bTs ] , session ) ;
99
10+ // rename fails with allowRenameOfImportPath disabled
1011 const response1 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( bTs , 'a";' ) ) ;
1112 assert . deepEqual < protocol . RenameResponseBody | undefined > ( response1 , {
1213 info : {
@@ -16,6 +17,7 @@ namespace ts.projectSystem {
1617 locs : [ { file : bTs . path , locs : [ protocolRenameSpanFromSubstring ( bTs . content , "./a" ) ] } ] ,
1718 } ) ;
1819
20+ // rename succeeds with allowRenameOfImportPath enabled in host
1921 session . getProjectService ( ) . setHostConfiguration ( { preferences : { allowRenameOfImportPath : true } } ) ;
2022 const response2 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( bTs , 'a";' ) ) ;
2123 assert . deepEqual < protocol . RenameResponseBody | undefined > ( response2 , {
@@ -30,6 +32,23 @@ namespace ts.projectSystem {
3032 } ,
3133 locs : [ { file : bTs . path , locs : [ protocolRenameSpanFromSubstring ( bTs . content , "./a" ) ] } ] ,
3234 } ) ;
35+
36+ // rename succeeds with allowRenameOfImportPath enabled in file
37+ session . getProjectService ( ) . setHostConfiguration ( { preferences : { allowRenameOfImportPath : false } } ) ;
38+ session . getProjectService ( ) . setHostConfiguration ( { file : "/b.ts" , formatOptions : { } , preferences : { allowRenameOfImportPath : true } } ) ;
39+ const response3 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( bTs , 'a";' ) ) ;
40+ assert . deepEqual < protocol . RenameResponseBody | undefined > ( response3 , {
41+ info : {
42+ canRename : true ,
43+ fileToRename : aTs . path ,
44+ displayName : aTs . path ,
45+ fullDisplayName : aTs . path ,
46+ kind : ScriptElementKind . moduleElement ,
47+ kindModifiers : "" ,
48+ triggerSpan : protocolTextSpanFromSubstring ( bTs . content , "a" , { index : 1 } ) ,
49+ } ,
50+ locs : [ { file : bTs . path , locs : [ protocolRenameSpanFromSubstring ( bTs . content , "./a" ) ] } ] ,
51+ } ) ;
3352 } ) ;
3453
3554 it ( "works with prefixText and suffixText when enabled" , ( ) => {
@@ -61,7 +80,7 @@ namespace ts.projectSystem {
6180 ] ,
6281 } ) ;
6382
64- // rename with prefixText and suffixText enabled
83+ // rename with prefixText and suffixText enabled in host
6584 session . getProjectService ( ) . setHostConfiguration ( { preferences : { providePrefixAndSuffixTextForRename : true } } ) ;
6685 const response2 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( aTs , "x" ) ) ;
6786 assert . deepEqual < protocol . RenameResponseBody | undefined > ( response2 , {
@@ -84,6 +103,93 @@ namespace ts.projectSystem {
84103 } ,
85104 ] ,
86105 } ) ;
106+
107+ // rename with prefixText and suffixText enabled for file
108+ session . getProjectService ( ) . setHostConfiguration ( { preferences : { providePrefixAndSuffixTextForRename : false } } ) ;
109+ session . getProjectService ( ) . setHostConfiguration ( { file : "/a.ts" , formatOptions : { } , preferences : { providePrefixAndSuffixTextForRename : true } } ) ;
110+ const response3 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( aTs , "x" ) ) ;
111+ assert . deepEqual < protocol . RenameResponseBody | undefined > ( response3 , {
112+ info : {
113+ canRename : true ,
114+ fileToRename : undefined ,
115+ displayName : "x" ,
116+ fullDisplayName : "x" ,
117+ kind : ScriptElementKind . constElement ,
118+ kindModifiers : ScriptElementKindModifier . none ,
119+ triggerSpan : protocolTextSpanFromSubstring ( aTs . content , "x" ) ,
120+ } ,
121+ locs : [
122+ {
123+ file : aTs . path ,
124+ locs : [
125+ protocolRenameSpanFromSubstring ( aTs . content , "x" ) ,
126+ protocolRenameSpanFromSubstring ( aTs . content , "x" , { index : 1 } , { prefixText : "x: " } ) ,
127+ ] ,
128+ } ,
129+ ] ,
130+ } ) ;
131+ } ) ;
132+
133+ it ( "rename behavior is based on file of rename initiation" , ( ) => {
134+ const aTs : File = { path : "/a.ts" , content : "const x = 1; export { x };" } ;
135+ const bTs : File = { path : "/b.ts" , content : `import { x } from "./a"; const y = x + 1;` } ;
136+ const host = createServerHost ( [ aTs , bTs ] ) ;
137+ const session = createSession ( host ) ;
138+ openFilesForSession ( [ aTs , bTs ] , session ) ;
139+
140+ // rename from file with prefixText and suffixText enabled
141+ session . getProjectService ( ) . setHostConfiguration ( { file : "/a.ts" , formatOptions : { } , preferences : { providePrefixAndSuffixTextForRename : true } } ) ;
142+ const response1 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( aTs , "x" ) ) ;
143+ assert . deepEqual < protocol . RenameResponseBody | undefined > ( response1 , {
144+ info : {
145+ canRename : true ,
146+ fileToRename : undefined ,
147+ displayName : "x" ,
148+ fullDisplayName : "x" ,
149+ kind : ScriptElementKind . constElement ,
150+ kindModifiers : ScriptElementKindModifier . none ,
151+ triggerSpan : protocolTextSpanFromSubstring ( aTs . content , "x" ) ,
152+ } ,
153+ locs : [
154+ {
155+ file : aTs . path ,
156+ locs : [
157+ protocolRenameSpanFromSubstring ( aTs . content , "x" ) ,
158+ protocolRenameSpanFromSubstring ( aTs . content , "x" , { index : 2 } , { suffixText : " as x" } ) ,
159+ ] ,
160+ } ,
161+ ] ,
162+ } ) ;
163+
164+ // rename from file with prefixText and suffixText disabled
165+ const response2 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( bTs , "x" ) ) ;
166+ assert . deepEqual < protocol . RenameResponseBody | undefined > ( response2 , {
167+ info : {
168+ canRename : true ,
169+ fileToRename : undefined ,
170+ displayName : "x" ,
171+ fullDisplayName : "x" ,
172+ kind : ScriptElementKind . alias ,
173+ kindModifiers : ScriptElementKindModifier . none ,
174+ triggerSpan : protocolTextSpanFromSubstring ( bTs . content , "x" ) ,
175+ } ,
176+ locs : [
177+ {
178+ file : bTs . path ,
179+ locs : [
180+ protocolRenameSpanFromSubstring ( bTs . content , "x" ) ,
181+ protocolRenameSpanFromSubstring ( bTs . content , "x" , { index : 1 } )
182+ ]
183+ } ,
184+ {
185+ file : aTs . path ,
186+ locs : [
187+ protocolRenameSpanFromSubstring ( aTs . content , "x" ) ,
188+ protocolRenameSpanFromSubstring ( aTs . content , "x" , { index : 2 } ) ,
189+ ] ,
190+ } ,
191+ ] ,
192+ } ) ;
87193 } ) ;
88194 } ) ;
89195}
0 commit comments