<commit_before>do ->

  loadWorkspace = (options, workspace) ->

    {machineLabel, workspaceSlug, username} = options

    machine = getMachine machineLabel, username

    if workspace
      loadIDE { machine, workspace, username }

    else if workspaceSlug is 'my-workspace'
      workspace = new KD.remote.api.JWorkspace
        _id          : 'my-workspace'
        isDummy      : yes
        isDefault    : yes
        slug         : 'my-workspace'
        machineLabel : machine?.slug or machine?.label

      loadIDE { machine, workspace, username }

    else
      routeToLatestWorkspace()


  findWorkspace = (options, callback) ->

    {machineLabel, workspaceSlug, username} = options

    kallback = (workspaces) ->

      for workspace in workspaces
        continue  unless workspace.machineLabel is machineLabel
        continue  unless workspace.slug is workspaceSlug

        return callback workspace

      return callback null

    if username
    then filterWorkspacesByUsername username, kallback
    else kallback KD.userWorkspaces


  filterWorkspacesByUsername = (username, callback) ->

    KD.remote.cacheable username, (err, [account]) ->
      if err
        console.error err
        callback []

      originId = account.getId()

      callback KD.userWorkspaces.filter (workspace) ->
        originId is workspace.originId


  selectWorkspaceOnSidebar = (data) ->
    mainView = KD.getSingleton 'mainView'
    mainView.activitySidebar.selectWorkspace data


  getMachine = (label, username) ->
    machine = null

    for m in KD.userMachines
      hasSameLabel = (m.label is label) or (m.slug is label)
      hasSameUser  = m.data.credential is username

      if hasSameLabel and hasSameUser
        machine = m

    return machine


  loadIDE = (data) ->

    { machine, workspace, username, channelId } = data

    appManager = KD.getSingleton 'appManager'
    ideApps    = appManager.appControllers.IDE
    machineUId = machine?.uid
    callback   = ->
      appManager.open 'IDE', { forceNew: yes }, (app) ->
        app.mountedMachineUId   = machineUId
        app.workspaceData       = workspace

        if username and username isnt KD.nick()
          app.isInSession       = yes
          app.amIHost           = no
          app.collaborationHost = username
          app.channelId         = channelId
        else
          app.amIHost           = yes

        appManager.tell 'IDE', 'mountMachineByMachineUId', machineUId
        selectWorkspaceOnSidebar data

    return callback()  unless ideApps?.instances

    for instance in ideApps.instances
      isSameMachine   = instance.mountedMachineUId is machineUId
      isSameWorkspace = instance.workspaceData is workspace

      if isSameMachine
        if isSameWorkspace then ideInstance = instance
        else if workspace.slug is 'my-workspace'
          if instance.workspaceData?.isDefault
            ideInstance = instance

    if ideInstance
      appManager.showInstance ideInstance
      selectWorkspaceOnSidebar data
    else
      callback()


  putVMInWorkspace = (machine)->
    localStorage    = KD.getSingleton("localStorageController").storage "IDE"
    latestWorkspace = localStorage.getValue 'LatestWorkspace'

    machineLabel    = machine?.slug or machine?.label or ''
    workspaceSlug   = 'my-workspace'

    if latestWorkspace
      for ws in KD.userWorkspaces when ws.slug is latestWorkspace.workspaceSlug
        {machineLabel, workspaceSlug} = latestWorkspace
<commit_msg>Workspace: Compare by instance identifiers<commit_after>do ->

  loadWorkspace = (options, workspace) ->

    {machineLabel, workspaceSlug, username} = options

    machine = getMachine machineLabel, username

    if workspace
      loadIDE { machine, workspace, username }

    else if workspaceSlug is 'my-workspace'
      workspace = new KD.remote.api.JWorkspace
        _id          : 'my-workspace'
        isDummy      : yes
        isDefault    : yes
        slug         : 'my-workspace'
        machineLabel : machine?.slug or machine?.label

      loadIDE { machine, workspace, username }

    else
      routeToLatestWorkspace()


  findWorkspace = (options, callback) ->

    {machineLabel, workspaceSlug, username} = options

    kallback = (workspaces) ->

      for workspace in workspaces
        continue  unless workspace.machineLabel is machineLabel
        continue  unless workspace.slug is workspaceSlug

        return callback workspace

      return callback null

    if username
    then filterWorkspacesByUsername username, kallback
    else kallback KD.userWorkspaces


  filterWorkspacesByUsername = (username, callback) ->

    KD.remote.cacheable username, (err, [account]) ->
      if err
        console.error err
        callback []

      originId = account.getId()

      callback KD.userWorkspaces.filter (workspace) ->
        originId is workspace.originId


  selectWorkspaceOnSidebar = (data) ->
    mainView = KD.getSingleton 'mainView'
    mainView.activitySidebar.selectWorkspace data


  getMachine = (label, username) ->
    machine = null

    for m in KD.userMachines
      hasSameLabel = (m.label is label) or (m.slug is label)
      hasSameUser  = m.data.credential is username

      if hasSameLabel and hasSameUser
        machine = m

    return machine


  loadIDE = (data) ->

    { machine, workspace, username, channelId } = data

    appManager = KD.getSingleton 'appManager'
    ideApps    = appManager.appControllers.IDE
    machineUId = machine?.uid
    callback   = ->
      appManager.open 'IDE', { forceNew: yes }, (app) ->
        app.mountedMachineUId   = machineUId
        app.workspaceData       = workspace

        if username and username isnt KD.nick()
          app.isInSession       = yes
          app.amIHost           = no
          app.collaborationHost = username
          app.channelId         = channelId
        else
          app.amIHost           = yes

        appManager.tell 'IDE', 'mountMachineByMachineUId', machineUId
        selectWorkspaceOnSidebar data

    return callback()  unless ideApps?.instances

    for instance in ideApps.instances
      isSameMachine   = instance.mountedMachineUId is machineUId
      isSameWorkspace = instance.workspaceData.getId() is workspace.getId()

      if isSameMachine
        if isSameWorkspace then ideInstance = instance
        else if workspace.slug is 'my-workspace'
          if instance.workspaceData?.isDefault
            ideInstance = instance

    if ideInstance
      appManager.showInstance ideInstance
      selectWorkspaceOnSidebar data
    else
      callback()


  putVMInWorkspace = (machine)->
    localStorage    = KD.getSingleton("localStorageController").storage "IDE"
    latestWorkspace = localStorage.getValue 'LatestWorkspace'

    machineLabel    = machine?.slug or machine?.label or ''
    workspaceSlug   = 'my-workspace'

    if latestWorkspace
      for ws in KD.userWorkspaces when ws.slug is latestWorkspace.workspaceSlug
        {machineLabel, workspaceSlug} = latestWorkspace

{'fraction_non_alphanumeric': 0.07109408258833547, 'fraction_numerical': 0.0, 'mean_word_length': 2.4736323311976345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3480811', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1882, 'n_words': 510}