2014-01-03 13:32:13 -05:00
|
|
|
View = require 'views/kinds/CocoView'
|
|
|
|
template = require 'templates/play/level/tome/spell'
|
|
|
|
{me} = require 'lib/auth'
|
|
|
|
filters = require 'lib/image_filter'
|
|
|
|
Range = ace.require("ace/range").Range
|
|
|
|
Problem = require './problem'
|
2014-01-21 12:03:04 -05:00
|
|
|
SpellDebugView = require './spell_debug_view'
|
|
|
|
SpellToolbarView = require './spell_toolbar_view'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
module.exports = class SpellView extends View
|
|
|
|
id: 'spell-view'
|
|
|
|
className: 'shown'
|
|
|
|
template: template
|
|
|
|
controlsEnabled: true
|
|
|
|
eventsSuppressed: true
|
|
|
|
writable: true
|
|
|
|
|
2014-03-15 18:14:57 -04:00
|
|
|
editModes:
|
|
|
|
'javascript': 'ace/mode/javascript'
|
2014-03-28 09:42:08 -04:00
|
|
|
'coffeescript': 'ace/mode/coffee'
|
2014-03-15 18:14:57 -04:00
|
|
|
|
2014-03-13 21:49:58 -04:00
|
|
|
keyBindings:
|
|
|
|
'default': null
|
|
|
|
'vim': 'ace/keyboard/vim'
|
|
|
|
'emacs': 'ace/keyboard/emacs'
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
subscriptions:
|
|
|
|
'level-disable-controls': 'onDisableControls'
|
|
|
|
'level-enable-controls': 'onEnableControls'
|
|
|
|
'surface:frame-changed': 'onFrameChanged'
|
2014-03-12 20:50:59 -04:00
|
|
|
'surface:coordinate-selected': 'onCoordinateSelected'
|
2014-01-03 13:32:13 -05:00
|
|
|
'god:new-world-created': 'onNewWorld'
|
|
|
|
'god:user-code-problem': 'onUserCodeProblem'
|
|
|
|
'tome:manual-cast': 'onManualCast'
|
|
|
|
'tome:reload-code': 'onCodeReload'
|
2014-01-15 14:58:39 -05:00
|
|
|
'tome:spell-changed': 'onSpellChanged'
|
2014-01-16 11:13:14 -05:00
|
|
|
'level:session-will-save': 'onSessionWillSave'
|
2014-01-03 13:32:13 -05:00
|
|
|
'modal-closed': 'focus'
|
|
|
|
'focus-editor': 'focus'
|
2014-01-21 12:03:04 -05:00
|
|
|
'tome:spell-statement-index-updated': 'onStatementIndexUpdated'
|
2014-03-16 21:14:04 -04:00
|
|
|
'tome:change-language': 'onChangeLanguage'
|
|
|
|
'tome:change-config': 'onChangeEditorConfig'
|
2014-02-22 19:58:54 -05:00
|
|
|
'spell-beautify': 'onSpellBeautify'
|
2014-01-16 13:10:27 -05:00
|
|
|
|
2014-01-07 00:25:18 -05:00
|
|
|
events:
|
2014-01-21 12:03:04 -05:00
|
|
|
'mouseout': 'onMouseOut'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
|
|
|
@session = options.session
|
2014-03-24 12:58:34 -04:00
|
|
|
@listenTo(@session, 'change:multiplayer', @onMultiplayerChanged)
|
2014-01-03 13:32:13 -05:00
|
|
|
@spell = options.spell
|
|
|
|
@problems = {}
|
|
|
|
@writable = false unless me.team in @spell.permissions.readwrite # TODO: make this do anything
|
|
|
|
@highlightCurrentLine = _.throttle @highlightCurrentLine, 100
|
|
|
|
|
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
@createACE()
|
|
|
|
@createACEShortcuts()
|
|
|
|
@fillACE()
|
2014-02-22 15:01:05 -05:00
|
|
|
if @session.get('multiplayer')
|
2014-01-03 13:32:13 -05:00
|
|
|
@createFirepad()
|
|
|
|
else
|
|
|
|
# needs to happen after the code generating this view is complete
|
|
|
|
setTimeout @onLoaded, 1
|
|
|
|
|
|
|
|
createACE: ->
|
|
|
|
# Test themes and settings here: http://ace.ajax.org/build/kitchen-sink.html
|
2014-03-14 10:46:36 -04:00
|
|
|
aceConfig = me.get('aceConfig') ? {}
|
2014-01-03 13:32:13 -05:00
|
|
|
@ace = ace.edit @$el.find('.ace')[0]
|
|
|
|
@aceSession = @ace.getSession()
|
|
|
|
@aceDoc = @aceSession.getDocument()
|
|
|
|
@aceSession.setUseWorker false
|
2014-03-15 18:14:57 -04:00
|
|
|
@aceSession.setMode @editModes[aceConfig.language ? 'javascript']
|
2014-01-03 13:32:13 -05:00
|
|
|
@aceSession.setWrapLimitRange null
|
|
|
|
@aceSession.setUseWrapMode true
|
2014-01-04 19:41:25 -05:00
|
|
|
@aceSession.setNewLineMode "unix"
|
|
|
|
@aceSession.setUseSoftTabs true
|
2014-01-03 13:32:13 -05:00
|
|
|
@ace.setTheme 'ace/theme/textmate'
|
2014-03-14 13:14:48 -04:00
|
|
|
@ace.setDisplayIndentGuides aceConfig.indentGuides
|
2014-01-03 13:32:13 -05:00
|
|
|
@ace.setShowPrintMargin false
|
2014-03-14 13:14:48 -04:00
|
|
|
@ace.setShowInvisibles aceConfig.invisibles
|
|
|
|
@ace.setBehavioursEnabled aceConfig.behaviors
|
2014-01-24 11:01:00 -05:00
|
|
|
@ace.setAnimatedScroll true
|
2014-03-14 13:14:48 -04:00
|
|
|
@ace.setKeyboardHandler @keyBindings[aceConfig.keyBindings ? 'default']
|
2014-01-03 13:32:13 -05:00
|
|
|
@toggleControls null, @writable
|
|
|
|
@aceSession.selection.on 'changeCursor', @onCursorActivity
|
|
|
|
$(@ace.container).find('.ace_gutter').on 'click', '.ace_error, .ace_warning, .ace_info', @onAnnotationClick
|
|
|
|
|
|
|
|
createACEShortcuts: ->
|
2014-02-12 16:43:17 -05:00
|
|
|
@aceCommands = aceCommands = []
|
|
|
|
ace = @ace
|
|
|
|
addCommand = (c) ->
|
|
|
|
ace.commands.addCommand c
|
|
|
|
aceCommands.push c.name
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-03 13:32:13 -05:00
|
|
|
name: 'run-code'
|
|
|
|
bindKey: {win: 'Shift-Enter|Ctrl-Enter|Ctrl-S', mac: 'Shift-Enter|Command-Enter|Ctrl-Enter|Command-S|Ctrl-S'}
|
2014-02-12 16:43:17 -05:00
|
|
|
exec: -> Backbone.Mediator.publish 'tome:manual-cast', {}
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-03 13:32:13 -05:00
|
|
|
name: 'toggle-playing'
|
|
|
|
bindKey: {win: 'Ctrl-P', mac: 'Command-P|Ctrl-P'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'level-toggle-playing'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-03 13:32:13 -05:00
|
|
|
name: 'end-current-script'
|
|
|
|
bindKey: {win: 'Shift-Space', mac: 'Shift-Space'}
|
2014-03-12 13:01:31 -04:00
|
|
|
passEvent: true # https://github.com/ajaxorg/ace/blob/master/lib/ace/keyboard/keybinding.js#L114
|
|
|
|
# No easy way to selectively cancel shift+space, since we don't get access to the event.
|
|
|
|
# Maybe we could temporarily set ourselves to read-only if we somehow know that a script is active?
|
2014-01-03 13:32:13 -05:00
|
|
|
exec: -> Backbone.Mediator.publish 'level:shift-space-pressed'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-03 13:32:13 -05:00
|
|
|
name: 'end-all-scripts'
|
|
|
|
bindKey: {win: 'Escape', mac: 'Escape'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'level:escape-pressed'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-21 12:03:04 -05:00
|
|
|
name: 'toggle-grid'
|
|
|
|
bindKey: {win: 'Ctrl-G', mac: 'Command-G|Ctrl-G'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'level-toggle-grid'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-21 12:03:04 -05:00
|
|
|
name: 'toggle-debug'
|
|
|
|
bindKey: {win: 'Ctrl-\\', mac: 'Command-\\|Ctrl-\\'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'level-toggle-debug'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-31 19:32:46 -05:00
|
|
|
name: 'toggle-pathfinding'
|
|
|
|
bindKey: {win: 'Ctrl-O', mac: 'Command-O|Ctrl-O'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'level-toggle-pathfinding'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-21 12:03:04 -05:00
|
|
|
name: 'level-scrub-forward'
|
|
|
|
bindKey: {win: 'Ctrl-]', mac: 'Command-]|Ctrl-]'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'level-scrub-forward'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-21 12:03:04 -05:00
|
|
|
name: 'level-scrub-back'
|
|
|
|
bindKey: {win: 'Ctrl-[', mac: 'Command-[|Ctrl-]'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'level-scrub-back'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-21 12:03:04 -05:00
|
|
|
name: 'spell-step-forward'
|
|
|
|
bindKey: {win: 'Ctrl-Alt-]', mac: 'Command-Alt-]|Ctrl-Alt-]'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'spell-step-forward'
|
2014-02-12 15:41:41 -05:00
|
|
|
addCommand
|
2014-01-21 12:03:04 -05:00
|
|
|
name: 'spell-step-backward'
|
|
|
|
bindKey: {win: 'Ctrl-Alt-[', mac: 'Command-Alt-[|Ctrl-Alt-]'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'spell-step-backward'
|
2014-02-22 19:58:54 -05:00
|
|
|
addCommand
|
|
|
|
name: 'spell-beautify'
|
|
|
|
bindKey: {win: 'Ctrl-Shift-B', mac: 'Command-Shift-B|Ctrl-Shift-B'}
|
|
|
|
exec: -> Backbone.Mediator.publish 'spell-beautify'
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
fillACE: ->
|
|
|
|
@ace.setValue @spell.source
|
|
|
|
@ace.clearSelection()
|
|
|
|
|
2014-02-11 18:38:36 -05:00
|
|
|
onMultiplayerChanged: ->
|
2014-02-22 15:01:05 -05:00
|
|
|
if @session.get('multiplayer')
|
2014-01-03 13:32:13 -05:00
|
|
|
@createFirepad()
|
|
|
|
else
|
|
|
|
@firepad?.dispose()
|
|
|
|
|
|
|
|
createFirepad: ->
|
|
|
|
# load from firebase or the original source if there's nothing there
|
|
|
|
return if @firepadLoading
|
|
|
|
@eventsSuppressed = true
|
|
|
|
@loaded = false
|
|
|
|
@previousSource = @ace.getValue()
|
|
|
|
@ace.setValue('')
|
|
|
|
fireURL = 'https://codecombat.firebaseio.com/' + @spell.pathComponents.join('/')
|
|
|
|
@fireRef = new Firebase fireURL
|
|
|
|
firepadOptions = userId: me.id
|
|
|
|
@firepad = Firepad.fromACE @fireRef, @ace, firepadOptions
|
|
|
|
@firepad.on 'ready', @onFirepadLoaded
|
|
|
|
@firepadLoading = true
|
|
|
|
|
|
|
|
onFirepadLoaded: =>
|
|
|
|
@firepadLoading = false
|
|
|
|
firepadSource = @ace.getValue()
|
|
|
|
if firepadSource
|
|
|
|
@spell.source = firepadSource
|
|
|
|
else
|
|
|
|
@ace.setValue @previousSource
|
|
|
|
@ace.clearSelection()
|
|
|
|
@onLoaded()
|
|
|
|
|
|
|
|
onLoaded: =>
|
|
|
|
@spell.transpile @spell.source
|
|
|
|
@spell.loaded = true
|
|
|
|
Backbone.Mediator.publish 'tome:spell-loaded', spell: @spell
|
|
|
|
@eventsSuppressed = false # Now that the initial change is in, we can start running any changed code
|
2014-01-25 18:11:29 -05:00
|
|
|
@createToolbarView()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-01-19 10:08:28 -05:00
|
|
|
createDebugView: ->
|
2014-01-24 16:03:04 -05:00
|
|
|
@debugView = new SpellDebugView ace: @ace, thang: @thang
|
2014-01-19 10:08:28 -05:00
|
|
|
@$el.append @debugView.render().$el.hide()
|
|
|
|
|
2014-01-21 12:03:04 -05:00
|
|
|
createToolbarView: ->
|
|
|
|
@toolbarView = new SpellToolbarView ace: @ace
|
2014-01-25 18:11:29 -05:00
|
|
|
@$el.append @toolbarView.render().$el
|
2014-01-21 12:03:04 -05:00
|
|
|
|
|
|
|
onMouseOut: (e) ->
|
|
|
|
@debugView.onMouseOut e
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
getSource: ->
|
|
|
|
@ace.getValue() # could also do @firepad.getText()
|
|
|
|
|
|
|
|
setThang: (thang) ->
|
|
|
|
@focus()
|
|
|
|
return if thang.id is @thang?.id
|
|
|
|
@thang = thang
|
|
|
|
@spellThang = @spell.thangs[@thang.id]
|
2014-01-24 16:03:04 -05:00
|
|
|
@createDebugView() unless @debugView
|
|
|
|
@debugView.thang = @thang
|
2014-01-31 19:16:59 -05:00
|
|
|
@toolbarView?.toggleFlow false
|
2014-01-03 13:32:13 -05:00
|
|
|
@updateAether false, true
|
|
|
|
@highlightCurrentLine()
|
|
|
|
|
|
|
|
cast: ->
|
|
|
|
Backbone.Mediator.publish 'tome:cast-spell', spell: @spell, thang: @thang
|
|
|
|
|
|
|
|
notifySpellChanged: =>
|
|
|
|
Backbone.Mediator.publish 'tome:spell-changed', spell: @spell
|
|
|
|
|
|
|
|
notifyEditingEnded: =>
|
|
|
|
return if @aceDoc.undergoingFirepadOperation # from my Firepad ACE adapter
|
|
|
|
Backbone.Mediator.publish('tome:editing-ended')
|
|
|
|
|
|
|
|
notifyEditingBegan: =>
|
|
|
|
return if @aceDoc.undergoingFirepadOperation # from my Firepad ACE adapter
|
|
|
|
Backbone.Mediator.publish('tome:editing-began')
|
|
|
|
|
|
|
|
onManualCast: (e) ->
|
|
|
|
cast = @$el.parent().length
|
|
|
|
@recompile cast
|
|
|
|
@focus() if cast
|
|
|
|
|
|
|
|
onCodeReload: (e) ->
|
|
|
|
return unless e.spell is @spell
|
|
|
|
@reloadCode true
|
|
|
|
|
|
|
|
reloadCode: (cast=true) ->
|
|
|
|
@updateACEText @spell.originalSource
|
|
|
|
@recompile cast
|
|
|
|
|
2014-02-12 15:41:41 -05:00
|
|
|
recompileIfNeeded: =>
|
|
|
|
@recompile() if @recompileNeeded
|
|
|
|
|
2014-02-12 16:43:17 -05:00
|
|
|
recompile: (cast=true) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
@setRecompileNeeded false
|
|
|
|
return if @spell.source is @getSource()
|
|
|
|
@spell.transpile @getSource()
|
|
|
|
@updateAether true, false
|
|
|
|
@cast() if cast
|
|
|
|
@notifySpellChanged()
|
|
|
|
|
|
|
|
updateACEText: (source) ->
|
|
|
|
@eventsSuppressed = true
|
|
|
|
if @firepad
|
|
|
|
@firepad.setText source
|
|
|
|
else
|
|
|
|
@ace.setValue source
|
|
|
|
@eventsSuppressed = false
|
|
|
|
@ace.resize true # hack: @ace may not have updated its text properly, so we force it to refresh
|
|
|
|
|
|
|
|
# Called from CastButtonView initially and whenever the delay is changed
|
|
|
|
setAutocastDelay: (@autocastDelay) ->
|
|
|
|
@createOnCodeChangeHandlers()
|
|
|
|
|
|
|
|
createOnCodeChangeHandlers: ->
|
|
|
|
@aceDoc.removeListener 'change', @onCodeChangeMetaHandler if @onCodeChangeMetaHandler
|
|
|
|
autocastDelay = @autocastDelay ? 3000
|
|
|
|
onSignificantChange = [
|
|
|
|
_.debounce @setRecompileNeeded, autocastDelay - 100
|
2014-02-12 15:41:41 -05:00
|
|
|
@currentAutocastHandler = _.debounce @recompileIfNeeded, autocastDelay
|
2014-01-03 13:32:13 -05:00
|
|
|
]
|
|
|
|
onAnyChange = [
|
|
|
|
_.debounce @updateAether, 500
|
|
|
|
_.debounce @notifyEditingEnded, 1000
|
|
|
|
_.throttle @notifyEditingBegan, 250
|
|
|
|
_.throttle @notifySpellChanged, 300
|
|
|
|
]
|
|
|
|
@onCodeChangeMetaHandler = =>
|
|
|
|
return if @eventsSuppressed
|
2014-02-08 18:32:14 -05:00
|
|
|
if not @spellThang or @spell.hasChangedSignificantly @getSource(), @spellThang.aether.raw
|
2014-01-03 13:32:13 -05:00
|
|
|
callback() for callback in onSignificantChange # Do these first
|
|
|
|
callback() for callback in onAnyChange # Then these
|
|
|
|
@aceDoc.on 'change', @onCodeChangeMetaHandler
|
|
|
|
|
|
|
|
setRecompileNeeded: (needed=true) =>
|
|
|
|
if needed
|
|
|
|
@recompileNeeded = needed # and @recompileValid # todo, remove if not caring about validity
|
|
|
|
else
|
|
|
|
@recompileNeeded = false
|
|
|
|
|
|
|
|
onCursorActivity: =>
|
|
|
|
@currentAutocastHandler?()
|
|
|
|
|
|
|
|
# Design for a simpler system?
|
|
|
|
# * Turn off ACE's JSHint worker
|
|
|
|
# * Keep Aether linting, debounced, on any significant change
|
|
|
|
# - Don't send runtime errors from in-progress worlds
|
|
|
|
# - All problems just vanish when you make any change to the code
|
|
|
|
# * You wouldn't accept any Aether updates/runtime information/errors unless its code was current when you got it
|
|
|
|
# * Store the last run Aether in each spellThang and use it whenever its code actually is current
|
|
|
|
# This suffers from the problem that any whitespace/comment changes will lose your info, but what else
|
|
|
|
# could you do other than somehow maintain a mapping from current to original code locations?
|
|
|
|
# I guess you could use dynamic markers for problem ranges and keep annotations/alerts in when insignificant
|
|
|
|
# changes happen, but always treat any change in the (trimmed) number of lines as a significant change.
|
|
|
|
# Ooh, that's pretty nice. Gets you most of the way there and is simple.
|
|
|
|
# - All problems have a master representation as a Problem, and we can easily generate all Problems from
|
|
|
|
# any Aether instance. Then when we switch contexts in any way, we clear, recreate, and reapply the Problems.
|
|
|
|
# * Problem alerts will have their own templated ProblemAlertViews
|
|
|
|
# * We'll only show the first problem alert, and it will always be at the bottom.
|
|
|
|
# Annotations and problem ranges can show all, I guess.
|
|
|
|
# * The editor will reserve space for one annotation as a codeless area.
|
|
|
|
# - Problem alerts and ranges will only show on fully cast worlds. Annotations will show continually.
|
|
|
|
|
|
|
|
updateAether: (force=false, fromCodeChange=true) =>
|
|
|
|
# Depending on whether we have any code changes, significant code changes, or have switched
|
|
|
|
# to a new spellThang, we may want to refresh our Aether display.
|
|
|
|
return unless aether = @spellThang?.aether
|
|
|
|
source = @getSource()
|
|
|
|
codeHasChangedSignificantly = force or @spell.hasChangedSignificantly source, aether.raw
|
2014-02-03 16:58:25 -05:00
|
|
|
needsUpdate = codeHasChangedSignificantly or @spellThang isnt @lastUpdatedAetherSpellThang
|
|
|
|
return if not needsUpdate and aether is @displayedAether
|
2014-01-03 13:32:13 -05:00
|
|
|
castAether = @spellThang.castAether
|
|
|
|
codeIsAsCast = castAether and not @spell.hasChangedSignificantly source, castAether.raw
|
|
|
|
aether = castAether if codeIsAsCast
|
2014-02-03 16:58:25 -05:00
|
|
|
return if not needsUpdate and aether is @displayedAether
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# Now that that's figured out, perform the update.
|
|
|
|
@clearAetherDisplay()
|
|
|
|
aether.transpile source if codeHasChangedSignificantly and not codeIsAsCast
|
|
|
|
@displayAether aether
|
|
|
|
@lastUpdatedAetherSpellThang = @spellThang
|
|
|
|
@guessWhetherFinished aether if fromCodeChange
|
|
|
|
|
|
|
|
clearAetherDisplay: ->
|
|
|
|
problem.destroy() for problem in @problems
|
|
|
|
@problems = []
|
|
|
|
@aceSession.setAnnotations []
|
|
|
|
@highlightCurrentLine {} # This'll remove all highlights
|
|
|
|
|
|
|
|
displayAether: (aether) ->
|
2014-02-03 16:58:25 -05:00
|
|
|
@displayedAether = aether
|
2014-01-03 13:32:13 -05:00
|
|
|
isCast = not _.isEmpty(aether.metrics) or _.some aether.problems.errors, {type: 'runtime'}
|
|
|
|
@problems = []
|
|
|
|
annotations = []
|
2014-02-24 17:12:41 -05:00
|
|
|
seenProblemKeys = {}
|
2014-01-03 13:32:13 -05:00
|
|
|
for aetherProblem, problemIndex in aether.getAllProblems()
|
2014-02-27 23:01:27 -05:00
|
|
|
continue if key = aetherProblem.userInfo?.key and key of seenProblemKeys
|
|
|
|
seenProblemKeys[key] = true if key
|
2014-01-03 13:32:13 -05:00
|
|
|
@problems.push problem = new Problem aether, aetherProblem, @ace, isCast and problemIndex is 0, isCast
|
|
|
|
annotations.push problem.annotation if problem.annotation
|
|
|
|
@aceSession.setAnnotations annotations
|
|
|
|
@highlightCurrentLine aether.flow unless _.isEmpty aether.flow
|
|
|
|
#console.log ' and we could do the metrics', aether.metrics unless _.isEmpty aether.metrics
|
|
|
|
#console.log ' and we could do the style', aether.style unless _.isEmpty aether.style
|
|
|
|
#console.log ' and we could do the visualization', aether.visualization unless _.isEmpty aether.visualization
|
|
|
|
# Could use the user-code-problem style... or we could leave that to other places.
|
|
|
|
@ace[if @problems.length then 'setStyle' else 'unsetStyle'] 'user-code-problem'
|
2014-02-22 21:02:58 -05:00
|
|
|
@ace[if isCast then 'setStyle' else 'unsetStyle'] 'spell-cast'
|
2014-01-03 13:32:13 -05:00
|
|
|
Backbone.Mediator.publish 'tome:problems-updated', spell: @spell, problems: @problems, isCast: isCast
|
2014-02-22 19:58:54 -05:00
|
|
|
@ace.resize()
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
# Autocast:
|
|
|
|
# Goes immediately if the code is a) changed and b) complete/valid and c) the cursor is at beginning or end of a line
|
|
|
|
# We originall thought it would:
|
|
|
|
# - Go after specified delay if a) and b) but not c)
|
|
|
|
# - Go only when manually cast or deselecting a Thang when there are errors
|
|
|
|
# But the error message display was delayed, so now trying:
|
|
|
|
# - Go after specified delay if a) and not b) or c)
|
|
|
|
guessWhetherFinished: (aether) ->
|
|
|
|
return if @autocastDelay > 60000
|
|
|
|
#@recompileValid = not aether.getAllProblems().length
|
|
|
|
valid = not aether.getAllProblems().length
|
|
|
|
cursorPosition = @ace.getCursorPosition()
|
2014-02-23 14:23:26 -05:00
|
|
|
currentLine = _.string.rtrim(@aceDoc.$lines[cursorPosition.row].replace(/[ \t]*\/\/[^"']*/g, '')) # trim // unless inside "
|
2014-01-03 13:32:13 -05:00
|
|
|
endOfLine = cursorPosition.column >= currentLine.length # just typed a semicolon or brace, for example
|
|
|
|
beginningOfLine = not currentLine.substr(0, cursorPosition.column).trim().length # uncommenting code, for example
|
|
|
|
#console.log "finished?", valid, endOfLine, beginningOfLine, cursorPosition, currentLine.length, aether, new Date() - 0, currentLine
|
|
|
|
if valid and endOfLine or beginningOfLine
|
|
|
|
@recompile()
|
|
|
|
#console.log "recompile now!"
|
|
|
|
#else if not valid
|
|
|
|
# # if this works, we can get rid of all @recompileValid logic
|
|
|
|
# console.log "not valid, but so we'll wait to do it in", @autocastDelay + "ms"
|
|
|
|
#else
|
|
|
|
# console.log "valid but not at end of line; recompile in", @autocastDelay + "ms"
|
|
|
|
|
2014-01-15 14:58:39 -05:00
|
|
|
onSpellChanged: (e) ->
|
2014-01-16 11:13:14 -05:00
|
|
|
@spellHasChanged = true
|
|
|
|
|
|
|
|
onSessionWillSave: (e) ->
|
2014-01-25 18:11:29 -05:00
|
|
|
return unless @spellHasChanged
|
2014-01-16 13:10:27 -05:00
|
|
|
setTimeout(=>
|
|
|
|
unless @spellHasChanged
|
|
|
|
@$el.find('.save-status').finish().show().fadeOut(2000)
|
|
|
|
, 1000)
|
2014-01-16 11:13:14 -05:00
|
|
|
@spellHasChanged = false
|
2014-01-15 14:58:39 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onUserCodeProblem: (e) ->
|
|
|
|
return @onInfiniteLoop e if e.problem.id is "runtime_InfiniteLoop"
|
|
|
|
return unless e.problem.userInfo.methodName is @spell.name
|
|
|
|
return unless spellThang = _.find @spell.thangs, (spellThang, thangID) -> thangID is e.problem.userInfo.thangID
|
|
|
|
return if @spell.hasChangedSignificantly @getSource() # don't show this error if we've since edited the code
|
|
|
|
spellThang.aether.addProblem e.problem
|
|
|
|
@lastUpdatedAetherSpellThang = null # force a refresh without a re-transpile
|
|
|
|
@updateAether false, false
|
|
|
|
|
|
|
|
onInfiniteLoop: (e) ->
|
|
|
|
return unless @spellThang
|
|
|
|
@spellThang.aether.addProblem e.problem
|
|
|
|
@lastUpdatedAetherSpellThang = null # force a refresh without a re-transpile
|
|
|
|
@updateAether false, false
|
|
|
|
|
|
|
|
onNewWorld: (e) ->
|
2014-02-05 18:16:59 -05:00
|
|
|
@spell.removeThangID thangID for thangID of @spell.thangs when not e.world.getThangByID thangID
|
2014-01-03 13:32:13 -05:00
|
|
|
for thangID, spellThang of @spell.thangs
|
2014-02-05 18:16:59 -05:00
|
|
|
thang = e.world.getThangByID(thangID)
|
|
|
|
aether = e.world.userCodeMap[thangID]?[@spell.name] # Might not be there if this is a new Programmable Thang.
|
2014-01-03 13:32:13 -05:00
|
|
|
spellThang.castAether = aether
|
2014-02-05 18:16:59 -05:00
|
|
|
spellThang.aether = @spell.createAether thang
|
2014-02-03 16:58:25 -05:00
|
|
|
#console.log thangID, @spell.spellKey, "ran", aether.metrics.callsExecuted, "times over", aether.metrics.statementsExecuted, "statements, with max recursion depth", aether.metrics.maxDepth, "and full flow/metrics", aether.metrics, aether.flow
|
2014-01-03 13:32:13 -05:00
|
|
|
@spell.transpile()
|
|
|
|
@updateAether false, false
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
focus: ->
|
|
|
|
# TODO: it's a hack checking if a modal is visible; the events should be removed somehow
|
|
|
|
# but this view is not part of the normal subview destroying because of how it's swapped
|
|
|
|
return unless @controlsEnabled and @writable and $('.modal:visible').length is 0
|
|
|
|
return if @ace.isFocused()
|
|
|
|
@ace.focus()
|
|
|
|
@ace.clearSelection()
|
|
|
|
|
|
|
|
onFrameChanged: (e) ->
|
2014-03-13 18:35:28 -04:00
|
|
|
return unless @spellThang and e.selectedThang?.id is @spellThang?.thang.id
|
2014-01-03 13:32:13 -05:00
|
|
|
@thang = e.selectedThang # update our thang to the current version
|
|
|
|
@highlightCurrentLine()
|
|
|
|
|
2014-03-12 20:50:59 -04:00
|
|
|
onCoordinateSelected: (e) ->
|
|
|
|
return unless e.x? and e.y?
|
|
|
|
@ace.insert "{x: #{e.x}, y: #{e.y}}"
|
2014-03-13 19:52:18 -04:00
|
|
|
@highlightCurrentLine()
|
2014-03-12 20:50:59 -04:00
|
|
|
|
2014-01-21 12:03:04 -05:00
|
|
|
onStatementIndexUpdated: (e) ->
|
|
|
|
return unless e.ace is @ace
|
|
|
|
@highlightCurrentLine()
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
highlightCurrentLine: (flow) =>
|
2014-01-21 12:03:04 -05:00
|
|
|
# TODO: move this whole thing into SpellDebugView or somewhere?
|
2014-02-20 20:21:15 -05:00
|
|
|
@highlightComments() unless @destroyed
|
2014-01-03 13:32:13 -05:00
|
|
|
flow ?= @spellThang?.castAether?.flow
|
|
|
|
return unless flow
|
|
|
|
executed = []
|
2014-03-11 18:47:27 -04:00
|
|
|
executedRows = {}
|
2014-01-03 13:32:13 -05:00
|
|
|
matched = false
|
2014-01-21 12:03:04 -05:00
|
|
|
states = flow.states ? []
|
|
|
|
currentCallIndex = null
|
|
|
|
for callState, callNumber in states
|
|
|
|
if not currentCallIndex? and callState.userInfo?.time > @thang.world.age
|
|
|
|
currentCallIndex = callNumber - 1
|
2014-01-03 13:32:13 -05:00
|
|
|
if matched
|
|
|
|
executed.pop()
|
|
|
|
break
|
|
|
|
executed.push []
|
2014-01-19 10:08:28 -05:00
|
|
|
for state, statementNumber in callState.statements
|
2014-01-03 13:32:13 -05:00
|
|
|
if state.userInfo?.time > @thang.world.age
|
|
|
|
matched = true
|
|
|
|
break
|
|
|
|
_.last(executed).push state
|
2014-03-11 18:47:27 -04:00
|
|
|
executedRows[state.range[0].row] = true
|
2014-01-03 13:32:13 -05:00
|
|
|
#state.executing = true if state.userInfo?.time is @thang.world.age # no work
|
2014-01-21 12:03:04 -05:00
|
|
|
currentCallIndex ?= callNumber - 1
|
|
|
|
#console.log "got call index", currentCallIndex, "for time", @thang.world.age, "out of", states.length
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-03-11 20:26:11 -04:00
|
|
|
@decoratedGutter = @decoratedGutter || {}
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
# TODO: don't redo the markers if they haven't actually changed
|
|
|
|
for markerRange in (@markerRanges ?= [])
|
|
|
|
markerRange.start.detach()
|
|
|
|
markerRange.end.detach()
|
|
|
|
@aceSession.removeMarker markerRange.id
|
|
|
|
@markerRanges = []
|
2014-03-11 18:47:27 -04:00
|
|
|
for row in [0 ... @aceSession.getLength()]
|
|
|
|
unless executedRows[row]
|
|
|
|
@aceSession.removeGutterDecoration row, 'executing'
|
|
|
|
@aceSession.removeGutterDecoration row, 'executed'
|
|
|
|
@decoratedGutter[row] = ''
|
2014-01-25 18:11:29 -05:00
|
|
|
if not executed.length or (@spell.name is "plan" and @spellThang.castAether.metrics.statementsExecuted < 20)
|
|
|
|
@toolbarView?.toggleFlow false
|
2014-02-24 11:59:50 -05:00
|
|
|
@debugView.setVariableStates {}
|
2014-01-21 12:03:04 -05:00
|
|
|
return
|
2014-01-03 13:32:13 -05:00
|
|
|
lastExecuted = _.last executed
|
2014-01-25 18:11:29 -05:00
|
|
|
@toolbarView?.toggleFlow true
|
2014-01-21 12:03:04 -05:00
|
|
|
statementIndex = Math.max 0, lastExecuted.length - 1
|
|
|
|
@toolbarView?.setCallState states[currentCallIndex], statementIndex, currentCallIndex, @spellThang.castAether.metrics
|
2014-01-03 13:32:13 -05:00
|
|
|
marked = {}
|
2014-01-21 12:03:04 -05:00
|
|
|
lastExecuted = lastExecuted[0 .. @toolbarView.statementIndex] if @toolbarView?.statementIndex?
|
2014-02-24 11:59:50 -05:00
|
|
|
gotVariableStates = false
|
2014-01-03 13:32:13 -05:00
|
|
|
for state, i in lastExecuted
|
2014-02-03 16:58:25 -05:00
|
|
|
[start, end] = state.range
|
2014-01-03 13:32:13 -05:00
|
|
|
clazz = if i is lastExecuted.length - 1 then 'executing' else 'executed'
|
|
|
|
if clazz is 'executed'
|
2014-01-24 20:48:11 -05:00
|
|
|
continue if marked[start.row]
|
|
|
|
marked[start.row] = true
|
|
|
|
markerType = "fullLine"
|
2014-01-19 10:08:28 -05:00
|
|
|
else
|
|
|
|
@debugView.setVariableStates state.variables
|
2014-02-24 11:59:50 -05:00
|
|
|
gotVariableStates = true
|
2014-03-05 16:17:34 -05:00
|
|
|
markerType = "text"
|
2014-02-03 16:58:25 -05:00
|
|
|
markerRange = new Range start.row, start.col, end.row, end.col
|
2014-01-03 13:32:13 -05:00
|
|
|
markerRange.start = @aceDoc.createAnchor markerRange.start
|
|
|
|
markerRange.end = @aceDoc.createAnchor markerRange.end
|
2014-01-24 20:48:11 -05:00
|
|
|
markerRange.id = @aceSession.addMarker markerRange, clazz, markerType
|
2014-01-03 13:32:13 -05:00
|
|
|
@markerRanges.push markerRange
|
2014-03-11 18:47:27 -04:00
|
|
|
if executedRows[start.row] and @decoratedGutter[start.row] isnt clazz
|
2014-03-11 20:26:11 -04:00
|
|
|
@aceSession.removeGutterDecoration start.row, @decoratedGutter[start.row] if @decoratedGutter[start.row] isnt ''
|
2014-03-11 18:47:27 -04:00
|
|
|
@aceSession.addGutterDecoration start.row, clazz
|
|
|
|
@decoratedGutter[start.row] = clazz
|
2014-02-24 11:59:50 -05:00
|
|
|
@debugView.setVariableStates {} unless gotVariableStates
|
2014-01-24 11:01:00 -05:00
|
|
|
null
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-20 19:14:31 -05:00
|
|
|
highlightComments: ->
|
|
|
|
lines = $(@ace.container).find('.ace_text-layer .ace_line_group')
|
|
|
|
session = @aceSession
|
2014-03-23 19:00:23 -04:00
|
|
|
top = Math.floor @ace.renderer.getScrollTopRow()
|
2014-02-20 19:14:31 -05:00
|
|
|
$(@ace.container).find('.ace_gutter-cell').each (index, el) ->
|
|
|
|
line = $(lines[index])
|
2014-03-23 19:00:23 -04:00
|
|
|
index = index - top
|
2014-02-20 19:14:31 -05:00
|
|
|
session.removeGutterDecoration index, 'comment-line'
|
|
|
|
if line.find('.ace_comment').length
|
|
|
|
session.addGutterDecoration index, 'comment-line'
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
onAnnotationClick: ->
|
|
|
|
alertBox = $("<div class='alert alert-info fade in'>#{msg}</div>")
|
|
|
|
offset = $(@).offset()
|
|
|
|
offset.left -= 162 # default width of the Bootstrap alert here
|
|
|
|
alertBox.css(offset).css('z-index', 500).css('position', 'absolute')
|
|
|
|
$('body').append(alertBox.alert())
|
|
|
|
_.delay (-> alertBox.alert('close')), 2500
|
|
|
|
|
|
|
|
onDisableControls: (e) -> @toggleControls e, false
|
|
|
|
onEnableControls: (e) -> @toggleControls e, @writable
|
|
|
|
toggleControls: (e, enabled) ->
|
|
|
|
return if e?.controls and not ('editor' in e.controls)
|
|
|
|
return if enabled is @controlsEnabled
|
|
|
|
@controlsEnabled = enabled and @writable
|
|
|
|
disabled = not enabled
|
|
|
|
$('body').focus() if disabled and $(document.activeElement).is('.ace_text-input')
|
|
|
|
@ace.setReadOnly disabled
|
|
|
|
@ace[if disabled then "setStyle" else "unsetStyle"] "disabled"
|
|
|
|
@toggleBackground()
|
|
|
|
|
|
|
|
toggleBackground: =>
|
|
|
|
# TODO: make the background an actual background and do the CSS trick
|
|
|
|
# used in spell_list_entry.sass for disabling
|
|
|
|
background = @$el.find('.code-background')[0]
|
|
|
|
if background.naturalWidth is 0 # not loaded yet
|
|
|
|
return _.delay @toggleBackground, 100
|
|
|
|
filters.revertImage background if @controlsEnabled
|
|
|
|
filters.darkenImage background, 0.8 unless @controlsEnabled
|
|
|
|
|
2014-02-22 19:58:54 -05:00
|
|
|
onSpellBeautify: (e) ->
|
2014-02-22 20:18:58 -05:00
|
|
|
return unless @spellThang and (@ace.isFocused() or e.spell is @spell)
|
2014-02-22 19:58:54 -05:00
|
|
|
ugly = @getSource()
|
|
|
|
pretty = @spellThang.aether.beautify ugly
|
|
|
|
@ace.setValue pretty
|
|
|
|
|
2014-03-13 21:49:58 -04:00
|
|
|
onChangeEditorConfig: (e) ->
|
2014-03-28 09:42:08 -04:00
|
|
|
aceConfig = me.get('aceConfig') ? {}
|
2014-03-15 18:14:57 -04:00
|
|
|
@ace.setDisplayIndentGuides aceConfig.indentGuides # default false
|
2014-03-16 21:14:04 -04:00
|
|
|
@ace.setShowInvisibles aceConfig.invisibles # default false
|
2014-03-15 18:14:57 -04:00
|
|
|
@ace.setKeyboardHandler @keyBindings[aceConfig.keyBindings ? 'default']
|
2014-03-28 09:42:08 -04:00
|
|
|
# @aceSession.setMode @editModes[aceConfig.language ? 'javascript']
|
2014-03-13 21:49:58 -04:00
|
|
|
|
2014-03-16 21:14:04 -04:00
|
|
|
onChangeLanguage: (e) ->
|
2014-03-28 09:42:08 -04:00
|
|
|
aceConfig = me.get('aceConfig') ? {}
|
2014-03-16 21:14:04 -04:00
|
|
|
@aceSession.setMode @editModes[aceConfig.language ? 'javascript']
|
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
dismiss: ->
|
|
|
|
@recompile() if @spell.hasChangedSignificantly @getSource()
|
|
|
|
|
|
|
|
destroy: ->
|
2014-02-12 15:41:41 -05:00
|
|
|
$(@ace?.container).find('.ace_gutter').off 'click', '.ace_error, .ace_warning, .ace_info', @onAnnotationClick
|
2014-01-03 13:32:13 -05:00
|
|
|
@firepad?.dispose()
|
2014-02-12 15:41:41 -05:00
|
|
|
@ace?.commands.removeCommand command for command in @aceCommands
|
|
|
|
@ace?.destroy()
|
|
|
|
@aceDoc?.off 'change', @onCodeChangeMetaHandler
|
|
|
|
@aceSession?.selection.off 'changeCursor', @onCursorActivity
|
2014-01-30 18:03:55 -05:00
|
|
|
@debugView?.destroy()
|
2014-02-14 13:57:47 -05:00
|
|
|
super()
|