Fix matching links in the LSP server

pull/112/head
Mickaël Menu 3 years ago
parent d9807485ec
commit 5a0fc6be5c

@ -276,10 +276,6 @@ func (d *NoteDAO) findIdsByHrefs(hrefs []string, allowPartialHrefs bool) ([]core
}
ids = append(ids, cids...)
}
if len(ids) == 0 {
return ids, fmt.Errorf("could not find notes at: " + strings.Join(hrefs, ", "))
}
return ids, nil
}
@ -393,11 +389,12 @@ func (d *NoteDAO) expandMentionsIntoMatch(opts core.NoteFindOpts) (core.NoteFind
if err != nil {
return opts, err
}
if len(ids) == 0 {
return opts, fmt.Errorf("could not find notes at: " + strings.Join(opts.Mention, ", "))
}
// Exclude the mentioned notes from the results.
for _, id := range ids {
opts = opts.ExcludingID(id)
}
opts = opts.ExcludingIDs(ids)
// Find their titles.
titlesQuery := "SELECT title, metadata FROM notes WHERE id IN (" + joinNoteIDs(ids, ",") + ")"
@ -538,7 +535,7 @@ func (d *NoteDAO) findRows(opts core.NoteFindOpts, selection noteSelection) (*sq
if err != nil {
return nil, err
}
opts.IncludeIDs = append(opts.IncludeIDs, ids...)
opts = opts.IncludingIDs(ids)
}
if opts.ExcludeHrefs != nil {
@ -546,7 +543,7 @@ func (d *NoteDAO) findRows(opts core.NoteFindOpts, selection noteSelection) (*sq
if err != nil {
return nil, err
}
opts.ExcludeIDs = append(opts.ExcludeIDs, ids...)
opts = opts.ExcludingIDs(ids)
}
if opts.Tags != nil {
@ -602,11 +599,12 @@ WHERE collection_id IN (SELECT id FROM collections t WHERE kind = '%s' AND (%s))
if err != nil {
return nil, err
}
if len(ids) == 0 {
return nil, fmt.Errorf("could not find notes at: " + strings.Join(opts.MentionedBy, ", "))
}
// Exclude the mentioning notes from the results.
for _, id := range ids {
opts = opts.ExcludingID(id)
}
opts = opts.ExcludingIDs(ids)
snippetCol = `snippet(nsrc.notes_fts, 2, '<zk:match>', '</zk:match>', '…', 20)`
joinClauses = append(joinClauses, "JOIN notes_fts nsrc ON nsrc.rowid IN ("+joinNoteIDs(ids, ",")+") AND nsrc.notes_fts MATCH mention_query(n.title, n.metadata)")

@ -243,6 +243,48 @@ func TestNoteDAOFindIdsByHref(t *testing.T) {
test("ref", true, []core.NoteID{8})
}
func TestNoteDAOFindIncludingHrefs(t *testing.T) {
test := func(href string, allowPartialHref bool, expected []string) {
testNoteDAOFindPaths(t,
core.NoteFindOpts{
IncludeHrefs: []string{href},
AllowPartialHrefs: allowPartialHref,
},
expected,
)
}
test("test", false, []string{})
test("test", true, []string{"ref/test/ref.md", "ref/test/b.md", "ref/test/a.md"})
// Filename takes precedence over the rest of the path.
// See https://github.com/mickael-menu/zk/issues/111
test("ref", true, []string{"ref/test/ref.md"})
}
func TestNoteDAOFindExcludingHrefs(t *testing.T) {
test := func(href string, allowPartialHref bool, expected []string) {
testNoteDAOFindPaths(t,
core.NoteFindOpts{
ExcludeHrefs: []string{href},
AllowPartialHrefs: allowPartialHref,
},
expected,
)
}
test("test", false, []string{"ref/test/ref.md", "ref/test/b.md",
"f39c8.md", "ref/test/a.md", "log/2021-01-03.md", "log/2021-02-04.md",
"index.md", "log/2021-01-04.md"})
test("test", true, []string{"f39c8.md", "log/2021-01-03.md",
"log/2021-02-04.md", "index.md", "log/2021-01-04.md"})
// Filename takes precedence over the rest of the path.
// See https://github.com/mickael-menu/zk/issues/111
test("ref", true, []string{"ref/test/b.md", "f39c8.md", "ref/test/a.md",
"log/2021-01-03.md", "log/2021-02-04.md", "index.md", "log/2021-01-04.md"})
}
func TestNoteDAOFindMinimalAll(t *testing.T) {
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
notes, err := dao.FindMinimal(core.NoteFindOpts{})
@ -471,13 +513,13 @@ func TestNoteDAOFindInPathWithFilePrefix(t *testing.T) {
// For directory, only complete names work, no prefixes.
func TestNoteDAOFindInPathRequiresCompleteDirName(t *testing.T) {
testNoteDAO(t, func(tx Transaction, dao *NoteDAO) {
_, err := dao.Find(core.NoteFindOpts{
testNoteDAOFindPaths(t,
core.NoteFindOpts{
IncludeHrefs: []string{"lo"},
AllowPartialHrefs: false,
})
assert.Err(t, err, "could not find notes at: lo")
})
},
[]string{},
)
testNoteDAOFindPaths(t,
core.NoteFindOpts{
IncludeHrefs: []string{"log"},

@ -54,14 +54,25 @@ type NoteFindOpts struct {
Sorters []NoteSorter
}
// ExcludingID creates a new FinderOpts after adding the given ID to the list
// IncludingIDs creates a new FinderOpts after adding the given IDs to the list
// of excluded note IDs.
func (o NoteFindOpts) ExcludingID(id NoteID) NoteFindOpts {
func (o NoteFindOpts) IncludingIDs(ids []NoteID) NoteFindOpts {
if o.IncludeIDs == nil {
o.IncludeIDs = []NoteID{}
}
o.IncludeIDs = append(o.IncludeIDs, ids...)
return o
}
// ExcludingIDs creates a new FinderOpts after adding the given IDs to the list
// of excluded note IDs.
func (o NoteFindOpts) ExcludingIDs(ids []NoteID) NoteFindOpts {
if o.ExcludeIDs == nil {
o.ExcludeIDs = []NoteID{}
}
o.ExcludeIDs = append(o.ExcludeIDs, id)
o.ExcludeIDs = append(o.ExcludeIDs, ids...)
return o
}

Loading…
Cancel
Save