simplify nid/nids searches, and ditch helper function

- IdList could be re-used for a cids: search in the future if required.
- Embedding the message means it's easy to access from Python as
an attribute of SearchTerm.
This commit is contained in:
Damien Elmes 2021-01-30 11:23:32 +10:00
parent 1adc9952f4
commit cb6b88da0f
6 changed files with 23 additions and 18 deletions

View File

@ -784,11 +784,5 @@ table.review-log {{ {revlog_style} }}
)
def nid_search_term(nids: List[int]) -> SearchTerm:
"""Helper function for building a NoteIDs message."""
return SearchTerm(nids=NoteIDs(nids=nids))
# legacy name
_Collection = Collection

View File

@ -7,7 +7,7 @@ import aqt.deckchooser
import aqt.editor
import aqt.forms
import aqt.modelchooser
from anki.collection import nid_search_term
from anki.backend_pb2 import SearchTerm
from anki.consts import MODEL_CLOZE
from anki.notes import Note
from anki.utils import htmlToTextLine, isMac
@ -145,7 +145,7 @@ class AddCards(QDialog):
def onHistory(self) -> None:
m = QMenu(self)
for nid in self.history:
if self.mw.col.findNotes(nid_search_term([nid])):
if self.mw.col.findNotes(SearchTerm(nid=nid)):
note = self.mw.col.getNote(nid)
fields = note.fields
txt = htmlToTextLine(", ".join(fields))
@ -162,7 +162,7 @@ class AddCards(QDialog):
m.exec_(self.historyButton.mapToGlobal(QPoint(0, 0)))
def editHistory(self, nid):
self.mw.browser_search(nid_search_term([nid]))
self.mw.browser_search(SearchTerm(nid=nid))
def addNote(self, note) -> Optional[Note]:
note.model()["did"] = self.deckChooser.selectedId()

View File

@ -13,7 +13,7 @@ from typing import List, Optional, Sequence, Tuple, cast
import aqt
import aqt.forms
from anki.cards import Card
from anki.collection import Collection, InvalidInput, SearchTerm, nid_search_term
from anki.collection import Collection, InvalidInput, SearchTerm
from anki.consts import *
from anki.lang import without_unicode_isolation
from anki.models import NoteType
@ -681,7 +681,7 @@ class Browser(QMainWindow):
nid = card and card.nid
if nid:
self.card = card
search = self.col.build_search_string(nid_search_term([nid]))
search = self.col.build_search_string(SearchTerm(nid=nid))
search = gui_hooks.default_search(search, card)
self.form.searchEdit.lineEdit().setText(search)
self.onSearchActivated()
@ -1522,7 +1522,9 @@ where id in %s"""
tv = self.form.tableView
tv.selectionModel().clear()
search = self.col.build_search_string(nid_search_term(nids))
search = self.col.build_search_string(
SearchTerm(nids=SearchTerm.IdList(ids=nids))
)
self.search_for(search)
tv.selectAll()
@ -1731,7 +1733,11 @@ where id in %s"""
t += (
"""<li><a href=# onclick="pycmd('%s');return false;">%s</a>: %s</a>"""
% (
html.escape(self.col.build_search_string(nid_search_term(nids))),
html.escape(
self.col.build_search_string(
SearchTerm(nids=SearchTerm.IdList(ids=nids))
)
),
tr(TR.BROWSING_NOTE_COUNT, count=len(nids)),
html.escape(val),
)

View File

@ -9,7 +9,7 @@ from concurrent.futures import Future
from typing import Iterable, List, Optional, Sequence, TypeVar
import aqt
from anki.collection import nid_search_term
from anki.backend_pb2 import SearchTerm
from anki.rsbackend import TR, Interrupted, ProgressKind, pb
from aqt.qt import *
from aqt.utils import (
@ -146,7 +146,7 @@ class MediaChecker:
if out is not None:
nid, err = out
self.mw.browser_search(nid_search_term([nid]))
self.mw.browser_search(SearchTerm(nid=nid))
showText(err, type="html")
else:
tooltip(tr(TR.MEDIA_CHECK_ALL_LATEX_RENDERED))

View File

@ -797,12 +797,15 @@ message SearchTerm {
SUSPENDED = 4;
BURIED = 5;
}
message IdList {
repeated int64 ids = 1;
}
oneof filter {
string tag = 1;
string deck = 2;
string note = 3;
uint32 template = 4;
NoteIDs nids = 5;
int64 nid = 5;
Dupe dupe = 6;
string field_name = 7;
Rated rated = 8;
@ -812,6 +815,7 @@ message SearchTerm {
bool current_deck = 12;
Flag flag = 13;
CardState card_state = 14;
IdList nids = 15;
}
}

View File

@ -262,9 +262,9 @@ impl From<pb::NoteId> for NoteID {
}
}
impl pb::NoteIDs {
impl pb::search_term::IdList {
fn into_id_string(self) -> String {
self.nids
self.ids
.iter()
.map(|i| i.to_string())
.collect::<Vec<_>>()
@ -307,6 +307,7 @@ impl From<pb::SearchTerm> for Node<'_> {
Filter::Template(u) => {
Node::Search(SearchNode::CardTemplate(TemplateKind::Ordinal(u as u16)))
}
Filter::Nid(nid) => Node::Search(SearchNode::NoteIDs(nid.to_string().into())),
Filter::Nids(nids) => Node::Search(SearchNode::NoteIDs(nids.into_id_string().into())),
Filter::Dupe(dupe) => Node::Search(SearchNode::Duplicates {
note_type_id: dupe.notetype_id.into(),