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