Skip to content

Commit

Permalink
Add new option to c3lsp.json to be able to add more compiler arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
pherrymason committed Sep 13, 2024
1 parent 712e4d4 commit d163069
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
10 changes: 6 additions & 4 deletions server/cmd/lsp/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime/debug"
"time"

"github.com/pherrymason/c3-lsp/internal/c3c"
"github.com/pherrymason/c3-lsp/internal/lsp/server"
"github.com/pherrymason/c3-lsp/pkg/option"
)
Expand Down Expand Up @@ -47,10 +48,11 @@ func cmdLineArguments() (server.ServerOpts, bool, bool) {
//log.Printf("---------------")

return server.ServerOpts{
C3: server.C3Opts{
Version: option.None[string](),
Path: c3cPathOpt,
StdlibPath: stdlibPathOpt,
C3: c3c.C3Opts{
Version: option.None[string](),
Path: c3cPathOpt,
StdlibPath: stdlibPathOpt,
CompileArgs: []string{},
},
Diagnostics: server.DiagnosticsOpts{
Delay: time.Duration(*diagnosticsDelay),
Expand Down
13 changes: 10 additions & 3 deletions server/internal/c3c/c3c.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ func GetC3Version(c3Path option.Option[string]) option.Option[string] {
return option.None[string]()
}

func CheckC3ErrorsCommand(c3Path option.Option[string], projectPath string) (bytes.Buffer, bytes.Buffer, error) {
binary := binaryPath(c3Path)
command := exec.Command(binary, "build", "--test")
func CheckC3ErrorsCommand(c3Options C3Opts, projectPath string) (bytes.Buffer, bytes.Buffer, error) {
binary := binaryPath(c3Options.Path)

args := []string{"build", "--test"}
if len(c3Options.CompileArgs) > 0 {
args = append(args, c3Options.CompileArgs...)
}
log.Printf("C3C arguments used:", args)

command := exec.Command(binary, args...)
command.Dir = projectPath

// set var to get the output
Expand Down
10 changes: 10 additions & 0 deletions server/internal/c3c/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package c3c

import "github.com/pherrymason/c3-lsp/pkg/option"

type C3Opts struct {
Version option.Option[string] `json:"version"`
Path option.Option[string] `json:"path"`
StdlibPath option.Option[string] `json:"stdlib-path"`
CompileArgs []string `json:"compile-args"`
}
2 changes: 1 addition & 1 deletion server/internal/lsp/server/Diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *Server) RunDiagnostics(state *project_state.ProjectState, notify glsp.N
}

runDiagnostics := func() {
out, stdErr, err := c3c.CheckC3ErrorsCommand(s.options.C3.Path, state.GetProjectRootURI())
out, stdErr, err := c3c.CheckC3ErrorsCommand(s.options.C3, state.GetProjectRootURI())
log.Println("output:", out.String())
log.Println("output:", stdErr.String())
if err == nil {
Expand Down
20 changes: 10 additions & 10 deletions server/internal/lsp/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,14 @@ import (
"github.com/pherrymason/c3-lsp/pkg/option"
)

type C3Opts struct {
Version option.Option[string] `json:"version"`
Path option.Option[string] `json:"path"`
StdlibPath option.Option[string] `json:"stdlib-path"`
}

type DiagnosticsOpts struct {
Enabled bool `json:"enabled"`
Delay time.Duration `json:"delay"`
}

// ServerOpts holds the options to create a new Server.
type ServerOpts struct {
C3 C3Opts `json:"C3Opts"`
C3 c3c.C3Opts `json:"C3Opts"`
Diagnostics DiagnosticsOpts `json:"Diagnostics"`

LogFilepath option.Option[string]
Expand All @@ -34,9 +28,10 @@ type ServerOpts struct {

type ServerOptsJson struct {
C3 struct {
Version *string `json:"version,omitempty"`
Path *string `json:"path,omitempty"`
StdlibPath *string `json:"stdlib-path,omitempty"`
Version *string `json:"version,omitempty"`
Path *string `json:"path,omitempty"`
StdlibPath *string `json:"stdlib-path,omitempty"`
CompileArgs []string `json:"compile-args"`
}

Diagnostics struct {
Expand Down Expand Up @@ -78,11 +73,16 @@ func (s *Server) loadServerConfigurationForWorkspace(path string) {
if options.C3.Version != nil {
s.options.C3.Version = option.Some(*options.C3.Version)
}

if options.C3.Path != nil {
s.options.C3.Path = option.Some(*options.C3.Path)
// Get version from binary
}

if len(options.C3.CompileArgs) > 0 {
s.options.C3.CompileArgs = options.C3.CompileArgs
}

c3Version := c3c.GetC3Version(s.options.C3.Path)
if c3Version.IsSome() {
s.options.C3.Version = c3Version
Expand Down

0 comments on commit d163069

Please sign in to comment.