Merge pull request #40 from MiYanni/generalize-regions

Added prefix and postfix so region generation is not specific to PowerShell region logic
This commit is contained in:
Garrett Serack 2019-04-23 19:44:10 -07:00 коммит произвёл GitHub
Родитель 41e64337c9 8b15fb2327
Коммит 3b205c3c2b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 26 добавлений и 20 удалений

Просмотреть файл

@ -95,21 +95,26 @@ export class Text extends Initializer implements IText {
}
export class TextWithRegions extends Text {
constructor(content?: TextPossibilities, objectIntializer?: Partial<TextWithRegions>) {
private prefix: string;
private postfix: string;
constructor(content?: TextPossibilities, objectIntializer?: Partial<TextWithRegions>, prefix: string = '#', postfix: string = '') {
super(content);
this.apply(objectIntializer);
this.prefix = prefix;
this.postfix = postfix;
}
removeRegion(region: string) {
this.add({ edit: (s: string) => setRegion(s, region, '') });
this.add({ edit: (s: string) => setRegion(s, region, '', undefined, this.prefix, this.postfix) });
}
setRegion(region: string, content: TextPossibilities, prepend = true) {
this.add({ edit: (s: string) => setRegion(s, region, content, prepend) });
this.add({ edit: (s: string) => setRegion(s, region, content, prepend, this.prefix, this.postfix) });
}
has(name: string) {
for (const each of getRegions(this.text)) {
for (const each of getRegions(this.text, this.prefix, this.postfix)) {
if (each.name === name) {
return true;
}
@ -118,16 +123,16 @@ export class TextWithRegions extends Text {
}
append(name: string, content: TextPossibilities) {
this.add({ edit: (s: string) => setRegion(s, name, content, false) });
this.add({ edit: (s: string) => setRegion(s, name, content, false, this.prefix, this.postfix) });
}
prepend(name: string, content: TextPossibilities) {
this.add({ edit: (s: string) => setRegion(s, name, content, true) });
this.add({ edit: (s: string) => setRegion(s, name, content, true, this.prefix, this.postfix) });
}
get regions() {
if (!this.text.trim()) {
return [];
}
return [...getRegions(this.text)];
return [...getRegions(this.text, this.prefix, this.postfix)];
}
}

Просмотреть файл

@ -299,10 +299,10 @@ export function nameof(text: string): string {
}
export function* getRegions(source: string) {
export function* getRegions(source: string, prefix: string = '#', postfix: string = '') {
source = source.replace(/[\r?\n]/g, '«');
const rx = new RegExp(`(.*?)«?(\\s*#\\s*region\\s*(.*?))\\s*«(.*?)«(\\s*#\\s*endregion)\\s*?«`, 'g');
const rx = new RegExp(`(.*?)«?(\\s*${prefix}\\s*region\\s*(.*?)\\s*${postfix})\\s*«(.*?)«(\\s*${prefix}\\s*endregion\\s*${postfix})\\s*?«`, 'g');
let match;
let finalPosition = 0;
while (match = rx.exec(source)) {
@ -337,11 +337,11 @@ export function* getRegions(source: string) {
}
}
export function setRegion(source: string, region: string, content: TextPossibilities, prepend = true) {
export function setRegion(source: string, region: string, content: TextPossibilities, prepend = true, prefix: string = '#', postfix: string = '') {
const result = new Array<string>();
const ct = new Text(content).text.replace(/[\r?\n]/g, '«').replace(/^«*/, '').replace(/«*$/, '');
const ct = new Text(content).text.replace(/\r?\n|\r/g, '«').replace(/^«*/, '').replace(/«*$/, '');
let found = false;
for (const each of getRegions(source)) {
for (const each of getRegions(source, prefix, postfix)) {
if (each.name === region) {
// found the region, replace it.
// (this also makes sure that we only have one region by that name when replacing/deleting)
@ -357,20 +357,21 @@ export function setRegion(source: string, region: string, content: TextPossibili
}
if (!found) {
if (prepend) {
result.splice(0, 0, `# region ${region}`, ct, '# endregion«');
result.splice(0, 0, `${prefix} region ${region} ${postfix}`, ct, `${prefix} endregion ${postfix}«`);
} else {
result.push(`# region ${region}`, ct, '# endregion«');
result.push(`${prefix} region ${region} ${postfix}`, ct, `${prefix} endregion ${postfix}«`);
}
}
return result.join('«').replace(/[\r?\n]/g, '«').replace(/^«*/, '').replace(/«*$/, '').replace(/«««*/g, '««').replace(/«/g, '\n');
return result.join('«').replace(/\r?\n|\r/g, '«').replace(/^«*/, '').replace(/«*$/, '').replace(/«««*/g, '««').replace(/«/g, '\n');
}
export function _setRegion(source: string, region: string, content: TextPossibilities, prepend = true) {
const ct = new Text(content).text.replace(/[\r?\n]/g, '«').replace(/^«*/, '').replace(/«*$/, '');
// Note: Where is this used?
export function _setRegion(source: string, region: string, content: TextPossibilities, prepend = true, prefix: string = '#', postfix: string = '') {
const ct = new Text(content).text.replace(/\r?\n|\r/g, '«').replace(/^«*/, '').replace(/«*$/, '');
source = source.replace(/[\r?\n]/g, '«');
source = source.replace(/\r?\n|\r/g, '«');
const rx = new RegExp(`«(\\s*#\\s*region\\s*${region})\\s*«.*?(«\\s*#\\s*endregion\\s*«?)`, 'g');
const rx = new RegExp(`«(\\s*${prefix}\\s*region\\s*${region}\\s*${postfix})\\s*«.*?(«\\s*${prefix}\\s*endregion\\s*${postfix}«?)`, 'g');
if (rx.test(source)) {
if (ct.length > 0) {
source = source.replace(rx, `«$${ct}$2`);
@ -379,7 +380,7 @@ export function _setRegion(source: string, region: string, content: TextPossibil
}
} else {
if (ct.length > 0) {
const text = `«# region ${region}«${ct}«# endregion«`;
const text = `«${prefix} region ${region} ${postfix}«${ct}«${prefix} endregion ${postfix}«`;
source = prepend ? text + source : source + text;
}
}