aboutsummaryrefslogtreecommitdiff
path: root/renderer.js
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2018-02-10 23:35:10 +0000
committerJames Barnett <noreply@jamesbarnett.xyz>2018-02-10 23:35:10 +0000
commit7761eafe0d32042ccb04440c3761c42ed02237d0 (patch)
tree4e5157c7036cdc0c067458590c222832888ff415 /renderer.js
parentcd9be261e3d54070845c21b84bfc20891cabde31 (diff)
downloadsql-plus-plus-7761eafe0d32042ccb04440c3761c42ed02237d0.tar.xz
sql-plus-plus-7761eafe0d32042ccb04440c3761c42ed02237d0.zip
Add statement parsing and highlighting
Diffstat (limited to 'renderer.js')
-rw-r--r--renderer.js87
1 files changed, 81 insertions, 6 deletions
diff --git a/renderer.js b/renderer.js
index b7da806..946d6bb 100644
--- a/renderer.js
+++ b/renderer.js
@@ -8,30 +8,107 @@ require("codemirror/mode/sql/sql");
const Split = require("split.js");
const editorContext = cm(document.getElementById("editor"), {
- value: "select *\nfrom information_schema.tables",
+ value: "select *\nfrom information_schema.tables\n/\nselect now()\n/\nselect *\nfrom foo",
mode: "text/x-sql",
theme: "dracula",
- lineNumbers: true
+ lineNumbers: true,
+ gutters: ["CodeMirror-linenumbers", "statement-pointer"]
});
+const statementDelimiter = "/"
+
let dataTable;
let execStartTime;
let execTimerInterval;
let execElapsedTime;
+let queryMark;
function runQuery() {
_setExecutionStatusIndicator("RUNNING");
_startExecTimer();
- var query = editorContext.getValue();
- console.log(query);
+ let query = findQuery();
_destroyDataTable();
ipcRenderer.send("queryExecutor.runQuery", query);
}
+/**
+ * If there's selected text, return it. Otherwise find the statement nearest the cursor.
+ * Statements are delimited by lines containing only a "/" character.
+ */
+function findQuery() {
+ let selectedText = editorContext.getSelection();
+
+ if(selectedText !== "") {
+ _clearQueryMarks();
+ return selectedText;
+ }
+ else {
+ let cursorLine = editorContext.getCursor().line;
+
+ let statementStartLine = editorContext.firstLine();
+
+ // lineCount rather than lastLine here, since lineCount is index 1 based.
+ // getRange(from, to) below is 0 based, but the range is exclusive, so if we need to include the last line we need the +1
+ let statementEndLine = editorContext.lineCount();
+
+ // if the current line is a delimiter, thats the end of the statement
+ if (editorContext.getLine(cursorLine) === statementDelimiter) {
+ statementEndLine = cursorLine;
+ }
+ else {
+ // move down the document until a delimiter or the end of the document is reached
+ for (let i = cursorLine + 1; i <= editorContext.lastLine(); i++) {
+ if (editorContext.getLine(i) === statementDelimiter) {
+ statementEndLine = i;
+ break;
+ }
+ }
+ }
+
+ // mode up the document until a previous statements delimiter is found or the start of the document is reached
+ for(let i = cursorLine - 1; i >= editorContext.firstLine(); i--) {
+ if (editorContext.getLine(i) === statementDelimiter) {
+ statementStartLine = i + 1;
+ break;
+ }
+ }
+
+ let query = editorContext.getRange(
+ { line: statementStartLine, ch: 0 },
+ { line: statementEndLine, ch: 0 }
+ );
+
+ console.log(query);
+
+ _clearQueryMarks();
+
+ queryMark = editorContext.markText(
+ { line: statementStartLine, ch: 0 },
+ { line: statementEndLine, ch: 0 },
+ { className: "selected-statement" }
+ );
+
+ editorContext.setGutterMarker(statementStartLine, "statement-pointer", _createGutterMarkerDom());
+
+ return query;
+ }
+}
+
+function _createGutterMarkerDom() {
+ return $("<div> > </div>").get(0);
+}
+
+function _clearQueryMarks() {
+ if (queryMark) {
+ queryMark.clear();
+ }
+ editorContext.clearGutter("statement-pointer");
+}
+
ipcRenderer.on("queryExecutor.runQueryComplete", (event, response) => {
_stopExecTimer();
if(response.error === undefined) {
@@ -41,8 +118,6 @@ ipcRenderer.on("queryExecutor.runQueryComplete", (event, response) => {
handleError(response.error);
}
-
-
});
function _startExecTimer() {