@@ -22,10 +22,10 @@ import androidx.annotation.VisibleForTesting
22
22
import anki.notes.NoteFieldsCheckResponse
23
23
import com.ichi2.libanki.Consts.DEFAULT_DECK_ID
24
24
import com.ichi2.libanki.backend.model.toBackendNote
25
+ import com.ichi2.libanki.utils.LibAnkiAlias
25
26
import com.ichi2.libanki.utils.NotInLibAnki
26
27
import com.ichi2.utils.KotlinCleanup
27
28
import com.ichi2.utils.emptyStringArray
28
- import java.util.AbstractSet
29
29
import java.util.regex.Pattern
30
30
31
31
@KotlinCleanup(" lots to do" )
@@ -77,6 +77,7 @@ class Note : Cloneable {
77
77
loadFromBackendNote(col, note)
78
78
}
79
79
80
+ @LibAnkiAlias(" _load_from_backend_note" )
80
81
private fun loadFromBackendNote (
81
82
col : Collection ,
82
83
note : anki.notes.Note ,
@@ -100,6 +101,7 @@ class Note : Cloneable {
100
101
101
102
fun cards (col : Collection ): List <Card > = cardIds(col).map { col.getCard(it) }
102
103
104
+ @LibAnkiAlias(" ephemeral_card" )
103
105
fun ephemeralCard (
104
106
col : Collection ,
105
107
ord : Int = 0,
@@ -139,6 +141,7 @@ class Note : Cloneable {
139
141
140
142
/* * The first card, assuming it exists. */
141
143
@CheckResult
144
+ @NotInLibAnki
142
145
fun firstCard (col : Collection ): Card =
143
146
col.getCard(
144
147
col.db.queryLongScalar(
@@ -171,6 +174,7 @@ class Note : Cloneable {
171
174
return result
172
175
}
173
176
177
+ @LibAnkiAlias(" _field_index" )
174
178
private fun fieldIndex (key : String ): Int {
175
179
val fieldPair =
176
180
fMap!! [key]
@@ -183,35 +187,48 @@ class Note : Cloneable {
183
187
return fieldPair.first
184
188
}
185
189
190
+ @LibAnkiAlias(" __getitem__" )
186
191
fun getItem (key : String ): String = fields[fieldIndex(key)]
187
192
193
+ @LibAnkiAlias(" __setitem__" )
188
194
fun setItem (
189
195
key : String ,
190
196
value : String ,
191
197
) {
192
198
fields[fieldIndex(key)] = value
193
199
}
194
200
201
+ @LibAnkiAlias(" __contains__" )
195
202
operator fun contains (key : String ): Boolean = fMap!! .containsKey(key)
196
203
204
+ @NotInLibAnki
205
+ fun setField (
206
+ index : Int ,
207
+ value : String ,
208
+ ) {
209
+ fields[index] = value
210
+ }
211
+
197
212
/* *
198
213
* Tags
199
214
* ***********************************************************
200
215
*/
216
+
217
+ @LibAnkiAlias(" has_tag" )
201
218
fun hasTag (
202
219
col : Collection ,
203
220
tag : String ,
204
221
): Boolean = col.tags.inList(tag, tags)
205
222
206
- fun stringTags (col : Collection ): String = col.tags.join(col.tags.canonify(tags))
207
-
208
- fun setTagsFromStr (
209
- col : Collection ,
210
- str : String ,
211
- ) {
212
- tags = col.tags.split(str)
223
+ /* *
224
+ * Add tag. Duplicates will be stripped on save.
225
+ */
226
+ @LibAnkiAlias(" add_tag" )
227
+ fun addTag (tag : String ) {
228
+ tags.add(tag)
213
229
}
214
230
231
+ @LibAnkiAlias(" remove_tag" )
215
232
fun removeTag (tag : String ) {
216
233
val rem: MutableList <String > =
217
234
ArrayList (
@@ -227,31 +244,24 @@ class Note : Cloneable {
227
244
}
228
245
}
229
246
230
- /*
231
- * duplicates will be stripped on save
232
- */
233
- fun addTag (tag : String ) {
234
- tags.add(tag)
235
- }
247
+ @LibAnkiAlias(" string_tags" )
248
+ fun stringTags (col : Collection ): String = col.tags.join(col.tags.canonify(tags))
236
249
237
- fun addTags (tags : AbstractSet <String >) {
238
- tags.addAll(tags)
250
+ @LibAnkiAlias(" set_tags_from_str" )
251
+ fun setTagsFromStr (
252
+ col : Collection ,
253
+ str : String ,
254
+ ) {
255
+ tags = col.tags.split(str)
239
256
}
240
257
241
258
/* *
242
259
* Unique/duplicate check
243
260
* ***********************************************************
244
261
*/
245
- fun fieldsCheck (col : Collection ): NoteFieldsCheckResponse .State = col.backend.noteFieldsCheck(toBackendNote()).state
246
-
247
- fun sFld (col : Collection ): String = col.db.queryString(" SELECT sfld FROM notes WHERE id = ?" , id)
248
262
249
- fun setField (
250
- index : Int ,
251
- value : String ,
252
- ) {
253
- fields[index] = value
254
- }
263
+ @LibAnkiAlias(" fields_check" )
264
+ fun fieldsCheck (col : Collection ): NoteFieldsCheckResponse .State = col.backend.noteFieldsCheck(toBackendNote()).state
255
265
256
266
public override fun clone (): Note =
257
267
try {
@@ -270,7 +280,7 @@ class Note : Cloneable {
270
280
override fun hashCode (): Int = (id xor (id ushr 32 )).toInt()
271
281
272
282
object ClozeUtils {
273
- private val mClozeRegexPattern = Pattern .compile(" \\ {\\ {c(\\ d+)::" )
283
+ private val clozeRegexPattern = Pattern .compile(" \\ {\\ {c(\\ d+)::" )
274
284
275
285
/* *
276
286
* Calculate the next number that should be used if inserting a new cloze deletion.
@@ -286,7 +296,7 @@ class Note : Cloneable {
286
296
// Begin looping through the fields
287
297
for (fieldLiteral in fieldValues) {
288
298
// Begin searching in the current field for cloze references
289
- val matcher = mClozeRegexPattern .matcher(fieldLiteral)
299
+ val matcher = clozeRegexPattern .matcher(fieldLiteral)
290
300
while (matcher.find()) {
291
301
val detectedClozeId = matcher.group(1 )!! .toInt()
292
302
if (detectedClozeId > highestClozeId) {
0 commit comments