Skip to content

Commit

Permalink
fix test for lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
utahta committed Aug 23, 2024
1 parent 1d0d9ea commit a2acfef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
11 changes: 11 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
type Compiler struct {
importPaths []string
manualImport bool
importRule bool
}

// Option represents compiler option.
Expand All @@ -50,6 +51,13 @@ func ManualImportOption() Option {
}
}

// ImportRuleOption used to reference proto files imported by grpc.federation.file.import rule.
func ImportRuleOption() Option {
return func(c *Compiler) {
c.importRule = true
}
}

// New creates compiler instance.
func New() *Compiler {
return &Compiler{}
Expand Down Expand Up @@ -183,6 +191,9 @@ func (c *Compiler) Compile(ctx context.Context, file *source.File, opts ...Optio
}
files := []string{relPath}
files = append(files, file.Imports()...)
if c.importRule {
files = append(files, file.ImportsByImportRule()...)
}
linkedFiles, err := compiler.Compile(ctx, files...)
if err != nil {
return nil, &CompilerError{Err: err, ErrWithPos: r.errs}
Expand Down
20 changes: 10 additions & 10 deletions lsp/server/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func TestCompletion(t *testing.T) {
completer := server.NewCompleter(compiler.New(), slog.New(slog.NewJSONHandler(os.Stdout, nil)))
t.Run("method", func(t *testing.T) {
// resolver.method value position of Post in service.proto file
_, candidates, err := completer.Completion(ctx, nil, path, file, source.Position{
Line: 39,
_, candidates, err := completer.Completion(ctx, []string{"testdata"}, path, file, source.Position{
Line: 40,
Col: 19,
})
if err != nil {
Expand All @@ -44,8 +44,8 @@ func TestCompletion(t *testing.T) {

t.Run("request.field", func(t *testing.T) {
// resolver.request.field value position of Post in service.proto file
_, candidates, err := completer.Completion(ctx, nil, path, file, source.Position{
Line: 40,
_, candidates, err := completer.Completion(ctx, []string{"testdata"}, path, file, source.Position{
Line: 41,
Col: 28,
})
if err != nil {
Expand All @@ -61,8 +61,8 @@ func TestCompletion(t *testing.T) {

t.Run("request.by", func(t *testing.T) {
// resolver.request.by value position os Post in service.proto file
_, candidates, err := completer.Completion(ctx, nil, path, file, source.Position{
Line: 40,
_, candidates, err := completer.Completion(ctx, []string{"testdata"}, path, file, source.Position{
Line: 41,
Col: 38,
})
if err != nil {
Expand All @@ -78,8 +78,8 @@ func TestCompletion(t *testing.T) {

t.Run("filter response", func(t *testing.T) {
// resolver.response.field value position of Post in service.proto file
_, candidates, err := completer.Completion(ctx, nil, path, file, source.Position{
Line: 43,
_, candidates, err := completer.Completion(ctx, []string{"testdata"}, path, file, source.Position{
Line: 44,
Col: 28,
})
if err != nil {
Expand All @@ -96,8 +96,8 @@ func TestCompletion(t *testing.T) {

t.Run("message", func(t *testing.T) {
// def[2].message value position of Post in service.proto file
_, candidates, err := completer.Completion(ctx, nil, path, file, source.Position{
Line: 47,
_, candidates, err := completer.Completion(ctx, []string{"testdata"}, path, file, source.Position{
Line: 48,
Col: 17,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lsp/server/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (h *Handler) definitionWithLink(ctx context.Context, params *protocol.Defin
return nil, nil
}
h.logger.Info("node", slog.String("text", nodeInfo.RawText()))
protoFiles, err := h.compiler.Compile(ctx, file, compiler.ImportPathOption(h.importPaths...))
protoFiles, err := h.compiler.Compile(ctx, file, compiler.ImportPathOption(h.importPaths...), compiler.ImportRuleOption())
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions lsp/server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestHandler_Definition(t *testing.T) {
URI: mustTestdataAbs(t, "testdata/service.proto"),
},
Position: protocol.Position{
Line: 24,
Line: 25,
Character: 15,
},
},
Expand All @@ -184,8 +184,8 @@ func TestHandler_Definition(t *testing.T) {
{
URI: mustTestdataAbs(t, "testdata/service.proto"),
Range: protocol.Range{
Start: protocol.Position{Line: 32, Character: 8},
End: protocol.Position{Line: 32, Character: 12},
Start: protocol.Position{Line: 33, Character: 8},
End: protocol.Position{Line: 33, Character: 12},
},
},
},
Expand All @@ -198,7 +198,7 @@ func TestHandler_Definition(t *testing.T) {
URI: mustTestdataAbs(t, "testdata/service.proto"),
},
Position: protocol.Position{
Line: 38,
Line: 39,
Character: 19,
},
},
Expand All @@ -221,7 +221,7 @@ func TestHandler_Definition(t *testing.T) {
URI: mustTestdataAbs(t, "testdata/service.proto"),
},
Position: protocol.Position{
Line: 19,
Line: 20,
Character: 0,
},
},
Expand Down

0 comments on commit a2acfef

Please sign in to comment.