pkulchenko / ZeroBraneStudio

Lightweight Lua-based IDE for Lua with code completion, syntax highlighting, live coding, remote debugger, and code analyzer; supports Lua 5.1, 5.2, 5.3, 5.4, LuaJIT and other Lua interpreters on Windows, macOS, and Linux
http://studio.zerobrane.com/
Other
2.61k stars 519 forks source link

"New File" and "New Directory" from right click in Project view do nothing #1062

Closed xopxe closed 4 years ago

xopxe commented 4 years ago

I can not create files nor directories from the context menu of a directory in the Project tab. Creating a file from the main menu (File->New) works. Nothing happens, no errors on the console.

Last from Git, Ubuntu 18.04,

Plugins:

cloneview.lua
edgemark.lua
highlightselected.lua
moveline.lua
referencepanel.lua
uniquetabname.lua
pkulchenko commented 4 years ago

Both "New File" and "New Directory" open a new item in the tree for editing where you can enter the file/directory name. If the current selection is on a folder, then the items are created in that folder. Does this not work for you? So far it works for me as expected, even with the same plugins installed.

I think this logic can be improved to open created file, so it's clear what actually happens.

xopxe commented 4 years ago

I remember that behavior and is what I was expecting. I did some tests, and it seem it only works once for me. After I open a project the first time I do New File it creates an entry. After I name the entry or hit Esc it does not work anymore. If I try to delete the created file it prompts me as if was deleting the directory where I have ZBS installed. This bad reference survives restarts. If I edit the new file and save it it works.

I did a screencast showing the issue: https://drive.google.com/file/d/1043hS0c0WBf9pcNPLE3SOgGZKLve92Ns/view?usp=sharing

pkulchenko commented 4 years ago

I'll check the screencast, but I don't have access to it. You can email me a private link if needed.

pkulchenko commented 4 years ago

@xopxe, I'm still not sure what's going on, but the following patch will move the selection to the newly added item:

diff --git a/src/editor/filetree.lua b/src/editor/filetree.lua
index d529f17b..a8cfc5f6 100644
--- a/src/editor/filetree.lua
+++ b/src/editor/filetree.lua
@@ -398,6 +398,8 @@ local function treeSetConnectorsAndIcons(tree)
     -- close the target document, since the source has already been updated for it
     if targetdoc and #sourcedocs > 0 then targetdoc:Close() end

+    tree:Thaw()
+
     local itemdst = tree:FindItem(target)
     if itemdst then
       tree:UnselectAll()
@@ -408,8 +410,6 @@ local function treeSetConnectorsAndIcons(tree)
       tree:SetScrollPos(wx.wxVERTICAL, pos)
     end

-    tree:Thaw()
-
     PackageEventHandle("onFiletreeFileRename", tree, itemsrc, source, target)

     return true
xopxe commented 4 years ago

Sorry for the private link, there's a public one: https://drive.google.com/file/d/1043hS0c0WBf9pcNPLE3SOgGZKLve92Ns/view?usp=sharing

xopxe commented 4 years ago

More tests (with the patch): If I create a file, type a name, hit enter to confirm it and hit enter again to edit it, everything works fine. Things get broken when I create a file, type a name, hit enter to confirm it and then click on another file without opening the one I just created. Then that file get somehow corrupted, and I cannot create more files.

pkulchenko commented 4 years ago

@xopxe, I think I figured out what's happening. It seems like the tree control is losing some of its internal state, as when you make the right click on it, the selected item is no longer highlighted (so the currently focused item is lost) and this causes all those effects you see.

I don't have a fix yet, but am exploring several options.

pkulchenko commented 4 years ago

@xopxe, can you apply the following patch to see if this works for you. It should also fix #1070.

diff --git a/src/editor/filetree.lua b/src/editor/filetree.lua
index 9d05ffba..55a5f52f 100644
--- a/src/editor/filetree.lua
+++ b/src/editor/filetree.lua
@@ -577,6 +577,10 @@ local function treeSetConnectorsAndIcons(tree)
     return item_id
   end

+  if ide.osname == "Unix" then
+    local gfi = tree.GetFocusedItem
+    function tree:GetFocusedItem(...) return self.focusedItem or gfi(self, ...) end
+  end
   tree:Connect(ID.NEWFILE, wx.wxEVT_COMMAND_MENU_SELECTED,
     function()
       tree:EditLabel(addItem(tree:GetFocusedItem(), empty, image.FILEOTHER))
@@ -759,7 +763,13 @@ local function treeSetConnectorsAndIcons(tree)
       local interval = wx.wxUpdateUIEvent.GetUpdateInterval()
       wx.wxUpdateUIEvent.SetUpdateInterval(-1) -- don't update

+      if ide.osname == "Unix" then
+        -- Linux implementation is losing focus item state after the popup window is shown,
+        -- so save it to use if needed
+        tree.focusedItem = tree:GetFocusedItem()
+      end
       tree:PopupMenu(menu)
+      tree.focusedItem = false
       wx.wxUpdateUIEvent.SetUpdateInterval(interval)
       collectgarbage("restart")
     end)
xopxe commented 4 years ago

It does fix this bug.

Bug #1070 is fixed for new projects too, but seems to still affect existing projects. Perhaps I should remove some state file?

pkulchenko commented 4 years ago

Thank you for the update. I responded in #1070 about the issue with the existing projects.

I'll apply the patch then.