     session.setTabSize if useSoftTabs then @guessTabSize code else 8
     session.setUndoManager new UndoManager
     session.on 'change', => @dirty = true
-    @setModeForSession session
     @ace.setSession session
+    @setModeForURL @url if @url
 
     window.setTitle @title()
     @dirty = false

    Gemfile: 'ruby'
    Rakefile: 'ruby'

  setModeForSession: (session) ->
    return if not @url

    if not modeName = @modeFileMap[ _.last @url.split '/' ]
      language = _.last @url.split '.'
      language = language.toLowerCase()
      modeName = @modeMap[language] or language

    try
      mode = require("ace/mode/#{modeName}").Mode
      session.setMode new mode
    catch e
      console.error e

  title: ->
    if @url then _.last @url.split '/' else 'untitled'


  setCode: (code) ->
    @ace.getSession().setValue code

  removeTrailingWhitespace: ->
    return
    @ace.replaceAll "",
