Skip to content

Commit abc8f54

Browse files
committed
language server support: --lang-from-project vs --lang-generic
1 parent add7655 commit abc8f54

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

cmd/bob/langserver.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,41 @@ import (
1010

1111
"github.com/function61/gokit/os/osutil"
1212
"github.com/function61/turbobob/pkg/bobfile"
13+
"github.com/samber/lo"
1314
"github.com/spf13/cobra"
1415
)
1516

1617
func langserverEntry() *cobra.Command {
1718
lang := ""
19+
langGeneric := ""
1820

1921
cmd := &cobra.Command{
2022
Use: "langserver",
2123
Short: "Launch a langserver process. Must be run in project's root dir. Intended to be run by your editor.",
2224
Args: cobra.NoArgs,
2325
Run: func(cmd *cobra.Command, _ []string) {
24-
if lang != "" { // explicit requested language (not defined by project)
25-
osutil.ExitIfError(langserverRunGeneric(osutil.CancelOnInterruptOrTerminate(nil), lang))
26+
if langGeneric != "" { // explicit requested language (not defined by project)
27+
osutil.ExitIfError(langserverRunGeneric(osutil.CancelOnInterruptOrTerminate(nil), langGeneric))
2628
return
2729
} else {
2830
// project's defined langserver
2931
osutil.ExitIfError(langserverRunShim(
30-
osutil.CancelOnInterruptOrTerminate(nil)))
32+
osutil.CancelOnInterruptOrTerminate(nil),
33+
lang))
3134

3235
}
3336
},
3437
}
3538

36-
cmd.Flags().StringVarP(&lang, "lang-generic", "", "", "Generic (= defined outside of project) language server to start")
39+
cmd.Flags().StringVarP(&lang, "lang-from-project", "", lang, "Language (go / ts / ...) whose language server to start")
40+
cmd.Flags().StringVarP(&langGeneric, "lang-generic", "", langGeneric, "Generic (= defined outside of project) language server to start")
3741

3842
return cmd
3943
}
4044

41-
// automatically detects which project this invocation concerns
42-
func langserverRunShim(ctx context.Context) error {
45+
// automatically detects which project this invocation concerns.
46+
// if `langCodeRequested` empty, accepts first language server that is found from builders.
47+
func langserverRunShim(ctx context.Context, langCodeRequested string) error {
4348
// access chosen project's details (so we know which langserver to start)
4449
projectFile, err := bobfile.Read()
4550
if err != nil {
@@ -54,11 +59,13 @@ func langserverRunShim(ctx context.Context) error {
5459
return nil, nil, fmt.Errorf("loadNonOptionalBaseImageConf: %w", err)
5560
}
5661

57-
if baseImageConf.Langserver == nil {
62+
lsDetails := baseImageConf.Langserver
63+
languageMatches := langCodeRequested == "" || lo.Contains(lsDetails.Languages, langCodeRequested)
64+
if lsDetails == nil || !languageMatches {
5865
continue
5966
}
6067

61-
return baseImageConf.Langserver.Command, &builder, nil
68+
return lsDetails.Command, &builder, nil
6269
}
6370

6471
return nil, nil, fmt.Errorf("%s doesn't define a compatible language server", projectFile.ProjectName)

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/moby/buildkit v0.19.0
1111
github.com/opencontainers/image-spec v1.1.0
1212
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
13+
github.com/samber/lo v1.50.0
1314
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4
1415
github.com/spf13/cobra v1.6.1
1516
go.i3wm.org/i3/v4 v4.18.0
@@ -32,7 +33,8 @@ require (
3233
github.com/rivo/uniseg v0.2.0 // indirect
3334
github.com/spf13/pflag v1.0.5 // indirect
3435
github.com/tonistiigi/go-csvvalue v0.0.0-20240710180619-ddb21b71c0b4 // indirect
35-
golang.org/x/sync v0.10.0 // indirect
36+
golang.org/x/sync v0.11.0 // indirect
3637
golang.org/x/sys v0.28.0 // indirect
38+
golang.org/x/text v0.22.0 // indirect
3739
google.golang.org/protobuf v1.35.2 // indirect
3840
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
4949
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
5050
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
5151
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
52+
github.com/samber/lo v1.50.0 h1:XrG0xOeHs+4FQ8gJR97zDz5uOFMW7OwFWiFVzqopKgY=
53+
github.com/samber/lo v1.50.0/go.mod h1:RjZyNk6WSnUFRKK6EyOhsRJMqft3G+pg7dCWHQCWvsc=
5254
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4 h1:8qmTC5ByIXO3GP/IzBkxcZ/99VITvnIETDhdFz/om7A=
5355
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
5456
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
@@ -79,6 +81,8 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
7981
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
8082
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
8183
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
84+
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
85+
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
8286
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8387
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8488
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -88,6 +92,8 @@ golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
8892
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
8993
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
9094
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
95+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
96+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
9197
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
9298
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
9399
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=

0 commit comments

Comments
 (0)