Skip to content

Commit

Permalink
Merge pull request #60 from pherrymason/file-config
Browse files Browse the repository at this point in the history
File config
  • Loading branch information
pherrymason authored Sep 1, 2024
2 parents 10abe30 + f165498 commit d401a25
Show file tree
Hide file tree
Showing 56 changed files with 1,289 additions and 1,150 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
2 changes: 1 addition & 1 deletion server/cmd/lsp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pherrymason/c3-lsp/internal/lsp/server"
)

const version = "0.2.1"
const version = "0.3.0"
const prerelease = false
const appName = "C3-LSP"

Expand Down
40 changes: 13 additions & 27 deletions server/cmd/stdlib_indexer/blurp.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func Generate_variable(variable *s.Variable, module *s.Module) jen.Code {
jen.Lit(variable.GetName()),
jen.Lit(variable.GetType().GetName()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
).
Dot("Build").Call()
}
Expand All @@ -28,8 +27,7 @@ func Generate_struct(strukt *s.Struct, module *s.Module) jen.Code {
Call(
jen.Lit(strukt.GetName()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
)

for _, member := range strukt.GetMembers() {
Expand All @@ -38,8 +36,7 @@ func Generate_struct(strukt *s.Struct, module *s.Module) jen.Code {
jen.Lit(member.GetName()),
jen.Lit(member.GetType().GetName()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
)
}

Expand All @@ -57,8 +54,7 @@ func Generate_bitstruct(bitstruct *s.Bitstruct, module *s.Module) jen.Code {
jen.Lit(bitstruct.GetName()),
jen.Lit(bitstruct.Type().GetName()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
)

for _, member := range bitstruct.Members() {
Expand All @@ -67,8 +63,7 @@ func Generate_bitstruct(bitstruct *s.Bitstruct, module *s.Module) jen.Code {
jen.Lit(member.GetName()),
jen.Lit(member.GetType().GetName()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
)
}

Expand All @@ -85,8 +80,7 @@ func Generate_definition(def *s.Def, module *s.Module) jen.Code {
Call(
jen.Lit(def.GetName()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
).
Dot("WithResolvesTo").
Call(
Expand All @@ -99,15 +93,13 @@ func Generate_definition(def *s.Def, module *s.Module) jen.Code {
}

func Generate_enum(enum *s.Enum, module *s.Module) jen.Code {
// NewEnumBuilder(name string, baseType string, module string, docId string)
enumDef := jen.
Qual(PackageName+"symbols", "NewEnumBuilder").
Call(
jen.Lit(enum.GetName()),
jen.Lit(enum.GetType()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
)

for _, enumerator := range enum.GetEnumerators() {
Expand All @@ -121,8 +113,7 @@ func Generate_enum(enum *s.Enum, module *s.Module) jen.Code {
jen.Lit(asv.GetName()),
jen.Lit(asv.GetType().GetName()),
jen.Lit(asv.GetModuleString()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
).
Dot("Build").Call(),
)
Expand All @@ -136,8 +127,7 @@ func Generate_enum(enum *s.Enum, module *s.Module) jen.Code {
jen.Qual(PackageName+"symbols", "NewEnumeratorBuilder").
Call(
jen.Lit(enumerator.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
).
Dot("WithAssociativeValues").Call(associativeValues).
Dot("Build").Call(),
Expand All @@ -150,15 +140,13 @@ func Generate_enum(enum *s.Enum, module *s.Module) jen.Code {
}

func Generate_fault(fault *s.Fault, module *s.Module) jen.Code {
// NewEnumBuilder(name string, baseType string, module string, docId string)
faultDef := jen.
Qual(PackageName+"symbols", "NewFaultBuilder").
Call(
jen.Lit(fault.GetName()),
jen.Lit(fault.GetType()),
jen.Lit(module.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(module.GetDocumentURI()),
)

for _, enumerator := range fault.GetConstants() {
Expand All @@ -168,7 +156,7 @@ func Generate_fault(fault *s.Fault, module *s.Module) jen.Code {
jen.Qual(PackageName+"symbols", "NewFaultConstantBuilder").
Call(
jen.Lit(enumerator.GetName()),
jen.Op("&").Id("docId"), //(enumerator.GetDocumentURI()),
jen.Lit(enumerator.GetDocumentURI()),
).
Dot("Build").Call(),
)
Expand All @@ -192,8 +180,7 @@ func Generate_function(fun *s.Function, mod *s.Module) jen.Code {
jen.Lit(mod.GetName()),
),
jen.Lit(mod.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(mod.GetDocumentURI()),
).
Dot("WithTypeIdentifier").
Call(jen.Lit(fun.GetTypeIdentifier()))
Expand All @@ -208,8 +195,7 @@ func Generate_function(fun *s.Function, mod *s.Module) jen.Code {
jen.Lit(mod.GetName()),
),
jen.Lit(mod.GetName()),
//jen.Lit(buildStdDocId(mod.GetDocumentURI())),
jen.Op("&").Id("docId"),
jen.Lit(mod.GetDocumentURI()),
)
}

Expand Down
10 changes: 6 additions & 4 deletions server/cmd/stdlib_indexer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ func generateCode(symbolsTable *symbols_table.SymbolsTable, c3Version string) {
dict := jen.Dict{}

uniqueModuleNames := map[string]bool{}
for _, ps := range symbolsTable.All() {
for docId, ps := range symbolsTable.All() {
for _, mod := range ps.Modules() {
if mod.IsPrivate() {
continue
}

// Rewrite its docId
mod.SetDocumentURI(strings.ReplaceAll(docId, "../../../assets/c3c/lib/std", "<stdlib-path>"))

_, ok := uniqueModuleNames[mod.GetName()]
if !ok {
uniqueModuleNames[mod.GetName()] = true
Expand All @@ -83,7 +86,7 @@ func generateCode(symbolsTable *symbols_table.SymbolsTable, c3Version string) {
Qual(PackageName+"symbols", "NewModuleBuilder").
Call(
jen.Lit(mod.GetName()),
jen.Op("&").Id("docId"),
jen.Lit(mod.GetDocumentURI()),
).
Dot("WithoutSourceCode").Call().
Dot("Build").Call()
Expand All @@ -100,7 +103,6 @@ func generateCode(symbolsTable *symbols_table.SymbolsTable, c3Version string) {
jen.Id("_").Op(",").Id("mod").Op(":=").Range().
Id("moduleCollection"),
).Block(
//jen.Qual("fmt", "Println").Call(jen.Id("i")),
jen.Id("parsedModules").Dot("RegisterModule").Call(jen.Id("mod")),
),
jen.Var().Id("module").Add(jen.Op("*")).Qual(PackageName+"symbols", "Module"),
Expand All @@ -122,7 +124,7 @@ func generateCode(symbolsTable *symbols_table.SymbolsTable, c3Version string) {
Call(
jen.Lit(gen.GetName()),
jen.Lit(mod.GetName()),
jen.Op("&").Id("docId"),
jen.Lit(mod.GetDocumentURI()),
jen.Qual(PackageName+"symbols", "NewRange").Call(
jen.Lit(0), jen.Lit(0), jen.Lit(0), jen.Lit(0),
),
Expand Down
13 changes: 9 additions & 4 deletions server/internal/lsp/project_state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ func NewProjectState(logger commonlog.Logger, languageVersion option.Option[stri
}

// Install stdlib symbols
stdlibModules := projectState.languageVersion.stdLibSymbols()
projectState.indexParsedSymbols(stdlibModules, stdlibModules.DocId())

projectState.symbolsTable.Register(stdlibModules, symbols_table.PendingToResolve{})
projectState.SetLanguageVersion(projectState.languageVersion)

return projectState
}
Expand Down Expand Up @@ -79,6 +76,14 @@ func (s *ProjectState) GetDocumentDiagnostics() map[string][]protocol.Diagnostic
return s.diagnostics
}

func (s *ProjectState) SetLanguageVersion(languageVersion Version) {
s.languageVersion = languageVersion
stdlibModules := languageVersion.stdLibSymbols()
s.indexParsedSymbols(stdlibModules, stdlibModules.DocId())

s.symbolsTable.Register(stdlibModules, symbols_table.PendingToResolve{})
}

func (s *ProjectState) SetDocumentDiagnostics(docId string, diagnostics []protocol.Diagnostic) {
s.diagnostics[docId] = diagnostics
}
Expand Down
8 changes: 4 additions & 4 deletions server/internal/lsp/search_params/search_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func buildPosition(line uint, character uint) idx.Position {
func TestSearchParams_BuildSearchBySymbolUnderCursor_finds_symbol_at_cursor_position(t *testing.T) {
sourceCode := "module system; int emu;"
doc := d.NewDocument("filename", sourceCode)
docId := &doc.URI
parsedModules := symbols_table.NewParsedModules(docId)
docId := doc.URI
parsedModules := symbols_table.NewParsedModules(&docId)
parsedModules.RegisterModule(idx.NewModule("system", docId, idx.NewRange(0, 0, 0, 0), idx.NewRange(0, 0, 0, 23)))

// position at int e|mu
Expand All @@ -43,7 +43,7 @@ func TestSearchParams_BuildSearchBySymbolUnderCursor_finds_all_parent_symbols(t
system.cpu.init();`
doc := d.NewDocument("filename", sourceCode)
parsedModules := symbols_table.NewParsedModules(&doc.URI)
parsedModules.RegisterModule(idx.NewModule("system", &doc.URI, idx.NewRange(0, 0, 0, 0), idx.NewRange(0, 0, 1, 18)))
parsedModules.RegisterModule(idx.NewModule("system", doc.URI, idx.NewRange(0, 0, 0, 0), idx.NewRange(0, 0, 1, 18)))

// Cursor at "i|init"
sp := BuildSearchBySymbolUnderCursor(&doc, parsedModules, buildPosition(2, 12))
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestSearchParams_BuildSearchBySymbolUnderCursor_finds_full_module_path(t *t
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
doc := d.NewDocument("filename", tt.source)
parsedModules := symbols_table.NewParsedModules(&doc.URI)
parsedModules.RegisterModule(idx.NewModule("system", &doc.URI, idx.NewRange(0, 0, 0, 0), idx.NewRange(0, 0, 10, 30)))
parsedModules.RegisterModule(idx.NewModule("system", doc.URI, idx.NewRange(0, 0, 0, 0), idx.NewRange(0, 0, 10, 30)))

sp := BuildSearchBySymbolUnderCursor(&doc, parsedModules, tt.position)

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: errInfo.File,
URI: fs.ConvertPathToURI(errInfo.File, s.options.C3.StdlibPath),
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
4 changes: 2 additions & 2 deletions server/internal/lsp/server/TextDocumentDeclaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func (h *Server) TextDocumentDeclaration(context *glsp.Context, params *protocol
}

symbol := identifierOption.Get()
if !symbol.HasSourceCode() {
if !symbol.HasSourceCode() && h.options.C3.StdlibPath.IsNone() {
return nil, nil
}

return protocol.Location{
URI: fs.ConvertPathToURI(symbol.GetDocumentURI()),
URI: fs.ConvertPathToURI(symbol.GetDocumentURI(), h.options.C3.StdlibPath),
Range: _prot.Lsp_NewRangeFromRange(symbol.GetIdRange()),
}, nil
}
4 changes: 2 additions & 2 deletions server/internal/lsp/server/TextDocumentDefinition.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func (h *Server) TextDocumentDefinition(context *glsp.Context, params *protocol.
}

symbol := identifierOption.Get()
if !symbol.HasSourceCode() {
if !symbol.HasSourceCode() && h.options.C3.StdlibPath.IsNone() {
return nil, nil
}

return protocol.Location{
URI: fs.ConvertPathToURI(symbol.GetDocumentURI()),
URI: fs.ConvertPathToURI(symbol.GetDocumentURI(), h.options.C3.StdlibPath),
Range: _prot.Lsp_NewRangeFromRange(symbol.GetIdRange()),
}, nil
}
Loading

0 comments on commit d401a25

Please sign in to comment.