From 5b13afa6da8f8f96474ffe48766fd2b3c139f4a4 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 13 Nov 2024 11:15:28 -0700 Subject: [PATCH] d2lsp: getboardatposition edge case --- d2lsp/d2lsp.go | 5 +++++ d2lsp/d2lsp_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/d2lsp/d2lsp.go b/d2lsp/d2lsp.go index 9454a6c55b..fbd2c4d753 100644 --- a/d2lsp/d2lsp.go +++ b/d2lsp/d2lsp.go @@ -143,6 +143,11 @@ func getBoardPathAtPosition(m d2ast.Map, currPath []string, pos d2ast.Position) return deeperPath } + // We're in between boards, e.g. layers.x.scenarios + // Which means, there's no board at this position + if len(newPath)%2 == 1 { + return nil + } // Nothing deeper matched but we're in this map's range, return current path return newPath } diff --git a/d2lsp/d2lsp_test.go b/d2lsp/d2lsp_test.go index 13cff76036..65c78e4be0 100644 --- a/d2lsp/d2lsp_test.go +++ b/d2lsp/d2lsp_test.go @@ -291,6 +291,19 @@ layers: { position: d2ast.Position{Line: 3, Column: 2}, want: []string{"layers", "basic"}, }, + { + name: "cursor in between", + fs: map[string]string{ + "index.d2": ` + layers: { + basic: { + } + }`, + }, + path: "index.d2", + position: d2ast.Position{Line: 2, Column: 2}, + want: nil, + }, } for _, tt := range tests {