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
23 changes: 21 additions & 2 deletions src/rTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export async function chooseTerminal(): Promise<vscode.Terminal> {
return rTerm;
}

export async function runSelectionInTerm(moveCursor: boolean): Promise<void> {
export async function runSelectionInTerm(moveCursor: boolean, useRepl = true): Promise<void> {
const selection = getSelection();
if (moveCursor && selection.linesDownToMoveCursor > 0) {
const lineCount = vscode.window.activeTextEditor.document.lineCount;
Expand All @@ -224,7 +224,11 @@ export async function runSelectionInTerm(moveCursor: boolean): Promise<void> {
await vscode.commands.executeCommand('cursorMove', { to: 'down', value: selection.linesDownToMoveCursor });
await vscode.commands.executeCommand('cursorMove', { to: 'wrappedLineFirstNonWhitespaceCharacter' });
}
await runTextInTerm(selection.selectedText);
if(useRepl && vscode.debug.activeDebugSession?.type === 'R-Debugger'){
await sendRangeToRepl(selection.range);
} else{
await runTextInTerm(selection.selectedText);
}
}

export async function runChunksInTerm(chunks: vscode.Range[]): Promise<void> {
Expand Down Expand Up @@ -266,3 +270,18 @@ function setFocus(term: vscode.Terminal) {
term.show(focus !== 'terminal');
}
}

export async function sendRangeToRepl(rng: vscode.Range): Promise<void> {
const editor = vscode.window.activeTextEditor;
const sel0 = editor.selections;
let sel1 = new vscode.Selection(rng.start, rng.end);
while(/^[\r\n]/.exec(editor.document.getText(sel1))){
sel1 = new vscode.Selection(sel1.start.translate(1), sel1.end);
}
while(/\r?\n\r?\n$/.exec(editor.document.getText(sel1))){
sel1 = new vscode.Selection(sel1.start, sel1.end.translate(-1));
}
editor.selections = [sel1];
await vscode.commands.executeCommand('editor.debug.action.selectionToRepl');
editor.selections = sel0;
}
41 changes: 23 additions & 18 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,39 @@ export function surroundSelection(text: string, rFunctionName: string[]): string
return text;
}

export function getSelection(): {
export interface RSelection {
linesDownToMoveCursor: number;
selectedText: string;
} {
const selection = { linesDownToMoveCursor: 0, selectedText: '' };
const { start, end } = window.activeTextEditor.selection;
startLine: number;
endLine: number;
range: Range;
}

export function getSelection(): RSelection {
const currentDocument = window.activeTextEditor.document;
const range = new Range(start, end);
const { start, end } = window.activeTextEditor.selection;
const selection = {
linesDownToMoveCursor: 0,
selectedText: '',
startLine: start.line,
endLine: end.line,
range: new Range(start, end)
};

let selectedLine = currentDocument.getText(range);
if (!selectedLine) {
const { startLine, endLine }
= extendSelection(start.line, (x) => currentDocument.lineAt(x).text, currentDocument.lineCount);
if (selection.range.isEmpty) {
const {startLine, endLine} = extendSelection(
start.line,
(x) => currentDocument.lineAt(x).text,
currentDocument.lineCount
);
const charactersOnLine = window.activeTextEditor.document.lineAt(endLine).text.length;
const newStart = new Position(startLine, 0);
const newEnd = new Position(endLine, charactersOnLine);
selection.linesDownToMoveCursor = endLine + 1 - start.line;
selectedLine = currentDocument.getText(new Range(newStart, newEnd));
} else if (start.line === end.line) {
selection.linesDownToMoveCursor = 0;
selection.selectedText = currentDocument.getText(new Range(start, end));

return selection;
} else {
selectedLine = currentDocument.getText(new Range(start, end));
selection.range = new Range(newStart, newEnd);
}

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

return selection;
}
Expand Down