Skip to content

Commit

Permalink
Support autovar commands
Browse files Browse the repository at this point in the history
  • Loading branch information
huderlem committed Aug 16, 2024
1 parent dd3a7b7 commit a8d34e4
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
40 changes: 40 additions & 0 deletions server/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion server/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 15 additions & 12 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a8d34e4

Please sign in to comment.