MCPcopy
hub / github.com/wavetermdev/waveterm / resolveFileQuery

Function resolveFileQuery

pkg/suggestion/suggestion.go:64–133  ·  view source on GitHub ↗

resolveFileQuery returns (baseDir, queryPrefix, searchTerm, error). Our approach is to use the presence of a trailing slash to decide whether to treat the query as a directory listing (searchTerm is empty) or a search filter. (This means that a query of exactly "." or ".." is treated as a search fi

(cwd string, query string)

Source from the content-addressed store, hash-verified

62// we treat everything before the last slash as a relative directory to search
63// in, and the portion after the last slash as the search term.
64func resolveFileQuery(cwd string, query string) (string, string, string, error) {
65 // If no current working directory, default to "~".
66 if cwd == "" {
67 cwd = "~"
68 }
69 var err error
70 cwd, err = wavebase.ExpandHomeDir(cwd)
71 if err != nil {
72 return "", "", "", fmt.Errorf("error expanding home dir: %w", err)
73 }
74 if query == "" {
75 return cwd, "", "", nil
76 }
77 // Expand home if needed.
78 tildeSlash := "~" + PathSepStr
79 if query == "~" || strings.HasPrefix(query, tildeSlash) {
80 ogQuery := query
81 query, err = wavebase.ExpandHomeDir(query)
82 if err != nil {
83 return "", "", "", fmt.Errorf("error expanding query home dir: %w", err)
84 }
85 if ogQuery == "~" || ogQuery == tildeSlash {
86 return query, tildeSlash, "", nil
87 }
88 }
89 // Handle absolute queries.
90 if filepath.IsAbs(query) {
91 if filepath.Dir(query) == query {
92 return query, query, "", nil
93 }
94 if strings.HasSuffix(query, PathSepStr) {
95 // Remove trailing slash for canonical directory path.
96 baseDir := strings.TrimRight(query, PathSepStr)
97 // But keep the trailing slash in the queryPrefix for display.
98 queryPrefix := query
99 return baseDir, queryPrefix, "", nil
100 }
101 // Otherwise, e.g. "/var/f"
102 baseDir := filepath.Dir(query)
103 queryPrefix := filepath.Dir(query)
104 searchTerm := filepath.Base(query)
105 return baseDir, queryPrefix, searchTerm, nil
106 }
107
108 // For relative queries:
109 // If the query ends with a slash (e.g. "./" or "waveterm/"), then treat it
110 // as a directory listing.
111 if strings.HasSuffix(query, PathSepStr) {
112 fullPath := filepath.Join(cwd, query)
113 baseDir := strings.TrimRight(fullPath, PathSepStr)
114 queryPrefix := query
115 return baseDir, queryPrefix, "", nil
116 }
117
118 // If there is a slash in the query, split into directory part and search term.
119 if idx := strings.LastIndex(query, PathSepStr); idx != -1 {
120 dirPart := query[:idx]
121 term := query[idx+1:]

Callers 1

fetchFileSuggestionsFunction · 0.85

Calls 2

ExpandHomeDirFunction · 0.92
ensureTrailingSlashFunction · 0.85

Tested by

no test coverage detected