Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support associate schemas with files in a regular expression #79

Merged
merged 1 commit into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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