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
109 changes: 109 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,54 @@
"category": "R",
"command": "r.goToNextChunk"
},
{
"command": "r.markdown.showPreviewToSide",
"title": "Open Preview to the Side",
"icon": "$(open-preview)",
"category": "R"
},
{
"command": "r.markdown.showPreview",
"title": "Open Preview",
"icon": "$(open-preview)",
"category": "R"
},
{
"command": "r.markdown.preview.refresh",
"title": "Refresh Preview",
"icon": "$(refresh)",
"category": "R"
},
{
"command": "r.markdown.preview.openExternal",
"title": "Open in External Browser",
"icon": "$(link-external)",
"category": "R"
},
{
"command": "r.markdown.preview.showSource",
"title": "Show Source",
"icon": "$(go-to-file)",
"category": "R"
},
{
"title": "Toggle Style",
"category": "R",
"command": "r.markdown.preview.toggleStyle",
"icon": "$(symbol-color)"
},
{
"title": "Enable Auto Refresh",
"category": "R",
"command": "r.markdown.preview.enableAutoRefresh",
"icon": "$(sync)"
},
{
"title": "Disable Auto Refresh",
"category": "R",
"command": "r.markdown.preview.disableAutoRefresh",
"icon": "$(sync-ignored)"
},
Comment on lines +529 to +576
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest using "category": "R Markdown" for (most of) these commands, otherwise it's not quite obvious what they do, when invoking them from the command palette

{
"title": "Launch RStudio Addin",
"category": "R",
Expand Down Expand Up @@ -797,6 +845,18 @@
"key": "Ctrl+alt+e",
"mac": "cmd+alt+e",
"when": "editorTextFocus && editorLangId == 'r'"
},
{
"command": "r.markdown.showPreviewToSide",
"key": "Ctrl+k v",
"mac": "cmd+k v",
"when": "editorTextFocus && editorLangId == 'rmd'"
},
{
"command": "r.markdown.showPreview",
"key": "Ctrl+shift+v",
"mac": "cmd+shift+v",
"when": "editorTextFocus && editorLangId == 'rmd'"
}
],
"menus": {
Expand All @@ -812,6 +872,36 @@
}
],
"editor/title": [
{
"command": "r.markdown.preview.openExternal",
"when": "resourceScheme =~ /webview/ && r.preview.active",
"group": "navigation@1"
},
{
"command": "r.markdown.preview.showSource",
"when": "resourceScheme =~ /webview/ && r.preview.active",
"group": "navigation@2"
},
{
"command": "r.markdown.preview.toggleStyle",
"when": "resourceScheme =~ /webview/ && r.preview.active",
"group": "navigation@3"
},
{
"command": "r.markdown.preview.refresh",
"when": "resourceScheme =~ /webview/ && r.preview.active",
"group": "navigation@4"
},
{
"command": "r.markdown.preview.enableAutoRefresh",
"when": "resourceScheme =~ /webview/ && r.preview.active && !r.preview.autoRefresh",
"group": "navigation@5"
},
{
"command": "r.markdown.preview.disableAutoRefresh",
"when": "resourceScheme =~ /webview/ && r.preview.active && r.preview.autoRefresh",
"group": "navigation@5"
},
{
"command": "r.browser.refresh",
"when": "resourceScheme =~ /webview/ && r.browser.active",
Expand Down Expand Up @@ -906,6 +996,12 @@
"group": "httpgd",
"when": "resourceScheme =~ /webview/ && r.plot.active",
"command": "r.plot.openUrl"
},
{
"command": "r.markdown.showPreviewToSide",
"alt": "r.markdown.showPreview",
"when": "editorLangId == rmd",
"group": "navigation"
}
],
"editor/context": [
Expand Down Expand Up @@ -1040,6 +1136,13 @@
"group": "navigation@3",
"when": "view == rLiveShare && r.liveShare:aborted"
}
],
"explorer/context": [
{
"command": "r.markdown.showPreview",
"when": "resourceLangId == rmd",
"group": "navigation"
}
]
},
"configuration": {
Expand Down Expand Up @@ -1108,6 +1211,11 @@
"default": "rgba(128, 128, 128, 0.1)",
"description": "RMarkdown chunk background color in RGBA or RGB value. Defaults to rgba(128, 128, 128, 0.1). Leave it empty to disable it (use default editor background color). Reload VS Code after changing settings.\n\nLearn how to set colors: https://www.w3schools.com/css/css_colors_rgb.asp.\n\nExamples for syntax rgba(<red>, <green>, <blue>, <alpha>):\nrgba(128, 128, 128, 0.1)\nrgba(128, 128, 128, 0.3)\nrgba(255, 165, 0, 0.1)\n\n"
},
"r.rmarkdown.preview.autoRefresh": {
"type": "boolean",
"default": true,
"description": "Enable automatic refresh of R Markdown preview on file update."
},
"r.helpPanel.enableSyntaxHighlighting": {
"type": "boolean",
"default": true,
Expand Down Expand Up @@ -1305,6 +1413,7 @@
"dependencies": {
"bootstrap": "^5.0.1",
"cheerio": "1.0.0-rc.10",
"crypto": "^1.0.1",
"datatables.net": "^1.10.25",
"datatables.net-bs4": "^1.10.25",
"datatables.net-fixedheader-jqui": "^3.1.9",
Expand Down
28 changes: 27 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

// interfaces, functions, etc. provided by vscode
import * as vscode from 'vscode';
import * as os from 'os';
import path = require('path');
import fs = require('fs');

// functions etc. implemented in this extension
import * as preview from './preview';
Expand All @@ -19,17 +22,28 @@ import * as completions from './completions';
import * as rShare from './liveshare';
import * as httpgdViewer from './plotViewer';

import { RMarkdownPreviewManager } from './rmarkdown/preview';

// global objects used in other files
export const extDir: string = path.join(os.homedir(), '.vscode-R');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we give this a slightly longer/more descriptive name? For local variables, these short names are fine, but global variables should be clear without having to "look them up" in a different file

export const tmpDir: string = path.join(extDir, 'tmp');
export let rWorkspace: workspaceViewer.WorkspaceDataProvider | undefined = undefined;
export let globalRHelp: rHelp.RHelp | undefined = undefined;
export let extensionContext: vscode.ExtensionContext;
export let enableSessionWatcher: boolean = undefined;
export let globalHttpgdManager: httpgdViewer.HttpgdManager | undefined = undefined;

export let rMarkdownPreview: RMarkdownPreviewManager | undefined = undefined;

// Called (once) when the extension is activated
export async function activate(context: vscode.ExtensionContext): Promise<apiImplementation.RExtensionImplementation> {
if (!fs.existsSync(extDir)) {
fs.mkdirSync(extDir);
}

if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir);
}

// create a new instance of RExtensionImplementation
// is used to export an interface to the help panel
// this export is used e.g. by vscode-r-debugger to show the help panel from within debug sessions
Expand Down Expand Up @@ -79,6 +93,15 @@ export async function activate(context: vscode.ExtensionContext): Promise<apiImp
'r.goToNextChunk': rmarkdown.goToNextChunk,
'r.runChunks': rTerminal.runChunksInTerm,

'r.markdown.showPreviewToSide': () => rMarkdownPreview.previewRmd(vscode.ViewColumn.Beside),
'r.markdown.showPreview': (uri: vscode.Uri) => rMarkdownPreview.previewRmd(vscode.ViewColumn.Active, uri),
'r.markdown.preview.refresh': () => rMarkdownPreview.refreshPanel(),
'r.markdown.preview.openExternal': () => void rMarkdownPreview.openExternalBrowser(),
'r.markdown.preview.showSource': () => rMarkdownPreview.showSource(),
'r.markdown.preview.toggleStyle': () => rMarkdownPreview.toggleTheme(),
'r.markdown.preview.enableAutoRefresh': () => rMarkdownPreview.enableAutoRefresh(),
'r.markdown.preview.disableAutoRefresh': () => rMarkdownPreview.disableAutoRefresh(),

// editor independent commands
'r.createGitignore': rGitignore.createGitignore,
'r.loadAll': () => rTerminal.runTextInTerm('devtools::load_all()'),
Expand Down Expand Up @@ -136,6 +159,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<apiImp
// initialize the package/help related functions
globalRHelp = await rHelp.initializeHelp(context, rExtension);

// init preview provider
rMarkdownPreview = new RMarkdownPreviewManager();
await rMarkdownPreview.init();

// register codelens and complmetion providers for r markdown
vscode.languages.registerCodeLensProvider(['r', 'rmd'], new rmarkdown.RMarkdownCodeLensProvider());
Expand Down
14 changes: 1 addition & 13 deletions src/helpViewer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as hljs from 'highlight.js';

import * as api from '../api';

import { config, getRpath, doWithProgress, DummyMemento, getRPathConfigEntry } from '../util';
import { config, getRpath, doWithProgress, DummyMemento, getRPathConfigEntry, escapeHtml } from '../util';
import { HelpPanel } from './panel';
import { HelpProvider, AliasProvider } from './helpProvider';
import { HelpTreeWrapper } from './treeView';
Expand Down Expand Up @@ -524,15 +524,3 @@ export class RHelp implements api.HelpPanel, vscode.WebviewPanelSerializer<strin
}
}

