Skip to content

Commit

Permalink
support associate schemas with files in a regular expression
Browse files Browse the repository at this point in the history
  • Loading branch information
928PJY committed Aug 7, 2018
1 parent 2684120 commit d4a05e3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/languageservice/services/jsonSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class FilePatternAssociation {
constructor(pattern: string) {
this.combinedSchemaId = 'schemaservice://combinedSchema/' + encodeURIComponent(pattern);
try {
this.patternRegExp = new RegExp(Strings.convertSimple2RegExpPattern(pattern) + '$');
this.patternRegExp = Strings.convertSimple2RegExp(pattern);
} catch (e) {
// invalid pattern
this.patternRegExp = null;
Expand Down
14 changes: 12 additions & 2 deletions src/languageservice/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ export function endsWith(haystack: string, needle: string): boolean {
}
}

export function convertSimple2RegExpPattern(pattern: string): string {
return pattern.replace(/[\-\\\{\}\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&').replace(/[\*]/g, '.*');
export function convertSimple2RegExp(pattern: string): RegExp {
var match = pattern.match(new RegExp('^/(.*?)/([gimy]*)$'));
return match ? convertRegexString2RegExp(match[1], match[2])
: convertGlobalPattern2RegExp(pattern)
}

function convertGlobalPattern2RegExp(pattern: string): RegExp {
return new RegExp(pattern.replace(/[\-\\\{\}\+\?\|\^\$\.\,\[\]\(\)\#\s]/g, '\\$&').replace(/[\*]/g, '.*') + '$');
}

function convertRegexString2RegExp(pattern: string, flag: string): RegExp {
return new RegExp(pattern, flag);
}
20 changes: 15 additions & 5 deletions test/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Red Hat. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {startsWith, endsWith, convertSimple2RegExpPattern} from '../src/languageservice/utils/strings';
import {startsWith, endsWith, convertSimple2RegExp} from '../src/languageservice/utils/strings';
var assert = require('assert');

suite("String Tests", () => {
Expand Down Expand Up @@ -75,12 +75,22 @@ suite("String Tests", () => {

});

describe('convertSimple2RegExpPattern', function(){
describe('convertSimple2RegExp', function(){

it('Test of convertSimple2RegExpPattern', () => {
it('Test of convertRegexString2RegExp', () => {

var result = convertSimple2RegExpPattern("/*");
assert.equal(result, "/.*");
var result = convertSimple2RegExp("/toc\\.yml/i").test("TOC.yml");
assert.equal(result, true);

});

it('Test of convertGlobalPattern2RegExp', () => {

var result = convertSimple2RegExp("toc.yml").test("toc.yml");
assert.equal(result, true);

result = convertSimple2RegExp("toc.yml").test("TOC.yml");
assert.equal(result, false);

});

Expand Down

0 comments on commit d4a05e3

Please sign in to comment.