Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 0ac9f34

Browse files
committed
intelligent run selection for R (runs complete statment not just line)
1 parent 631c5bd commit 0ac9f34

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## 1.54.0 (Unreleased)
44

5+
- Intelligent run selection for R (runs complete statment not just line)
56
- Improve handling of first-run files for create project command
67

78
## 1.53.0 (Release on 27 October 2022)

src/providers/cell/commands.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
codeFromBlock,
2727
ensureRequiredExtension,
2828
executeInteractive,
29+
executeSelectionInteractive,
2930
} from "./executors";
3031

3132
export function cellCommands(engine: MarkdownEngine): Command[] {
@@ -165,29 +166,36 @@ class RunSelectionCommand extends RunCommand implements Command {
165166
_line: number,
166167
block: Token
167168
) {
168-
// if the selection is empty take the whole line, otherwise
169-
// take the selected text exactly
170-
const selection = editor.selection.isEmpty
171-
? editor.document.getText(
172-
new Range(
173-
new Position(editor.selection.start.line, 0),
174-
new Position(
175-
editor.selection.end.line,
176-
editor.document.lineAt(editor.selection.end).text.length
169+
// get language and attempt language aware runSelection
170+
const language = languageNameFromBlock(block);
171+
const executed = await executeSelectionInteractive(language);
172+
173+
// if the executor isn't capable of lenguage aware runSelection
174+
// then determine the selection manually
175+
if (!executed) {
176+
// if the selection is empty take the whole line, otherwise
177+
// take the selected text exactly
178+
const selection = editor.selection.isEmpty
179+
? editor.document.getText(
180+
new Range(
181+
new Position(editor.selection.start.line, 0),
182+
new Position(
183+
editor.selection.end.line,
184+
editor.document.lineAt(editor.selection.end).text.length
185+
)
177186
)
178187
)
179-
)
180-
: editor.document.getText(editor.selection);
188+
: editor.document.getText(editor.selection);
181189

182-
// for empty selections we advance to the next line
183-
if (editor.selection.isEmpty) {
184-
const selPos = new Position(editor.selection.start.line + 1, 0);
185-
editor.selection = new Selection(selPos, selPos);
186-
}
190+
// for empty selections we advance to the next line
191+
if (editor.selection.isEmpty) {
192+
const selPos = new Position(editor.selection.start.line + 1, 0);
193+
editor.selection = new Selection(selPos, selPos);
194+
}
187195

188-
// run code
189-
const language = languageNameFromBlock(block);
190-
await executeInteractive(language, [selection]);
196+
// run code
197+
await executeInteractive(language, [selection]);
198+
}
191199
}
192200
}
193201

src/providers/cell/executors.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ export async function executeInteractive(
7272
}
7373
}
7474

75+
// attempt language aware execution of current selection (returns false
76+
// if the executor doesn't support this, in which case generic
77+
// executeInteractive will be called)
78+
export async function executeSelectionInteractive(language: string) {
79+
const executor = kCellExecutors.find((x) => x.language === language);
80+
if (executor?.executeSelection) {
81+
await executor.executeSelection();
82+
return true;
83+
} else {
84+
return false;
85+
}
86+
}
87+
7588
export function hasCellExecutor(language: string) {
7689
return !!kCellExecutors.find((x) => x.language === language);
7790
}
@@ -178,6 +191,7 @@ interface CellExecutor {
178191
requiredVersion?: string;
179192
isYamlOption: (line: string) => boolean;
180193
execute: (blocks: string[]) => Promise<void>;
194+
executeSelection?: () => Promise<void>;
181195
}
182196

183197
const pythonCellExecutor: CellExecutor = {
@@ -202,6 +216,9 @@ const rCellExecutor: CellExecutor = {
202216
execute: async (blocks: string[]) => {
203217
await commands.executeCommand("r.runSelection", blocks.join("\n").trim());
204218
},
219+
executeSelection: async () => {
220+
await commands.executeCommand("r.runSelection");
221+
},
205222
};
206223

207224
const juliaCellExecutor: CellExecutor = {

0 commit comments

Comments
 (0)