Browse Source

Shell: update cached_path when adding aliases

This has the nice side effect of fixing alias completion, because
cached_path is the source of truth for the completion system, and it was
only refreshed (with shell::cache_path()) in the shell's constructor,
before the rc files where loaded (ie no aliases)

This also means that shell::is_runnable can now rely on the cache, and
doesn't have to check the aliases itself.
Mathieu PATUREL 5 năm trước cách đây
mục cha
commit
f6d4c4f02c
2 tập tin đã thay đổi với 14 bổ sung5 xóa
  1. 14 0
      Shell/Builtin.cpp
  2. 0 5
      Shell/Shell.cpp

+ 14 - 0
Shell/Builtin.cpp

@@ -64,6 +64,20 @@ int Shell::builtin_alias(int argc, const char** argv)
             }
         } else {
             m_aliases.set(parts[0], parts[1]);
+            size_t index = 0;
+            auto match = binary_search(
+                cached_path.span(), parts[0], [](const String& name, const String& program) -> int {
+                    return strcmp(name.characters(), program.characters());
+                },
+                &index);
+
+            if (match)
+                continue;
+
+            while (strcmp(cached_path[index].characters(), parts[0].characters()) < 0) {
+                index++;
+            }
+            cached_path.insert(index, parts[0]);
         }
     }
 

+ 0 - 5
Shell/Shell.cpp

@@ -388,11 +388,6 @@ String Shell::resolve_alias(const String& name) const
 
 bool Shell::is_runnable(const StringView& name)
 {
-    // FIXME: for now, check aliases manually because cached path doesn't get
-    // updated with aliases. Should it?
-    if (!resolve_alias(name).is_null())
-        return true;
-
     if (access(name.to_string().characters(), X_OK) == 0)
         return true;