Skip to content

Commit

Permalink
cmd/cue: add hidden lsp command
Browse files Browse the repository at this point in the history
Add a hidden 'lsp' command as a precursor to landing more LSP
functionality.

The CUE LSP was initially made available via cmd/cuepls, but making it
an "in built" part of cmd/cue makes things easier for the end user. And
making the switch now ahead of the initial experimental version that
will land in v0.11.0 helps to avoid breakage in vscode-cue and other
editor plugins that need to know what command to run to start the LSP
server.

FWIW this adds ~20% to the binary size of cmd/cue for now. Note that
despite the fact that subsequent changes will add more functionality to
'cue lsp', they are likely to remove a significant amount of cruft that
was left over from the inherited cuepls code base. Hence subsequent
changes may actually reduce the binary size, causing this initial
increase to be less pronounced. The benefit of having a single binary vs
multiple is considered more significant than the slight (temporary) cost
in binary size.

Signed-off-by: Paul Jolly <paul@myitcv.io>
Change-Id: I66f4d835a96382b93c1584779946ef3567c51aae
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1203289
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
myitcv committed Oct 30, 2024
1 parent e83de3e commit 9faab39
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
56 changes: 56 additions & 0 deletions cmd/cue/cmd/lsp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2024 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"context"

goplscmd "cuelang.org/go/internal/golangorgx/gopls/cmd"
"cuelang.org/go/internal/golangorgx/gopls/hooks"
"cuelang.org/go/internal/golangorgx/tools/tool"

"github.com/spf13/cobra"
)

func newLSPCmd(c *Command) *cobra.Command {
cmd := &cobra.Command{
Hidden: true,
Use: "lsp",
Short: "start or interact with a CUE Language Server instance",
Run: func(cmd *cobra.Command, args []string) {
c.Command = cmd
runLSP(c, args)
},
DisableFlagParsing: true,
}

// TODO(myitcv): flesh out docs.

// TODO(myitcv): move the LSP towards the same flag processing as used here
// in cmd/cue.

// TODO(myitcv): add some means for 'cue help lsp' triggering 'cue lsp
// -help' until such time as we flip the LSP command itself over to cobra
// (if that's what we want to do).

// TODO(myitcv): prevent the 'lsp' command from inheriting the root flags.

return cmd
}

func runLSP(cmd *Command, args []string) {
ctx := context.Background()
tool.Main(ctx, goplscmd.New(hooks.Options), args)
}
3 changes: 2 additions & 1 deletion cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,15 @@ func New(args []string) (*Command, error) {
newFmtCmd(c),
newGetCmd(c),
newImportCmd(c),
newLoginCmd(c),
newModCmd(c),
newTrimCmd(c),
newVersionCmd(c),
newVetCmd(c),

// Hidden
newAddCmd(c),
newLoginCmd(c),
newLSPCmd(c),
} {
cmd.AddCommand(sub)
}
Expand Down

0 comments on commit 9faab39

Please sign in to comment.