@@ -6,19 +6,20 @@ import vscode = require('vscode');
6
6
import { IFeature } from '../feature' ;
7
7
import QuickPickItem = vscode . QuickPickItem ;
8
8
import { LanguageClient , RequestType , NotificationType } from 'vscode-languageclient' ;
9
+ import { Option , CheckboxQuickPick } from '../checkboxQuickPick' ;
9
10
const figures = require ( 'figures' ) ;
10
11
11
12
export namespace GetPSSARulesRequest {
12
13
export const type : RequestType < any , any , void > = { get method ( ) { return 'powerShell/getPSSARules' ; } } ;
13
14
}
14
15
15
16
export namespace SetPSSARulesRequest {
16
- export const type : RequestType < any , any , void > = { get method ( ) { return 'powerShell/setPSSARules' ; } }
17
+ export const type : RequestType < any , any , void > = { get method ( ) { return 'powerShell/setPSSARules' ; } }
17
18
}
18
19
19
20
interface RuleInfo {
20
- Name : string ;
21
- IsEnabled : boolean ;
21
+ name : string ;
22
+ isEnabled : boolean ;
22
23
}
23
24
24
25
export class SelectPSSARulesFeature implements IFeature {
@@ -33,95 +34,20 @@ export class SelectPSSARulesFeature implements IFeature {
33
34
return ;
34
35
}
35
36
36
- const editor = vscode . window . activeTextEditor ;
37
-
38
- var selection = editor . selection ;
39
- var doc = editor . document ;
40
- var cwr = doc . getWordRangeAtPosition ( selection . active )
41
- var text = doc . getText ( cwr ) ;
42
-
43
- let rules : RuleInfo [ ] = [ ] ;
37
+ let options : Option [ ] = [ ] ;
44
38
this . languageClient . sendRequest ( GetPSSARulesRequest . type , null ) . then ( ( returnedRules ) => {
45
- for ( var index = 0 ; index < returnedRules . length ; index ++ ) {
46
- var element = returnedRules [ index ] ;
47
- rules . push ( { Name : element . name , IsEnabled : element . isEnabled } )
48
- }
49
- this . GetSelections ( rules ) ;
39
+ let options : Option [ ] = returnedRules . map ( function ( rule ) : Option {
40
+ return { name : rule . name , isSelected : rule . isEnabled } ;
50
41
} ) ;
51
- } ) ;
52
- }
53
-
54
- private GetSelections ( rules : RuleInfo [ ] )
55
- {
56
- vscode . window . showQuickPick ( this . GetQuickPickItems ( rules ) )
57
- . then ( ( selection ) => {
58
- if ( ! selection )
59
- {
60
- return ;
61
- }
62
-
63
- if ( selection . label == figures . tick )
64
- {
65
- this . languageClient . sendRequest ( SetPSSARulesRequest . type , rules ) ; //TODO handle error
66
- return ;
67
- }
68
-
69
- let index = this . GetRuleIndex ( rules , selection . description ) ;
70
- rules [ index ] . IsEnabled = this . ToggleState ( rules [ index ] . IsEnabled ) ;
71
- this . GetSelections ( rules ) ;
42
+ ( new CheckboxQuickPick ( options ) ) . show ( ( updatedOptions ) => {
43
+ this . languageClient . sendRequest (
44
+ SetPSSARulesRequest . type ,
45
+ updatedOptions . map ( function ( option ) : RuleInfo {
46
+ return { name : option . name , isEnabled : option . isSelected } ;
47
+ } ) ) ;
72
48
} ) ;
73
- }
74
-
75
- private GetRuleIndex ( rules : RuleInfo [ ] , ruleName : string ) : number
76
- {
77
- return rules . findIndex ( rule => rule . Name == ruleName ) ;
78
- }
79
-
80
- private GetCheckBoxOn ( ) : string
81
- {
82
- return "[ x ]" ; // this looks better than figure.checkboxOn
83
- }
84
-
85
- private GetCheckBoxOff ( ) : string
86
- {
87
- return "[ ]" ; // this looks better than figure.checkboxOff
88
- }
89
-
90
- private ConvertToState ( checkBox : string ) : boolean
91
- {
92
- return checkBox == this . GetCheckBoxOn ( ) ;
93
- }
94
-
95
- private ToggleState ( state : boolean ) : boolean
96
- {
97
- return ! state ;
98
- }
99
-
100
- private ToggleCheckBox ( checkBox : string ) : string
101
- {
102
- return this . ConvertToCheckBox ( this . ToggleState ( this . ConvertToState ( checkBox ) ) ) ;
103
- }
104
-
105
- private ConvertToCheckBox ( state : boolean ) : string
106
- {
107
- if ( state )
108
- {
109
- return this . GetCheckBoxOn ( ) ;
110
- }
111
- else
112
- {
113
- return this . GetCheckBoxOff ( ) ;
114
- }
115
- }
116
-
117
- private GetQuickPickItems ( rules : RuleInfo [ ] ) : QuickPickItem [ ] {
118
- let qItems : QuickPickItem [ ] = [ ] ;
119
- for ( var index = 0 ; index < rules . length ; index ++ ) {
120
- var element = rules [ index ] ;
121
- qItems . push ( { label : this . ConvertToCheckBox ( element . IsEnabled ) , description : element . Name } )
122
- }
123
- qItems . push ( { label : figures . tick , description : "confirm" } ) ;
124
- return qItems ;
49
+ } ) ;
50
+ } ) ;
125
51
}
126
52
127
53
public setLanguageClient ( languageclient : LanguageClient ) {
0 commit comments