Repository navigation
Expand file tree
/
Copy pathRtools.py
More file actions
76 lines (58 loc) · 2.36 KB
/
Copy pathRtools.py
File metadata and controls
76 lines (58 loc) · 2.36 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
import sublime
import sublime_plugin
import os
import subprocess
import string
class RDocsCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel()[0]
params_reg = self.view.find('(?<=\().*(?=\))', sel.begin())
params_txt = self.view.substr(params_reg)
params = params_txt.split(',')
snippet = "#'<brief desc>\n#'\n#'<full description>\n"
for p in params:
snippet += "#' @param %s <what param does>\n" % p
snippet += "#' @export\n #' @keywords\n#' @seealso\n#' @return\n#' @alias\n#' @examples \dontrun{\n#'\n#'}\n"
self.view.insert(edit, sel.begin(), snippet)
class SendSelectionCommand(sublime_plugin.TextCommand):
@staticmethod
def cleanString(str):
str = string.replace(str, '\\', '\\\\')
str = string.replace(str, '"', '\\"')
return str
def run(self, edit):
# Check if it's an R file
if "R.tmLanguage" not in self.view.settings().get('syntax'):
return
# get selection
selection = ""
for region in self.view.sel():
if region.empty():
selection += self.view.substr(self.view.line(region)) + "\n"
self.advanceCursor(region)
else:
selection += self.view.substr(region) + "\n"
selection = (selection[::-1].replace('\n'[::-1], '', 1))[::-1]
# only proceed if selection is not empty
if(selection == ""):
return
# split selection into lines
selection = self.cleanString(selection).split("\n")
# define osascript arguments
args = ['osascript']
# add code lines to list of arguments
for part in selection:
args.extend(['-e', 'tell app "R64" to cmd "' + part + '"\n'])
# execute code
subprocess.Popen(args)
def advanceCursor(self, region):
(row, col) = self.view.rowcol(region.begin())
# Make sure not to go past end of next line
nextline = self.view.line(self.view.text_point(row + 1, 0))
if nextline.size() < col:
loc = self.view.text_point(row + 1, nextline.size())
else:
loc = self.view.text_point(row + 1, col)
# Remove the old region and add the new one
self.view.sel().subtract(region)
self.view.sel().add(sublime.Region(loc, loc))