@@ -14,84 +14,84 @@ import * as serverUtils from '../omnisharp/utils';
1414
1515export default class OmnisharpCodeActionProvider extends AbstractProvider implements CodeActionProvider {
1616
17- private _disabled : boolean ;
18- private _commandId : string ;
19-
20- constructor ( server : OmnisharpServer ) {
21- super ( server ) ;
22- this . _commandId = 'omnisharp.runCodeAction' ;
23-
24- this . _updateEnablement ( ) ;
25- let d1 = workspace . onDidChangeConfiguration ( this . _updateEnablement , this ) ;
26- let d2 = commands . registerCommand ( this . _commandId , this . _runCodeAction , this ) ;
27- this . _disposables . push ( d1 , d2 ) ;
28- }
29-
30- private _updateEnablement ( ) : void {
31- let value = workspace . getConfiguration ( ) . get ( 'csharp.disableCodeActions' , false ) ;
32- this . _disabled = value ;
33- }
34-
35- public provideCodeActions ( document : TextDocument , range : Range , context : CodeActionContext , token : CancellationToken ) : Promise < Command [ ] > {
36-
37- if ( this . _disabled ) {
38- return ;
39- }
40-
41- let req : protocol . V2 . GetCodeActionsRequest = {
42- Filename : document . fileName ,
43- Selection : OmnisharpCodeActionProvider . _asRange ( range )
44- } ;
45-
46- return serverUtils . getCodeActions ( this . _server , req , token ) . then ( response => {
47- return response . CodeActions . map ( codeAction => {
48- return {
49- title : codeAction . Name ,
50- command : this . _commandId ,
51- arguments : [ < protocol . V2 . RunCodeActionRequest > {
52- Filename : document . fileName ,
53- Selection : OmnisharpCodeActionProvider . _asRange ( range ) ,
54- Identifier : codeAction . Identifier ,
55- WantsTextChanges : true
56- } ]
57- } ;
58- } ) ;
59- } , ( error ) => {
60- return Promise . reject ( `Problem invoking 'GetCodeActions' on OmniSharp server: ${ error } ` ) ;
61- } ) ;
62- }
63-
64- private _runCodeAction ( req : protocol . V2 . RunCodeActionRequest ) : Promise < any > {
65-
66- return serverUtils . runCodeAction ( this . _server , req ) . then ( response => {
67-
68- if ( response && Array . isArray ( response . Changes ) ) {
69-
70- let edit = new WorkspaceEdit ( ) ;
71-
72- for ( let change of response . Changes ) {
73- let uri = Uri . file ( change . FileName ) ;
74- let edits : TextEdit [ ] = [ ] ;
75- for ( let textChange of change . Changes ) {
76- edits . push ( TextEdit . replace ( toRange2 ( textChange ) , textChange . NewText ) ) ;
77- }
78-
79- edit . set ( uri , edits ) ;
80- }
81-
82- return workspace . applyEdit ( edit ) ;
83- }
84-
85- } , ( error ) => {
86- return Promise . reject ( 'Problem invoking \'RunCodeAction\' on OmniSharp server: ' + error ) ;
87- } ) ;
88- }
89-
90- private static _asRange ( range : Range ) : protocol . V2 . Range {
91- let { start, end} = range ;
92- return {
93- Start : { Line : start . line + 1 , Column : start . character + 1 } ,
94- End : { Line : end . line + 1 , Column : end . character + 1 }
95- } ;
96- }
17+ private _disabled : boolean ;
18+ private _commandId : string ;
19+
20+ constructor ( server : OmnisharpServer ) {
21+ super ( server ) ;
22+ this . _commandId = 'omnisharp.runCodeAction' ;
23+
24+ this . _updateEnablement ( ) ;
25+ let d1 = workspace . onDidChangeConfiguration ( this . _updateEnablement , this ) ;
26+ let d2 = commands . registerCommand ( this . _commandId , this . _runCodeAction , this ) ;
27+ this . _disposables . push ( d1 , d2 ) ;
28+ }
29+
30+ private _updateEnablement ( ) : void {
31+ let value = workspace . getConfiguration ( ) . get ( 'csharp.disableCodeActions' , false ) ;
32+ this . _disabled = value ;
33+ }
34+
35+ public provideCodeActions ( document : TextDocument , range : Range , context : CodeActionContext , token : CancellationToken ) : Promise < Command [ ] > {
36+
37+ if ( this . _disabled ) {
38+ return ;
39+ }
40+
41+ let req : protocol . V2 . GetCodeActionsRequest = {
42+ Filename : document . fileName ,
43+ Selection : OmnisharpCodeActionProvider . _asRange ( range )
44+ } ;
45+
46+ return serverUtils . getCodeActions ( this . _server , req , token ) . then ( response => {
47+ return response . CodeActions . map ( codeAction => {
48+ return {
49+ title : codeAction . Name ,
50+ command : this . _commandId ,
51+ arguments : [ < protocol . V2 . RunCodeActionRequest > {
52+ Filename : document . fileName ,
53+ Selection : OmnisharpCodeActionProvider . _asRange ( range ) ,
54+ Identifier : codeAction . Identifier ,
55+ WantsTextChanges : true
56+ } ]
57+ } ;
58+ } ) ;
59+ } , ( error ) => {
60+ return Promise . reject ( `Problem invoking 'GetCodeActions' on OmniSharp server: ${ error } ` ) ;
61+ } ) ;
62+ }
63+
64+ private _runCodeAction ( req : protocol . V2 . RunCodeActionRequest ) : Promise < any > {
65+
66+ return serverUtils . runCodeAction ( this . _server , req ) . then ( response => {
67+
68+ if ( response && Array . isArray ( response . Changes ) ) {
69+
70+ let edit = new WorkspaceEdit ( ) ;
71+
72+ for ( let change of response . Changes ) {
73+ let uri = Uri . file ( change . FileName ) ;
74+ let edits : TextEdit [ ] = [ ] ;
75+ for ( let textChange of change . Changes ) {
76+ edits . push ( TextEdit . replace ( toRange2 ( textChange ) , textChange . NewText ) ) ;
77+ }
78+
79+ edit . set ( uri , edits ) ;
80+ }
81+
82+ return workspace . applyEdit ( edit ) ;
83+ }
84+
85+ } , ( error ) => {
86+ return Promise . reject ( 'Problem invoking \'RunCodeAction\' on OmniSharp server: ' + error ) ;
87+ } ) ;
88+ }
89+
90+ private static _asRange ( range : Range ) : protocol . V2 . Range {
91+ let { start, end} = range ;
92+ return {
93+ Start : { Line : start . line + 1 , Column : start . character + 1 } ,
94+ End : { Line : end . line + 1 , Column : end . character + 1 }
95+ } ;
96+ }
9797}
0 commit comments