function updateFsSuggestions()

in codex-cli/src/components/chat/terminal-chat-input.tsx [122:164]


  function updateFsSuggestions(
    txt: string,
    alwaysUpdateSelection: boolean = false,
  ) {
    // Clear file system completions if a space is typed
    if (txt.endsWith(" ")) {
      setFsSuggestions([]);
      setSelectedCompletion(-1);
    } else {
      // Determine the current token (last whitespace-separated word)
      const words = txt.trim().split(/\s+/);
      const lastWord = words[words.length - 1] ?? "";

      const shouldUpdateSelection =
        lastWord.startsWith("@") || alwaysUpdateSelection;

      // Strip optional leading '@' for the path prefix
      let pathPrefix: string;
      if (lastWord.startsWith("@")) {
        pathPrefix = lastWord.slice(1);
        // If only '@' is typed, list everything in the current directory
        pathPrefix = pathPrefix.length === 0 ? "./" : pathPrefix;
      } else {
        pathPrefix = lastWord;
      }

      if (shouldUpdateSelection) {
        const completions = getFileSystemSuggestions(pathPrefix);
        setFsSuggestions(completions);
        if (completions.length > 0) {
          setSelectedCompletion((prev) =>
            prev < 0 || prev >= completions.length ? 0 : prev,
          );
        } else {
          setSelectedCompletion(-1);
        }
      } else if (fsSuggestions.length > 0) {
        // Token cleared → clear menu
        setFsSuggestions([]);
        setSelectedCompletion(-1);
      }
    }
  }