Merge pull request #24 from KristianJBorgwarth/feat/full-link-completion

feat(link_completion): full link complete
This commit is contained in:
Kristian 2026-04-14 05:44:26 +02:00 committed by GitHub
commit 6e723fce64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 19 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/KristianJBorgwarth/dendrite.daemon/core/logging"
"github.com/KristianJBorgwarth/dendrite.daemon/core/server"
"github.com/KristianJBorgwarth/dendrite.daemon/core/services"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
_ "modernc.org/sqlite"
)
@ -19,7 +20,7 @@ func main() {
server := server.NewServer()
uow := repositories.NewUnitOfWork();
linkRepo := repositories.NewLinkRepository()
linkRepo := repositories.NewLinkRepository(*persistence.NewReadContext())
tagRepo := repositories.NewTagRepository()
noteRepo := repositories.NewNoteRepository()
tagService := services.NewTagService(tagRepo)
@ -29,7 +30,7 @@ func main() {
server.RegisterHandler("vault/init", vault.NewInitializeHandler())
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler())
server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler(linkRepo))
if err := server.Run(os.Stdin, os.Stdout); err != nil {
slog.Error("server error", "error", err)

View file

@ -4,20 +4,25 @@ import (
"context"
"encoding/json"
"log/slog"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
)
type completeLinkCommand struct {
Query string `json:"query"`
}
type completionItem struct {
Slug string `json:"slug"`
type completeLinkResult struct {
Slug string `json:"slug"`
Display string `json:"display"`
}
type CompleteLinkHandler struct{}
type CompleteLinkHandler struct {
linkRepo repositories.ILinkRepository
}
func NewCompleteLinkHandler() *CompleteLinkHandler {
return &CompleteLinkHandler{}
func NewCompleteLinkHandler(lr repositories.ILinkRepository) *CompleteLinkHandler {
return &CompleteLinkHandler{linkRepo: lr}
}
func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
@ -25,11 +30,16 @@ func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (
if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err
}
slog.Debug("handling complete link command", "query", cmd.Query)
links, err := h.linkRepo.Search(ctx, cmd.Query)
if err != nil {
slog.Error("Failed to search links", "error", err)
return nil, err
}
return []completionItem{
{Slug: "standard-streams"},
{Slug: "unit-of-work"},
{Slug: "treesitter-basics"},
}, nil
items := make([]completeLinkResult, len(links))
for i, link := range links {
items[i] = completeLinkResult{Slug: link.TargetSlug(), Display: link.Display()}
}
return items, nil
}

View file

@ -12,14 +12,16 @@ type ILinkRepository interface {
Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error
GetByNoteID(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) ([]*models.Link, error)
GetBySlug(ctx context.Context, dbContext persistence.IDbContext, targetSlug string) ([]*models.Link, error)
Search(ctx context.Context, dbContext persistence.IDbContext, query string) ([]*models.Link, error)
Search(ctx context.Context, query string) ([]*models.Link, error)
Delete(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) error
}
type linkRepository struct{}
type linkRepository struct {
readDBContext persistence.ReadContext
}
func NewLinkRepository() ILinkRepository {
return &linkRepository{}
func NewLinkRepository(rdb persistence.ReadContext) ILinkRepository {
return &linkRepository{readDBContext: rdb}
}
func (r *linkRepository) Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error {
@ -81,8 +83,8 @@ func (r *linkRepository) GetBySlug(ctx context.Context, dbContext persistence.ID
return links, nil
}
func (r *linkRepository) Search(ctx context.Context, dbContext persistence.IDbContext, query string) ([]*models.Link, error) {
rows, err := dbContext.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM link WHERE raw LIKE ?`, "%"+query+"%")
func (r *linkRepository) Search(ctx context.Context, query string) ([]*models.Link, error) {
rows, err := r.readDBContext.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM link WHERE raw LIKE ?`, "%"+query+"%")
if err != nil {
return nil, err
}