Skip to content

Commit

Permalink
Add goToPreviousChunk
Browse files Browse the repository at this point in the history
  • Loading branch information
kar9222 committed Nov 25, 2020
1 parent e4d7cdf commit 1c26e83
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"onCommand:r.runFromCurrentToBelowChunks",
"onCommand:r.runBelowChunks",
"onCommand:r.runAllChunks",
"onCommand:r.goToPreviousChunk",
"onCommand:r.createGitignore",
"onCommand:r.runCommandWithSelectionOrWord",
"onCommand:r.runCommandWithEditorPath",
Expand Down Expand Up @@ -419,6 +420,11 @@
"category": "R",
"command": "r.runAllChunks"
},
{
"title": "Go To Previous Chunk",
"category": "R",
"command": "r.goToPreviousChunk"
},
{
"title": "Launch RStudio Addin",
"category": "R",
Expand Down Expand Up @@ -486,6 +492,12 @@
"mac": "cmd+alt+r",
"when": "editorTextFocus && editorLangId == 'rmd'"
},
{
"command": "r.goToPreviousChunk",
"key": "Ctrl+shift+alt+p",
"mac": "cmd+shift+alt+p",
"when": "editorTextFocus && editorLangId == 'rmd'"
},
{
"command": "r.runSource",
"key": "shift+Ctrl+s",
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getWordOrSelection, surroundSelection } from './selection';
import { attachActive, deploySessionWatcher, globalenv, showPlotHistory, startRequestWatcher } from './session';
import { config, ToRStringLiteral, getRpath, getRpathFromSystem } from './util';
import { launchAddinPicker, trackLastActiveTextEditor } from './rstudioapi';
import { RMarkdownCodeLensProvider, RMarkdownCompletionItemProvider, runCurrentChunk, runAboveChunks, runFromCurrentToBelowChunks, runBelowChunks, runPreviousChunk, runNextChunk, runAllChunks } from './rmarkdown';
import { RMarkdownCodeLensProvider, RMarkdownCompletionItemProvider, runCurrentChunk, runAboveChunks, runFromCurrentToBelowChunks, runBelowChunks, runPreviousChunk, runNextChunk, runAllChunks, goToPreviousChunk } from './rmarkdown';

import * as path from 'path';

Expand Down Expand Up @@ -254,6 +254,7 @@ export async function activate(context: ExtensionContext) {
commands.registerCommand('r.runFromCurrentToBelowChunks', runFromCurrentToBelowChunks),
commands.registerCommand('r.runBelowChunks', runBelowChunks),
commands.registerCommand('r.runAllChunks', runAllChunks),
commands.registerCommand('r.goToPreviousChunk', goToPreviousChunk),
commands.registerCommand('r.runChunks', runChunksInTerm),
commands.registerCommand('r.createGitignore', createGitignore),
commands.registerCommand('r.previewDataframe', previewDataframe),
Expand Down
41 changes: 41 additions & 0 deletions src/rmarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Event, EventEmitter, Position, Range, TextDocument, TextEditorDecorationType, window
} from 'vscode';
import { runChunksInTerm } from './rTerminal';
import * as vscode from 'vscode';

function isChunkStartLine(text: string) {
if (text.match(/^\s*```+\s*\{\w+\s*.*$/g)) {
Expand Down Expand Up @@ -472,6 +473,46 @@ export async function runAllChunks() {
}
runChunksInTerm(codeRanges);
}

export async function goToPreviousChunk() {
const selection = window.activeTextEditor.selection;
const currentDocument = window.activeTextEditor.document;
const lines = currentDocument.getText().split(/\r?\n/);

// Find 'chunk start line' of the 'current' chunk, covering cases for within and outside of chunk. When the cursor is outside the chunk, the 'current' chunk is next chunk below the cursor.

let line = selection.start.line;
let chunkStartLineAtOrAbove = line;
// `- 1` to cover edge case when cursor is at 'chunk end line'
let chunkEndLineAbove = line - 1;

while (chunkStartLineAtOrAbove >= 0 && !isChunkStartLine(lines[chunkStartLineAtOrAbove])) {
chunkStartLineAtOrAbove--;
}

while (chunkEndLineAbove >= 0 && !isChunkEndLine(lines[chunkEndLineAbove])) {
chunkEndLineAbove--;
}

// Case: Cursor is within chunk
if (chunkEndLineAbove < chunkStartLineAtOrAbove) {
// Find the prev 'chunk start line'
chunkStartLineAtOrAbove--;
while (chunkStartLineAtOrAbove >= 0 && !isChunkStartLine(lines[chunkStartLineAtOrAbove])) {
chunkStartLineAtOrAbove--;
}
line = chunkStartLineAtOrAbove;
} else {
// Case: Cursor is outside of chunk
line = chunkStartLineAtOrAbove;
}

line++; // Move cursor 1 line below 'chunk start line'
window.activeTextEditor.selection = new vscode.Selection(line, 0, line, 0);
}
line++; // Move cursor 1 line below 'chunk start line'
window.activeTextEditor.selection = new vscode.Selection(line, 0, line, 0);
}
export class RMarkdownCompletionItemProvider implements CompletionItemProvider {

// obtained from R code
Expand Down

0 comments on commit 1c26e83

Please sign in to comment.