@@ -9,186 +9,143 @@ import {
9
9
libFile ,
10
10
} from "../virtualFileSystemWithWatch" ;
11
11
import {
12
+ baselineTsserverLogs ,
13
+ createLoggerWithInMemoryLogs ,
12
14
createSession ,
13
- TestSession ,
14
15
} from "./helpers" ;
15
16
16
17
describe ( "unittests:: tsserver:: applyChangesToOpenFiles" , ( ) => {
17
- const configFile : File = {
18
- path : "/a/b/tsconfig.json" ,
19
- content : "{}"
20
- } ;
21
- const file3 : File = {
22
- path : "/a/b/file3.ts" ,
23
- content : "let xyz = 1;"
24
- } ;
25
- const app : File = {
26
- path : "/a/b/app.ts" ,
27
- content : "let z = 1;"
28
- } ;
29
-
30
18
function fileContentWithComment ( file : File ) {
31
19
return `// some copy right notice
32
20
${ file . content } `;
33
21
}
34
22
35
- function verifyText ( service : ts . server . ProjectService , file : string , expected : string ) {
36
- const info = service . getScriptInfo ( file ) ! ;
37
- const snap = info . getSnapshot ( ) ;
38
- // Verified applied in reverse order
39
- assert . equal ( snap . getText ( 0 , snap . getLength ( ) ) , expected , `Text of changed file: ${ file } ` ) ;
40
- }
41
-
42
- function verifyProjectVersion ( project : ts . server . Project , expected : number ) {
43
- assert . equal ( Number ( project . getProjectVersion ( ) ) , expected ) ;
44
- }
45
-
46
- interface Verify {
47
- applyChangesToOpen : ( session : TestSession ) => void ;
48
- openFile1Again : ( session : TestSession ) => void ;
49
- }
50
- function verify ( { applyChangesToOpen, openFile1Again } : Verify ) {
23
+ function setup ( ) {
24
+ const configFile : File = {
25
+ path : "/a/b/tsconfig.json" ,
26
+ content : "{}"
27
+ } ;
28
+ const file3 : File = {
29
+ path : "/a/b/file3.ts" ,
30
+ content : "let xyz = 1;"
31
+ } ;
32
+ const app : File = {
33
+ path : "/a/b/app.ts" ,
34
+ content : "let z = 1;"
35
+ } ;
51
36
const host = createServerHost ( [ app , file3 , commonFile1 , commonFile2 , libFile , configFile ] ) ;
52
- const session = createSession ( host ) ;
37
+ const session = createSession ( host , { logger : createLoggerWithInMemoryLogs ( host ) } ) ;
53
38
session . executeCommandSeq < ts . server . protocol . OpenRequest > ( {
54
39
command : ts . server . protocol . CommandTypes . Open ,
55
40
arguments : { file : app . path }
56
41
} ) ;
57
- const service = session . getProjectService ( ) ;
58
- const project = service . configuredProjects . get ( configFile . path ) ! ;
59
- assert . isDefined ( project ) ;
60
- verifyProjectVersion ( project , 1 ) ;
61
42
session . executeCommandSeq < ts . server . protocol . OpenRequest > ( {
62
43
command : ts . server . protocol . CommandTypes . Open ,
63
44
arguments : {
64
45
file : file3 . path ,
65
46
fileContent : fileContentWithComment ( file3 )
66
47
}
67
48
} ) ;
68
- verifyProjectVersion ( project , 2 ) ;
69
-
70
- // Verify Texts
71
- verifyText ( service , commonFile1 . path , commonFile1 . content ) ;
72
- verifyText ( service , commonFile2 . path , commonFile2 . content ) ;
73
- verifyText ( service , app . path , app . content ) ;
74
- verifyText ( service , file3 . path , fileContentWithComment ( file3 ) ) ;
75
-
76
- // Apply changes
77
- applyChangesToOpen ( session ) ;
78
-
79
- // Verify again
80
- verifyProjectVersion ( project , 3 ) ;
81
- // Open file contents
82
- verifyText ( service , commonFile1 . path , fileContentWithComment ( commonFile1 ) ) ;
83
- verifyText ( service , commonFile2 . path , fileContentWithComment ( commonFile2 ) ) ;
84
- verifyText ( service , app . path , "let zzz = 10;let zz = 10;let z = 1;" ) ;
85
- verifyText ( service , file3 . path , file3 . content ) ;
86
-
87
- // Open file1 again
88
- openFile1Again ( session ) ;
89
- assert . isTrue ( service . getScriptInfo ( commonFile1 . path ) ! . isScriptOpen ( ) ) ;
90
-
91
- // Verify that file1 contents are changed
92
- verifyProjectVersion ( project , 4 ) ;
93
- verifyText ( service , commonFile1 . path , commonFile1 . content ) ;
94
- verifyText ( service , commonFile2 . path , fileContentWithComment ( commonFile2 ) ) ;
95
- verifyText ( service , app . path , "let zzz = 10;let zz = 10;let z = 1;" ) ;
96
- verifyText ( service , file3 . path , file3 . content ) ;
49
+ return { session, file3, app } ;
97
50
}
98
51
99
52
it ( "with applyChangedToOpenFiles request" , ( ) => {
100
- verify ( {
101
- applyChangesToOpen : session => session . executeCommandSeq < ts . server . protocol . ApplyChangedToOpenFilesRequest > ( {
102
- command : ts . server . protocol . CommandTypes . ApplyChangedToOpenFiles ,
103
- arguments : {
104
- openFiles : [
105
- {
106
- fileName : commonFile1 . path ,
107
- content : fileContentWithComment ( commonFile1 )
108
- } ,
109
- {
110
- fileName : commonFile2 . path ,
111
- content : fileContentWithComment ( commonFile2 )
112
- }
113
- ] ,
114
- changedFiles : [
115
- {
116
- fileName : app . path ,
117
- changes : [
118
- {
119
- span : { start : 0 , length : 0 } ,
120
- newText : "let zzz = 10;"
121
- } ,
122
- {
123
- span : { start : 0 , length : 0 } ,
124
- newText : "let zz = 10;"
125
- }
126
- ]
127
- }
128
- ] ,
129
- closedFiles : [
130
- file3 . path
131
- ]
132
- }
133
- } ) ,
134
- openFile1Again : session => session . executeCommandSeq < ts . server . protocol . ApplyChangedToOpenFilesRequest > ( {
135
- command : ts . server . protocol . CommandTypes . ApplyChangedToOpenFiles ,
136
- arguments : {
137
- openFiles : [ {
53
+ const { session, file3, app } = setup ( ) ;
54
+ // Apply changes
55
+ session . executeCommandSeq < ts . server . protocol . ApplyChangedToOpenFilesRequest > ( {
56
+ command : ts . server . protocol . CommandTypes . ApplyChangedToOpenFiles ,
57
+ arguments : {
58
+ openFiles : [
59
+ {
138
60
fileName : commonFile1 . path ,
139
- content : commonFile1 . content
140
- } ]
141
- }
142
- } ) ,
61
+ content : fileContentWithComment ( commonFile1 )
62
+ } ,
63
+ {
64
+ fileName : commonFile2 . path ,
65
+ content : fileContentWithComment ( commonFile2 )
66
+ }
67
+ ] ,
68
+ changedFiles : [
69
+ {
70
+ fileName : app . path ,
71
+ changes : [
72
+ {
73
+ span : { start : 0 , length : 0 } ,
74
+ newText : "let zzz = 10;"
75
+ } ,
76
+ {
77
+ span : { start : 0 , length : 0 } ,
78
+ newText : "let zz = 10;"
79
+ }
80
+ ]
81
+ }
82
+ ] ,
83
+ closedFiles : [
84
+ file3 . path
85
+ ]
86
+ }
143
87
} ) ;
88
+ // Open file1 again
89
+ session . executeCommandSeq < ts . server . protocol . ApplyChangedToOpenFilesRequest > ( {
90
+ command : ts . server . protocol . CommandTypes . ApplyChangedToOpenFiles ,
91
+ arguments : {
92
+ openFiles : [ {
93
+ fileName : commonFile1 . path ,
94
+ content : commonFile1 . content
95
+ } ]
96
+ }
97
+ } ) ;
98
+ baselineTsserverLogs ( "applyChangesToOpenFiles" , "with applyChangedToOpenFiles request" , session ) ;
144
99
} ) ;
145
100
146
101
it ( "with updateOpen request" , ( ) => {
147
- verify ( {
148
- applyChangesToOpen : session => session . executeCommandSeq < ts . server . protocol . UpdateOpenRequest > ( {
149
- command : ts . server . protocol . CommandTypes . UpdateOpen ,
150
- arguments : {
151
- openFiles : [
152
- {
153
- file : commonFile1 . path ,
154
- fileContent : fileContentWithComment ( commonFile1 )
155
- } ,
156
- {
157
- file : commonFile2 . path ,
158
- fileContent : fileContentWithComment ( commonFile2 )
159
- }
160
- ] ,
161
- changedFiles : [
162
- {
163
- fileName : app . path ,
164
- textChanges : [
165
- {
166
- start : { line : 1 , offset : 1 } ,
167
- end : { line : 1 , offset : 1 } ,
168
- newText : "let zzz = 10;" ,
169
- } ,
170
- {
171
- start : { line : 1 , offset : 1 } ,
172
- end : { line : 1 , offset : 1 } ,
173
- newText : "let zz = 10;" ,
174
- }
175
- ]
176
- }
177
- ] ,
178
- closedFiles : [
179
- file3 . path
180
- ]
181
- }
182
- } ) ,
183
- openFile1Again : session => session . executeCommandSeq < ts . server . protocol . UpdateOpenRequest > ( {
184
- command : ts . server . protocol . CommandTypes . UpdateOpen ,
185
- arguments : {
186
- openFiles : [ {
102
+ const { session, file3, app } = setup ( ) ;
103
+ // Apply changes
104
+ session . executeCommandSeq < ts . server . protocol . UpdateOpenRequest > ( {
105
+ command : ts . server . protocol . CommandTypes . UpdateOpen ,
106
+ arguments : {
107
+ openFiles : [
108
+ {
187
109
file : commonFile1 . path ,
188
- fileContent : commonFile1 . content
189
- } ]
190
- }
191
- } ) ,
110
+ fileContent : fileContentWithComment ( commonFile1 )
111
+ } ,
112
+ {
113
+ file : commonFile2 . path ,
114
+ fileContent : fileContentWithComment ( commonFile2 )
115
+ }
116
+ ] ,
117
+ changedFiles : [
118
+ {
119
+ fileName : app . path ,
120
+ textChanges : [
121
+ {
122
+ start : { line : 1 , offset : 1 } ,
123
+ end : { line : 1 , offset : 1 } ,
124
+ newText : "let zzz = 10;" ,
125
+ } ,
126
+ {
127
+ start : { line : 1 , offset : 1 } ,
128
+ end : { line : 1 , offset : 1 } ,
129
+ newText : "let zz = 10;" ,
130
+ }
131
+ ]
132
+ }
133
+ ] ,
134
+ closedFiles : [
135
+ file3 . path
136
+ ]
137
+ }
138
+ } ) ;
139
+ // Open file1 again
140
+ session . executeCommandSeq < ts . server . protocol . UpdateOpenRequest > ( {
141
+ command : ts . server . protocol . CommandTypes . UpdateOpen ,
142
+ arguments : {
143
+ openFiles : [ {
144
+ file : commonFile1 . path ,
145
+ fileContent : commonFile1 . content
146
+ } ]
147
+ }
192
148
} ) ;
149
+ baselineTsserverLogs ( "applyChangesToOpenFiles" , "with updateOpen request" , session ) ;
193
150
} ) ;
194
151
} ) ;
0 commit comments