chottolabs / kznllm.nvim

Apache License 2.0
33 stars 8 forks source link

Make get_project_files follow symbolic links #34

Closed dceluis closed 3 weeks ago

dceluis commented 3 weeks ago

Hi,

I am experimenting with context files and got to the same issue as the one you mention on the project's readme. I think this would work without having to use a fork of plenary?

chottolabs commented 3 weeks ago

hmm... looks promising but in my fork I got it to work when directories are symlinked, but it only seems to work for individual files here

Screenshot 2024-10-18 at 3 46 56 PM
chottolabs commented 3 weeks ago

not very elegant... but something like this works

diff --git a/lua/kznllm/init.lua b/lua/kznllm/init.lua
index 50bc004..a950606 100644
--- a/lua/kznllm/init.lua
+++ b/lua/kznllm/init.lua
@@ -160,19 +160,27 @@ end
 function M.get_project_files(context_dir, opts)
   vim.print('using context at: ' .. context_dir:absolute())
   local context = {}
-  Scan.scan_dir(
-    context_dir:absolute(),
-    {
+
+  local next_dir = { context_dir }
+  repeat
+    local current_dir = table.remove(next_dir, 1)
+    Scan.scan_dir(current_dir:absolute(), {
       hidden = false,
-      on_insert = function (file, typ)
+      on_insert = function(file, typ)
         if typ == 'link' then
           file = uv.fs_readlink(file)
+          local ln_type = uv.fs_stat(file).type
+          if ln_type == 'directory' then
+            local path = Path:new(file)
+            table.insert(next_dir, path)
+            return
+          end
         end
         local path = Path:new(file)
         table.insert(context, { path = path.filename, content = path:read() })
-      end
-    }
-  )
+      end,
+    })
+  until #next_dir == 0

   return context
 end
dceluis commented 3 weeks ago

ah. failed to consider symlinked folders. i see now that your fork indeed handles that case. I've updated the code to account for that and also resolve the symlinks folders before scanning them, so that the correct path is appended to the prompt as context.

chottolabs commented 3 weeks ago

very very clean, thanks! 🙏