"use strict"; const { ipcRenderer } = require('electron'); const $ = require("jquery"); const cm = require("codemirror"); require("datatables")(window, $); require("codemirror/mode/sql/sql"); const Split = require("split.js"); const editorContext = cm(document.getElementById("editor"), { value: "select *\nfrom information_schema.tables\n/\nselect now()\n/\nselect *\nfrom foo", mode: "text/x-sql", theme: "dracula", lineNumbers: true, gutters: ["CodeMirror-linenumbers", "statement-pointer"] }); editorContext.on("cursorActivity", (instance) => { let coords = instance.getCursor(); $("#cursor-coords").text("Ln " + (parseInt(coords.line)+1) + ", Col " + (parseInt(coords.ch)+1)); }); const statementDelimiter = "/"; let dataTable; let execStartTime; let execTimerInterval; let execElapsedTime; let queryMark; function runQuery() { _setExecutionStatusIndicator("RUNNING"); _startExecTimer(); 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 $("