-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathextension.ts
More file actions
202 lines (166 loc) · 9.22 KB
/
extension.ts
File metadata and controls
202 lines (166 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
// interfaces, functions, etc. provided by vscode
import * as vscode from 'vscode';
// functions etc. implemented in this extension
import * as preview from './preview';
import * as rGitignore from './rGitignore';
import * as rTerminal from './rTerminal';
import * as session from './session';
import * as util from './util';
import * as rstudioapi from './rstudioapi';
import * as rmarkdown from './rmarkdown';
import * as workspaceViewer from './workspaceViewer';
import * as apiImplementation from './apiImplementation';
import * as rHelp from './rHelp';
import * as completions from './completions';
// global objects used in other files
export let rWorkspace: workspaceViewer.WorkspaceDataProvider | undefined = undefined;
export let globalRHelp: rHelp.RHelp | undefined = undefined;
export let extensionContext: vscode.ExtensionContext | undefined = undefined;
// Called (once) when the extension is activated
export async function activate(context: vscode.ExtensionContext): Promise<apiImplementation.RExtensionImplementation> {
// 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
const rExtension = new apiImplementation.RExtensionImplementation();
// assign extension context to global variable
extensionContext = context;
// register commands specified in package.json
const commands = {
// create R terminal
'r.createRTerm': rTerminal.createRTerm,
// run code from editor in terminal
'r.nrow': () => rTerminal.runSelectionOrWord(['nrow']),
'r.length': () => rTerminal.runSelectionOrWord(['length']),
'r.head': () => rTerminal.runSelectionOrWord(['head']),
'r.thead': () => rTerminal.runSelectionOrWord(['t', 'head']),
'r.names': () => rTerminal.runSelectionOrWord(['names']),
'r.runSource': () => { void rTerminal.runSource(false); },
'r.runSelection': rTerminal.runSelection,
'r.runFromLineToEnd': rTerminal.runFromLineToEnd,
'r.runFromBeginningToLine': rTerminal.runFromBeginningToLine,
'r.runSelectionRetainCursor': rTerminal.runSelectionRetainCursor,
'r.runCommandWithSelectionOrWord': rTerminal.runCommandWithSelectionOrWord,
'r.runCommandWithEditorPath': rTerminal.runCommandWithEditorPath,
'r.runCommand': rTerminal.runCommand,
'r.runSourcewithEcho': () => { void rTerminal.runSource(true); },
// rmd related
'r.knitRmd': () => { void rTerminal.knitRmd(false, undefined); },
'r.knitRmdToPdf': () => { void rTerminal.knitRmd(false, 'pdf_document'); },
'r.knitRmdToHtml': () => { void rTerminal.knitRmd(false, 'html_document'); },
'r.knitRmdToAll': () => { void rTerminal.knitRmd(false, 'all'); },
'r.selectCurrentChunk': rmarkdown.selectCurrentChunk,
'r.runCurrentChunk': rmarkdown.runCurrentChunk,
'r.runPreviousChunk': rmarkdown.runPreviousChunk,
'r.runNextChunk': rmarkdown.runNextChunk,
'r.runAboveChunks': rmarkdown.runAboveChunks,
'r.runCurrentAndBelowChunks': rmarkdown.runCurrentAndBelowChunks,
'r.runBelowChunks': rmarkdown.runBelowChunks,
'r.runAllChunks': rmarkdown.runAllChunks,
'r.goToPreviousChunk': rmarkdown.goToPreviousChunk,
'r.goToNextChunk': rmarkdown.goToNextChunk,
'r.runChunks': rTerminal.runChunksInTerm,
// editor independent commands
'r.createGitignore': rGitignore.createGitignore,
'r.loadAll': () => rTerminal.runTextInTerm('devtools::load_all()'),
'r.test': () => rTerminal.runTextInTerm('devtools::test()'),
'r.install': () => rTerminal.runTextInTerm('devtools::install()'),
'r.build': () => rTerminal.runTextInTerm('devtools::build()'),
'r.document': () => rTerminal.runTextInTerm('devtools::document()'),
// interaction with R sessions
'r.previewDataframe': preview.previewDataframe,
'r.previewEnvironment': preview.previewEnvironment,
'r.attachActive': session.attachActive,
'r.showPlotHistory': session.showPlotHistory,
'r.launchAddinPicker': rstudioapi.launchAddinPicker,
// workspace viewer
'r.workspaceViewer.refreshEntry': () => rWorkspace.refresh(),
'r.workspaceViewer.view': (node: workspaceViewer.WorkspaceItem) => rTerminal.runTextInTerm(`View(${node.label})`),
'r.workspaceViewer.remove': (node: workspaceViewer.WorkspaceItem) => rTerminal.runTextInTerm(`rm(${node.label})`),
'r.workspaceViewer.clear': workspaceViewer.clearWorkspace,
'r.workspaceViewer.load': workspaceViewer.loadWorkspace,
'r.workspaceViewer.save': workspaceViewer.saveWorkspace,
// browser controls
'r.browser.refresh': session.refreshBrowser,
'r.browser.openExternal': session.openExternalBrowser
// (help related commands are registered in rHelp.initializeHelp)
};
for(const key in commands){
context.subscriptions.push(vscode.commands.registerCommand(key, commands[key]));
}
// keep track of terminals
context.subscriptions.push(vscode.window.onDidCloseTerminal(rTerminal.deleteTerminal));
// register on-enter rule for roxygen comments
const wordPattern = /(-?\d*\.\d\w*)|([^`~!@$^&*()=+[{\]}\\|;:'",<>/\s]+)/g;
vscode.languages.setLanguageConfiguration('r', {
onEnterRules: [
{
// Automatically continue roxygen comments: #'
action: { indentAction: vscode.IndentAction.None, appendText: '#\' ' },
beforeText: /^#'.*/,
},
],
wordPattern,
});
// initialize the package/help related functions
globalRHelp = await rHelp.initializeHelp(context, rExtension);
// register codelens and complmetion providers for r markdown
vscode.languages.registerCodeLensProvider('rmd', new rmarkdown.RMarkdownCodeLensProvider());
vscode.languages.registerCompletionItemProvider('rmd', new rmarkdown.RMarkdownCompletionItemProvider(), ' ', ',');
// register (session) hover and completion providers
vscode.languages.registerHoverProvider('r', new completions.HoverProvider());
vscode.languages.registerHoverProvider('r', new completions.HelpLinkHoverProvider());
vscode.languages.registerCompletionItemProvider('r', new completions.StaticCompletionItemProvider(), '@');
// register task provider
const type = 'R';
vscode.tasks.registerTaskProvider(type, {
provideTasks() {
return [
new vscode.Task({type: type}, vscode.TaskScope.Workspace, 'Check', 'R',
new vscode.ShellExecution('Rscript -e "devtools::check()"')),
new vscode.Task({type: type}, vscode.TaskScope.Workspace, 'Document', 'R',
new vscode.ShellExecution('Rscript -e "devtools::document()"')),
new vscode.Task({type: type}, vscode.TaskScope.Workspace, 'Install', 'R',
new vscode.ShellExecution('Rscript -e "devtools::install()"')),
new vscode.Task({type: type}, vscode.TaskScope.Workspace, 'Test', 'R',
new vscode.ShellExecution('Rscript -e "devtools::test()"')),
new vscode.Task({type: type}, vscode.TaskScope.Workspace, 'Knit', 'R',
new vscode.ShellExecution('Rscript -e "rmarkdown::render(\'${file}\')"')),
];
},
resolveTask(task: vscode.Task) {
return task;
}
});
// deploy session watcher (if configured by user)
const enableSessionWatcher = util.config().get<boolean>('sessionWatcher', false);
if (enableSessionWatcher) {
console.info('Initialize session watcher');
session.deploySessionWatcher(context.extensionPath);
// create status bar item that contains info about the session watcher
console.info('Create sessionStatusBarItem');
const sessionStatusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 1000);
sessionStatusBarItem.command = 'r.attachActive';
sessionStatusBarItem.text = 'R: (not attached)';
sessionStatusBarItem.tooltip = 'Attach Active Terminal';
sessionStatusBarItem.show();
context.subscriptions.push(sessionStatusBarItem);
session.startRequestWatcher(sessionStatusBarItem);
// track active text editor
rstudioapi.trackLastActiveTextEditor(vscode.window.activeTextEditor);
vscode.window.onDidChangeActiveTextEditor(rstudioapi.trackLastActiveTextEditor);
// register the R Workspace tree view
// creates a custom context value for the workspace view
// only shows view when session watcher is enabled
rWorkspace = new workspaceViewer.WorkspaceDataProvider();
vscode.window.registerTreeDataProvider(
'workspaceViewer',
rWorkspace
);
void vscode.commands.executeCommand('setContext', 'r.WorkspaceViewer:show', enableSessionWatcher);
// if session watcher is active, register dyamic completion provider
const liveTriggerCharacters = ['', '[', '(', ',', '$', '@', '"', '\'' ];
vscode.languages.registerCompletionItemProvider('r', new completions.LiveCompletionItemProvider(), ...liveTriggerCharacters);
}
return rExtension;
}