@@ -7,15 +7,27 @@ import { DisposableStore } from 'vs/base/common/lifecycle';
77import { URI } from 'vs/base/common/uri' ;
88import { Range } from 'vs/editor/common/core/range' ;
99import { TextModel } from 'vs/editor/common/model/textModel' ;
10- import { CodeAction , CodeActionContext , CodeActionProvider , CodeActionProviderRegistry , Command , LanguageIdentifier , ResourceTextEdit , WorkspaceEdit } from 'vs/editor/common/modes' ;
10+ import * as modes from 'vs/editor/common/modes' ;
1111import { getCodeActions } from 'vs/editor/contrib/codeAction/codeAction' ;
1212import { CodeActionKind } from 'vs/editor/contrib/codeAction/codeActionTrigger' ;
1313import { IMarkerData , MarkerSeverity } from 'vs/platform/markers/common/markers' ;
1414import { CancellationToken } from 'vs/base/common/cancellation' ;
1515
16+ function staticCodeActionProvider ( ...actions : modes . CodeAction [ ] ) : modes . CodeActionProvider {
17+ return new class implements modes . CodeActionProvider {
18+ provideCodeActions ( ) : modes . CodeActionList {
19+ return {
20+ actions : actions ,
21+ dispose : ( ) => { }
22+ } ;
23+ }
24+ } ;
25+ }
26+
27+
1628suite ( 'CodeAction' , ( ) => {
1729
18- let langId = new LanguageIdentifier ( 'fooLang' , 17 ) ;
30+ let langId = new modes . LanguageIdentifier ( 'fooLang' , 17 ) ;
1931 let uri = URI . parse ( 'untitled:path' ) ;
2032 let model : TextModel ;
2133 const disposables = new DisposableStore ( ) ;
@@ -46,7 +58,7 @@ suite('CodeAction', () => {
4658 } ,
4759 command : {
4860 abc : {
49- command : new class implements Command {
61+ command : new class implements modes . Command {
5062 id : '1' ;
5163 title : 'abc' ;
5264 } ,
@@ -56,8 +68,8 @@ suite('CodeAction', () => {
5668 spelling : {
5769 bcd : {
5870 diagnostics : < IMarkerData [ ] > [ ] ,
59- edit : new class implements WorkspaceEdit {
60- edits : ResourceTextEdit [ ] ;
71+ edit : new class implements modes . WorkspaceEdit {
72+ edits : modes . ResourceTextEdit [ ] ;
6173 } ,
6274 title : 'abc'
6375 }
@@ -90,20 +102,16 @@ suite('CodeAction', () => {
90102
91103 test ( 'CodeActions are sorted by type, #38623' , async function ( ) {
92104
93- const provider = new class implements CodeActionProvider {
94- provideCodeActions ( ) {
95- return [
96- testData . command . abc ,
97- testData . diagnostics . bcd ,
98- testData . spelling . bcd ,
99- testData . tsLint . bcd ,
100- testData . tsLint . abc ,
101- testData . diagnostics . abc
102- ] ;
103- }
104- } ;
105+ const provider = staticCodeActionProvider (
106+ testData . command . abc ,
107+ testData . diagnostics . bcd ,
108+ testData . spelling . bcd ,
109+ testData . tsLint . bcd ,
110+ testData . tsLint . abc ,
111+ testData . diagnostics . abc
112+ ) ;
105113
106- disposables . add ( CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
114+ disposables . add ( modes . CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
107115
108116 const expected = [
109117 // CodeActions with a diagnostics array are shown first ordered by diagnostics.message
@@ -123,17 +131,13 @@ suite('CodeAction', () => {
123131 } ) ;
124132
125133 test ( 'getCodeActions should filter by scope' , async function ( ) {
126- const provider = new class implements CodeActionProvider {
127- provideCodeActions ( ) : CodeAction [ ] {
128- return [
129- { title : 'a' , kind : 'a' } ,
130- { title : 'b' , kind : 'b' } ,
131- { title : 'a.b' , kind : 'a.b' }
132- ] ;
133- }
134- } ;
134+ const provider = staticCodeActionProvider (
135+ { title : 'a' , kind : 'a' } ,
136+ { title : 'b' , kind : 'b' } ,
137+ { title : 'a.b' , kind : 'a.b' }
138+ ) ;
135139
136- disposables . add ( CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
140+ disposables . add ( modes . CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
137141
138142 {
139143 const { actions } = await getCodeActions ( model , new Range ( 1 , 1 , 2 , 1 ) , { type : 'auto' , filter : { kind : new CodeActionKind ( 'a' ) } } , CancellationToken . None ) ;
@@ -155,32 +159,31 @@ suite('CodeAction', () => {
155159 } ) ;
156160
157161 test ( 'getCodeActions should forward requested scope to providers' , async function ( ) {
158- const provider = new class implements CodeActionProvider {
159- provideCodeActions ( _model : any , _range : Range , context : CodeActionContext , _token : any ) : CodeAction [ ] {
160- return [
161- { title : context . only || '' , kind : context . only }
162- ] ;
162+ const provider = new class implements modes . CodeActionProvider {
163+ provideCodeActions ( _model : any , _range : Range , context : modes . CodeActionContext , _token : any ) : modes . CodeActionList {
164+ return {
165+ actions : [
166+ { title : context . only || '' , kind : context . only }
167+ ] ,
168+ dispose : ( ) => { }
169+ } ;
163170 }
164171 } ;
165172
166- disposables . add ( CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
173+ disposables . add ( modes . CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
167174
168175 const { actions } = await getCodeActions ( model , new Range ( 1 , 1 , 2 , 1 ) , { type : 'auto' , filter : { kind : new CodeActionKind ( 'a' ) } } , CancellationToken . None ) ;
169176 assert . equal ( actions . length , 1 ) ;
170177 assert . strictEqual ( actions [ 0 ] . title , 'a' ) ;
171178 } ) ;
172179
173180 test ( 'getCodeActions should not return source code action by default' , async function ( ) {
174- const provider = new class implements CodeActionProvider {
175- provideCodeActions ( ) : CodeAction [ ] {
176- return [
177- { title : 'a' , kind : CodeActionKind . Source . value } ,
178- { title : 'b' , kind : 'b' }
179- ] ;
180- }
181- } ;
181+ const provider = staticCodeActionProvider (
182+ { title : 'a' , kind : CodeActionKind . Source . value } ,
183+ { title : 'b' , kind : 'b' }
184+ ) ;
182185
183- disposables . add ( CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
186+ disposables . add ( modes . CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
184187
185188 {
186189 const { actions } = await getCodeActions ( model , new Range ( 1 , 1 , 2 , 1 ) , { type : 'auto' } , CancellationToken . None ) ;
@@ -197,16 +200,16 @@ suite('CodeAction', () => {
197200
198201 test ( 'getCodeActions should not invoke code action providers filtered out by providedCodeActionKinds' , async function ( ) {
199202 let wasInvoked = false ;
200- const provider = new class implements CodeActionProvider {
201- provideCodeActions ( ) {
203+ const provider = new class implements modes . CodeActionProvider {
204+ provideCodeActions ( ) : modes . CodeActionList {
202205 wasInvoked = true ;
203- return [ ] ;
206+ return { actions : [ ] , dispose : ( ) => { } } ;
204207 }
205208
206209 providedCodeActionKinds = [ CodeActionKind . Refactor . value ] ;
207210 } ;
208211
209- disposables . add ( CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
212+ disposables . add ( modes . CodeActionProviderRegistry . register ( 'fooLang' , provider ) ) ;
210213
211214 const { actions } = await getCodeActions ( model , new Range ( 1 , 1 , 2 , 1 ) , {
212215 type : 'auto' ,
0 commit comments