// Helper function used to convert raw text files to html
function escapeHtml(source: string) {
const entityMap = new Map<string, string>(Object.entries({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#39;',
'/': '&#x2F;'
}));
return String(source).replace(/[&<>"'/]/g, (s: string) => entityMap.get(s) || '');
}
6 changes: 3 additions & 3 deletions src/rTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { isDeepStrictEqual } from 'util';

import * as vscode from 'vscode';

import { extensionContext } from './extension';
import { extensionContext, extDir } from './extension';
import * as util from './util';
import * as selection from './selection';
import { getSelection } from './selection';
import { removeSessionFiles, watcherDir } from './session';
import { removeSessionFiles } from './session';
import { config, delay, getRterm } from './util';
import { rGuestService, isGuestSession } from './liveshare';
export let rTerm: vscode.Terminal;
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function createRTerm(preserveshow?: boolean): Promise<boolean> {
R_PROFILE_USER_OLD: process.env.R_PROFILE_USER,
R_PROFILE_USER: newRprofile,
VSCODE_INIT_R: initR,
VSCODE_WATCHER_DIR: watcherDir
VSCODE_WATCHER_DIR: extDir
};
}
rTerm = vscode.window.createTerminal(termOptions);
Expand Down
4 changes: 2 additions & 2 deletions src/rmarkdown.ts → src/rmarkdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
CompletionItem, CompletionItemProvider,
Event, EventEmitter, Position, Range, TextDocument, TextEditorDecorationType, window, Selection, commands
} from 'vscode';
import { runChunksInTerm } from './rTerminal';
import { config } from './util';
import { runChunksInTerm } from '../rTerminal';
import { config } from '../util';

function isRDocument(document: TextDocument) {
return (document.languageId === 'r');
Expand Down
Loading