From 1c33f0274d5175803daf6654f97ba17e95a1f068 Mon Sep 17 00:00:00 2001 From: Diego Date: Sat, 23 Mar 2019 00:26:56 -0600 Subject: [PATCH] [ADD] code block compilation --- keymaps/supercollider.cson | 2 ++ lib/controller.coffee | 55 ++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 58 insertions(+) diff --git a/keymaps/supercollider.cson b/keymaps/supercollider.cson index 71b6fbf..88d04e1 100644 --- a/keymaps/supercollider.cson +++ b/keymaps/supercollider.cson @@ -14,6 +14,7 @@ "atom-workspace atom-text-editor[data-grammar~='supercollider']": 'ctrl-alt-l': 'supercollider:open-post-window' 'shift-enter': 'supercollider:eval' + 'alt-enter': 'supercollider:evalBlock' 'alt-.': 'supercollider:cmd-period' 'shift-alt-k': 'supercollider:clear-post-window' 'ctrl-shift-h': 'supercollider:open-help-file' @@ -26,6 +27,7 @@ ".platform-darwin atom-workspace atom-text-editor[data-grammar~='supercollider']": 'cmd-\\': 'supercollider:open-post-window' 'shift-enter': 'supercollider:eval' + 'cmd-enter': 'supercollider:evalBlock' 'shift-cmd-k': 'supercollider:recompile' 'cmd-.': 'supercollider:cmd-period' 'shift-cmd-c': 'supercollider:clear-post-window' diff --git a/lib/controller.coffee b/lib/controller.coffee index 6fd2a43..d9e67d5 100755 --- a/lib/controller.coffee +++ b/lib/controller.coffee @@ -37,6 +37,8 @@ class Controller 'supercollider:kill-all-servers', => @killAllServers() atom.commands.add 'atom-workspace', 'supercollider:eval', => @eval() + atom.commands.add 'atom-workspace', + 'supercollider:evalBlock', => @evalBlock() atom.commands.add 'atom-workspace', 'supercollider:open-help-file', => @openHelpFile() atom.commands.add 'atom-workspace', @@ -206,6 +208,59 @@ class Controller [expression, range] = @currentExpression() @evalWithRepl(expression, @currentPath(), range) + # Takes an editor and the position of a brace found in scanning and Determines + # if the brace found at that position can be ignored. If the brace is in a + # comment or inside a string it can be ignored. + isIgnorableBrace: (editor, pos)-> + scopes = editor.scopeDescriptorForBufferPosition(pos).scopes + scopes.indexOf("string.quoted.double.supercollider") >= 0 || + scopes.indexOf("comment.single.supercollider") >= 0 || + scopes.indexOf("comment.multiline.supercollider") >= 0 || + scopes.indexOf("entity.name.symbol.supercollider") >= 0 + + # Constructs a list of `Range`s, one for each top level form. + getTopLevelRanges: (editor) -> + ranges = [] + braceOpened = 0 + inTopLevelComment = false + rex = /[\{\}\[\]\(\)]/g + editor.scan rex, (result) => + if !(@isIgnorableBrace(editor, result.range.start)) + matchesComment = result.matchText.match(/^\(comment\s/) + if matchesComment and braceOpened == 0 + inTopLevelComment = true + c = ""+result.match[0] + if ["(","{","["].indexOf(c) >= 0 or matchesComment + if (braceOpened == 1 and inTopLevelComment == true) or + (braceOpened == 0 and inTopLevelComment == false) + ranges.push([result.range.start]) + braceOpened++ + else if [")","}","]"].indexOf(c) >= 0 + braceOpened-- + if (braceOpened == 1 and inTopLevelComment == true) or + (braceOpened == 0 and inTopLevelComment == false) + ranges[ranges.length - 1].push(result.range.end) + if braceOpened == 0 and inTopLevelComment == true + inTopLevelComment = false + ranges + .filter((range) -> range.length == 2) + .map((range) -> Range.fromObject(range)) + + getCursorInTopBlockRange: (editor)-> + pos = editor.getCursorBufferPosition() + topLevelRanges = @getTopLevelRanges(editor) + topLevelRanges.find (range) -> range.containsPoint(pos) + + evalBlock: -> + return unless @editorIsSC() + editor = atom.workspace.getActiveTextEditor() + try + range = @getCursorInTopBlockRange(editor) + expression = editor.getTextInBufferRange(range).trim() + @evalWithRepl(expression, @currentPath(), range) + catch e + @eval() + evalWithRepl: (expression, path, range) -> @destroyMarkers() diff --git a/package.json b/package.json index 2cdb8ea..36fbb01 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "activationCommands": { "atom-workspace": [ "supercollider:eval", + "supercollider:evalBlock", "supercollider:open-post-window", "supercollider:clear-post-window", "supercollider:open-help-file",