Skip to content

Commit

Permalink
feat: track diagnostics progress
Browse files Browse the repository at this point in the history
  • Loading branch information
laytan committed Apr 19, 2023
1 parent 3ef2d15 commit 173af9f
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions internal/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/laytan/go-lsp-protocol/pkg/lsp/protocol"
"github.com/laytan/phpls/internal/config"
"github.com/laytan/phpls/pkg/lsprogress"
"github.com/laytan/phpls/pkg/set"
)

Expand All @@ -28,7 +29,8 @@ type Analyzer interface {
}

type Runner struct {
client protocol.Client
client protocol.Client
progress *lsprogress.Tracker

diagnostics map[string]*fileDiagnostics
diagnosticsMu sync.Mutex
Expand Down Expand Up @@ -57,6 +59,7 @@ func NewRunner(
) *Runner {
return &Runner{
client: client,
progress: lsprogress.NewTracker(client),
diagnostics: make(map[string]*fileDiagnostics),
analyzers: analyzers,
saveAnalyzers: saveAnalyzers,
Expand Down Expand Up @@ -187,6 +190,17 @@ func (r *Runner) StopWatching(path string) error {
}

func (r *Runner) Run(ctx context.Context, version int, path string, code []byte) error {
p, err := r.progress.Start(ctx, "diagnostics on change", "Started", nil)
if err != nil {
log.Printf("[ERROR]: starting progress tracking for diagnostics on change: %v", err)
} else {
defer func() {
if err := p.End(ctx, "Done"); err != nil {
log.Printf("[ERROR]: stopping progress tracking for diagnostics: %v", err)
}
}()
}

return r.run(
ctx,
r.analyzers,
Expand Down Expand Up @@ -408,14 +422,26 @@ Loop:
}

func (r *Runner) runForSave(path string) {
ctx := context.Background()
p, err := r.progress.Start(ctx, "diagnostics on save", "Started", nil)
if err != nil {
log.Printf("[ERROR]: starting progress tracking for diagnostics on save: %v", err)
} else {
defer func() {
if err := p.End(context.Background(), "Done"); err != nil {
log.Printf("[ERROR]: stopping progress for diagnostics on save: %v", err)
}
}()
}

r.diagnosticsMu.Lock()
var version int
if fd, ok := r.diagnostics[path]; ok {
version = fd.version + 1
}
r.diagnosticsMu.Unlock()

err := r.run(
err = r.run(
context.Background(),
r.saveAnalyzers,
version,
Expand All @@ -429,7 +455,13 @@ func (r *Runner) runForSave(path string) {
r.AddSaveDiagnostics,
)
if err != nil && !errors.Is(err, context.Canceled) {
log.Printf("[ERROR]: running analyzers for save: %v", err)
log.Printf("[ERROR]: running diagnostics on save error: %v", err)
if err := r.client.LogMessage(ctx, &protocol.LogMessageParams{
Type: protocol.Error,
Message: fmt.Sprintf("Running diagnostics on save error: %v", err),
}); err != nil {
log.Printf("[ERROR]: sending diagnostics on save error log to client: %v", err)
}
}
}

Expand Down

0 comments on commit 173af9f

Please sign in to comment.