@@ -7,6 +7,7 @@ namespace ts.projectSystem {
7
7
const session = createSession ( createServerHost ( [ aTs , bTs ] ) ) ;
8
8
openFilesForSession ( [ bTs ] , session ) ;
9
9
10
+ // rename fails with allowRenameOfImportPath disabled
10
11
const response1 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( bTs , 'a";' ) ) ;
11
12
assert . deepEqual < protocol . RenameResponseBody | undefined > ( response1 , {
12
13
info : {
@@ -16,6 +17,7 @@ namespace ts.projectSystem {
16
17
locs : [ { file : bTs . path , locs : [ protocolRenameSpanFromSubstring ( bTs . content , "./a" ) ] } ] ,
17
18
} ) ;
18
19
20
+ // rename succeeds with allowRenameOfImportPath enabled in host
19
21
session . getProjectService ( ) . setHostConfiguration ( { preferences : { allowRenameOfImportPath : true } } ) ;
20
22
const response2 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( bTs , 'a";' ) ) ;
21
23
assert . deepEqual < protocol . RenameResponseBody | undefined > ( response2 , {
@@ -30,6 +32,23 @@ namespace ts.projectSystem {
30
32
} ,
31
33
locs : [ { file : bTs . path , locs : [ protocolRenameSpanFromSubstring ( bTs . content , "./a" ) ] } ] ,
32
34
} ) ;
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
+ } ) ;
33
52
} ) ;
34
53
35
54
it ( "works with prefixText and suffixText when enabled" , ( ) => {
@@ -61,7 +80,7 @@ namespace ts.projectSystem {
61
80
] ,
62
81
} ) ;
63
82
64
- // rename with prefixText and suffixText enabled
83
+ // rename with prefixText and suffixText enabled in host
65
84
session . getProjectService ( ) . setHostConfiguration ( { preferences : { providePrefixAndSuffixTextForRename : true } } ) ;
66
85
const response2 = executeSessionRequest < protocol . RenameRequest , protocol . RenameResponse > ( session , protocol . CommandTypes . Rename , protocolFileLocationFromSubstring ( aTs , "x" ) ) ;
67
86
assert . deepEqual < protocol . RenameResponseBody | undefined > ( response2 , {
@@ -84,6 +103,93 @@ namespace ts.projectSystem {
84
103
} ,
85
104
] ,
86
105
} ) ;
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
+ } ) ;
87
193
} ) ;
88
194
} ) ;
89
195
}
0 commit comments