11'use strict' ;
2- import { commands , ConfigurationTarget , Disposable , Event , EventEmitter , TextDocumentContentProvider , Uri , ViewColumn , workspace } from 'vscode' ;
2+ import { commands , ConfigurationTarget , Disposable , Event , EventEmitter , TextDocument , TextDocumentContentProvider , Uri , ViewColumn , workspace } from 'vscode' ;
33import { Container } from './container' ;
44import { configuration } from './configuration' ;
55import { Logger } from './logger' ;
@@ -15,11 +15,13 @@ export class PageProvider extends Disposable implements TextDocumentContentProvi
1515 }
1616
1717 private readonly _disposable : Disposable ;
18+ private _scope : Map < string , 'user' | 'workspace' > = new Map ( ) ;
1819
1920 constructor ( ) {
2021 super ( ( ) => this . dispose ( ) ) ;
2122
2223 this . _disposable = Disposable . from (
24+ workspace . onDidCloseTextDocument ( this . onTextDocumentClosed , this ) ,
2325 workspace . registerTextDocumentContentProvider ( settingsUri . scheme , this ) ,
2426 commands . registerCommand ( 'gitlens.showSettingsPage' , this . showSettings , this ) ,
2527 commands . registerCommand ( 'gitlens.showWelcomePage' , this . showWelcome , this ) ,
@@ -31,33 +33,59 @@ export class PageProvider extends Disposable implements TextDocumentContentProvi
3133 this . _disposable . dispose ( ) ;
3234 }
3335
36+ private onTextDocumentClosed ( e : TextDocument ) {
37+ this . _scope . delete ( e . uri . toString ( ) ) ;
38+ }
39+
3440 async provideTextDocumentContent ( uri : Uri ) : Promise < string > {
3541 const doc = await workspace . openTextDocument ( Uri . file ( Container . context . asAbsolutePath ( `${ uri . path } .html` ) ) ) ;
3642
3743 let text = doc . getText ( ) . replace ( / { { root} } / g, Uri . file ( Container . context . asAbsolutePath ( '.' ) ) . toString ( ) ) ;
38- if ( text . includes ( '\'{{config}}\'' ) ) {
39- text = text . replace ( / ' { { c o n f i g } } ' / g, JSON . stringify ( Container . config ) ) ;
44+ if ( text . includes ( '\'{{data}}\'' ) ) {
45+ text = text . replace ( / ' { { d a t a } } ' / g, JSON . stringify ( {
46+ config : Container . config ,
47+ scope : this . getScope ( uri ) ,
48+ scopes : this . getAvailableScopes ( ) ,
49+ uri : uri . toString ( )
50+ } ) ) ;
4051 }
4152
4253 return text ;
4354 }
4455
45- refresh ( uri ?: Uri ) {
56+ private getAvailableScopes ( ) : [ 'user' | 'workspace' , string ] [ ] {
57+ const scopes : [ 'user' | 'workspace' , string ] [ ] = [ [ 'user' , 'User Settings' ] ] ;
58+ if ( workspace . workspaceFolders !== undefined && workspace . workspaceFolders . length ) {
59+ scopes . push ( [ 'workspace' , 'Workspace Settings' ] ) ;
60+ }
61+ return scopes ;
62+ }
63+
64+ private getScope ( uri : Uri ) : 'user' | 'workspace' {
65+ return this . _scope . get ( uri . toString ( ) ) || 'user' ;
66+ }
67+
68+ refresh ( uri ?: Uri ) {
4669 Logger . log ( 'PageProvider.refresh' ) ;
4770
4871 this . _onDidChange . fire ( uri || settingsUri ) ;
4972 }
5073
51- async save ( changes : { [ key : string ] : any } ) {
52- Logger . log ( `PageProvider.save: changes=${ JSON . stringify ( changes ) } ` ) ;
74+ async save ( options : { changes : { [ key : string ] : any } , scope : 'user' | 'workspace' , uri : string } ) {
75+ Logger . log ( `PageProvider.save: options=${ JSON . stringify ( options ) } ` ) ;
76+
77+ this . _scope . set ( options . uri , options . scope ) ;
78+ const target = options . scope === 'workspace'
79+ ? ConfigurationTarget . Workspace
80+ : ConfigurationTarget . Global ;
5381
54- for ( const key in changes ) {
82+ for ( const key in options . changes ) {
5583 const inspect = await configuration . inspect ( key ) ! ;
56- if ( inspect . defaultValue === changes [ key ] ) {
57- await configuration . update ( key , undefined , ConfigurationTarget . Global ) ;
84+ if ( inspect . defaultValue === options . changes [ key ] ) {
85+ await configuration . update ( key , undefined , target ) ;
5886 }
5987 else {
60- await configuration . update ( key , changes [ key ] , ConfigurationTarget . Global ) ;
88+ await configuration . update ( key , options . changes [ key ] , target ) ;
6189 }
6290 }
6391 }
0 commit comments