| 1941 | } |
| 1942 | |
| 1943 | bool linenoiseAddCompletionMarker(const char* buf, uint64_t len, std::string& result_buffer, |
| 1944 | std::vector<highlightToken>& tokens, struct linenoiseState* l) { |
| 1945 | if (!completionEnabled) { |
| 1946 | return false; |
| 1947 | } |
| 1948 | if (!l->continuePromptActive) { |
| 1949 | // don't render when pressing ctrl+c, only when editing |
| 1950 | return false; |
| 1951 | } |
| 1952 | static constexpr const uint64_t MAX_COMPLETION_LENGTH = 1000; |
| 1953 | if (len >= MAX_COMPLETION_LENGTH) { |
| 1954 | return false; |
| 1955 | } |
| 1956 | if (!l->insert || l->pos != len) { |
| 1957 | // only show when inserting a character at the end |
| 1958 | return false; |
| 1959 | } |
| 1960 | if (l->pos < 1) { |
| 1961 | // we need at least 1 bytes |
| 1962 | return false; |
| 1963 | } |
| 1964 | if (!tokens.empty() && tokens.back().type == tokenType::TOKEN_ERROR) { |
| 1965 | // don't show auto-completion when we have errors |
| 1966 | return false; |
| 1967 | } |
| 1968 | // we ONLY show completion if we have typed at least one character that is supported for |
| 1969 | // completion for now this is ONLY the characters a-z, A-Z, underscore (_), period (.), and |
| 1970 | // colon (:) |
| 1971 | if (!isCompletionCharacter(buf[l->pos - 1])) { |
| 1972 | return false; |
| 1973 | } |
| 1974 | auto completion = linenoiseTabComplete(l); |
| 1975 | if (completion.completions.empty()) { |
| 1976 | // no completions found |
| 1977 | return false; |
| 1978 | } |
| 1979 | if (completion.completions[0].completion.size() <= len) { |
| 1980 | // completion is not long enough |
| 1981 | return false; |
| 1982 | } |
| 1983 | // we have stricter requirements for rendering completions - the completion must match exactly |
| 1984 | for (uint64_t i = l->pos; i > 0; i--) { |
| 1985 | auto cpos = i - 1; |
| 1986 | if (!isCompletionCharacter(buf[cpos])) { |
| 1987 | break; |
| 1988 | } |
| 1989 | if (completion.completions[0].completion[cpos] != buf[cpos]) { |
| 1990 | return false; |
| 1991 | } |
| 1992 | } |
| 1993 | // add the first completion found for rendering purposes |
| 1994 | result_buffer = std::string(buf, len); |
| 1995 | result_buffer += completion.completions[0].completion.substr(len); |
| 1996 | |
| 1997 | highlightToken completion_token; |
| 1998 | completion_token.start = len; |
| 1999 | completion_token.type = tokenType::TOKEN_COMMENT; |
| 2000 | tokens.push_back(completion_token); |
no test coverage detected