all repos

clerk @ cb22f86

missing tooling for ledger/hledger

clerk/internal/lsp/server.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
lsp: add config, 2 months ago
1
package lsp
2
3
import (
4
	"context"
5
	"log/slog"
6
	"sync"
7
8
	"go.lsp.dev/protocol"
9
	"go.lsp.dev/uri"
10
11
	"olexsmir.xyz/clerk/internal/analyzer"
12
	"olexsmir.xyz/clerk/internal/linter"
13
	"olexsmir.xyz/clerk/journal"
14
	"olexsmir.xyz/clerk/journal/ast"
15
	"olexsmir.xyz/clerk/journal/printer"
16
)
17
18
type server struct {
19
	protocol.UnimplementedServer
20
21
	client protocol.Client
22
	log    *slog.Logger
23
24
	version, name string
25
26
	linter  *linter.Linter
27
	loader  *journal.Loader
28
	printer *printer.Config
29
30
	mu       sync.Mutex
31
	openDocs map[uri.URI]docState
32
	current  *analyzer.Analysis
33
34
	cfgMu  sync.RWMutex
35
	config Config
36
37
	diagMu     sync.Mutex
38
	diagCancel context.CancelFunc
39
}
40
41
type docState struct {
42
	text       string
43
	version    int32
44
	languageID protocol.LanguageKind
45
	journal    *ast.Journal // cached parse; nil on parse failure
46
}
47
48
func (s *server) Initialize(ctx context.Context, params *protocol.InitializeParams) (*protocol.InitializeResult, error) {
49
	s.applySettings(params.InitializationOptions)
50
	return &protocol.InitializeResult{
51
		ServerInfo: protocol.ServerInfo{
52
			Name:    s.name,
53
			Version: protocol.NewOptional(s.version),
54
		},
55
		Capabilities: protocol.ServerCapabilities{
56
			DocumentFormattingProvider: &protocol.DocumentFormattingOptions{},
57
			TextDocumentSync: &protocol.TextDocumentSyncOptions{
58
				OpenClose: new(true),
59
				Change:    new(protocol.TextDocumentSyncKindFull),
60
			},
61
			SemanticTokensProvider: &protocol.SemanticTokensOptions{
62
				Legend: getSemanticTokensLegend(),
63
				Range:  protocol.Boolean(true),
64
				Full:   protocol.Boolean(true),
65
			},
66
		},
67
	}, nil
68
}
69
70
func (s *server) Initialized(ctx context.Context, params *protocol.InitializedParams) error {
71
	s.scheduleDiagnostics(ctx)
72
	return nil
73
}
74
75
func (s *server) DidChangeConfiguration(ctx context.Context, params *protocol.DidChangeConfigurationParams) error {
76
	s.applySettings(params.Settings)
77
	return nil
78
}
79
80
func (s *server) Shutdown(ctx context.Context) error {
81
	return nil
82
}
83
84
func (s *server) Exit(ctx context.Context) error {
85
	return nil
86
}
87
88
func (s *server) applySettings(v protocol.LSPAny) {
89
	s.cfgMu.Lock()
90
	s.config.merge(v)
91
	s.cfgMu.Unlock()
92
}