44/// <reference path="editorServices.ts" />
55
66namespace ts . server {
7- const spaceCache : string [ ] = [ ] ;
8-
97 interface StackTraceError extends Error {
108 stack ?: string ;
119 }
@@ -22,50 +20,21 @@ namespace ts.server {
2220 return ( ( 1e9 * seconds ) + nanoseconds ) / 1000000.0 ;
2321 }
2422
25- export interface ServerHost {
23+ export interface ServerHost extends System {
24+ setTimeout ( callback : ( ...args : any [ ] ) => void , ms : number , ...args : any [ ] ) : any ;
25+ clearTimeout ( timeoutId : any ) : void ;
26+ setImmediate ( callback : ( ...args : any [ ] ) => void , ...args : any [ ] ) : any ;
27+ clearImmediate ( timeoutId : any ) : void ;
2628 writeCompressedData ( prefix : string , data : CompressedData , suffix : string ) : void ;
2729 }
2830
29- export function generateSpaces ( n : number ) : string {
30- if ( ! spaceCache [ n ] ) {
31- let strBuilder = "" ;
32- for ( let i = 0 ; i < n ; i ++ ) {
33- strBuilder += " " ;
34- }
35- spaceCache [ n ] = strBuilder ;
36- }
37- return spaceCache [ n ] ;
38- }
39-
40- export function generateIndentString ( n : number , editorOptions : EditorSettings ) : string {
41- if ( editorOptions . convertTabsToSpaces ) {
42- return generateSpaces ( n ) ;
43- }
44- else {
45- let result = "" ;
46- for ( let i = 0 ; i < Math . floor ( n / editorOptions . tabSize ) ; i ++ ) {
47- result += "\t" ;
48- }
49- for ( let i = 0 ; i < n % editorOptions . tabSize ; i ++ ) {
50- result += " " ;
51- }
52- return result ;
53- }
54- }
55-
5631 interface FileStart {
5732 file : string ;
5833 start : ILineInfo ;
5934 }
6035
6136 function compareNumber ( a : number , b : number ) {
62- if ( a < b ) {
63- return - 1 ;
64- }
65- else if ( a === b ) {
66- return 0 ;
67- }
68- else return 1 ;
37+ return a - b ;
6938 }
7039
7140 function compareFileStart ( a : FileStart , b : FileStart ) {
@@ -107,8 +76,8 @@ namespace ts.server {
10776 }
10877
10978 function allEditsBeforePos ( edits : ts . TextChange [ ] , pos : number ) {
110- for ( let i = 0 , len = edits . length ; i < len ; i ++ ) {
111- if ( ts . textSpanEnd ( edits [ i ] . span ) >= pos ) {
79+ for ( const edit of edits ) {
80+ if ( textSpanEnd ( edit . span ) >= pos ) {
11281 return false ;
11382 }
11483 }
@@ -181,13 +150,6 @@ namespace ts.server {
181150 export const ProjectLanguageServiceDisabled = new Error ( "The project's language service is disabled." ) ;
182151 }
183152
184- export interface ServerHost extends System {
185- setTimeout ( callback : ( ...args : any [ ] ) => void , ms : number , ...args : any [ ] ) : any ;
186- clearTimeout ( timeoutId : any ) : void ;
187- setImmediate ( callback : ( ...args : any [ ] ) => void , ...args : any [ ] ) : any ;
188- clearImmediate ( timeoutId : any ) : void ;
189- }
190-
191153 export class Session {
192154 protected projectService : ProjectService ;
193155 private errorTimer : any ; /*NodeJS.Timer | number*/
@@ -218,33 +180,25 @@ namespace ts.server {
218180 }
219181
220182 public logError ( err : Error , cmd : string ) {
221- const typedErr = < StackTraceError > err ;
222183 let msg = "Exception on executing command " + cmd ;
223- if ( typedErr . message ) {
224- msg += ":\n" + typedErr . message ;
225- if ( typedErr . stack ) {
226- msg += "\n" + typedErr . stack ;
184+ if ( err . message ) {
185+ msg += ":\n" + err . message ;
186+ if ( ( < StackTraceError > err ) . stack ) {
187+ msg += "\n" + ( < StackTraceError > err ) . stack ;
227188 }
228189 }
229190 this . projectService . log ( msg ) ;
230191 }
231192
232- private sendLineToClient ( line : string ) {
233- this . host . write ( line + this . host . newLine ) ;
234- }
235-
236- private sendCompressedDataToClient ( prefix : string , data : CompressedData ) {
237- this . host . writeCompressedData ( prefix , data , this . host . newLine ) ;
238- }
239-
240193 public send ( msg : protocol . Message , canCompressResponse : boolean ) {
241194 const json = JSON . stringify ( msg ) ;
242195 if ( this . logger . isVerbose ( ) ) {
243196 this . logger . info ( msg . type + ": " + json ) ;
244197 }
198+
245199 const len = this . byteLength ( json , "utf8" ) ;
246200 if ( len < this . maxUncompressedMessageSize || ! canCompressResponse ) {
247- this . sendLineToClient ( " Content-Length: " + ( 1 + this . byteLength ( json , "utf8" ) ) + " \r\n\r\n" + json ) ;
201+ this . host . write ( ` Content-Length: ${ 1 + this . byteLength ( json , "utf8" ) } \r\n\r\n${ json } ${ this . host . newLine } ` ) ;
248202 }
249203 else {
250204 const start = this . logger . isVerbose ( ) && this . hrtime ( ) ;
@@ -253,7 +207,7 @@ namespace ts.server {
253207 const elapsed = this . hrtime ( start ) ;
254208 this . logger . info ( `compressed message ${ json . length } to ${ compressed . length } in ${ hrTimeToMilliseconds ( elapsed ) } ms using ${ compressed . compressionKind } ` ) ;
255209 }
256- this . sendCompressedDataToClient ( `Content-Length: ${ compressed . length + 1 } ${ compressed . compressionKind } \r\n\r\n` , compressed ) ;
210+ this . host . writeCompressedData ( `Content-Length: ${ compressed . length + 1 } ${ compressed . compressionKind } \r\n\r\n` , compressed , this . host . newLine ) ;
257211 }
258212 }
259213
@@ -282,7 +236,7 @@ namespace ts.server {
282236 this . send ( ev , /*canCompressResponse*/ false ) ;
283237 }
284238
285- private response ( info : any , cmdName : string , canCompressResponse : boolean , reqSeq = 0 , errorMsg ?: string ) {
239+ public output ( info : any , cmdName : string , canCompressResponse : boolean , reqSeq = 0 , errorMsg ?: string ) {
286240 const res : protocol . Response = {
287241 seq : 0 ,
288242 type : "response" ,
@@ -299,10 +253,6 @@ namespace ts.server {
299253 this . send ( res , canCompressResponse ) ;
300254 }
301255
302- public output ( body : any , commandName : string , canCompressResponse : boolean , requestSequence = 0 , errorMessage ?: string ) {
303- this . response ( body , commandName , canCompressResponse , requestSequence , errorMessage ) ;
304- }
305-
306256 private getLocation ( position : number , scriptInfo : ScriptInfo ) : protocol . Location {
307257 const { line, offset } = scriptInfo . positionToLineOffset ( position ) ;
308258 return { line, offset : offset + 1 } ;
@@ -335,10 +285,6 @@ namespace ts.server {
335285 }
336286 }
337287
338- private reloadProjects ( ) {
339- this . projectService . reloadProjects ( ) ;
340- }
341-
342288 private updateProjectStructure ( seq : number , matchSeq : ( seq : number ) => boolean , ms = 1500 ) {
343289 this . host . setTimeout ( ( ) => {
344290 if ( matchSeq ( seq ) ) {
@@ -942,7 +888,7 @@ namespace ts.server {
942888 const firstNoWhiteSpacePosition = lineInfo . offset + i ;
943889 edits . push ( {
944890 span : ts . createTextSpanFromBounds ( lineInfo . offset , firstNoWhiteSpacePosition ) ,
945- newText : generateIndentString ( preferredIndent , formatOptions )
891+ newText : formatting . getIndentationString ( preferredIndent , formatOptions )
946892 } ) ;
947893 }
948894 }
@@ -1488,7 +1434,7 @@ namespace ts.server {
14881434 return this . requiredResponse ( this . getProjectInfo ( request . arguments ) ) ;
14891435 } ,
14901436 [ CommandNames . ReloadProjects ] : ( request : protocol . ReloadProjectsRequest ) => {
1491- this . reloadProjects ( ) ;
1437+ this . projectService . reloadProjects ( ) ;
14921438 return this . notRequired ( ) ;
14931439 }
14941440 } ;
0 commit comments