1
1
/// <reference path='..\services\services.ts' />
2
2
/// <reference path='..\services\shims.ts' />
3
+ /// <reference path='..\server\client.ts' />
3
4
/// <reference path='harness.ts' />
4
5
5
6
module Harness . LanguageService {
@@ -23,18 +24,18 @@ module Harness.LanguageService {
23
24
this . version ++ ;
24
25
}
25
26
26
- public editContent ( minChar : number , limChar : number , newText : string ) : void {
27
+ public editContent ( start : number , end : number , newText : string ) : void {
27
28
// Apply edits
28
- var prefix = this . content . substring ( 0 , minChar ) ;
29
+ var prefix = this . content . substring ( 0 , start ) ;
29
30
var middle = newText ;
30
- var suffix = this . content . substring ( limChar ) ;
31
+ var suffix = this . content . substring ( end ) ;
31
32
this . setContent ( prefix + middle + suffix ) ;
32
33
33
34
// Store edit range + new length of script
34
35
this . editRanges . push ( {
35
36
length : this . content . length ,
36
37
textChangeRange : ts . createTextChangeRange (
37
- ts . createTextSpanFromBounds ( minChar , limChar ) , newText . length )
38
+ ts . createTextSpanFromBounds ( start , end ) , newText . length )
38
39
} ) ;
39
40
40
41
// Update version #
@@ -145,24 +146,17 @@ module Harness.LanguageService {
145
146
this . fileNameToScript [ fileName ] = new ScriptInfo ( fileName , content ) ;
146
147
}
147
148
148
- public updateScript ( fileName : string , content : string ) {
149
+ public editScript ( fileName : string , start : number , end : number , newText : string ) {
149
150
var script = this . getScriptInfo ( fileName ) ;
150
151
if ( script !== null ) {
151
- script . updateContent ( content ) ;
152
+ script . editContent ( start , end , newText ) ;
152
153
return ;
153
154
}
154
155
155
- this . addScript ( fileName , content ) ;
156
+ throw new Error ( "No script with name '" + fileName + "'" ) ;
156
157
}
157
158
158
- public editScript ( fileName : string , minChar : number , limChar : number , newText : string ) {
159
- var script = this . getScriptInfo ( fileName ) ;
160
- if ( script !== null ) {
161
- script . editContent ( minChar , limChar , newText ) ;
162
- return ;
163
- }
164
-
165
- throw new Error ( "No script with name '" + fileName + "'" ) ;
159
+ public openFile ( fileName : string ) : void {
166
160
}
167
161
168
162
/**
@@ -236,8 +230,7 @@ module Harness.LanguageService {
236
230
getFilenames ( ) : string [ ] { return this . nativeHost . getFilenames ( ) ; }
237
231
getScriptInfo ( fileName : string ) : ScriptInfo { return this . nativeHost . getScriptInfo ( fileName ) ; }
238
232
addScript ( fileName : string , content : string ) : void { this . nativeHost . addScript ( fileName , content ) ; }
239
- updateScript ( fileName : string , content : string ) : void { return this . nativeHost . updateScript ( fileName , content ) ; }
240
- editScript ( fileName : string , minChar : number , limChar : number , newText : string ) : void { this . nativeHost . editScript ( fileName , minChar , limChar , newText ) ; }
233
+ editScript ( fileName : string , start : number , end : number , newText : string ) : void { this . nativeHost . editScript ( fileName , start , end , newText ) ; }
241
234
lineColToPosition ( fileName : string , line : number , col : number ) : number { return this . nativeHost . lineColToPosition ( fileName , line , col ) ; }
242
235
positionToZeroBasedLineCol ( fileName : string , position : number ) : ts . LineAndCharacter { return this . nativeHost . positionToZeroBasedLineCol ( fileName , position ) ; }
243
236
@@ -442,5 +435,156 @@ module Harness.LanguageService {
442
435
return convertResult ;
443
436
}
444
437
}
438
+
439
+ // Server adapter
440
+ class SessionClientHost extends NativeLanguageServiceHost implements ts . server . SessionClientHost {
441
+ private client : ts . server . SessionClient ;
442
+
443
+ constructor ( cancellationToken : ts . CancellationToken , settings : ts . CompilerOptions ) {
444
+ super ( cancellationToken , settings ) ;
445
+ }
446
+
447
+ onMessage ( message : string ) : void {
448
+
449
+ }
450
+
451
+ writeMessage ( message : string ) : void {
452
+
453
+ }
454
+
455
+ setClient ( client : ts . server . SessionClient ) {
456
+ this . client = client ;
457
+ }
458
+
459
+ openFile ( fileName : string ) : void {
460
+ super . openFile ( fileName ) ;
461
+ this . client . openFile ( fileName ) ;
462
+ }
463
+
464
+ editScript ( fileName : string , start : number , end : number , newText : string ) {
465
+ super . editScript ( fileName , start , end , newText ) ;
466
+ this . client . changeFile ( fileName , start , end , newText ) ;
467
+ }
468
+ }
469
+
470
+ class SessionServerHost implements ts . server . ServerHost , ts . server . Logger {
471
+ args : string [ ] = [ ] ;
472
+ newLine : string ;
473
+ useCaseSensitiveFileNames : boolean = false ;
474
+
475
+ constructor ( private host : NativeLanguageServiceHost ) {
476
+ this . newLine = this . host . getNewLine ( ) ;
477
+ }
478
+
479
+ onMessage ( message : string ) : void {
480
+
481
+ }
482
+
483
+ writeMessage ( message : string ) : void {
484
+ }
485
+
486
+ write ( message : string ) : void {
487
+ this . writeMessage ( message ) ;
488
+ }
489
+
490
+ readFile ( fileName : string ) : string {
491
+ if ( fileName . indexOf ( Harness . Compiler . defaultLibFileName ) >= 0 ) {
492
+ fileName = Harness . Compiler . defaultLibFileName ;
493
+ }
494
+
495
+ var snapshot = this . host . getScriptSnapshot ( fileName ) ;
496
+ return snapshot && snapshot . getText ( 0 , snapshot . getLength ( ) ) ;
497
+ }
498
+
499
+ writeFile ( name : string , text : string , writeByteOrderMark : boolean ) : void {
500
+ }
501
+
502
+ resolvePath ( path : string ) : string {
503
+ return path ;
504
+ }
505
+
506
+ fileExists ( path : string ) : boolean {
507
+ return ! ! this . host . getScriptSnapshot ( path ) ;
508
+ }
509
+
510
+ directoryExists ( path : string ) : boolean {
511
+ return false ;
512
+ }
513
+
514
+ getExecutingFilePath ( ) : string {
515
+ return "" ;
516
+ }
517
+
518
+ exit ( exitCode : number ) : void {
519
+ }
520
+
521
+ createDirectory ( directoryName : string ) : void {
522
+ throw new Error ( "Not Implemented Yet." ) ;
523
+ }
524
+
525
+ getCurrentDirectory ( ) : string {
526
+ return this . host . getCurrentDirectory ( ) ;
527
+ }
528
+
529
+ readDirectory ( path : string , extension ?: string ) : string [ ] {
530
+ throw new Error ( "Not implemented Yet." ) ;
531
+ }
532
+
533
+ watchFile ( fileName : string , callback : ( fileName : string ) => void ) : ts . FileWatcher {
534
+ return { close ( ) { } } ;
535
+ }
536
+
537
+ close ( ) : void {
538
+ }
539
+
540
+ info ( message : string ) : void {
541
+ return this . host . log ( message ) ;
542
+ }
543
+
544
+ msg ( message : string ) {
545
+ return this . host . log ( message ) ;
546
+ }
547
+
548
+ endGroup ( ) : void {
549
+ }
550
+
551
+ perftrc ( message : string ) : void {
552
+ return this . host . log ( message ) ;
553
+ }
554
+
555
+ startGroup ( ) : void {
556
+ }
557
+ }
558
+
559
+ export class ServerLanugageServiceAdapter implements LanguageServiceAdapter {
560
+ private host : SessionClientHost ;
561
+ private client : ts . server . SessionClient ;
562
+ constructor ( cancellationToken ?: ts . CancellationToken , options ?: ts . CompilerOptions ) {
563
+ // This is the main host that tests use to direct tests
564
+ var clientHost = new SessionClientHost ( cancellationToken , options ) ;
565
+ var client = new ts . server . SessionClient ( clientHost ) ;
566
+
567
+ // This host is just a proxy for the clientHost, it uses the client
568
+ // host to answer server queries about files on disk
569
+ var serverHost = new SessionServerHost ( clientHost ) ;
570
+ var server = new ts . server . Session ( serverHost , serverHost ) ;
571
+
572
+ // Fake the connection between the client and the server
573
+ serverHost . writeMessage = client . onMessage . bind ( client ) ;
574
+ clientHost . writeMessage = server . onMessage . bind ( server ) ;
575
+
576
+ // Wire the client to the host to get notifications when a file is open
577
+ // or edited.
578
+ clientHost . setClient ( client ) ;
579
+
580
+ // Set the properties
581
+ this . client = client ;
582
+ this . host = clientHost ;
583
+ }
584
+ getHost ( ) { return this . host ; }
585
+ getLanguageService ( ) : ts . LanguageService { return this . client ; }
586
+ getClassifier ( ) : ts . Classifier { throw new Error ( "getClassifier is not available using the server interface." ) ; }
587
+ getPreProcessedFileInfo ( fileName : string , fileContents : string ) : ts . PreProcessedFileInfo { throw new Error ( "getPreProcessedFileInfo is not available using the server interface." ) ; }
588
+ }
445
589
}
446
590
0 commit comments