Skip to content

Commit 07fa8fb

Browse files
gowercrenkun-ken
andauthored
Remove leading comments from terminal submission (#1244) (#1245)
* Remove leading comments from terminal submission (#1244) * converted in -> of to appease lintr * updates to appease lintr * added unit test for double comment line * Add r.removeLeadingComments to settings --------- Co-authored-by: Kun Ren <[email protected]>
1 parent 77aab53 commit 07fa8fb

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,12 @@
16041604
"r.bracketedPaste": {
16051605
"type": "boolean",
16061606
"default": false,
1607-
"description": "Use bracketed paste mode when sending code to console. Enable for Radian console."
1607+
"markdownDescription": "Use bracketed paste mode when sending code to terminal. Enable for [radian](https://github.com/randy3k/radian) console."
1608+
},
1609+
"r.removeLeadingComments": {
1610+
"type": "boolean",
1611+
"default": false,
1612+
"description": "Remove leading comments when sending code to terminal."
16081613
},
16091614
"r.sessionWatcher": {
16101615
"type": "boolean",

src/selection.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { Position, Range, window } from 'vscode';
44

55
import { LineCache } from './lineCache';
6+
import { config } from './util';
67

78
export function getWordOrSelection(): string | undefined {
89
const textEditor = window.activeTextEditor;
@@ -72,7 +73,13 @@ export function getSelection(): RSelection | undefined {
7273
selection.range = new Range(newStart, newEnd);
7374
}
7475

75-
selection.selectedText = currentDocument.getText(selection.range).trim();
76+
let selectedText = currentDocument.getText(selection.range).trim();
77+
78+
if (config().get<boolean>('removeLeadingComments')) {
79+
selectedText = removeLeadingComments(selectedText);
80+
}
81+
82+
selection.selectedText = selectedText;
7683

7784
return selection;
7885
}
@@ -278,3 +285,23 @@ export function extendSelection(line: number, getLine: (line: number) => string,
278285

279286
return ({ startLine: poss[0].line, endLine: poss[1].line });
280287
}
288+
289+
290+
/**
291+
* This function removes leading R comments from a block of code text
292+
* I.e. All blank and commented out lines are removed up until we hit the first
293+
* non-blank / non-comment line.
294+
* @param text A block of R code as a string
295+
*/
296+
export function removeLeadingComments(text: string): string {
297+
const textArray = text.split('\n');
298+
let endSearchIndex = 0;
299+
for (const lineContent of textArray) {
300+
if (lineContent.search('(^ *$|^ *#)') !== -1) {
301+
endSearchIndex += 1;
302+
} else {
303+
break;
304+
}
305+
}
306+
return textArray.slice(endSearchIndex).join('\n');
307+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
3+
import * as assert from 'assert';
4+
import { removeLeadingComments } from '../../selection';
5+
6+
// Defines a Mocha test suite to group tests of similar kind together
7+
suite('removeLeadingComments Tests', () => {
8+
9+
10+
test('Check that nothing changes if no comments', () => {
11+
const input = `\
12+
function (x) {
13+
y = x
14+
y
15+
}
16+
`;
17+
const expectedOutput = `\
18+
function (x) {
19+
y = x
20+
y
21+
}
22+
`;
23+
const result = removeLeadingComments(input);
24+
assert.strictEqual(result, expectedOutput);
25+
});
26+
27+
test('Check that basic comments are removed', () => {
28+
const input = `\
29+
# a leading comment
30+
function (x) {
31+
y = x
32+
y
33+
}
34+
`;
35+
const expectedOutput = `\
36+
function (x) {
37+
y = x
38+
y
39+
}
40+
`;
41+
const result = removeLeadingComments(input);
42+
assert.strictEqual(result, expectedOutput);
43+
});
44+
45+
test('Check that inner comments are not removed', () => {
46+
const input = `\
47+
48+
# a leading comment
49+
# Another leading comment
50+
51+
function (x) {
52+
y = x
53+
# inner comment
54+
y
55+
}
56+
`;
57+
const expectedOutput = `\
58+
function (x) {
59+
y = x
60+
# inner comment
61+
y
62+
}
63+
`;
64+
const result = removeLeadingComments(input);
65+
assert.strictEqual(result, expectedOutput);
66+
});
67+
});

0 commit comments

Comments
 (0)