Skip to content
This repository was archived by the owner on Jan 8, 2025. It is now read-only.

Remove implicit g flag from snippet transformations #7

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
4 changes: 2 additions & 2 deletions lib/snippet-body.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ variableContentChar = !variable char:('\\}' / [^}]) { return char; }
escapedForwardSlash = pair:'\\/' { return pair; }

// A pattern and replacement for a transformed tab stop.
transformationSubstitution = '/' find:(escapedForwardSlash / [^/])* '/' replace:formatString* '/' flags:[imy]* {
let reFind = new RegExp(find.join(''), flags.join('') + 'g');
transformationSubstitution = '/' find:(escapedForwardSlash / [^/])* '/' replace:formatString* '/' flags:[gimy]* {
let reFind = new RegExp(find.join(''), flags.join(''));
return { find: reFind, replace: replace[0] };
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snippets",
"version": "1.6.1",
"version": "1.7.0",
"main": "./lib/snippets",
"description": "Expand snippets matching the current prefix with `tab`.",
"repository": "https://github.com/atom/snippets",
Expand Down
23 changes: 18 additions & 5 deletions spec/body-parser-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ the quick brown $1fox \${2:jumped \${3:over}

it("parses a snippet with transformations", () => {
const bodyTree = BodyParser.parse("<${1:p}>$0</${1/f/F/}>");
expect(bodyTree).toEqual([
'<',
{index: 1, content: ['p']},
'>',
{index: 0, content: []},
'</',
{index: 1, content: [], substitution: {find: /f/, replace: ['F']}},
'>'
]);
});

it("parses a snippet with transformations and a global flag", () => {
const bodyTree = BodyParser.parse("<${1:p}>$0</${1/f/F/g}>");
expect(bodyTree).toEqual([
'<',
{index: 1, content: ['p']},
Expand All @@ -78,7 +91,7 @@ the quick brown $1fox \${2:jumped \${3:over}
});

it("parses a snippet with multiple tab stops with transformations", () => {
const bodyTree = BodyParser.parse("${1:placeholder} ${1/(.)/\\u$1/} $1 ${2:ANOTHER} ${2/^(.*)$/\\L$1/} $2");
const bodyTree = BodyParser.parse("${1:placeholder} ${1/(.)/\\u$1/g} $1 ${2:ANOTHER} ${2/^(.*)$/\\L$1/} $2");
expect(bodyTree).toEqual([
{index: 1, content: ['placeholder']},
' ',
Expand All @@ -102,7 +115,7 @@ the quick brown $1fox \${2:jumped \${3:over}
index: 2,
content: [],
substitution: {
find: /^(.*)$/g,
find: /^(.*)$/,
replace: [
{escape: 'L'},
{backreference: 1}
Expand All @@ -116,7 +129,7 @@ the quick brown $1fox \${2:jumped \${3:over}


it("parses a snippet with transformations and mirrors", () => {
const bodyTree = BodyParser.parse("${1:placeholder}\n${1/(.)/\\u$1/}\n$1");
const bodyTree = BodyParser.parse("${1:placeholder}\n${1/(.)/\\u$1/g}\n$1");
expect(bodyTree).toEqual([
{index: 1, content: ['placeholder']},
'\n',
Expand Down Expand Up @@ -148,7 +161,7 @@ the quick brown $1fox \${2:jumped \${3:over}
index: 1,
content: [],
substitution: {
find: /(.)(.*)/g,
find: /(.)(.*)/,
replace: [
{escape: 'u'},
{backreference: 1},
Expand All @@ -174,7 +187,7 @@ the quick brown $1fox \${2:jumped \${3:over}
index: 1,
content: [],
substitution: {
find: /(.)\/(.*)/g,
find: /(.)\/(.*)/,
replace: [
{escape: 'u'},
{backreference: 1},
Expand Down
40 changes: 35 additions & 5 deletions spec/snippets-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,27 +265,27 @@ third tabstop $3\
},
"transform with non-transforming mirrors": {
prefix: "t13",
body: "${1:placeholder}\n${1/(.)/\\u$1/}\n$1"
body: "${1:placeholder}\n${1/(.)/\\u$1/g}\n$1"
},
"multiple tab stops, some with transforms and some without": {
prefix: "t14",
body: "${1:placeholder} ${1/(.)/\\u$1/} $1 ${2:ANOTHER} ${2/^(.*)$/\\L$1/} $2"
body: "${1:placeholder} ${1/(.)/\\u$1/g} $1 ${2:ANOTHER} ${2/^(.*)$/\\L$1/} $2"
},
"has a transformed tab stop without a corresponding ordinary tab stop": {
prefix: 't15',
body: "${1/(.)/\\u$1/} & $2"
body: "${1/(.)/\\u$1/g} & $2"
},
"has a transformed tab stop that occurs before the corresponding ordinary tab stop": {
prefix: 't16',
body: "& ${1/(.)/\\u$1/} & ${1:q}"
body: "& ${1/(.)/\\u$1/g} & ${1:q}"
},
"has a placeholder that mirrors another tab stop's content": {
prefix: 't17',
body: "$4console.${3:log}('${2:uh $1}', $1);$0"
},
"has a transformed tab stop such that it is possible to move the cursor between the ordinary tab stop and its transformed version without an intermediate step": {
prefix: 't18',
body: '// $1\n// ${1/./=/}'
body: '// $1\n// ${1/./=/g}'
},
"has two tab stops adjacent to one another": {
prefix: 't19',
Expand All @@ -294,6 +294,14 @@ third tabstop $3\
"has several adjacent tab stops, one of which has a placeholder with reference to another tab stop at its edge": {
prefix: 't20',
body: '${1:foo}${2:bar}${3:baz $1}$4'
},
"banner without global flag": {
prefix: "bannerWrong",
body: "// $1\n// ${1/./=/}"
},
"banner with globalFlag": {
prefix: "bannerCorrect",
body: "// $1\n// ${1/./=/g}"
}
}
});
Expand Down Expand Up @@ -904,6 +912,28 @@ foo\
});
});

describe("when the snippet contains a transformation without a global flag", () => {
it("should transform only the first character", () => {
editor.setText('bannerWrong');
editor.setCursorScreenPosition([0, 11]);
simulateTabKeyEvent();
expect(editor.getText()).toBe("// \n// ");
editor.insertText('TEST');
expect(editor.getText()).toBe("// TEST\n// =EST");
});
});

describe("when the snippet contains a transformation with a global flag", () => {
it("should transform all characters", () => {
editor.setText('bannerCorrect');
editor.setCursorScreenPosition([0, 13]);
simulateTabKeyEvent();
expect(editor.getText()).toBe("// \n// ");
editor.insertText('TEST');
expect(editor.getText()).toBe("// TEST\n// ====");
});
});

describe("when the snippet contains multiple tab stops, some with transformations and some without", () => {
it("does not get confused", () => {
editor.setText('t14');
Expand Down