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

When wrapping with Emmet, consider the document language when setting the syntax. #68326

Merged
merged 4 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion extensions/emmet/src/abbreviationActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ function doWrapping(individualLines: boolean, args: any) {
return;
}
}
const syntax = 'html';
args = args || {};
if (!args['language']) {
args['language'] = editor.document.languageId;
}
const syntax = getSyntaxFromArgs(args) || 'html';
const rootNode = parseDocument(editor.document, false);

let inPreview = false;
Expand Down
153 changes: 50 additions & 103 deletions extensions/emmet/src/test/wrapWithAbbreviation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ const wrapInlineElementExpectedFormatFalse = `
</ul>
`;

const wrapMultiLineJsxExpected = `
<ul class="nav main">
<div className="hello">
<li class="item1">img</li>
<li class="item2">$hithere</li>
</div>
</ul>
`;

const wrapIndividualLinesJsxExpected = `
<ul class="nav main">
<div className="hello1"><li class="item1">img</li></div>
<div className="hello2"><li class="item2">$hithere</li></div>
</ul>
`;

mattkwiecien marked this conversation as resolved.
Show resolved Hide resolved
suite('Tests for Wrap with Abbreviations', () => {
teardown(closeAllEditors);

Expand Down Expand Up @@ -134,19 +150,7 @@ suite('Tests for Wrap with Abbreviations', () => {
<!-- /.hello -->
</ul>
`;

return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(2, 0, 2, 0)];
const promise = wrapWithAbbreviation({ abbreviation: 'li.hello|c' });
if (!promise) {
assert.equal(1, 2, 'Wrap returned undefined instead of promise.');
return Promise.resolve();
}
return promise.then(() => {
assert.equal(editor.document.getText(), expectedContents);
return Promise.resolve();
});
});
return testWrapWithAbbreviation([new Selection(2, 0, 2, 0)], 'li.hello|c', expectedContents, contents);
});

test('Wrap with abbreviation entire node when cursor is on opening tag', () => {
Expand All @@ -162,19 +166,7 @@ suite('Tests for Wrap with Abbreviations', () => {
</div>
</div>
`;

return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(1, 1, 1, 1)];
const promise = wrapWithAbbreviation({ abbreviation: 'div' });
if (!promise) {
assert.equal(1, 2, 'Wrap returned undefined instead of promise.');
return Promise.resolve();
}
return promise.then(() => {
assert.equal(editor.document.getText(), expectedContents);
return Promise.resolve();
});
});
return testWrapWithAbbreviation([new Selection(1, 1, 1, 1)], 'div', expectedContents, contents);
});

test('Wrap with abbreviation entire node when cursor is on closing tag', () => {
Expand All @@ -190,19 +182,7 @@ suite('Tests for Wrap with Abbreviations', () => {
</div>
</div>
`;

return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(3, 1, 3, 1)];
const promise = wrapWithAbbreviation({ abbreviation: 'div' });
if (!promise) {
assert.equal(1, 2, 'Wrap returned undefined instead of promise.');
return Promise.resolve();
}
return promise.then(() => {
assert.equal(editor.document.getText(), expectedContents);
return Promise.resolve();
});
});
return testWrapWithAbbreviation([new Selection(3, 1, 3, 1)], 'div', expectedContents, contents);
});

test('Wrap with multiline abbreviation doesnt add extra spaces', () => {
Expand All @@ -215,19 +195,7 @@ suite('Tests for Wrap with Abbreviations', () => {
<li><a href="">hello</a></li>
</ul>
`;

return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(1, 2, 1, 2)];
const promise = wrapWithAbbreviation({ abbreviation: 'ul>li>a' });
if (!promise) {
assert.equal(1, 2, 'Wrap returned undefined instead of promise.');
return Promise.resolve();
}
return promise.then(() => {
assert.equal(editor.document.getText(), expectedContents);
return Promise.resolve();
});
});
return testWrapWithAbbreviation([new Selection(1, 2, 1, 2)], 'ul>li>a', expectedContents, contents);
});

