Skip to content

Commit

Permalink
Support more functions (#70)
Browse files Browse the repository at this point in the history
Fetching attributes through builder pattern + mixins
  • Loading branch information
julienduchesne authored Sep 8, 2022
1 parent 462c4b9 commit 076cbec
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
17 changes: 14 additions & 3 deletions pkg/ast/processing/find_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,14 @@ func extractObjectRangesFromDesugaredObjs(stack *nodestack.NodeStack, vm *jsonne
if sameFileOnly && len(result) > 0 && result[0].Filename != stack.From.Loc().FileName {
continue
}
return result, err
if len(result) > 0 {
return result, err
}

fieldNodes = append(fieldNodes, fieldNode.Target)
case *ast.Function:
stack.Push(fieldNode.Body)
desugaredObjs = append(desugaredObjs, findDesugaredObjectFromStack(stack))
case *ast.Import:
filename := fieldNode.File.Value
newObjs := findTopLevelObjectsInFile(vm, filename, string(fieldNode.Loc().File.DiagnosticFileName))
Expand Down Expand Up @@ -217,8 +224,12 @@ func findObjectFieldInObject(objectNode *ast.DesugaredObject, index string) *ast

func findDesugaredObjectFromStack(stack *nodestack.NodeStack) *ast.DesugaredObject {
for !stack.IsEmpty() {
if curr, ok := stack.Pop().(*ast.DesugaredObject); ok {
return curr
switch node := stack.Pop().(type) {
case *ast.DesugaredObject:
return node
case *ast.Binary:
stack.Push(node.Left)
stack.Push(node.Right)
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func TestConfiguration_Formatting(t *testing.T) {
{
name: "all settings",
settings: map[string]interface{}{
"log_level": "info",
"log_level": "error",
"formatting": map[string]interface{}{
"Indent": 4,
"MaxBlankLines": 10,
Expand Down
42 changes: 36 additions & 6 deletions pkg/server/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,22 +741,22 @@ var definitionTestCases = []definitionTestCase{
{
name: "goto field through function",
filename: "testdata/goto-functions-advanced.libsonnet",
position: protocol.Position{Line: 6, Character: 46},
position: protocol.Position{Line: 15, Character: 48},
results: []definitionResult{{
targetRange: protocol.Range{
Start: protocol.Position{Line: 2, Character: 2},
End: protocol.Position{Line: 2, Character: 12},
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 12},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 2, Character: 2},
End: protocol.Position{Line: 2, Character: 6},
Start: protocol.Position{Line: 1, Character: 2},
End: protocol.Position{Line: 1, Character: 6},
},
}},
},
{
name: "goto field through function-created object",
filename: "testdata/goto-functions-advanced.libsonnet",
position: protocol.Position{Line: 8, Character: 52},
position: protocol.Position{Line: 16, Character: 54},
results: []definitionResult{{
targetRange: protocol.Range{
Start: protocol.Position{Line: 2, Character: 2},
Expand All @@ -768,6 +768,36 @@ var definitionTestCases = []definitionTestCase{
},
}},
},
{
name: "goto field through builder pattern attribute",
filename: "testdata/goto-functions-advanced.libsonnet",
position: protocol.Position{Line: 17, Character: 67},
results: []definitionResult{{
targetRange: protocol.Range{
Start: protocol.Position{Line: 5, Character: 4},
End: protocol.Position{Line: 5, Character: 14},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 5, Character: 4},
End: protocol.Position{Line: 5, Character: 8},
},
}},
},
{
name: "goto field through mixin attribute",
filename: "testdata/goto-functions-advanced.libsonnet",
position: protocol.Position{Line: 18, Character: 58},
results: []definitionResult{{
targetRange: protocol.Range{
Start: protocol.Position{Line: 11, Character: 4},
End: protocol.Position{Line: 11, Character: 14},
},
targetSelectionRange: protocol.Range{
Start: protocol.Position{Line: 11, Character: 4},
End: protocol.Position{Line: 11, Character: 8},
},
}},
},
}

func TestDefinition(t *testing.T) {
Expand Down
17 changes: 14 additions & 3 deletions pkg/server/testdata/goto-functions-advanced.libsonnet
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
local myfunc(arg1, arg2) = {
arg1: arg1,
arg2: arg2,

builderPattern(arg3):: self {
arg3: arg3,
},
};

{
accessThroughFunc: myfunc('test', 'test').arg2,
funcCreatedObj: myfunc('test', 'test'),
accesThroughFuncCreatedObj: self.funcCreatedObj.arg2,
withMixin(arg4):: {
arg4: arg4,
},
funcCreatedObj: myfunc('test1', 'test2').builderPattern('test3') + self.withMixin('test4'),

accessThroughFunc: myfunc('test1', 'test2').arg1,
accessThroughFuncCreatedObj: self.funcCreatedObj.arg2,
accessBuilderPatternThroughFuncCreatedObj: self.funcCreatedObj.arg3,
accessMixinThroughFuncCreatedObj: self.funcCreatedObj.arg4,

}

0 comments on commit 076cbec

Please sign in to comment.