1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 05:50:27 +00:00

Categories: Move them when a hypha is renamed

This commit is contained in:
Timur Ismagilov 2022-07-01 19:22:54 +05:00
parent d44c4484de
commit 5b829f1d82
2 changed files with 24 additions and 2 deletions

View File

@ -94,3 +94,19 @@ func removeHyphaFromCategory(hyphaName, catName string) {
mutex.Unlock() mutex.Unlock()
go saveToDisk() go saveToDisk()
} }
// RenameHyphaInAllCategories finds all mentions of oldName and replaces them with newName. Pass canonical names. Make sure newName is not taken. If oldName is not in any category, RenameHyphaInAllCategories is a no-op.
func RenameHyphaInAllCategories(oldName, newName string) {
mutex.Lock()
defer mutex.Unlock()
if node, ok := hyphaToCategories[oldName]; ok {
hyphaToCategories[newName] = node
delete(hyphaToCategories, oldName) // node should still be in memory 🙏
for _, catName := range node.categoryList {
if catNode, ok := categoryToHyphae[catName]; ok {
catNode.removeHypha(oldName)
catNode.storeHypha(newName)
}
}
}
}

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/backlinks" "github.com/bouncepaw/mycorrhiza/backlinks"
"github.com/bouncepaw/mycorrhiza/categories"
"regexp" "regexp"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
@ -36,6 +37,7 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us
var ( var (
re = regexp.MustCompile(`(?i)` + oldHypha.CanonicalName()) re = regexp.MustCompile(`(?i)` + oldHypha.CanonicalName())
replaceName = func(str string) string { replaceName = func(str string) string {
// Can we drop that util.CanonicalName?
return re.ReplaceAllString(util.CanonicalName(str), newName) return re.ReplaceAllString(util.CanonicalName(str), newName)
} }
hyphaeToRename = findHyphaeToRename(oldHypha, recursive) hyphaeToRename = findHyphaeToRename(oldHypha, recursive)
@ -67,9 +69,13 @@ func Rename(oldHypha hyphae.ExistingHypha, newName string, recursive bool, u *us
} }
for _, h := range hyphaeToRename { for _, h := range hyphaeToRename {
oldName := h.CanonicalName() var (
hyphae.RenameHyphaTo(h, replaceName(h.CanonicalName()), replaceName) oldName = h.CanonicalName()
newName = replaceName(oldName)
)
hyphae.RenameHyphaTo(h, newName, replaceName)
backlinks.UpdateBacklinksAfterRename(h, oldName) backlinks.UpdateBacklinksAfterRename(h, oldName)
categories.RenameHyphaInAllCategories(oldName, newName)
} }
return nil return nil