9
9
* ------------------------------------------------------------------------------------------ */
10
10
'use strict' ;
11
11
12
- import { env , languages , commands , workspace , window , ExtensionContext , Memento , IndentAction , Diagnostic , DiagnosticCollection , Range , Disposable , Uri , MessageItem , TextEditor , DiagnosticSeverity } from 'vscode' ;
12
+ import { env , languages , commands , workspace , window , ExtensionContext , Memento , IndentAction , Diagnostic , DiagnosticCollection , Range , Disposable , Uri , MessageItem , TextEditor , DiagnosticSeverity , TextDocument } from 'vscode' ;
13
13
14
14
// This must be the first statement otherwise modules might got loaded with
15
15
// the wrong locale.
@@ -54,7 +54,6 @@ interface LanguageDescription {
54
54
id : string ;
55
55
diagnosticSource : string ;
56
56
modeIds : string [ ] ;
57
- extensions : string [ ] ;
58
57
configFile : string ;
59
58
}
60
59
@@ -79,14 +78,12 @@ export function activate(context: ExtensionContext): void {
79
78
id : 'typescript' ,
80
79
diagnosticSource : 'ts' ,
81
80
modeIds : [ MODE_ID_TS , MODE_ID_TSX ] ,
82
- extensions : [ '.ts' , '.tsx' ] ,
83
81
configFile : 'tsconfig.json'
84
82
} ,
85
83
{
86
84
id : 'javascript' ,
87
85
diagnosticSource : 'js' ,
88
86
modeIds : [ MODE_ID_JS , MODE_ID_JSX ] ,
89
- extensions : [ '.js' , '.jsx' , '.es6' , '.mjs' ] ,
90
87
configFile : 'jsconfig.json'
91
88
}
92
89
] , context . storagePath , context . globalState , context . workspaceState ) ;
@@ -136,8 +133,6 @@ export function activate(context: ExtensionContext): void {
136
133
const validateSetting = 'validate.enable' ;
137
134
138
135
class LanguageProvider {
139
-
140
- private readonly extensions : ObjectMap < boolean > ;
141
136
private syntaxDiagnostics : ObjectMap < Diagnostic [ ] > ;
142
137
private readonly currentDiagnostics : DiagnosticCollection ;
143
138
private readonly bufferSyncSupport : BufferSyncSupport ;
@@ -160,14 +155,11 @@ class LanguageProvider {
160
155
private readonly client : TypeScriptServiceClient ,
161
156
private readonly description : LanguageDescription
162
157
) {
163
- this . extensions = Object . create ( null ) ;
164
- description . extensions . forEach ( extension => this . extensions [ extension ] = true ) ;
165
-
166
158
this . bufferSyncSupport = new BufferSyncSupport ( client , description . modeIds , {
167
159
delete : ( file : string ) => {
168
160
this . currentDiagnostics . delete ( client . asUrl ( file ) ) ;
169
161
}
170
- } , this . extensions ) ;
162
+ } ) ;
171
163
this . syntaxDiagnostics = Object . create ( null ) ;
172
164
this . currentDiagnostics = languages . createDiagnosticCollection ( description . id ) ;
173
165
@@ -334,13 +326,20 @@ class LanguageProvider {
334
326
}
335
327
}
336
328
337
- public handles ( file : string ) : boolean {
338
- const extension = path . extname ( file ) ;
339
- if ( ( extension && this . extensions [ extension ] ) || this . bufferSyncSupport . handles ( file ) ) {
329
+ public handles ( file : string , doc : TextDocument ) : boolean {
330
+ if ( doc && this . description . modeIds . indexOf ( doc . languageId ) >= 0 ) {
340
331
return true ;
341
332
}
333
+
334
+ if ( this . bufferSyncSupport . handles ( file ) ) {
335
+ return true ;
336
+ }
337
+
342
338
const basename = path . basename ( file ) ;
343
- return ! ! basename && basename === this . description . configFile ;
339
+ if ( ! ! basename && basename === this . description . configFile ) {
340
+ return true ;
341
+ }
342
+ return false ;
344
343
}
345
344
346
345
public get id ( ) : string {
@@ -558,14 +557,15 @@ class TypeScriptServiceClientHost implements ITypescriptServiceClientHost {
558
557
} ) ;
559
558
}
560
559
561
- private findLanguage ( file : string ) : LanguageProvider | null {
562
- for ( let i = 0 ; i < this . languages . length ; i ++ ) {
563
- let language = this . languages [ i ] ;
564
- if ( language . handles ( file ) ) {
565
- return language ;
560
+ private findLanguage ( file : string ) : Thenable < LanguageProvider | null > {
561
+ return workspace . openTextDocument ( file ) . then ( ( doc : TextDocument ) => {
562
+ for ( const language of this . languages ) {
563
+ if ( language . handles ( file , doc ) ) {
564
+ return language ;
565
+ }
566
566
}
567
- }
568
- return null ;
567
+ return null ;
568
+ } , ( ) => null ) ;
569
569
}
570
570
571
571
private triggerAllDiagnostics ( ) {
@@ -580,22 +580,24 @@ class TypeScriptServiceClientHost implements ITypescriptServiceClientHost {
580
580
}
581
581
582
582
/* internal */ syntaxDiagnosticsReceived ( event : Proto . DiagnosticEvent ) : void {
583
- let body = event . body ;
583
+ const body = event . body ;
584
584
if ( body && body . diagnostics ) {
585
- let language = this . findLanguage ( body . file ) ;
586
- if ( language ) {
587
- language . syntaxDiagnosticsReceived ( body . file , this . createMarkerDatas ( body . diagnostics , language . diagnosticSource ) ) ;
588
- }
585
+ this . findLanguage ( body . file ) . then ( language => {
586
+ if ( language ) {
587
+ language . syntaxDiagnosticsReceived ( body . file , this . createMarkerDatas ( body . diagnostics , language . diagnosticSource ) ) ;
588
+ }
589
+ } ) ;
589
590
}
590
591
}
591
592
592
593
/* internal */ semanticDiagnosticsReceived ( event : Proto . DiagnosticEvent ) : void {
593
- let body = event . body ;
594
+ const body = event . body ;
594
595
if ( body && body . diagnostics ) {
595
- let language = this . findLanguage ( body . file ) ;
596
- if ( language ) {
597
- language . semanticDiagnosticsReceived ( body . file , this . createMarkerDatas ( body . diagnostics , language . diagnosticSource ) ) ;
598
- }
596
+ this . findLanguage ( body . file ) . then ( language => {
597
+ if ( language ) {
598
+ language . semanticDiagnosticsReceived ( body . file , this . createMarkerDatas ( body . diagnostics , language . diagnosticSource ) ) ;
599
+ }
600
+ } ) ;
599
601
}
600
602
/*
601
603
if (Is.defined(body.queueLength)) {
@@ -611,47 +613,48 @@ class TypeScriptServiceClientHost implements ITypescriptServiceClientHost {
611
613
return ;
612
614
}
613
615
614
- const language = body . triggerFile ? this . findLanguage ( body . triggerFile ) : this . findLanguage ( body . configFile ) ;
615
- if ( ! language ) {
616
- return ;
617
- }
618
- if ( body . diagnostics . length === 0 ) {
619
- language . configFileDiagnosticsReceived ( body . configFile , [ ] ) ;
620
- } else if ( body . diagnostics . length >= 1 ) {
621
- workspace . openTextDocument ( Uri . file ( body . configFile ) ) . then ( ( document ) => {
622
- let curly : [ number , number , number ] | undefined = undefined ;
623
- let nonCurly : [ number , number , number ] | undefined = undefined ;
624
- let diagnostic : Diagnostic ;
625
- for ( let index = 0 ; index < document . lineCount ; index ++ ) {
626
- const line = document . lineAt ( index ) ;
627
- const text = line . text ;
628
- const firstNonWhitespaceCharacterIndex = line . firstNonWhitespaceCharacterIndex ;
629
- if ( firstNonWhitespaceCharacterIndex < text . length ) {
630
- if ( text . charAt ( firstNonWhitespaceCharacterIndex ) === '{' ) {
631
- curly = [ index , firstNonWhitespaceCharacterIndex , firstNonWhitespaceCharacterIndex + 1 ] ;
632
- break ;
633
- } else {
634
- const matches = / \s * ( [ ^ \s ] * ) (?: \s * | $ ) / . exec ( text . substr ( firstNonWhitespaceCharacterIndex ) ) ;
635
- if ( matches && matches . length >= 1 ) {
636
- nonCurly = [ index , firstNonWhitespaceCharacterIndex , firstNonWhitespaceCharacterIndex + matches [ 1 ] . length ] ;
616
+ ( body . triggerFile ? this . findLanguage ( body . triggerFile ) : this . findLanguage ( body . configFile ) ) . then ( language => {
617
+ if ( ! language ) {
618
+ return ;
619
+ }
620
+ if ( body . diagnostics . length === 0 ) {
621
+ language . configFileDiagnosticsReceived ( body . configFile , [ ] ) ;
622
+ } else if ( body . diagnostics . length >= 1 ) {
623
+ workspace . openTextDocument ( Uri . file ( body . configFile ) ) . then ( ( document ) => {
624
+ let curly : [ number , number , number ] | undefined = undefined ;
625
+ let nonCurly : [ number , number , number ] | undefined = undefined ;
626
+ let diagnostic : Diagnostic ;
627
+ for ( let index = 0 ; index < document . lineCount ; index ++ ) {
628
+ const line = document . lineAt ( index ) ;
629
+ const text = line . text ;
630
+ const firstNonWhitespaceCharacterIndex = line . firstNonWhitespaceCharacterIndex ;
631
+ if ( firstNonWhitespaceCharacterIndex < text . length ) {
632
+ if ( text . charAt ( firstNonWhitespaceCharacterIndex ) === '{' ) {
633
+ curly = [ index , firstNonWhitespaceCharacterIndex , firstNonWhitespaceCharacterIndex + 1 ] ;
634
+ break ;
635
+ } else {
636
+ const matches = / \s * ( [ ^ \s ] * ) (?: \s * | $ ) / . exec ( text . substr ( firstNonWhitespaceCharacterIndex ) ) ;
637
+ if ( matches && matches . length >= 1 ) {
638
+ nonCurly = [ index , firstNonWhitespaceCharacterIndex , firstNonWhitespaceCharacterIndex + matches [ 1 ] . length ] ;
639
+ }
637
640
}
638
641
}
639
642
}
640
- }
641
- const match = curly || nonCurly ;
642
- if ( match ) {
643
- diagnostic = new Diagnostic ( new Range ( match [ 0 ] , match [ 1 ] , match [ 0 ] , match [ 2 ] ) , body . diagnostics [ 0 ] . text ) ;
644
- } else {
645
- diagnostic = new Diagnostic ( new Range ( 0 , 0 , 0 , 0 ) , body . diagnostics [ 0 ] . text ) ;
646
- }
647
- if ( diagnostic ) {
648
- diagnostic . source = language . diagnosticSource ;
649
- language . configFileDiagnosticsReceived ( body . configFile , [ diagnostic ] ) ;
650
- }
651
- } , _error => {
652
- language . configFileDiagnosticsReceived ( body . configFile , [ new Diagnostic ( new Range ( 0 , 0 , 0 , 0 ) , body . diagnostics [ 0 ] . text ) ] ) ;
653
- } ) ;
654
- }
643
+ const match = curly || nonCurly ;
644
+ if ( match ) {
645
+ diagnostic = new Diagnostic ( new Range ( match [ 0 ] , match [ 1 ] , match [ 0 ] , match [ 2 ] ) , body . diagnostics [ 0 ] . text ) ;
646
+ } else {
647
+ diagnostic = new Diagnostic ( new Range ( 0 , 0 , 0 , 0 ) , body . diagnostics [ 0 ] . text ) ;
648
+ }
649
+ if ( diagnostic ) {
650
+ diagnostic . source = language . diagnosticSource ;
651
+ language . configFileDiagnosticsReceived ( body . configFile , [ diagnostic ] ) ;
652
+ }
653
+ } , _error => {
654
+ language . configFileDiagnosticsReceived ( body . configFile , [ new Diagnostic ( new Range ( 0 , 0 , 0 , 0 ) , body . diagnostics [ 0 ] . text ) ] ) ;
655
+ } ) ;
656
+ }
657
+ } ) ;
655
658
}
656
659
657
660
private createMarkerDatas ( diagnostics : Proto . Diagnostic [ ] , source : string ) : Diagnostic [ ] {
0 commit comments