test('Wrap individual lines with abbreviation', () => {
Expand All @@ -245,18 +213,7 @@ suite('Tests for Wrap with Abbreviations', () => {
</ul>
</ul>
`;
return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(2, 2, 3, 33)];
const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello$*' });
if (!promise) {
assert.equal(1, 2, 'Wrap Individual Lines with Abbreviation returned undefined.');
return Promise.resolve();
}
return promise.then(() => {
assert.equal(editor.document.getText(), wrapIndividualLinesExpected);
return Promise.resolve();
});
});
return testWrapIndividualLinesWithAbbreviation([new Selection(2, 2, 3, 33)], 'ul>li.hello$*', wrapIndividualLinesExpected, contents);
});

test('Wrap individual lines with abbreviation with extra space selected', () => {
Expand All @@ -274,18 +231,7 @@ suite('Tests for Wrap with Abbreviations', () => {
</ul>
</ul>
`;
return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(2, 1, 4, 0)];
const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello$*' });
if (!promise) {
assert.equal(1, 2, 'Wrap Individual Lines with Abbreviation returned undefined.');
return Promise.resolve();
}
return promise.then(() => {
assert.equal(editor.document.getText(), wrapIndividualLinesExpected);
return Promise.resolve();
});
});
return testWrapIndividualLinesWithAbbreviation([new Selection(2, 1, 4, 0)], 'ul>li.hello$*', wrapIndividualLinesExpected, contents);
});

test('Wrap individual lines with abbreviation with comment filter', () => {
Expand All @@ -305,18 +251,7 @@ suite('Tests for Wrap with Abbreviations', () => {
</ul>
</ul>
`;
return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(2, 2, 3, 33)];
const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello*|c' });
if (!promise) {
assert.equal(1, 2, 'Wrap Individual Lines with Abbreviation returned undefined.');
return Promise.resolve();
}
return promise.then(() => {
assert.equal(editor.document.getText(), wrapIndividualLinesExpected);
return Promise.resolve();
});
});
return testWrapIndividualLinesWithAbbreviation([new Selection(2, 2, 3, 33)], 'ul>li.hello*|c', wrapIndividualLinesExpected, contents);
});

test('Wrap individual lines with abbreviation and trim', () => {
Expand All @@ -334,19 +269,7 @@ suite('Tests for Wrap with Abbreviations', () => {
</ul>
</ul>
`;
return withRandomFileEditor(contents, 'html', (editor, _) => {
editor.selections = [new Selection(2, 3, 3, 16)];
const promise = wrapIndividualLinesWithAbbreviation({ abbreviation: 'ul>li.hello$*|t' });
if (!promise) {
assert.equal(1, 2, 'Wrap Individual Lines with Abbreviation returned undefined.');
return Promise.resolve();
}

return promise.then(() => {
assert.equal(editor.document.getText(), wrapIndividualLinesExpected);
return Promise.resolve();
});
});
return testWrapIndividualLinesWithAbbreviation([new Selection(2, 3, 3, 16)], 'ul>li.hello$*|t', wrapIndividualLinesExpected, contents);
});

test('Wrap with abbreviation and format set to false', () => {
Expand Down Expand Up @@ -384,11 +307,19 @@ suite('Tests for Wrap with Abbreviations', () => {

return testWrapWithAbbreviation([new Selection(2, 4, 3, 9), new Selection(5, 4, 6, 9)], 'div', wrapMultiLineExpected, htmlContentsForWrapMultiLineTests);
});

test('Wrap multiline with abbreviation uses className for jsx files', () => {
return testWrapWithAbbreviation([new Selection(2,2,3,33)], '.hello', wrapMultiLineJsxExpected, htmlContentsForWrapTests, 'jsx');
});

test('Wrap individual line with abbreviation uses className for jsx files', () => {
return testWrapIndividualLinesWithAbbreviation([new Selection(2,2,3,33)], '.hello$*', wrapIndividualLinesJsxExpected, htmlContentsForWrapTests, 'jsx');
});
});


function testWrapWithAbbreviation(selections: Selection[], abbreviation: string, expectedContents: string, input: string = htmlContentsForWrapTests): Thenable<any> {
return withRandomFileEditor(input, 'html', (editor, _) => {
function testWrapWithAbbreviation(selections: Selection[], abbreviation: string, expectedContents: string, input: string = htmlContentsForWrapTests, fileExtension: string = 'html'): Thenable<any> {
return withRandomFileEditor(input, fileExtension, (editor, _) => {
editor.selections = selections;
const promise = wrapWithAbbreviation({ abbreviation });
if (!promise) {
Expand All @@ -402,3 +333,19 @@ function testWrapWithAbbreviation(selections: Selection[], abbreviation: string,
});
});
}

function testWrapIndividualLinesWithAbbreviation(selections: Selection[], abbreviation: string, expectedContents: string, input: string = htmlContentsForWrapTests, fileExtension: string = 'html'): Thenable<any> {
return withRandomFileEditor(input, fileExtension, (editor, _) => {
editor.selections = selections;
const promise = wrapIndividualLinesWithAbbreviation({ abbreviation });
if (!promise) {
assert.equal(1, 2, 'Wrap individual lines with Abbreviation returned undefined.');
return Promise.resolve();
}

return promise.then(() => {
assert.equal(editor.document.getText(), expectedContents);
return Promise.resolve();
});
});
}