mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-22 09:38:38 +01:00
Support character markup in mardown smart select (#110195)
* create tests and add selection functions for inline ranges
This commit is contained in:
@@ -416,6 +416,157 @@ suite('markdown.SmartSelect', () => {
|
||||
`- level ${CURSOR}1`));
|
||||
assertNestedLineNumbersEqual(ranges![0], [3, 3], [0, 3]);
|
||||
});
|
||||
test('Smart select without multiple ranges', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`# main header 1`,
|
||||
``,
|
||||
``,
|
||||
`- ${CURSOR}paragraph`,
|
||||
`- content`));
|
||||
|
||||
assertNestedLineNumbersEqual(ranges![0], [3, 3], [3, 4], [1, 4], [0, 4]);
|
||||
});
|
||||
test('Smart select on second level of a list', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`* level 0`,
|
||||
` * level 1`,
|
||||
` * level 1`,
|
||||
` * level 2`,
|
||||
` * level 1`,
|
||||
` * level ${CURSOR}1`,
|
||||
`* level 0`));
|
||||
|
||||
assertNestedLineNumbersEqual(ranges![0], [5, 5], [1, 5], [0, 5], [0, 6]);
|
||||
});
|
||||
test('Smart select on third level of a list', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`* level 0`,
|
||||
` * level 1`,
|
||||
` * level 1`,
|
||||
` * level ${CURSOR}2`,
|
||||
` * level 2`,
|
||||
` * level 1`,
|
||||
` * level 1`,
|
||||
`* level 0`));
|
||||
assertNestedLineNumbersEqual(ranges![0], [3, 3], [3, 4], [2, 4], [1, 6], [0, 6], [0, 7]);
|
||||
});
|
||||
test('Smart select level 2 then level 1', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`* level 1`,
|
||||
` * level ${CURSOR}2`,
|
||||
` * level 2`,
|
||||
`* level 1`));
|
||||
assertNestedLineNumbersEqual(ranges![0], [1, 1], [1, 2], [0, 2], [0, 3]);
|
||||
});
|
||||
test('Smart select bold', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`stuff here **new${CURSOR}item** and here`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 13, 0, 30], [0, 11, 0, 32], [0, 0, 0, 41]);
|
||||
});
|
||||
test('Smart select link', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`stuff here [text](https${CURSOR}://google.com) and here`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 18, 0, 46], [0, 17, 0, 47], [0, 11, 0, 47], [0, 0, 0, 56]);
|
||||
});
|
||||
test('Smart select brackets', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`stuff here [te${CURSOR}xt](https://google.com) and here`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 12, 0, 26], [0, 11, 0, 27], [0, 11, 0, 47], [0, 0, 0, 56]);
|
||||
});
|
||||
test('Smart select brackets under header in list', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`# main header 1`,
|
||||
``,
|
||||
`- list`,
|
||||
`paragraph`,
|
||||
`## sub header`,
|
||||
`- list`,
|
||||
`- stuff here [te${CURSOR}xt](https://google.com) and here`,
|
||||
`- list`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [6, 14, 6, 28], [6, 13, 6, 29], [6, 13, 6, 49], [6, 0, 6, 59], [5, 0, 7, 6], [4, 0, 7, 6], [1, 0, 7, 6], [0, 0, 7, 6]);
|
||||
});
|
||||
test('Smart select link under header in list', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`# main header 1`,
|
||||
``,
|
||||
`- list`,
|
||||
`paragraph`,
|
||||
`## sub header`,
|
||||
`- list`,
|
||||
`- stuff here [text](${CURSOR}https://google.com) and here`,
|
||||
`- list`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [6, 20, 6, 48], [6, 19, 6, 49], [6, 13, 6, 49], [6, 0, 6, 59], [5, 0, 7, 6], [4, 0, 7, 6], [1, 0, 7, 6], [0, 0, 7, 6]);
|
||||
});
|
||||
test('Smart select bold within list where multiple bold elements exists', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`# main header 1`,
|
||||
``,
|
||||
`- list`,
|
||||
`paragraph`,
|
||||
`## sub header`,
|
||||
`- list`,
|
||||
`- stuff here [text]**${CURSOR}items in here** and **here**`,
|
||||
`- list`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [6, 21, 6, 44], [6, 19, 6, 46], [6, 0, 6, 60], [5, 0, 7, 6], [4, 0, 7, 6], [1, 0, 7, 6], [0, 0, 7, 6]);
|
||||
});
|
||||
test('Smart select link in paragraph with multiple links', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`This[extension](https://marketplace.visualstudio.com/items?itemName=meganrogge.template-string-converter) addresses this [requ${CURSOR}est](https://github.com/microsoft/vscode/issues/56704) to convert Javascript/Typescript quotes to backticks when has been entered within a string.`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 123, 0, 140], [0, 122, 0, 141], [0, 122, 0, 191], [0, 0, 0, 283]);
|
||||
});
|
||||
test('Smart select bold link', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`**[extens${CURSOR}ion](https://google.com)**`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 3, 0, 22], [0, 2, 0, 23], [0, 2, 0, 43], [0, 2, 0, 43], [0, 0, 0, 45], [0, 0, 0, 45]);
|
||||
});
|
||||
test('Smart select inline code block', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`[\`code ${CURSOR} link\`]`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 2, 0, 22], [0, 1, 0, 23], [0, 0, 0, 24]);
|
||||
});
|
||||
test('Smart select link with inline code block text', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`[\`code ${CURSOR} link\`](http://example.com)`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 2, 0, 22], [0, 1, 0, 23], [0, 1, 0, 23], [0, 0, 0, 24], [0, 0, 0, 44], [0, 0, 0, 44]);
|
||||
});
|
||||
test('Smart select italic', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`*some nice ${CURSOR}text*`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 1, 0, 25], [0, 0, 0, 26], [0, 0, 0, 26]);
|
||||
});
|
||||
test('Smart select italic link', async () => {
|
||||
const ranges = await getSelectionRangesForDocument(
|
||||
joinLines(
|
||||
`*[extens${CURSOR}ion](https://google.com)*`
|
||||
));
|
||||
assertNestedRangesEqual(ranges![0], [0, 2, 0, 21], [0, 1, 0, 22], [0, 1, 0, 42], [0, 1, 0, 42], [0, 0, 0, 43], [0, 0, 0, 43]);
|
||||
});
|
||||
});
|
||||
|
||||
function assertNestedLineNumbersEqual(range: vscode.SelectionRange, ...expectedRanges: [number, number][]) {
|
||||
@@ -426,6 +577,16 @@ function assertNestedLineNumbersEqual(range: vscode.SelectionRange, ...expectedR
|
||||
}
|
||||
}
|
||||
|
||||
function assertNestedRangesEqual(range: vscode.SelectionRange, ...expectedRanges: [number, number, number, number][]) {
|
||||
const lineage = getLineage(range);
|
||||
assert.strictEqual(lineage.length, expectedRanges.length, `expected depth: ${expectedRanges.length}, but was ${lineage.length} ${getValues(lineage)}`);
|
||||
for (let i = 0; i < lineage.length; i++) {
|
||||
assertLineNumbersEqual(lineage[i], expectedRanges[i][0], expectedRanges[i][2], `parent at a depth of ${i}`);
|
||||
assert(lineage[i].range.start.character === expectedRanges[i][1], `parent at a depth of ${i} on start char`);
|
||||
assert(lineage[i].range.end.character === expectedRanges[i][3], `parent at a depth of ${i} on end char`);
|
||||
}
|
||||
}
|
||||
|
||||
function getLineage(range: vscode.SelectionRange): vscode.SelectionRange[] {
|
||||
const result: vscode.SelectionRange[] = [];
|
||||
let currentRange: vscode.SelectionRange | undefined = range;
|
||||
@@ -436,6 +597,12 @@ function getLineage(range: vscode.SelectionRange): vscode.SelectionRange[] {
|
||||
return result;
|
||||
}
|
||||
|
||||
function getValues(ranges: vscode.SelectionRange[]): string[] {
|
||||
return ranges.map(range => {
|
||||
return range.range.start.line + ' ' + range.range.start.character + ' ' + range.range.end.line + ' ' + range.range.end.character;
|
||||
});
|
||||
}
|
||||
|
||||
function assertLineNumbersEqual(selectionRange: vscode.SelectionRange, startLine: number, endLine: number, message: string) {
|
||||
assert.strictEqual(selectionRange.range.start.line, startLine, `failed on start line ${message}`);
|
||||
assert.strictEqual(selectionRange.range.end.line, endLine, `failed on end line ${message}`);
|
||||
|
||||
Reference in New Issue
Block a user