Skip to content

Commit

Permalink
Merge pull request #102 from diegovdc/feature/code-block-compilation
Browse files Browse the repository at this point in the history
Code block compilation
  • Loading branch information
crucialfelix authored Apr 24, 2019
2 parents 7b1d593 + 1c33f02 commit a5e6d19
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions keymaps/supercollider.cson
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
55 changes: 55 additions & 0 deletions lib/controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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()

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"activationCommands": {
"atom-workspace": [
"supercollider:eval",
"supercollider:evalBlock",
"supercollider:open-post-window",
"supercollider:clear-post-window",
"supercollider:open-help-file",
Expand Down

0 comments on commit a5e6d19

Please sign in to comment.