Skip to content

Commit

Permalink
fix go to definition for local orbs
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Feb 6, 2024
1 parent 770540b commit 42da6f8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/ast/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type OrbInfo struct {
}

type OrbParsedAttributes struct {
URI protocol.URI
Name string

Commands map[string]Command
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/local_orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (doc *YamlDocument) parseLocalOrb(name string, orbNode *sitter.Node, adding
Source: orbContent,
Description: orbDoc.Description,
OrbParsedAttributes: ast.OrbParsedAttributes{
URI: doc.URI,
Name: name,

Commands: orbDoc.Commands,
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/yamlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,7 @@ func (doc *YamlDocument) ToOrbParsedAttributes() ast.OrbParsedAttributes {

func (doc *YamlDocument) FromOrbParsedAttributesToYamlDocument(orb ast.OrbParsedAttributes) YamlDocument {
return YamlDocument{
URI: orb.URI,
LocalOrbName: orb.Name,

RootNode: doc.RootNode,
Expand Down
93 changes: 93 additions & 0 deletions pkg/services/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (

"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/parser"
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/services/definition"
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/testHelpers"
utils "github.com/CircleCI-Public/circleci-yaml-language-server/pkg/utils"
"github.com/stretchr/testify/assert"
"go.lsp.dev/protocol"
"go.lsp.dev/uri"
)
Expand Down Expand Up @@ -430,3 +432,94 @@ func TestDefinition(t *testing.T) {
})
}
}

func TestDefinitionForLocalOrbsCommand(t *testing.T) {
fileURI := uri.File("some-uri")
yaml := `version: 2.1
orbs:
local:
commands:
cmd:
steps:
- run: echo << parameters.target >>
jobs:
job:
docker:
- image: cimg/node:21.6.1
steps:
- cmd`
context := testHelpers.GetDefaultLsContext()

doc, err := parser.ParseFromContent([]byte(yaml), context, fileURI, protocol.Position{})
assert.Nil(t, err)

def := definition.DefinitionStruct{Cache: utils.CreateCache(), Params: protocol.DefinitionParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: fileURI,
},
// Position is the job step `cmd`
Position: protocol.Position{
Line: 13,
Character: 14,
},
},
}, Doc: doc}
locations, err := def.Definition()
assert.Nil(t, err)
assert.Equal(t, locations, []protocol.Location{
{
URI: fileURI,
Range: protocol.Range{
Start: protocol.Position{Line: 5, Character: 6},
End: protocol.Position{Line: 7, Character: 45},
},
},
})
}

func TestDefinitionForLocalOrbsExecutor(t *testing.T) {
fileURI := uri.File("some-uri")
yaml := `version: 2.1
orbs:
local:
executors:
executor:
docker:
- image: cimg/base:2024.01
jobs:
job:
executor: executor
steps:
- run: echo "Hello World"`
context := testHelpers.GetDefaultLsContext()

doc, err := parser.ParseFromContent([]byte(yaml), context, fileURI, protocol.Position{})
assert.Nil(t, err)

def := definition.DefinitionStruct{Cache: utils.CreateCache(), Params: protocol.DefinitionParams{
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: fileURI,
},
// Position is the job step `cmd`
Position: protocol.Position{
Line: 10,
Character: 24,
},
},
}, Doc: doc}
locations, err := def.Definition()
assert.Nil(t, err)
assert.Equal(t, locations, []protocol.Location{
{
URI: fileURI,
Range: protocol.Range{
Start: protocol.Position{Line: 5, Character: 6},
End: protocol.Position{Line: 7, Character: 36},
},
},
})
}

0 comments on commit 42da6f8

Please sign in to comment.