Skip to content
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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,12 @@
"r.bracketedPaste": {
"type": "boolean",
"default": false,
"description": "Use bracketed paste mode when sending code to console. Enable for Radian console."
"markdownDescription": "Use bracketed paste mode when sending code to terminal. Enable for [radian](https://github.com/randy3k/radian) console."
},
"r.removeLeadingComments": {
"type": "boolean",
"default": false,
"description": "Remove leading comments when sending code to terminal."
},
"r.sessionWatcher": {
"type": "boolean",
Expand Down
29 changes: 28 additions & 1 deletion src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Position, Range, window } from 'vscode';

import { LineCache } from './lineCache';
import { config } from './util';

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

selection.selectedText = currentDocument.getText(selection.range).trim();
let selectedText = currentDocument.getText(selection.range).trim();

if (config().get<boolean>('removeLeadingComments')) {
selectedText = removeLeadingComments(selectedText);
}

selection.selectedText = selectedText;

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

return ({ startLine: poss[0].line, endLine: poss[1].line });
}


/**
* This function removes leading R comments from a block of code text
* I.e. All blank and commented out lines are removed up until we hit the first
* non-blank / non-comment line.
* @param text A block of R code as a string
*/
export function removeLeadingComments(text: string): string {
const textArray = text.split('\n');
let endSearchIndex = 0;
for (const lineContent of textArray) {
if (lineContent.search('(^ *$|^ *#)') !== -1) {
endSearchIndex += 1;
} else {
break;
}
}
return textArray.slice(endSearchIndex).join('\n');
}
67 changes: 67 additions & 0 deletions src/test/suite/removeLeadingComments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@


import * as assert from 'assert';
import { removeLeadingComments } from '../../selection';

// Defines a Mocha test suite to group tests of similar kind together
suite('removeLeadingComments Tests', () => {


test('Check that nothing changes if no comments', () => {
const input = `\
function (x) {
y = x
y
}
`;
const expectedOutput = `\
function (x) {
y = x
y
}
`;
const result = removeLeadingComments(input);
assert.strictEqual(result, expectedOutput);
});

test('Check that basic comments are removed', () => {
const input = `\
# a leading comment
function (x) {
y = x
y
}
`;
const expectedOutput = `\
function (x) {
y = x
y
}
`;
const result = removeLeadingComments(input);
assert.strictEqual(result, expectedOutput);
});

test('Check that inner comments are not removed', () => {
const input = `\

# a leading comment
# Another leading comment

function (x) {
y = x
# inner comment
y
}
`;
const expectedOutput = `\
function (x) {
y = x
# inner comment
y
}
`;
const result = removeLeadingComments(input);
assert.strictEqual(result, expectedOutput);
});
});