Skip to content

Commit

Permalink
Insert logic to bound rather than inserts into prefix and suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Lawrence Angrave committed Aug 23, 2013
1 parent 11bf906 commit 6fc0ce9
Showing 1 changed file with 50 additions and 37 deletions.
87 changes: 50 additions & 37 deletions web/scripts/playerCodeEditor.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class window.EditorManager
@onStudentCodeChange()
@moveEditorButtonDelay = 30
setTimeout @moveEditorButtons, @moveEditorButtonDelay
@editor.gotoLine @findFirstNonCommentLine(@codeConfig.initial)
return

setUpInsertButtons: ->
Expand Down Expand Up @@ -118,12 +117,12 @@ class window.EditorManager
else
line = @editor.createBlankFunctionHeader(command) + ';'
funct = codeEditor.insertCommand
usesRemaining = @commands[command]['usesRemaining']

button = jQuery '<button>', {
id: command,
value: command,
text: "#{line}",
title: "#{usesRemaining} remain"
title: @toUsesRemainingText(maxUses, maxUses),
click: (e) ->
(codeEditor.button codeEditor.usesCurrentRow \
codeEditor.usesTextDocument funct )
Expand Down Expand Up @@ -221,17 +220,28 @@ class window.EditorManager
usesRemaining = remaining[command]
else
usesRemaining = @commands[command]['usesRemaining']


button.attr 'title',@toUsesRemainingText(usesRemaining, @commands[command].maxUses)
if usesRemaining <= 0
button.attr 'disabled', true
if usesRemaining < 0
valid = false
else
button.attr 'disabled', false
button.text "#{line}"

### #{usesRemaining}" ###
@onCommandRemainingValid? valid
return

toUsesRemainingText: (usesRemaining,maxUses) ->
return 'Already used (delete the code line to re-use this button)' if usesRemaining<=0
return 'Can only appear once!' if usesRemaining==1 and maxUses==1
return 'Can only appear one more time!' if usesRemaining==1
return"Can be used #{usesRemaining} more times"


moveEditorButtons: =>
row = @editor.editor.getCursorPosition().row
maxrows = @editor.editSession.getLength()
Expand Down Expand Up @@ -409,35 +419,13 @@ class window.EditorManager
return i
return -1 # Not Found

#Helper method for constructor so that we can insert code on the first non-comment line
#Not a real parser e.g. print("/*") would be mistaken for a multiline comment.
findFirstNonCommentLine: (src) ->
lines = src.split('\n')
count=0
inMLC = false #Multi-line comment
for line in src.split('\n')
count +=1
if(line.match(/^S*$/))
continue # Ignore empty lines
isSLC = !!line.match(/^\s*\/\//)

countStartMLC = line.split('/*').length-1
countEndMLC = line.split('*/').length-1

if(inMLC)
if(!isSLC)
inMLC = countStartMLC >countEndMLC
else
if( (!isSLC && !(inMLC=countStartMLC > countEndMLC)))
break
return count

class window.PlayerCodeEditor
###
Creates and provides functionality for an Ace editor representing player's code.
###
constructor: (
@editorDivId, @commands, codeText, @wrapCode, @codePrefix,
@editorDivId, @commands, @initialText, @wrapCode, @codePrefix,
@codeSuffix, @hiddenSuffix, @freeEdit, @interpreter) ->
###
Sets internal variables, the default text and buttons
Expand All @@ -456,22 +444,21 @@ class window.PlayerCodeEditor
@codeSuffixLength = 0
if @wrapCode == true
if @codePrefix != ""
@codeText = @codePrefix + codeText
@codeText = @codePrefix + @initialText
@codePrefixLength = @codePrefix.split('\n').length - 1
if @codeSuffix != ""
@codeText += '\n' + @codeSuffix
@codeSuffixLength = @codeSuffix.split('\n').length - 1 + 1
else
@codePrefix = ""
@codeSuffix = ""
@codeText = codeText
@codeText = @initialText

@enableKeyboardShortcuts()

@resetState()
@onChangeCallback = null
@editor.on 'change', @onChange
@gotoLine @codePrefixLength + 1
return

getStudentCode: ->
Expand All @@ -480,6 +467,7 @@ class window.PlayerCodeEditor
code += '\n' + @hiddenSuffix
return code


gotoLine: (row) ->
column = @editor.getCursorPosition().column
@editor.gotoLine row, column, true
Expand Down Expand Up @@ -566,19 +554,15 @@ class window.PlayerCodeEditor
return

insertCommand: ({text, line, currentRow}) ->
maxRow = @editSession.getLength()
if currentRow + 1 < @codePrefixLength or currentRow + 1 >= maxRow - (@codeSuffixLength - 1)
return

@commands[line]['usesRemaining']--
printLine = (@createBlankFunctionHeader line) + ';'
@insertLine {text: text, line:printLine, currentRow:currentRow}
return

insertLine: ({text, line, currentRow}) ->
maxRow = @editSession.getLength()
if currentRow + 1 < @codePrefixLength or currentRow + 1 >= maxRow - (@codeSuffixLength - 1)
return
currentRow = Math.max(currentRow, @codePrefixLength-1)
currentRow = Math.min(currentRow, maxRow-@codeSuffixLength-1 )
currentLine = text.getLine currentRow

if @commands.hasOwnProperty line
Expand Down Expand Up @@ -618,7 +602,9 @@ class window.PlayerCodeEditor
@editor.clearSelection()
@editor.resize()
@reIndentCode()
@gotoLine @codePrefixLength + 1

@gotoLine @codePrefixLength + 1 + @findFirstNonCommentLine(@initialText)

@editor.renderer.scrollToRow @codePrefixLength
for name, command of @commands
command['usesRemaining'] = command['maxUses']
Expand Down Expand Up @@ -777,3 +763,30 @@ class window.PlayerCodeEditor
if argument['namedArgumentsFlag'] != true
return false
return true


findFirstNonCommentLine: (src) ->
###
Returns the line numer of the first non-comment line.
This is obviously not a complete parser so obscure edge cases are unsupported
e.g. print("/*") would be mistaken for a multiline comment.
###
lines = src.split('\n')
count=0
inMLC = false #Multi-line comment
for line in src.split('\n')
count +=1
if(line.match(/^S*$/))
continue # Ignore empty lines
isSLC = !!line.match(/^\s*\/\//)

countStartMLC = line.split('/*').length-1
countEndMLC = line.split('*/').length-1

if(inMLC)
if(!isSLC)
inMLC = countStartMLC >countEndMLC
else
if( (!isSLC && !(inMLC=countStartMLC > countEndMLC)))
break
return count

0 comments on commit 6fc0ce9

Please sign in to comment.