[education/rkward] rkward: RKWard utils snippets started
Stefan Rödiger
null at kde.org
Thu Jun 23 14:39:48 BST 2022
Git commit b04a5cf9b516e376615ac777878333391bb794cb by Stefan Rödiger.
Committed on 23/06/2022 at 13:38.
Pushed by srodiger into branch 'master'.
RKWard utils snippets started
styler::style_text() added as first applications
M +1 -0 rkward/CMakeLists.txt
A +35 -0 rkward/RKWardutils.xml
https://invent.kde.org/education/rkward/commit/b04a5cf9b516e376615ac777878333391bb794cb
diff --git a/rkward/CMakeLists.txt b/rkward/CMakeLists.txt
index 0c879fb5..8957f50e 100644
--- a/rkward/CMakeLists.txt
+++ b/rkward/CMakeLists.txt
@@ -128,3 +128,4 @@ INSTALL(FILES RKWardRMd.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets
INSTALL(FILES RKWardLaTeX.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets/data)
INSTALL(FILES RKWardR.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets/data)
INSTALL(FILES RKWardroxygen2.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets/data)
+INSTALL(FILES RKWardutils.xml DESTINATION ${DATA_INSTALL_DIR}/ktexteditor_snippets/data)
diff --git a/rkward/RKWardutils.xml b/rkward/RKWardutils.xml
new file mode 100644
index 00000000..873476e1
--- /dev/null
+++ b/rkward/RKWardutils.xml
@@ -0,0 +1,35 @@
+<snippets name="R and R Markdown Utils (RKWard)" filetypes="R Markdown" authors="RKWard Team" namespace="SnippetsRmd" license="BSD">
+ <script>require("range.js")
+require("cursor.js")
+require("document.js")
+require("view.js")
+
+function fileName() { return document.fileName(); }
+function encoding() { return document.encoding(); }
+function year() { return new Date().getFullYear(); }
+function upper(x) { return x.toUpperCase(); }
+function lower(x) { return x.toLowerCase(); }
+function braces(text) { return "{" + text + "}"; }
+function lineCommand(command, replaces="", label_if_empty="Heading") {
+ let c = view.cursorPosition();
+ // NOTE: snippet scripts work by 1. inserting the raw snippet as a raw "template" 2. evaluating any commands inside the template 3. replacing the template with the evaluated template. As a result, when this function gets called, it contains the call to itself...
+ let line = document.line(c.line).replace(/\$\{lineCommand\s*\([^\}]*\}/g, "");
+ line = line.replace(new RegExp(replaces), "");
+ if (line == "") line = label_if_empty;
+ else document.removeLine(c.line);
+ return command + " " + line + "\n";
+}
+
+function rangeCommand(command, def) {
+ if (view.selectedText().length > 0) {
+ return command.replace("%%1", view.selectedText());
+ } else {
+ return command.replace("%%1", def);
+ }
+}
+</script>
+ <item>
+ <match>Style text (styler)</match>
+ <fillin>${rangeCommand("styler::style_text(\"%%1\")", "R or Rmd code")}</fillin>
+ </item>
+</snippets>
More information about the rkward-tracker
mailing list