Skip to content

Commit

Permalink
Read c3lsp.json configuration file on workspace initialization and ov…
Browse files Browse the repository at this point in the history
…erwrite setted options.
  • Loading branch information
pherrymason committed Aug 29, 2024
1 parent a2b64e3 commit c6036f3
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 19 deletions.
11 changes: 11 additions & 0 deletions server/c3lsp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"C3": {
"version": "0.6.2",
"path": "c3c",
"stdlib-path": ""
},
"Diagnostics": {
"enabled": true,
"delay": 2000
}
}
18 changes: 11 additions & 7 deletions server/cmd/lsp/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ func cmdLineArguments() (server.ServerOpts, bool) {
//log.Printf("---------------")

return server.ServerOpts{
C3Version: c3VersionOpt,
C3CPath: c3cPathOpt,
DiagnosticsDelay: time.Duration(*diagnosticsDelay),
DiagnosticsEnabled: true,
LogFilepath: logFilePathOpt,
Debug: *debug,
SendCrashReports: *sendCrashReports,
C3: server.C3Opts{
Version: c3VersionOpt,
Path: c3cPathOpt,
},
Diagnostics: server.DiagnosticsOpts{
Delay: time.Duration(*diagnosticsDelay),
Enabled: true,
},
LogFilepath: logFilePathOpt,
Debug: *debug,
SendCrashReports: *sendCrashReports,
}, *showHelp
}

Expand Down
11 changes: 6 additions & 5 deletions server/internal/lsp/server/Diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import (

"github.com/pherrymason/c3-lsp/internal/lsp/project_state"
"github.com/pherrymason/c3-lsp/pkg/cast"
"github.com/pherrymason/c3-lsp/pkg/fs"
"github.com/tliron/glsp"
protocol "github.com/tliron/glsp/protocol_3_16"
)

func (s *Server) RunDiagnostics(state *project_state.ProjectState, notify glsp.NotifyFunc, delay bool) {
if !s.options.DiagnosticsEnabled {
if !s.options.Diagnostics.Enabled {
return
}

binary := "c3c"
if s.options.C3CPath.IsSome() {
binary = s.options.C3CPath.Get()
if s.options.C3.Path.IsSome() {
binary = s.options.C3.Path.Get()
}
command := exec.Command(binary, "build", "--test")
command.Dir = state.GetProjectRootURI()
Expand All @@ -46,7 +47,7 @@ func (s *Server) RunDiagnostics(state *project_state.ProjectState, notify glsp.N
errorsInfo, diagnosticsDisabled := extractErrorDiagnostics(stdErr.String())

if diagnosticsDisabled {
s.options.DiagnosticsEnabled = false
s.options.Diagnostics.Enabled = false
clearOldDiagnostics(s.state, notify)
return
}
Expand All @@ -71,7 +72,7 @@ func (s *Server) RunDiagnostics(state *project_state.ProjectState, notify glsp.N
go notify(
protocol.ServerTextDocumentPublishDiagnostics,
protocol.PublishDiagnosticsParams{
URI: fs.ConvertPathTOURI(errInfo.File),
URI: fs.ConvertPathToURI(errInfo.File),
Diagnostics: newDiagnostics,
})
}
Expand Down
4 changes: 3 additions & 1 deletion server/internal/lsp/server/Initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ func (s *Server) Initialize(serverName string, serverVersion string, capabilitie

if params.RootURI != nil {
s.state.SetProjectRootURI(utils.NormalizePath(*params.RootURI))
path, _ := fs.UriToPath(*params.RootURI)
s.loadServerConfigurationForWorkspace(path)
s.indexWorkspace()

s.RunDiagnostics(s.state, context.Notify, false)
}

if *params.Capabilities.TextDocument.PublishDiagnostics.RelatedInformation == false {
s.options.DiagnosticsEnabled = false
s.options.Diagnostics.Enabled = false
}

return protocol.InitializeResult{
Expand Down
64 changes: 64 additions & 0 deletions server/internal/lsp/server/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package server

import (
"encoding/json"
"io"
"log"
"os"
"time"

"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"`
Diagnostics DiagnosticsOpts `json:"Diagnostics"`

LogFilepath option.Option[string]
SendCrashReports bool
Debug bool
}

func (s *Server) loadServerConfigurationForWorkspace(path string) {
file, err := os.Open(path + "/c3lsp.json")
if err != nil {
// No configuration project file found.
log.Print("No configuration " + path + "/c3lsp.json found")
return
}
defer file.Close()

log.Print("Reading configuration " + path + "/c3lsp.json")

// Lee el archivo
data, err := io.ReadAll(file)
if err != nil {
log.Fatalf("Error reading config json: %v", err)
}

var options ServerOpts
err = json.Unmarshal(data, &options)
if err != nil {
log.Fatalf("Error deserializing config json: %v", err)
}

s.options = options

// Change log filepath?
// Should be able to do that form c3lsp.json?

// Enable/disable sendCrashReports
// Should be able to do that form c3lsp.json?
}
12 changes: 6 additions & 6 deletions server/internal/lsp/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Server struct {
}

// ServerOpts holds the options to create a new Server.
type ServerOpts struct {
/*type ServerOpts struct {
C3Version option.Option[string]
C3CPath option.Option[string]
LogFilepath option.Option[string]
Expand All @@ -42,7 +42,7 @@ type ServerOpts struct {
SendCrashReports bool
Debug bool
}
}*/

func NewServer(opts ServerOpts, appName string, version string) *Server {
var logpath *string
Expand All @@ -61,14 +61,14 @@ func NewServer(opts ServerOpts, appName string, version string) *Server {
logger.Debug("No crash reports")
}

if opts.C3Version.IsSome() {
logger.Debug(fmt.Sprintf("C3 Language version specified: %s", opts.C3Version.Get()))
if opts.C3.Version.IsSome() {
logger.Debug(fmt.Sprintf("C3 Language version specified: %s", opts.C3.Version.Get()))
}

handler := protocol.Handler{}
glspServer := glspserv.NewServer(&handler, appName, true)

requestedLanguageVersion := checkRequestedLanguageVersion(opts.C3Version)
requestedLanguageVersion := checkRequestedLanguageVersion(opts.C3.Version)

state := l.NewProjectState(logger, option.Some(requestedLanguageVersion.Number), opts.Debug)
parser := p.NewParser(logger)
Expand All @@ -83,7 +83,7 @@ func NewServer(opts ServerOpts, appName string, version string) *Server {
parser: &parser,
search: search,

diagnosticDebounced: debounce.New(opts.DiagnosticsDelay * time.Millisecond),
diagnosticDebounced: debounce.New(opts.Diagnostics.Delay * time.Millisecond),
}

handler.Initialized = func(context *glsp.Context, params *protocol.InitializedParams) error {
Expand Down

0 comments on commit c6036f3

Please sign in to comment.