From a8d34e45378207ffa41583abbe12cb0077210226 Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Thu, 15 Aug 2024 19:12:43 -0500 Subject: [PATCH] Support autovar commands --- .github/workflows/go-dev.yml | 6 +++--- config/config.go | 7 +++++-- go.mod | 2 +- go.sum | 6 ++---- server/cache.go | 40 ++++++++++++++++++++++++++++++++++++ server/diagnostics.go | 5 ++++- server/server.go | 27 +++++++++++++----------- 7 files changed, 70 insertions(+), 23 deletions(-) diff --git a/.github/workflows/go-dev.yml b/.github/workflows/go-dev.yml index 7140d02..8fe3abe 100644 --- a/.github/workflows/go-dev.yml +++ b/.github/workflows/go-dev.yml @@ -45,7 +45,7 @@ jobs: run: mv poryscript-pls poryscript-pls-linux - name: Create Release - uses: softprops/action-gh-release@v1 + uses: softprops/action-gh-release@v2 if: startsWith(github.ref, 'refs/tags/') with: files: poryscript-pls-linux @@ -79,7 +79,7 @@ jobs: run: mv poryscript-pls poryscript-pls-mac - name: Create Release - uses: softprops/action-gh-release@v1 + uses: softprops/action-gh-release@v2 if: startsWith(github.ref, 'refs/tags/') with: files: poryscript-pls-mac @@ -113,7 +113,7 @@ jobs: run: mv poryscript-pls.exe poryscript-pls-windows.exe - name: Create Release - uses: softprops/action-gh-release@v1 + uses: softprops/action-gh-release@v2 if: startsWith(github.ref, 'refs/tags/') with: files: poryscript-pls-windows.exe diff --git a/config/config.go b/config/config.go index 8c006d4..b05f833 100644 --- a/config/config.go +++ b/config/config.go @@ -26,6 +26,8 @@ type PoryscriptSettings struct { CommandIncludes []string `json:"commandIncludes"` // Filepaths for constant and symbol definitions. SymbolIncludes []TokenIncludeSetting `json:"symbolIncludes"` + // Filepath for command config. + CommandConfigFilepath string `json:"commandConfigFilepath"` } type TokenIncludeSetting struct { @@ -35,8 +37,9 @@ type TokenIncludeSetting struct { } var defaultPoryscriptSettings = PoryscriptSettings{ - CommandIncludes: []string{"asm/macros/event.inc", "asm/macros/movement.inc"}, - SymbolIncludes: []TokenIncludeSetting{}, + CommandIncludes: []string{"asm/macros/event.inc", "asm/macros/movement.inc"}, + SymbolIncludes: []TokenIncludeSetting{}, + CommandConfigFilepath: "tools/poryscript/command_config.json", } func New() Config { diff --git a/go.mod b/go.mod index 38a420e..a08870e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/huderlem/poryscript-pls go 1.17 require ( - github.com/huderlem/poryscript v0.0.0-20230115161016-dced71bed8b2 + github.com/huderlem/poryscript v0.0.0-20240816000600-ab7aa38068e9 github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d github.com/sourcegraph/jsonrpc2 v0.1.0 ) diff --git a/go.sum b/go.sum index ba7d9f9..499fa42 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,8 @@ github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/huderlem/poryscript v0.0.0-20220620201125-93efa4266f6a h1:FfAgMbKLfs1vgyz6EJ5V21atXJR9L+iryp4agXe17fo= -github.com/huderlem/poryscript v0.0.0-20220620201125-93efa4266f6a/go.mod h1:MnP/SQUqwI0gdrSC388x39hss09TCEXQ/mCgDSWVWDE= -github.com/huderlem/poryscript v0.0.0-20230115161016-dced71bed8b2 h1:/lbUTFCE25myIOgS66rLYdN39ocG/IQs+42VsXWYULs= -github.com/huderlem/poryscript v0.0.0-20230115161016-dced71bed8b2/go.mod h1:MnP/SQUqwI0gdrSC388x39hss09TCEXQ/mCgDSWVWDE= +github.com/huderlem/poryscript v0.0.0-20240816000600-ab7aa38068e9 h1:n7V8ocyzTMMQo7C4MIYDDkl71qZMr3hLJSQgsc7s8+M= +github.com/huderlem/poryscript v0.0.0-20240816000600-ab7aa38068e9/go.mod h1:MnP/SQUqwI0gdrSC388x39hss09TCEXQ/mCgDSWVWDE= github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d h1:afLbh+ltiygTOB37ymZVwKlJwWZn+86syPTbrrOAydY= github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d/go.mod h1:SULmZY7YNBsvNiQbrb/BEDdEJ84TGnfyUQxaHt8t8rY= github.com/sourcegraph/jsonrpc2 v0.1.0 h1:ohJHjZ+PcaLxDUjqk2NC3tIGsVa5bXThe1ZheSXOjuk= diff --git a/server/cache.go b/server/cache.go index 4686c53..a76c47d 100644 --- a/server/cache.go +++ b/server/cache.go @@ -2,9 +2,11 @@ package server import ( "context" + "encoding/json" "net/url" "github.com/huderlem/poryscript-pls/parse" + "github.com/huderlem/poryscript/parser" ) // Gets the aggregate list of Commands from the collection of files that define @@ -66,6 +68,44 @@ func (s *poryscriptServer) getAndCacheCommandsInFile(ctx context.Context, uri st return commandSet, nil } +func (s *poryscriptServer) getAutovarCommands(ctx context.Context, uri string) (parser.CommandConfig, error) { + s.commandConfigMutex.Lock() + defer s.commandConfigMutex.Unlock() + + uri, _ = url.QueryUnescape(uri) + if autovarCommands, ok := s.cachedAutovarCommands[uri]; ok { + return autovarCommands, nil + } + return s.getAndCacheAutovarCommands(ctx, uri) +} + +func (s *poryscriptServer) getAndCacheAutovarCommands(ctx context.Context, uri string) (parser.CommandConfig, error) { + settings, err := s.config.GetFileSettings(ctx, s.connection, uri) + if err != nil { + return parser.CommandConfig{}, err + } + + if len(settings.CommandConfigFilepath) == 0 { + return parser.CommandConfig{}, nil + } + + commandConfigUri, _ := url.QueryUnescape(settings.CommandConfigFilepath) + var content string + if err := s.connection.Call(ctx, "poryscript/readfile", commandConfigUri, &content); err != nil { + return parser.CommandConfig{}, err + } + if !s.config.HasWorkspaceFolderCapability { + return parser.CommandConfig{}, nil + } + + var config parser.CommandConfig + if err := json.Unmarshal([]byte(content), &config); err != nil { + return parser.CommandConfig{}, err + } + + return config, nil +} + // Gets the list of poryscript constants from the given file uri. The constants // are cached for the file so that parsing is avoided in future calls. func (s *poryscriptServer) getConstantsInFile(ctx context.Context, uri string) (map[string]parse.ConstantSymbol, error) { diff --git a/server/diagnostics.go b/server/diagnostics.go index 5aa2cfc..6db5350 100644 --- a/server/diagnostics.go +++ b/server/diagnostics.go @@ -33,7 +33,10 @@ func (s *poryscriptServer) validatePoryscriptFile(ctx context.Context, fileUri s return err } - p := parser.NewLintParser(lexer.New(content)) + // TODO: should this potential error be ignored? + commandConfig, _ := s.getAutovarCommands(ctx, fileUri) + + p := parser.NewLintParser(lexer.New(content), commandConfig) program, err := p.ParseProgram() if err == nil { // The poryscript file is syntactically correct. Check for warnings. diff --git a/server/server.go b/server/server.go index 275c261..fee67c5 100644 --- a/server/server.go +++ b/server/server.go @@ -12,6 +12,7 @@ import ( "github.com/huderlem/poryscript-pls/lsp" "github.com/huderlem/poryscript-pls/parse" "github.com/huderlem/poryscript/lexer" + "github.com/huderlem/poryscript/parser" "github.com/huderlem/poryscript/token" "github.com/sourcegraph/jsonrpc2" ) @@ -103,18 +104,20 @@ func (server *poryscriptServer) handle(ctx context.Context, conn *jsonrpc2.Conn, // poryscriptServer is the main handler for the Poryscript LSP server. It implements the // LspServer interface. type poryscriptServer struct { - connection *jsonrpc2.Conn - config config.Config - cachedDocuments map[string]string - cachedCommands map[string]map[string]parse.Command - cachedConstants map[string]map[string]parse.ConstantSymbol - cachedSymbols map[string]map[string]parse.Symbol - cachedMiscTokens map[string]map[string]parse.MiscToken - documentsMutex sync.Mutex - commandsMutex sync.Mutex - constantsMutex sync.Mutex - symbolsMutex sync.Mutex - miscTokensMutex sync.Mutex + connection *jsonrpc2.Conn + config config.Config + cachedDocuments map[string]string + cachedCommands map[string]map[string]parse.Command + cachedConstants map[string]map[string]parse.ConstantSymbol + cachedSymbols map[string]map[string]parse.Symbol + cachedMiscTokens map[string]map[string]parse.MiscToken + cachedAutovarCommands map[string]parser.CommandConfig + documentsMutex sync.Mutex + commandsMutex sync.Mutex + constantsMutex sync.Mutex + symbolsMutex sync.Mutex + miscTokensMutex sync.Mutex + commandConfigMutex sync.Mutex } // Runs the LSP server indefinitely.