| @@ -74,7 +74,7 @@ use modalkit::{ |
| 74 | 74 | ApplicationStore, |
| 75 | 75 | ApplicationWindowId, |
| 76 | 76 | }, |
| 77 | | completion::{complete_path, CompletionMap}, |
| 77 | completion::{complete_path, Completer, CompletionMap}, |
| 78 | 78 | context::EditContext, |
| 79 | 79 | cursor::Cursor, |
| 80 | 80 | rope::EditRope, |
| @@ -1914,11 +1914,20 @@ impl ApplicationInfo for IambInfo { |
| 1914 | 1914 | type WindowId = IambId; |
| 1915 | 1915 | type ContentId = IambBufferId; |
| 1916 | 1916 | |
| 1917 | fn content_of_command(ct: CommandType) -> IambBufferId { |
| 1918 | IambBufferId::Command(ct) |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | pub struct IambCompleter; |
| 1923 | |
| 1924 | impl Completer<IambInfo> for IambCompleter { |
| 1917 | 1925 | fn complete( |
| 1926 | &mut self, |
| 1918 | 1927 | text: &EditRope, |
| 1919 | 1928 | cursor: &mut Cursor, |
| 1920 | 1929 | content: &IambBufferId, |
| 1921 | | store: &mut ProgramStore, |
| 1930 | store: &mut ChatStore, |
| 1922 | 1931 | ) -> Vec<String> { |
| 1923 | 1932 | match content { |
| 1924 | 1933 | IambBufferId::Command(CommandType::Command) => complete_cmdbar(text, cursor, store), |
| @@ -1936,21 +1945,16 @@ impl ApplicationInfo for IambInfo { |
| 1936 | 1945 | IambBufferId::UnreadList => vec![], |
| 1937 | 1946 | } |
| 1938 | 1947 | } |
| 1939 | | |
| 1940 | | fn content_of_command(ct: CommandType) -> IambBufferId { |
| 1941 | | IambBufferId::Command(ct) |
| 1942 | | } |
| 1943 | 1948 | } |
| 1944 | 1949 | |
| 1945 | 1950 | /// Tab completion for user IDs. |
| 1946 | | fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 1951 | fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ChatStore) -> Vec<String> { |
| 1947 | 1952 | let id = text |
| 1948 | 1953 | .get_prefix_word_mut(cursor, &MATRIX_ID_WORD) |
| 1949 | 1954 | .unwrap_or_else(EditRope::empty); |
| 1950 | 1955 | let id = Cow::from(&id); |
| 1951 | 1956 | |
| 1952 | 1957 | store |
| 1953 | | .application |
| 1954 | 1958 | .presences |
| 1955 | 1959 | .complete(id.as_ref()) |
| 1956 | 1960 | .into_iter() |
| @@ -1959,7 +1963,7 @@ fn complete_users(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> |
| 1959 | 1963 | } |
| 1960 | 1964 | |
| 1961 | 1965 | /// Tab completion within the message bar. |
| 1962 | | fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 1966 | fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ChatStore) -> Vec<String> { |
| 1963 | 1967 | let id = text |
| 1964 | 1968 | .get_prefix_word_mut(cursor, &MATRIX_ID_WORD) |
| 1965 | 1969 | .unwrap_or_else(EditRope::empty); |
| @@ -1968,13 +1972,12 @@ fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) - |
| 1968 | 1972 | match id.chars().next() { |
| 1969 | 1973 | // Complete room aliases. |
| 1970 | 1974 | Some('#') => { |
| 1971 | | return store.application.names.complete(id.as_ref()); |
| 1975 | return store.names.complete(id.as_ref()); |
| 1972 | 1976 | }, |
| 1973 | 1977 | |
| 1974 | 1978 | // Complete room identifiers. |
| 1975 | 1979 | Some('!') => { |
| 1976 | 1980 | return store |
| 1977 | | .application |
| 1978 | 1981 | .rooms |
| 1979 | 1982 | .complete(id.as_ref()) |
| 1980 | 1983 | .into_iter() |
| @@ -1984,7 +1987,7 @@ fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) - |
| 1984 | 1987 | |
| 1985 | 1988 | // Complete Emoji shortcodes. |
| 1986 | 1989 | Some(':') => { |
| 1987 | | let list = store.application.emojis.complete(&id[1..]); |
| 1990 | let list = store.emojis.complete(&id[1..]); |
| 1988 | 1991 | let iter = list.into_iter().take(200).map(|s| format!(":{}:", s)); |
| 1989 | 1992 | |
| 1990 | 1993 | return iter.collect(); |
| @@ -1993,7 +1996,6 @@ fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) - |
| 1993 | 1996 | // Complete usernames for @ and empty strings. |
| 1994 | 1997 | Some('@') | None => { |
| 1995 | 1998 | return store |
| 1996 | | .application |
| 1997 | 1999 | .presences |
| 1998 | 2000 | .complete(id.as_ref()) |
| 1999 | 2001 | .into_iter() |
| @@ -2007,28 +2009,23 @@ fn complete_msgbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) - |
| 2007 | 2009 | } |
| 2008 | 2010 | |
| 2009 | 2011 | /// Tab completion for Matrix identifiers (usernames, room aliases, etc.) |
| 2010 | | fn complete_matrix_names( |
| 2011 | | text: &EditRope, |
| 2012 | | cursor: &mut Cursor, |
| 2013 | | store: &ProgramStore, |
| 2014 | | ) -> Vec<String> { |
| 2012 | fn complete_matrix_names(text: &EditRope, cursor: &mut Cursor, store: &ChatStore) -> Vec<String> { |
| 2015 | 2013 | let id = text |
| 2016 | 2014 | .get_prefix_word_mut(cursor, &MATRIX_ID_WORD) |
| 2017 | 2015 | .unwrap_or_else(EditRope::empty); |
| 2018 | 2016 | let id = Cow::from(&id); |
| 2019 | 2017 | |
| 2020 | | let list = store.application.names.complete(id.as_ref()); |
| 2018 | let list = store.names.complete(id.as_ref()); |
| 2021 | 2019 | if !list.is_empty() { |
| 2022 | 2020 | return list; |
| 2023 | 2021 | } |
| 2024 | 2022 | |
| 2025 | | let list = store.application.presences.complete(id.as_ref()); |
| 2023 | let list = store.presences.complete(id.as_ref()); |
| 2026 | 2024 | if !list.is_empty() { |
| 2027 | 2025 | return list.into_iter().map(|i| i.to_string()).collect(); |
| 2028 | 2026 | } |
| 2029 | 2027 | |
| 2030 | 2028 | store |
| 2031 | | .application |
| 2032 | 2029 | .rooms |
| 2033 | 2030 | .complete(id.as_ref()) |
| 2034 | 2031 | .into_iter() |
| @@ -2037,12 +2034,12 @@ fn complete_matrix_names( |
| 2037 | 2034 | } |
| 2038 | 2035 | |
| 2039 | 2036 | /// Tab completion for Emoji shortcode names. |
| 2040 | | fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 2037 | fn complete_emoji(text: &EditRope, cursor: &mut Cursor, store: &ChatStore) -> Vec<String> { |
| 2041 | 2038 | let sc = text.get_prefix_word_mut(cursor, &WordStyle::Little); |
| 2042 | 2039 | let sc = sc.unwrap_or_else(EditRope::empty); |
| 2043 | 2040 | let sc = Cow::from(&sc); |
| 2044 | 2041 | |
| 2045 | | store.application.emojis.complete(sc.as_ref()) |
| 2042 | store.emojis.complete(sc.as_ref()) |
| 2046 | 2043 | } |
| 2047 | 2044 | |
| 2048 | 2045 | /// Tab completion for command names. |
| @@ -2050,11 +2047,11 @@ fn complete_cmdname( |
| 2050 | 2047 | desc: CommandDescription, |
| 2051 | 2048 | text: &EditRope, |
| 2052 | 2049 | cursor: &mut Cursor, |
| 2053 | | store: &ProgramStore, |
| 2050 | store: &ChatStore, |
| 2054 | 2051 | ) -> Vec<String> { |
| 2055 | 2052 | // Complete command name and set cursor position. |
| 2056 | 2053 | let _ = text.get_prefix_word_mut(cursor, &WordStyle::Little); |
| 2057 | | store.application.cmds.complete_name(desc.command.as_str()) |
| 2054 | store.cmds.complete_name(desc.command.as_str()) |
| 2058 | 2055 | } |
| 2059 | 2056 | |
| 2060 | 2057 | /// Tab completion for command arguments. |
| @@ -2062,9 +2059,9 @@ fn complete_cmdarg( |
| 2062 | 2059 | desc: CommandDescription, |
| 2063 | 2060 | text: &EditRope, |
| 2064 | 2061 | cursor: &mut Cursor, |
| 2065 | | store: &ProgramStore, |
| 2062 | store: &ChatStore, |
| 2066 | 2063 | ) -> Vec<String> { |
| 2067 | | let cmd = match store.application.cmds.get(desc.command.as_str()) { |
| 2064 | let cmd = match store.cmds.get(desc.command.as_str()) { |
| 2068 | 2065 | Ok(cmd) => cmd, |
| 2069 | 2066 | Err(_) => return vec![], |
| 2070 | 2067 | }; |
| @@ -2087,12 +2084,7 @@ fn complete_cmdarg( |
| 2087 | 2084 | } |
| 2088 | 2085 | |
| 2089 | 2086 | /// Tab completion for commands. |
| 2090 | | fn complete_cmd( |
| 2091 | | cmd: &str, |
| 2092 | | text: &EditRope, |
| 2093 | | cursor: &mut Cursor, |
| 2094 | | store: &ProgramStore, |
| 2095 | | ) -> Vec<String> { |
| 2087 | fn complete_cmd(cmd: &str, text: &EditRope, cursor: &mut Cursor, store: &ChatStore) -> Vec<String> { |
| 2096 | 2088 | match CommandDescription::from_str(cmd) { |
| 2097 | 2089 | Ok(desc) => { |
| 2098 | 2090 | if desc.arg.untrimmed.is_empty() { |
| @@ -2109,7 +2101,7 @@ fn complete_cmd( |
| 2109 | 2101 | } |
| 2110 | 2102 | |
| 2111 | 2103 | /// Tab completion for the command bar. |
| 2112 | | fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ProgramStore) -> Vec<String> { |
| 2104 | fn complete_cmdbar(text: &EditRope, cursor: &mut Cursor, store: &ChatStore) -> Vec<String> { |
| 2113 | 2105 | let eo = text.cursor_to_offset(cursor); |
| 2114 | 2106 | let slice = text.slice(..eo); |
| 2115 | 2107 | let cow = Cow::from(&slice); |
| @@ -2289,6 +2281,7 @@ pub mod tests { |
| 2289 | 2281 | #[tokio::test] |
| 2290 | 2282 | async fn test_complete_msgbar() { |
| 2291 | 2283 | let store = mock_store().await; |
| 2284 | let store = store.application; |
| 2292 | 2285 | |
| 2293 | 2286 | let text = EditRope::from("going for a walk :walk "); |
| 2294 | 2287 | let mut cursor = Cursor::new(0, 22); |
| @@ -2312,6 +2305,7 @@ pub mod tests { |
| 2312 | 2305 | #[tokio::test] |
| 2313 | 2306 | async fn test_complete_cmdbar() { |
| 2314 | 2307 | let store = mock_store().await; |
| 2308 | let store = store.application; |
| 2315 | 2309 | let users = vec![ |
| 2316 | 2310 | "@user1:example.com", |
| 2317 | 2311 | "@user2:example.com", |