Skip to content

Commit

Permalink
Include original case in snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Apr 2, 2024
1 parent 46f05ec commit 5032416
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
6 changes: 5 additions & 1 deletion extensions/positron-r/src/test/indentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as assert from 'assert';
import * as fs from 'fs';
import { CURSOR, type, withFileEditor } from './editor-utils';
import { EXTENSION_ROOT_DIR } from '../constants';
import { removeLeadingLines } from '../util';

const snapshotsFolder = `${EXTENSION_ROOT_DIR}/src/test/snapshots`;
const snippetsPath = `${snapshotsFolder}/indentation-cases.R`;
Expand Down Expand Up @@ -51,10 +52,13 @@ async function regenerateIndentSnapshots() {
const snapshots: string[] = ['# File generated from `indentation-cases.R`.\n\n'];

for (const snippet of snippets) {
const bareSnippet = snippet.split('\n').slice(0, -1).join('\n');

await withFileEditor(snippet, 'R', async (_editor, doc) => {
// Type one newline character to trigger indentation
await type(doc, `\n${CURSOR}`);
snapshots.push(doc.getText());
const snapshot = removeLeadingLines(doc.getText(), /^$|^#/);
snapshots.push(bareSnippet + '\n# ->\n' + snapshot);
});
}

Expand Down
60 changes: 60 additions & 0 deletions extensions/positron-r/src/test/snapshots/indentation-snapshots.R
Original file line number Diff line number Diff line change
@@ -1,52 +1,91 @@
# File generated from `indentation-cases.R`.

# ---
1 +"<>"

# ->
1 +
"<>"

# ---
1 +
2 +"<>"

# ->
1 +
2 +
"<>"

# ---
data |>"<>"

# ->
data |>
"<>"

# ---
data |>
fn()"<>"

# ->
data |>
fn()
"<>"

# ---
# https://github.com/posit-dev/positron/issues/1727
# FIXME
data |>
fn()
"<>"

# ->
data |>
fn()

"<>"

# ---
# https://github.com/posit-dev/positron/issues/1316
data |>
fn() |>"<>"

# ->
data |>
fn() |>
"<>"

# ---
# With trailing whitespace
# https://github.com/posit-dev/positron/pull/1655#issuecomment-1780093395
data |>
fn() |> "<>"

# ->
data |>
fn() |>
"<>"

# ---
data |>
fn1() |>
fn2() |>"<>"

# ->
data |>
fn1() |>
fn2() |>
"<>"

# ---
# FIXME
data |>
fn1() |>
fn2(
"arg"
)"<>"

# ->
data |>
fn1() |>
fn2(
Expand All @@ -57,19 +96,35 @@ data |>
# ---
# https://github.com/posit-dev/positron-beta/discussions/46
# FIXME
data |>
fn("<>")

# ->
data |>
fn(
"<>")

# ---
# FIXME
{
fn(function() {}"<>")
}

# ->
{
fn(function() {}
"<>")
}

# ---
# FIXME
{
fn(function() {
#
}"<>")
}

# ->
{
fn(function() {
#
Expand All @@ -78,11 +133,16 @@ data |>
}

# ---
for (i in NA) NULL"<>"

# ->
for (i in NA) NULL
"<>"

# ---
# https://github.com/posit-dev/positron/issues/1880
# FIXME
for (i in 1) fn()"<>"
# ->
for (i in 1) fn()
"<>"
15 changes: 15 additions & 0 deletions extensions/positron-r/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,18 @@ export function extractValue(str: string, key: string, delim: string = '='): str
const m = str.match(re);
return m?.[1] ?? '';
}

export function removeLeadingLines(x: string, pattern: RegExp): string {
const lines = x.split('\n');
let output = lines;

for (const line of lines) {
if (pattern.test(line)) {
output = output.slice(1);
continue;
}
break;
}

return output.join('\n');
}

0 comments on commit 5032416

Please sign in to comment.