From 93707956399058f7b22ed944b1a80d38f5980cc8 Mon Sep 17 00:00:00 2001 From: skovhus Date: Mon, 2 Jan 2023 21:34:52 +0100 Subject: [PATCH 1/3] Support resolving loop variables --- server/src/__tests__/analyzer.test.ts | 25 +++++++++++++++++++++++++ server/src/util/declarations.ts | 10 ++++++++++ testing/fixtures/scope.sh | 5 +++++ 3 files changed, 40 insertions(+) diff --git a/server/src/__tests__/analyzer.test.ts b/server/src/__tests__/analyzer.test.ts index c804c730..c798c8f8 100644 --- a/server/src/__tests__/analyzer.test.ts +++ b/server/src/__tests__/analyzer.test.ts @@ -176,6 +176,31 @@ describe('findDeclarationLocations', () => { ] `) }) + + it('returns local declarations for loop variables', () => { + const result = analyzer.findDeclarationLocations({ + position: { character: 18, line: 39 }, + uri: FIXTURE_URI.SCOPE, + word: 'i', + }) + expect(result).toMatchInlineSnapshot(` + Array [ + Object { + "range": Object { + "end": Object { + "character": 5, + "line": 37, + }, + "start": Object { + "character": 4, + "line": 37, + }, + }, + "uri": "file://${FIXTURE_FOLDER}scope.sh", + }, + ] + `) + }) }) describe('findReferences', () => { diff --git a/server/src/util/declarations.ts b/server/src/util/declarations.ts index 4ada75fe..b288aabd 100644 --- a/server/src/util/declarations.ts +++ b/server/src/util/declarations.ts @@ -129,6 +129,16 @@ export function getLocalDeclarations({ } } else if (TreeSitterUtil.isDefinition(childNode)) { symbol = nodeToSymbolInformation({ node: childNode, uri }) + } else if (childNode.type === 'for_statement') { + const variableNode = childNode.child(1) + if (variableNode && variableNode.type === 'variable_name') { + symbol = LSP.SymbolInformation.create( + variableNode.text, + LSP.SymbolKind.Variable, + TreeSitterUtil.range(variableNode), + uri, + ) + } } if (symbol) { diff --git a/testing/fixtures/scope.sh b/testing/fixtures/scope.sh index a13da0c0..6eb5fc12 100644 --- a/testing/fixtures/scope.sh +++ b/testing/fixtures/scope.sh @@ -34,3 +34,8 @@ echo "${X}" f echo "${GLOBAL_2}" + +for i in 1 2 3 4 5 +do + echo "$GLOBAL_1 $i" +done From 4bd9ba6517ea7887e1fc8b27d2252a639affebee Mon Sep 17 00:00:00 2001 From: skovhus Date: Mon, 2 Jan 2023 21:35:04 +0100 Subject: [PATCH 2/3] Remove unnessary analyze call --- server/src/__tests__/analyzer.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/server/src/__tests__/analyzer.test.ts b/server/src/__tests__/analyzer.test.ts index c798c8f8..63bcec91 100644 --- a/server/src/__tests__/analyzer.test.ts +++ b/server/src/__tests__/analyzer.test.ts @@ -152,7 +152,6 @@ describe('findDeclarationLocations', () => { }) it('returns local declarations', () => { - analyzer.analyze({ uri: CURRENT_URI, document: FIXTURE_DOCUMENT.INSTALL }) const result = analyzer.findDeclarationLocations({ position: { character: 12, line: 12 }, uri: FIXTURE_URI.SCOPE, From 3db9798d4f73632311b95890e7420d6128641764 Mon Sep 17 00:00:00 2001 From: skovhus Date: Mon, 2 Jan 2023 21:38:17 +0100 Subject: [PATCH 3/3] Bump version to 4.2.1 --- server/CHANGELOG.md | 4 ++++ server/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/server/CHANGELOG.md b/server/CHANGELOG.md index e37c5e2d..2ef76faa 100644 --- a/server/CHANGELOG.md +++ b/server/CHANGELOG.md @@ -1,5 +1,9 @@ # Bash Language Server +## 4.2.1 + +- Add support for resolving loop variables https://github.com/bash-lsp/bash-language-server/pull/653 + ## 4.2.0 - Improve heuristic for resolving variables by taking the scope into account, both locally and when sourcing files. https://github.com/bash-lsp/bash-language-server/pull/649 diff --git a/server/package.json b/server/package.json index bcfb347a..e048f564 100644 --- a/server/package.json +++ b/server/package.json @@ -3,7 +3,7 @@ "description": "A language server for Bash", "author": "Mads Hartmann", "license": "MIT", - "version": "4.2.0", + "version": "4.2.1", "publisher": "mads-hartmann", "main": "./out/server.js", "typings": "./out/server.d.ts",