z_mirror/luapls

git clone https://git.raiguard.me/z_mirror/luapls

Caleb HeuerLog to stderr instead of a temp file2c65741

main
2.4 KiB96 linesraw
1package lsp
2
3import (
4	"github.com/raiguard/luapls/lua/ast"
5	"github.com/raiguard/luapls/lua/types"
6	"github.com/tliron/commonlog"
7	"github.com/tliron/glsp"
8	protocol "github.com/tliron/glsp/protocol_3_16"
9	glspserv "github.com/tliron/glsp/server"
10
11	// To enable logging
12	_ "github.com/tliron/commonlog/simple"
13)
14
15const LS_NAME = "luapls"
16
17// Server contains the state for the LSP session.
18type Server struct {
19	environment *types.Environment
20	handler     protocol.Handler
21	log         commonlog.Logger
22	rootPath    string
23	server      *glspserv.Server
24
25	config Config
26
27	isInitialized bool
28}
29
30func Run(logLevel int) {
31	commonlog.Configure(logLevel, nil)
32
33	s := Server{
34		environment: types.NewEnvironment(),
35	}
36
37	s.handler.Initialize = s.initialize
38	s.handler.Initialized = s.initialized
39	s.handler.WorkspaceDidChangeConfiguration = s.didChangeConfiguration
40	s.handler.Shutdown = s.shutdown
41	s.handler.SetTrace = s.setTrace
42	s.handler.TextDocumentDidOpen = s.textDocumentDidOpen
43	s.handler.TextDocumentDidChange = s.textDocumentDidChange
44	s.handler.TextDocumentDidClose = s.textDocumentDidClose
45	s.handler.TextDocumentDocumentHighlight = s.textDocumentHighlight
46	s.handler.TextDocumentHover = s.textDocumentHover
47	s.handler.TextDocumentDefinition = s.textDocumentDefinition
48
49	s.server = glspserv.NewServer(&s.handler, LS_NAME, logLevel > 2)
50
51	s.log = s.server.Log
52
53	s.server.RunStdio()
54}
55
56func (s *Server) initialize(ctx *glsp.Context, params *protocol.InitializeParams) (any, error) {
57	capabilities := s.handler.CreateServerCapabilities()
58	// TODO: RootURI / WorkspaceFolders fallbacks
59	s.environment.RootPath = *params.RootPath
60
61	s.updateConfig(params.InitializationOptions)
62
63	s.environment.Init()
64
65	return protocol.InitializeResult{
66		Capabilities: capabilities,
67		ServerInfo:   &protocol.InitializeResultServerInfo{Name: LS_NAME},
68	}, nil
69}
70
71func (s *Server) initialized(ctx *glsp.Context, params *protocol.InitializedParams) error {
72	s.isInitialized = true
73
74	for _, file := range s.environment.Files {
75		s.publishDiagnostics(ctx, file)
76	}
77
78	return nil
79}
80
81func (s *Server) shutdown(ctx *glsp.Context) error {
82	protocol.SetTraceValue(protocol.TraceValueOff)
83	return nil
84}
85
86func (s *Server) setTrace(ctx *glsp.Context, params *protocol.SetTraceParams) error {
87	protocol.SetTraceValue(params.Value)
88	return nil
89}
90
91func (s *Server) getFile(uri protocol.URI) *ast.File {
92	if !s.isInitialized {
93		return nil
94	}
95	return s.environment.Files[uri]
96}