z_mirror/luapls
git clone https://git.raiguard.me/z_mirror/luapls
2c65741
main
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 { 19environment * types.Environment 20handler protocol.Handler 21log commonlog.Logger 22rootPath string 23server * glspserv.Server 24 25config Config 26 27isInitialized bool 28} 29 30func Run (logLevel int ) { 31commonlog .Configure (logLevel ,nil ) 32 33s := Server { 34environment :types .NewEnvironment (), 35 } 36 37s .handler .Initialize = s .initialize 38s .handler .Initialized = s .initialized 39s .handler .WorkspaceDidChangeConfiguration = s .didChangeConfiguration 40s .handler .Shutdown = s .shutdown 41s .handler .SetTrace = s .setTrace 42s .handler .TextDocumentDidOpen = s .textDocumentDidOpen 43s .handler .TextDocumentDidChange = s .textDocumentDidChange 44s .handler .TextDocumentDidClose = s .textDocumentDidClose 45s .handler .TextDocumentDocumentHighlight = s .textDocumentHighlight 46s .handler .TextDocumentHover = s .textDocumentHover 47s .handler .TextDocumentDefinition = s .textDocumentDefinition 48 49s .server = glspserv .NewServer (& s .handler ,LS_NAME ,logLevel > 2 ) 50 51s .log = s .server .Log 52 53s .server .RunStdio () 54} 55 56func (s * Server )initialize (ctx * glsp.Context ,params * protocol.InitializeParams ) (any ,error ) { 57capabilities := s .handler .CreateServerCapabilities () 58// TODO: RootURI / WorkspaceFolders fallbacks 59s .environment .RootPath = * params .RootPath 60 61s .updateConfig (params .InitializationOptions ) 62 63s .environment .Init () 64 65return protocol.InitializeResult { 66Capabilities :capabilities , 67ServerInfo :& protocol.InitializeResultServerInfo {Name :LS_NAME }, 68 },nil 69} 70 71func (s * Server )initialized (ctx * glsp.Context ,params * protocol.InitializedParams )error { 72s .isInitialized = true 73 74for _ ,file := range s .environment .Files { 75s .publishDiagnostics (ctx ,file ) 76 } 77 78return nil 79} 80 81func (s * Server )shutdown (ctx * glsp.Context )error { 82protocol .SetTraceValue (protocol .TraceValueOff ) 83return nil 84} 85 86func (s * Server )setTrace (ctx * glsp.Context ,params * protocol.SetTraceParams )error { 87protocol .SetTraceValue (params .Value ) 88return nil 89} 90 91func (s * Server )getFile (uri protocol.URI )* ast.File { 92if ! s .isInitialized { 93return nil 94 } 95return s .environment .Files [uri ] 96}