ide: add recent files to workspace for the recent files menu when a workspace is...
[sdk] / ide / src / project / Workspace.ec
index 73e843d..0ba89ae 100644 (file)
@@ -205,6 +205,7 @@ public:
    property List<Breakpoint> breakpoints { set { breakpoints = value; } get { return breakpoints; } isset { return breakpoints && breakpoints.count; } }
    property List<Watch> watches { set { watches = value; } get { return watches; } isset { return watches && watches.count; } }
    property List<OpenedFileInfo> openedFiles { set { openedFiles = value; } get { return openedFiles; } isset { return openedFiles && openedFiles.count; } }
+   property RecentPaths recentFiles { set { recentFiles = value; } get { return recentFiles; } isset { return recentFiles && recentFiles.count; } }
 
    bool useValgrind;
    ValgrindLeakCheck vgLeakCheck;
@@ -223,6 +224,7 @@ private:
    List<Breakpoint> breakpoints;
    List<Watch> watches;
    List<OpenedFileInfo> openedFiles;
+   RecentPaths recentFiles;
 
    char * workspaceFile;
    char * workspaceDir;
@@ -534,20 +536,26 @@ public:
       return node;
    }
 
-   OpenedFileInfo UpdateOpenedFileInfo(const char * fileName, OpenedFileState state)
+   OpenedFileInfo UpdateOpenedFileInfo(const char * fileName, OpenedFileState state, bool fileOpen)
    {
+      bool insert = false;
       char absolutePath[MAX_LOCATION];
       char relativePath[MAX_LOCATION];
       OpenedFileInfo ofi;
       GetSlashPathBuffer(absolutePath, fileName);
       MakeRelativePath(relativePath, fileName);
       ofi = FindOpenedFileInfo(relativePath, absolutePath);
+      if(fileOpen && ofi)
+      {
+         openedFiles.Remove(openedFiles.Find(ofi));
+         insert = true;
+      }
       if(state)
       {
          if(!ofi)
          {
             ofi = OpenedFileInfo { path = CopyString(relativePath) };
-            openedFiles.Add(ofi);
+            insert = true;
          }
          ofi.state = state;
          ofi.modified = GetLocalTimeStamp();
@@ -562,6 +570,8 @@ public:
          if(!holdTracking)
             modified = true;
       }
+      if(insert)
+         openedFiles.Insert(null, ofi);
       return ofi;
    }
 
@@ -593,28 +603,30 @@ public:
 
    void OpenPreviouslyOpenedFiles(bool noParsing)
    {
+      Array<String> filePaths {};
       holdTracking = true;
-      for(ofi : openedFiles)
+      for(ofi : openedFiles; ofi.state != closed)
+         filePaths.Add(CopyString(ofi.path));
+      for(path : filePaths)
       {
-         if(ofi.state != closed)
+         Window file;
+         char absolutePath[MAX_LOCATION];
+         strcpy(absolutePath, workspaceDir);
+         PathCatSlash(absolutePath, path);
+         file = ide.OpenFile(absolutePath, false, true, null, no, normal, noParsing);
+         if(file)
          {
-            Window file;
-            char absolutePath[MAX_LOCATION];
-            strcpy(absolutePath, workspaceDir);
-            PathCatSlash(absolutePath, ofi.path);
-            file = ide.OpenFile(absolutePath, false, true, null, no, normal, noParsing);
-            if(file)
+            for(prj : projects)
             {
-               for(prj : projects)
-               {
-                  ProjectNode node = prj.topNode.FindByFullPath(absolutePath, true);
-                  if(node)
-                     node.EnsureVisible();
-               }
+               ProjectNode node = prj.topNode.FindByFullPath(absolutePath, true);
+               if(node)
+                  node.EnsureVisible();
             }
          }
       }
       holdTracking = false;
+      filePaths.Free();
+      delete filePaths;
    }
 
    OpenedFileInfo FindOpenedFileInfo(const char * relativePath, const char * absolutePath)
@@ -639,7 +651,7 @@ public:
 
    void RestorePreviouslyOpenedFileState(CodeEditor editor)
    {
-      if((editor.openedFileInfo = UpdateOpenedFileInfo(editor.fileName, opened)))
+      if((editor.openedFileInfo = UpdateOpenedFileInfo(editor.fileName, opened, true)))
          editor.openedFileInfo.SetCodeEditorState(editor);
    }
 
@@ -929,6 +941,7 @@ public:
       if(!breakpoints) breakpoints = { };
       if(!watches) watches = { };
       if(!openedFiles) openedFiles = { };
+      if(!recentFiles) recentFiles = { };
    }
 
    void Free()
@@ -948,6 +961,7 @@ public:
       if(breakpoints) { breakpoints.Free(); delete breakpoints; }
       if(watches) { watches.Free(); delete watches; }
       if(openedFiles) { openedFiles.Free(); delete openedFiles; }
+      if(recentFiles) { recentFiles.Free(); delete recentFiles; }
 
       projects.Free();
    }
@@ -974,7 +988,6 @@ public:
       SetSourceDirs(null);
       Free();
    }
-
 }
 
 Workspace LoadWorkspace(const char * filePath, const char * fromProjectFile)
@@ -1074,6 +1087,21 @@ Workspace LoadWorkspace(const char * filePath, const char * fromProjectFile)
 
       if(workspace)
       {
+         if(!workspace.recentFiles || !workspace.recentFiles.count)
+         {
+            int c;
+            if(!workspace.recentFiles) workspace.recentFiles = { };
+            if(workspace.openedFiles && workspace.openedFiles.count)
+            {
+               for(c = workspace.openedFiles.count - 1; c >= 0; c--)
+               {
+                 char path[MAX_LOCATION];
+                 strcpy(path, workspace.workspaceDir);
+                 PathCatSlash(path, workspace.openedFiles[c].path);
+                 workspace.recentFiles.addRecent(CopyString(path));
+               }
+            }
+         }
          workspace.Init();
          if(!workspace.projects.first)
          {