@@ -31,6 +31,7 @@ interface MatchingJSONSchema extends JSONSchema {
3131
3232interface SchemaItem extends QuickPickItem {
3333 schema ?: MatchingJSONSchema ;
34+ disableSchemaDetection ?: boolean ;
3435}
3536
3637interface SchemaVersionItem extends QuickPickItem {
@@ -50,6 +51,7 @@ let client: CommonLanguageClient;
5051let versionSelection : SchemaItem = undefined ;
5152
5253const selectVersionLabel = 'Select Different Version' ;
54+ const noSchemaLabel = 'No JSON Schema' ;
5355
5456export function createJSONSchemaStatusBarItem ( context : ExtensionContext , languageclient : CommonLanguageClient ) : void {
5557 if ( statusBarItem ) {
@@ -73,10 +75,11 @@ export function createJSONSchemaStatusBarItem(context: ExtensionContext, languag
7375async function updateStatusBar ( editor : TextEditor ) : Promise < void > {
7476 if ( editor && editor . document . languageId === 'yaml' ) {
7577 versionSelection = undefined ;
78+ const fileUri = editor . document . uri . toString ( ) ;
7679 // get schema info there
77- const schema = await client . sendRequest ( getSchemaRequestType , editor . document . uri . toString ( ) ) ;
80+ const schema = await client . sendRequest ( getSchemaRequestType , fileUri ) ;
7881 if ( ! schema || schema . length === 0 ) {
79- statusBarItem . text = 'No JSON Schema' ;
82+ statusBarItem . text = noSchemaLabel ;
8083 statusBarItem . tooltip = 'Select JSON Schema' ;
8184 statusBarItem . backgroundColor = undefined ;
8285 } else if ( schema . length === 1 ) {
@@ -85,7 +88,7 @@ async function updateStatusBar(editor: TextEditor): Promise<void> {
8588 if ( schema [ 0 ] . versions ) {
8689 version = findUsedVersion ( schema [ 0 ] . versions , schema [ 0 ] . uri ) ;
8790 } else {
88- const schemas = await client . sendRequest ( getJSONSchemasRequestType , window . activeTextEditor . document . uri . toString ( ) ) ;
91+ const schemas = await client . sendRequest ( getJSONSchemasRequestType , fileUri ) ;
8992 let versionSchema : JSONSchema ;
9093 const schemaStoreItem = findSchemaStoreItem ( schemas , schema [ 0 ] . uri ) ;
9194 if ( schemaStoreItem ) {
@@ -113,7 +116,8 @@ async function updateStatusBar(editor: TextEditor): Promise<void> {
113116}
114117
115118async function showSchemaSelection ( ) : Promise < void > {
116- const schemas = await client . sendRequest ( getJSONSchemasRequestType , window . activeTextEditor . document . uri . toString ( ) ) ;
119+ const fileUri = window . activeTextEditor . document . uri . toString ( ) ;
120+ const schemas = await client . sendRequest ( getJSONSchemasRequestType , fileUri ) ;
117121 const schemasPick = window . createQuickPick < SchemaItem > ( ) ;
118122 let pickItems : SchemaItem [ ] = [ ] ;
119123
@@ -151,6 +155,12 @@ async function showSchemaSelection(): Promise<void> {
151155 return a . label . localeCompare ( b . label ) ;
152156 } ) ;
153157
158+ pickItems . unshift ( {
159+ label : noSchemaLabel ,
160+ alwaysShow : true ,
161+ disableSchemaDetection : true ,
162+ } ) ;
163+
154164 schemasPick . items = pickItems ;
155165 schemasPick . placeholder = 'Search JSON schema' ;
156166 schemasPick . title = 'Select JSON schema' ;
@@ -160,9 +170,11 @@ async function showSchemaSelection(): Promise<void> {
160170 try {
161171 if ( selection . length > 0 ) {
162172 if ( selection [ 0 ] . label === selectVersionLabel ) {
163- handleSchemaVersionSelection ( selection [ 0 ] . schema ) ;
173+ handleSchemaVersionSelection ( selection [ 0 ] . schema , fileUri ) ;
174+ } else if ( selection [ 0 ] . disableSchemaDetection ) {
175+ writeDisableSchemaDetectionMapping ( fileUri ) ;
164176 } else if ( selection [ 0 ] . schema ) {
165- writeSchemaUriMapping ( selection [ 0 ] . schema . uri ) ;
177+ writeSchemaUriMapping ( selection [ 0 ] . schema . uri , fileUri ) ;
166178 }
167179 }
168180 } catch ( err ) {
@@ -192,6 +204,34 @@ function deleteExistingFilePattern(settings: Record<string, unknown>, fileUri: s
192204 return settings ;
193205}
194206
207+ function removeFilePatternFromSetting ( setting : unknown , fileUri : string ) : string | string [ ] {
208+ if ( Array . isArray ( setting ) ) {
209+ return setting . filter ( ( value ) : value is string => typeof value === 'string' && value !== fileUri ) ;
210+ }
211+
212+ if ( setting === fileUri ) {
213+ return [ ] ;
214+ }
215+
216+ return typeof setting === 'string' ? setting : [ ] ;
217+ }
218+
219+ function addFilePatternToSetting ( setting : unknown , fileUri : string ) : string | string [ ] {
220+ if ( Array . isArray ( setting ) ) {
221+ const filePatterns = setting . filter ( ( value ) : value is string => typeof value === 'string' ) ;
222+ if ( ! filePatterns . includes ( fileUri ) ) {
223+ filePatterns . push ( fileUri ) ;
224+ }
225+ return filePatterns ;
226+ }
227+
228+ if ( typeof setting === 'string' ) {
229+ return setting === fileUri ? setting : [ setting , fileUri ] ;
230+ }
231+
232+ return [ fileUri ] ;
233+ }
234+
195235function createSelectVersionItem ( version : string , schema : MatchingJSONSchema ) : SchemaItem {
196236 return {
197237 label : selectVersionLabel ,
@@ -212,9 +252,11 @@ function findSchemaStoreItem(schemas: JSONSchema[], url: string): [string, JSONS
212252 }
213253}
214254
215- function writeSchemaUriMapping ( schemaUrl : string ) : void {
216- const settings : Record < string , unknown > = workspace . getConfiguration ( 'yaml' ) . get ( 'schemas' ) ;
217- const fileUri = window . activeTextEditor . document . uri . toString ( ) ;
255+ function writeSchemaUriMapping ( schemaUrl : string , fileUri : string ) : void {
256+ const yamlConfiguration = workspace . getConfiguration ( 'yaml' ) ;
257+ const settings : Record < string , unknown > = yamlConfiguration . get ( 'schemas' ) ;
258+ const disableSchemaDetection = yamlConfiguration . get ( 'disableSchemaDetection' ) ;
259+ yamlConfiguration . update ( 'disableSchemaDetection' , removeFilePatternFromSetting ( disableSchemaDetection , fileUri ) ) ;
218260 const newSettings = Object . assign ( { } , settings ) ;
219261 deleteExistingFilePattern ( newSettings , fileUri ) ;
220262 const schemaSettings = newSettings [ schemaUrl ] ;
@@ -227,10 +269,16 @@ function writeSchemaUriMapping(schemaUrl: string): void {
227269 } else {
228270 newSettings [ schemaUrl ] = fileUri ;
229271 }
230- workspace . getConfiguration ( 'yaml' ) . update ( 'schemas' , newSettings ) ;
272+ yamlConfiguration . update ( 'schemas' , newSettings ) ;
273+ }
274+
275+ function writeDisableSchemaDetectionMapping ( fileUri : string ) : void {
276+ const yamlConfiguration = workspace . getConfiguration ( 'yaml' ) ;
277+ const disableSchemaDetection = yamlConfiguration . get ( 'disableSchemaDetection' ) ;
278+ yamlConfiguration . update ( 'disableSchemaDetection' , addFilePatternToSetting ( disableSchemaDetection , fileUri ) ) ;
231279}
232280
233- function handleSchemaVersionSelection ( schema : MatchingJSONSchema ) : void {
281+ function handleSchemaVersionSelection ( schema : MatchingJSONSchema , fileUri : string ) : void {
234282 const versionPick = window . createQuickPick < SchemaVersionItem > ( ) ;
235283 const versionItems : SchemaVersionItem [ ] = [ ] ;
236284 const usedVersion = findUsedVersion ( schema . versions , schema . uri ) ;
@@ -249,7 +297,7 @@ function handleSchemaVersionSelection(schema: MatchingJSONSchema): void {
249297
250298 versionPick . onDidChangeSelection ( ( items ) => {
251299 if ( items && items . length === 1 ) {
252- writeSchemaUriMapping ( items [ 0 ] . url ) ;
300+ writeSchemaUriMapping ( items [ 0 ] . url , fileUri ) ;
253301 }
254302 versionPick . hide ( ) ;
255303 } ) ;
0 commit comments