Skip to content

Commit

Permalink
Merge pull request #653 from bash-lsp/support-loop-variables
Browse files Browse the repository at this point in the history
Support resolving loop variables
  • Loading branch information
skovhus authored Jan 2, 2023
2 parents e099658 + 3db9798 commit a18aff0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 25 additions & 1 deletion server/src/__tests__/analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -176,6 +175,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', () => {
Expand Down
10 changes: 10 additions & 0 deletions server/src/util/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions testing/fixtures/scope.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ echo "${X}"
f

echo "${GLOBAL_2}"

for i in 1 2 3 4 5
do
echo "$GLOBAL_1 $i"
done

0 comments on commit a18aff0

Please sign in to comment.