7
7
8
8
import * as assert from 'assert' ;
9
9
import { join } from 'path' ;
10
- import {
11
- languages , workspace , commands , Uri , Diagnostic , Range , Command , Disposable , CancellationToken ,
12
- CompletionList , CompletionItem , CompletionItemKind , TextDocument , Position
13
- } from 'vscode' ;
14
-
10
+ import * as vscode from 'vscode' ;
11
+ import { createRandomFile } from '../utils' ;
15
12
16
13
suite ( 'languages namespace tests' , ( ) => {
17
14
15
+ test ( 'setTextDocumentLanguage -> close/open event' , async function ( ) {
16
+ const file = await createRandomFile ( 'foo\nbar\nbar' ) ;
17
+ const doc = await vscode . workspace . openTextDocument ( file ) ;
18
+ const langIdNow = doc . languageId ;
19
+ let clock = 0 ;
20
+
21
+ let close = new Promise ( resolve => {
22
+ vscode . workspace . onDidCloseTextDocument ( e => {
23
+ if ( e === doc ) {
24
+ assert . equal ( doc . languageId , langIdNow ) ;
25
+ assert . equal ( clock , 0 ) ;
26
+ clock += 1 ;
27
+ resolve ( ) ;
28
+ }
29
+ } ) ;
30
+ } ) ;
31
+ let open = new Promise ( resolve => {
32
+ vscode . workspace . onDidOpenTextDocument ( e => {
33
+ if ( e === doc ) { // same instance!
34
+ assert . equal ( doc . languageId , 'json' ) ;
35
+ assert . equal ( clock , 1 ) ;
36
+ clock += 1 ;
37
+ resolve ( ) ;
38
+ }
39
+ } ) ;
40
+ } ) ;
41
+ let change = vscode . languages . setTextDocumentLanguage ( doc , 'json' ) ;
42
+ await Promise . all ( [ change , close , open ] ) ;
43
+ assert . equal ( clock , 2 ) ;
44
+ assert . equal ( doc . languageId , 'json' ) ;
45
+ } ) ;
46
+
47
+ test ( 'setTextDocumentLanguage -> error when language does not exist' , async function ( ) {
48
+ const file = await createRandomFile ( 'foo\nbar\nbar' ) ;
49
+ const doc = await vscode . workspace . openTextDocument ( file ) ;
50
+
51
+ try {
52
+ await vscode . languages . setTextDocumentLanguage ( doc , 'fooLangDoesNotExist' ) ;
53
+ assert . ok ( false ) ;
54
+ } catch ( err ) {
55
+ assert . ok ( err ) ;
56
+ }
57
+ } ) ;
58
+
18
59
test ( 'diagnostics, read & event' , function ( ) {
19
- let uri = Uri . file ( '/foo/bar.txt' ) ;
20
- let col1 = languages . createDiagnosticCollection ( 'foo1' ) ;
21
- col1 . set ( uri , [ new Diagnostic ( new Range ( 0 , 0 , 0 , 12 ) , 'error1' ) ] ) ;
60
+ let uri = vscode . Uri . file ( '/foo/bar.txt' ) ;
61
+ let col1 = vscode . languages . createDiagnosticCollection ( 'foo1' ) ;
62
+ col1 . set ( uri , [ new vscode . Diagnostic ( new vscode . Range ( 0 , 0 , 0 , 12 ) , 'error1' ) ] ) ;
22
63
23
- let col2 = languages . createDiagnosticCollection ( 'foo2' ) ;
24
- col2 . set ( uri , [ new Diagnostic ( new Range ( 0 , 0 , 0 , 12 ) , 'error1' ) ] ) ;
64
+ let col2 = vscode . languages . createDiagnosticCollection ( 'foo2' ) ;
65
+ col2 . set ( uri , [ new vscode . Diagnostic ( new vscode . Range ( 0 , 0 , 0 , 12 ) , 'error1' ) ] ) ;
25
66
26
- let diag = languages . getDiagnostics ( uri ) ;
67
+ let diag = vscode . languages . getDiagnostics ( uri ) ;
27
68
assert . equal ( diag . length , 2 ) ;
28
69
29
- let tuples = languages . getDiagnostics ( ) ;
70
+ let tuples = vscode . languages . getDiagnostics ( ) ;
30
71
let found = false ;
31
72
for ( let [ thisUri , ] of tuples ) {
32
73
if ( thisUri . toString ( ) === uri . toString ( ) ) {
@@ -40,21 +81,21 @@ suite('languages namespace tests', () => {
40
81
41
82
test ( 'diagnostics & CodeActionProvider' , function ( ) {
42
83
43
- class D2 extends Diagnostic {
84
+ class D2 extends vscode . Diagnostic {
44
85
customProp = { complex ( ) { } } ;
45
86
constructor ( ) {
46
- super ( new Range ( 0 , 2 , 0 , 7 ) , 'sonntag' ) ;
87
+ super ( new vscode . Range ( 0 , 2 , 0 , 7 ) , 'sonntag' ) ;
47
88
}
48
89
}
49
90
50
- let diag1 = new Diagnostic ( new Range ( 0 , 0 , 0 , 5 ) , 'montag' ) ;
91
+ let diag1 = new vscode . Diagnostic ( new vscode . Range ( 0 , 0 , 0 , 5 ) , 'montag' ) ;
51
92
let diag2 = new D2 ( ) ;
52
93
53
94
let ran = false ;
54
- let uri = Uri . parse ( 'ttt:path.far' ) ;
95
+ let uri = vscode . Uri . parse ( 'ttt:path.far' ) ;
55
96
56
- let r1 = languages . registerCodeActionsProvider ( { pattern : '*.far' , scheme : 'ttt' } , {
57
- provideCodeActions ( document , range , ctx ) : Command [ ] {
97
+ let r1 = vscode . languages . registerCodeActionsProvider ( { pattern : '*.far' , scheme : 'ttt' } , {
98
+ provideCodeActions ( document , range , ctx ) : vscode . Command [ ] {
58
99
59
100
assert . equal ( ctx . diagnostics . length , 2 ) ;
60
101
let [ first , second ] = ctx . diagnostics ;
@@ -66,44 +107,44 @@ suite('languages namespace tests', () => {
66
107
}
67
108
} ) ;
68
109
69
- let r2 = workspace . registerTextDocumentContentProvider ( 'ttt' , {
110
+ let r2 = vscode . workspace . registerTextDocumentContentProvider ( 'ttt' , {
70
111
provideTextDocumentContent ( ) {
71
112
return 'this is some text' ;
72
113
}
73
114
} ) ;
74
115
75
- let r3 = languages . createDiagnosticCollection ( ) ;
116
+ let r3 = vscode . languages . createDiagnosticCollection ( ) ;
76
117
r3 . set ( uri , [ diag1 ] ) ;
77
118
78
- let r4 = languages . createDiagnosticCollection ( ) ;
119
+ let r4 = vscode . languages . createDiagnosticCollection ( ) ;
79
120
r4 . set ( uri , [ diag2 ] ) ;
80
121
81
- return workspace . openTextDocument ( uri ) . then ( doc => {
82
- return commands . executeCommand ( 'vscode.executeCodeActionProvider' , uri , new Range ( 0 , 0 , 0 , 10 ) ) ;
122
+ return vscode . workspace . openTextDocument ( uri ) . then ( doc => {
123
+ return vscode . commands . executeCommand ( 'vscode.executeCodeActionProvider' , uri , new vscode . Range ( 0 , 0 , 0 , 10 ) ) ;
83
124
} ) . then ( commands => {
84
125
assert . ok ( ran ) ;
85
- Disposable . from ( r1 , r2 , r3 , r4 ) . dispose ( ) ;
126
+ vscode . Disposable . from ( r1 , r2 , r3 , r4 ) . dispose ( ) ;
86
127
} ) ;
87
128
} ) ;
88
129
89
130
test ( 'completions with document filters' , function ( ) {
90
131
let ran = false ;
91
- let uri = Uri . file ( join ( workspace . rootPath || '' , './bower.json' ) ) ;
132
+ let uri = vscode . Uri . file ( join ( vscode . workspace . rootPath || '' , './bower.json' ) ) ;
92
133
93
134
let jsonDocumentFilter = [ { language : 'json' , pattern : '**/package.json' } , { language : 'json' , pattern : '**/bower.json' } , { language : 'json' , pattern : '**/.bower.json' } ] ;
94
135
95
- let r1 = languages . registerCompletionItemProvider ( jsonDocumentFilter , {
96
- provideCompletionItems : ( document : TextDocument , position : Position , token : CancellationToken ) : CompletionItem [ ] => {
97
- let proposal = new CompletionItem ( 'foo' ) ;
98
- proposal . kind = CompletionItemKind . Property ;
136
+ let r1 = vscode . languages . registerCompletionItemProvider ( jsonDocumentFilter , {
137
+ provideCompletionItems : ( document : vscode . TextDocument , position : vscode . Position , token : vscode . CancellationToken ) : vscode . CompletionItem [ ] => {
138
+ let proposal = new vscode . CompletionItem ( 'foo' ) ;
139
+ proposal . kind = vscode . CompletionItemKind . Property ;
99
140
ran = true ;
100
141
return [ proposal ] ;
101
142
}
102
143
} ) ;
103
144
104
- return workspace . openTextDocument ( uri ) . then ( doc => {
105
- return commands . executeCommand < CompletionList > ( 'vscode.executeCompletionItemProvider' , uri , new Position ( 1 , 0 ) ) ;
106
- } ) . then ( ( result : CompletionList | undefined ) => {
145
+ return vscode . workspace . openTextDocument ( uri ) . then ( doc => {
146
+ return vscode . commands . executeCommand < vscode . CompletionList > ( 'vscode.executeCompletionItemProvider' , uri , new vscode . Position ( 1 , 0 ) ) ;
147
+ } ) . then ( ( result : vscode . CompletionList | undefined ) => {
107
148
r1 . dispose ( ) ;
108
149
assert . ok ( ran ) ;
109
150
console . log ( result ! . items ) ;
0 commit comments