@@ -6,136 +6,89 @@ import {
66} from "vscode" ;
77
88export const CONFIG_SECTION_NAME = "markiscodecoverage" ;
9- export const CONFIG_OPTION_ENABLE_ON_STARTUP = "enableOnStartup" ;
10- export const CONFIG_OPTION_SEARCH_CRITERIA = "searchCriteria" ;
11- export const CONFIG_OPTION_COVERAGE_THRESHOLD = "coverageThreshold" ;
12- export const CONFIG_OPTION_SHOW_DECORATIONS = "enableDecorations" ;
13-
14- export const DEFAULT_SEARCH_CRITERIA = "coverage/lcov*.info" ;
15- export const DEFAULT_CODE_COVERAGE_THRESHOLD = 80 ;
9+ const DEFAULT_SHOW_COVERAGE = true ;
10+ const DEFAULT_SHOW_DECORATIONS = false ;
11+ const DEFAULT_SEARCH_CRITERIA = "coverage/lcov*.info" ;
12+ const DEFAULT_CODE_COVERAGE_THRESHOLD = 80 ;
13+
14+ export enum ConfigurationOptions {
15+ enableOnStartup = "enableOnStartup" ,
16+ searchCriteria = "searchCriteria" ,
17+ coverageThreshold = "coverageThreshold" ,
18+ showDecorations = "enableDecorations" ,
19+ }
1620
1721export class ExtensionConfiguration extends Disposable {
18- private readonly _onConfigOptionUpdated = new EventEmitter < string > ( ) ;
19- private _isDisposed = false ;
20- private _showCoverage = true ;
21- private _searchCriteria = "" ;
22- private _coverageThreshold = 0 ;
23- private _showDecorations = false ;
24-
25- constructor ( config : WorkspaceConfiguration ) {
26- // use dummy function for callOnDispose since dispose() will be overrided
27- super ( ( ) => true ) ;
28-
29- this . showCoverage =
30- ! config . has ( CONFIG_OPTION_ENABLE_ON_STARTUP ) ||
31- ( config . get ( CONFIG_OPTION_ENABLE_ON_STARTUP ) ?? true ) ;
32-
33- const configSearchCriteria =
34- config . has ( CONFIG_OPTION_SEARCH_CRITERIA ) &&
35- config . get ( CONFIG_OPTION_SEARCH_CRITERIA ) ;
36- this . searchCriteria =
37- configSearchCriteria && typeof configSearchCriteria === "string"
38- ? configSearchCriteria
39- : DEFAULT_SEARCH_CRITERIA ;
40-
41- this . coverageThreshold =
42- config . get ( CONFIG_OPTION_COVERAGE_THRESHOLD ) ??
43- DEFAULT_CODE_COVERAGE_THRESHOLD ;
44-
45- this . showDecorations = config . get ( CONFIG_OPTION_SHOW_DECORATIONS , false ) ;
46- }
47-
48- public override dispose ( ) : void {
49- if ( ! this . _isDisposed ) {
50- this . _onConfigOptionUpdated . dispose ( ) ;
51-
52- this . _isDisposed = true ;
53- }
22+ constructor (
23+ config : WorkspaceConfiguration ,
24+ public showCoverage = config . get (
25+ ConfigurationOptions . enableOnStartup ,
26+ DEFAULT_SHOW_COVERAGE ,
27+ ) ,
28+ public showDecorations = config . get (
29+ ConfigurationOptions . showDecorations ,
30+ DEFAULT_SHOW_DECORATIONS ,
31+ ) ,
32+ public searchCriteria = config . get (
33+ ConfigurationOptions . searchCriteria ,
34+ DEFAULT_SEARCH_CRITERIA ,
35+ ) ,
36+ public coverageThreshold = config . get (
37+ ConfigurationOptions . coverageThreshold ,
38+ DEFAULT_CODE_COVERAGE_THRESHOLD ,
39+ ) ,
40+ private configSectionName = CONFIG_SECTION_NAME ,
41+ private readonly _onConfigOptionUpdated :
42+ | EventEmitter < string >
43+ | undefined = new EventEmitter < string > ( ) ,
44+ ) {
45+ super ( ( ) => {
46+ _onConfigOptionUpdated . dispose ( ) ;
47+ } ) ;
5448 }
5549
5650 public onConfigOptionUpdated ( listener : ( e : string ) => any ) : Disposable {
57- this . _checkDisposed ( ) ;
51+ if ( ! this . _onConfigOptionUpdated ) return Disposable . from ( ) ;
5852 return this . _onConfigOptionUpdated . event ( listener ) ;
5953 }
6054
61- get showCoverage ( ) {
62- this . _checkDisposed ( ) ;
63- return this . _showCoverage ;
64- }
65- set showCoverage ( value : boolean ) {
66- this . _checkDisposed ( ) ;
67- this . _showCoverage = value ;
68- }
69-
70- get showDecorations ( ) {
71- this . _checkDisposed ( ) ;
72- return this . _showDecorations ;
73- }
74- set showDecorations ( value : boolean ) {
75- this . _checkDisposed ( ) ;
76- this . _showDecorations = value ;
77- }
78-
79- get searchCriteria ( ) {
80- this . _checkDisposed ( ) ;
81- return this . _searchCriteria ;
82- }
83- set searchCriteria ( value : string ) {
84- this . _checkDisposed ( ) ;
85- this . _searchCriteria = value ;
86- }
87-
88- get coverageThreshold ( ) {
89- this . _checkDisposed ( ) ;
90- return this . _coverageThreshold ;
91- }
92- set coverageThreshold ( value : number ) {
93- this . _checkDisposed ( ) ;
94- this . _coverageThreshold = value ;
95- }
96-
9755 dispatchConfigUpdate (
9856 evtSrc : ConfigurationChangeEvent ,
9957 latestSnapshot : WorkspaceConfiguration ,
10058 ) : void {
101- this . _checkDisposed ( ) ;
102-
103- if ( this . _hasBeenUpdated ( evtSrc , CONFIG_OPTION_SEARCH_CRITERIA ) ) {
104- const configSearchCriteria =
105- latestSnapshot . has ( CONFIG_OPTION_SEARCH_CRITERIA ) &&
106- latestSnapshot . get ( CONFIG_OPTION_SEARCH_CRITERIA ) ;
107- this . searchCriteria =
108- configSearchCriteria && typeof configSearchCriteria === "string"
109- ? configSearchCriteria
110- : this . searchCriteria ;
59+ if ( ! this . _onConfigOptionUpdated ) return ;
11160
112- this . _onConfigOptionUpdated . fire ( CONFIG_OPTION_SEARCH_CRITERIA ) ;
113- } else if ( this . _hasBeenUpdated ( evtSrc , CONFIG_OPTION_COVERAGE_THRESHOLD ) ) {
114- this . coverageThreshold =
115- latestSnapshot . get ( CONFIG_OPTION_COVERAGE_THRESHOLD ) ??
116- this . coverageThreshold ;
117-
118- this . _onConfigOptionUpdated . fire ( CONFIG_OPTION_COVERAGE_THRESHOLD ) ;
119- } else if ( this . _hasBeenUpdated ( evtSrc , CONFIG_OPTION_SHOW_DECORATIONS ) ) {
61+ if ( this . _hasBeenUpdated ( evtSrc , ConfigurationOptions . searchCriteria ) ) {
62+ this . searchCriteria = latestSnapshot . get (
63+ ConfigurationOptions . searchCriteria ,
64+ DEFAULT_SEARCH_CRITERIA ,
65+ ) ;
66+ this . _onConfigOptionUpdated . fire ( ConfigurationOptions . searchCriteria ) ;
67+ } else if (
68+ this . _hasBeenUpdated ( evtSrc , ConfigurationOptions . coverageThreshold )
69+ ) {
70+ this . coverageThreshold = latestSnapshot . get (
71+ ConfigurationOptions . coverageThreshold ,
72+ DEFAULT_CODE_COVERAGE_THRESHOLD ,
73+ ) ;
74+ this . _onConfigOptionUpdated . fire ( ConfigurationOptions . coverageThreshold ) ;
75+ } else if (
76+ this . _hasBeenUpdated ( evtSrc , ConfigurationOptions . showDecorations )
77+ ) {
12078 this . showDecorations = latestSnapshot . get (
121- CONFIG_OPTION_SHOW_DECORATIONS ,
122- this . showDecorations ,
79+ ConfigurationOptions . showDecorations ,
80+ DEFAULT_SHOW_DECORATIONS ,
12381 ) ;
124-
125- this . _onConfigOptionUpdated . fire ( CONFIG_OPTION_SHOW_DECORATIONS ) ;
82+ this . _onConfigOptionUpdated . fire ( ConfigurationOptions . showDecorations ) ;
12683 }
12784 }
12885
12986 private _hasBeenUpdated (
13087 evtSrc : ConfigurationChangeEvent ,
13188 optionName : string ,
13289 ) : boolean {
133- return evtSrc . affectsConfiguration ( `${ CONFIG_SECTION_NAME } .${ optionName } ` ) ;
134- }
135-
136- private _checkDisposed ( ) {
137- if ( this . _isDisposed ) {
138- throw new Error ( "illegal state - object is disposed" ) ;
139- }
90+ return evtSrc . affectsConfiguration (
91+ `${ this . configSectionName } .${ optionName } ` ,
92+ ) ;
14093 }
14194}
0 commit comments