diff --git a/proto/anki/tags.proto b/proto/anki/tags.proto index d178a4da6..a0e39ae86 100644 --- a/proto/anki/tags.proto +++ b/proto/anki/tags.proto @@ -63,6 +63,7 @@ message FindAndReplaceTagRequest { message CompleteTagRequest { // a partial tag, optionally delimited with :: string input = 1; + uint32 amount = 2; } message CompleteTagResponse { diff --git a/rslib/src/backend/tags.rs b/rslib/src/backend/tags.rs index e6c770bd1..8a788df16 100644 --- a/rslib/src/backend/tags.rs +++ b/rslib/src/backend/tags.rs @@ -91,7 +91,7 @@ impl TagsService for Backend { fn complete_tag(&self, input: pb::CompleteTagRequest) -> Result { self.with_col(|col| { - let tags = col.complete_tag(&input.input)?; + let tags = col.complete_tag(&input.input, input.amount)?; Ok(pb::CompleteTagResponse { tags }) }) } diff --git a/rslib/src/tags/complete.rs b/rslib/src/tags/complete.rs index 23e86a0ad..0c5cb723a 100644 --- a/rslib/src/tags/complete.rs +++ b/rslib/src/tags/complete.rs @@ -2,18 +2,19 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html use regex::Regex; +use std::convert::TryInto; use crate::prelude::*; impl Collection { - pub fn complete_tag(&self, input: &str) -> Result> { + pub fn complete_tag(&self, input: &str, amount: u32) -> Result> { let filters: Vec<_> = input .split("::") .map(component_to_regex) .collect::>()?; let mut tags = vec![]; self.storage.get_tags_by_predicate(|tag| { - if filters_match(&filters, tag) { + if tags.len() <= amount.try_into().unwrap() && filters_match(&filters, tag) { tags.push(tag.to_string()); } // we only need the tag name diff --git a/ts/editor/TagEditor.svelte b/ts/editor/TagEditor.svelte index f03993b3d..b85d8e88b 100644 --- a/ts/editor/TagEditor.svelte +++ b/ts/editor/TagEditor.svelte @@ -58,7 +58,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html const data = await postRequest( "/_anki/completeTag", Tags.CompleteTagRequest.encode( - Tags.CompleteTagRequest.create({ input: replaceWithColons(input) }) + Tags.CompleteTagRequest.create({ input, amount: 500 }) ).finish() ); const response = Tags.CompleteTagResponse.decode(data); @@ -72,10 +72,12 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html autocompleteDisabled = activeName.length === 0; suggestionsPromise = autocompleteDisabled ? noSuggestions - : fetchSuggestions(activeTag.name).then((names: string[]): string[] => { - autocompleteDisabled = names.length === 0; - return names.map(replaceWithUnicodeSeparator); - }); + : fetchSuggestions(replaceWithColons(activeTag.name)).then( + (names: string[]): string[] => { + autocompleteDisabled = names.length === 0; + return names.map(replaceWithUnicodeSeparator); + } + ); } function onAutocomplete(selected: string): void {