Skip to content

Parse argument expansion : "${VARIABLE:="default"}" #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.6.0

- Support parsing `: "${VARIABLE:="default"}"` as a variable definition https://github.com/bash-lsp/bash-language-server/pull/693

## 4.5.5

- Use sourcing info even if `includeAllWorkspaceSymbols` is true to ensure that files not matching the `globPattern` (and therefore not part of the background analysis) is still resolved based on source commands. https://github.com/bash-lsp/bash-language-server/pull/695
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.5.5",
"version": "4.6.0",
"main": "./out/server.js",
"typings": "./out/server.d.ts",
"bin": {
Expand Down
17 changes: 17 additions & 0 deletions server/src/__tests__/__snapshots__/analyzer.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -934,5 +934,22 @@ Array [
},
"name": "ret",
},
Object {
"kind": 13,
"location": Object {
"range": Object {
"end": Object {
"character": 24,
"line": 273,
},
"start": Object {
"character": 5,
"line": 273,
},
},
"uri": "dummy-uri.sh",
},
"name": "FILE_PATH_EXPANSION",
},
]
`;
17 changes: 17 additions & 0 deletions server/src/__tests__/analyzer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,23 @@ describe('getAllVariables', () => {
},
"name": "RESET",
},
Object {
"kind": 13,
"location": Object {
"range": Object {
"end": Object {
"character": 14,
"line": 25,
},
"start": Object {
"character": 5,
"line": 25,
},
},
"uri": "file://__REPO_ROOT_FOLDER__/testing/fixtures/extension.inc",
},
"name": "FILE_PATH",
},
]
`)
})
Expand Down
121 changes: 66 additions & 55 deletions server/src/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,61 +560,72 @@ describe('server', () => {

// they are all variables
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"data": Object {
"type": 3,
},
"documentation": undefined,
"kind": 6,
"label": "BOLD",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **RED** - *defined in extension.inc*",
},
"kind": 6,
"label": "RED",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **GREEN** - *defined in extension.inc*",
},
"kind": 6,
"label": "GREEN",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **BLUE** - *defined in extension.inc*",
},
"kind": 6,
"label": "BLUE",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **RESET** - *defined in extension.inc*",
},
"kind": 6,
"label": "RESET",
},
]
`)
Array [
Object {
"data": Object {
"type": 3,
},
"documentation": undefined,
"kind": 6,
"label": "BOLD",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **RED** - *defined in extension.inc*",
},
"kind": 6,
"label": "RED",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **GREEN** - *defined in extension.inc*",
},
"kind": 6,
"label": "GREEN",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **BLUE** - *defined in extension.inc*",
},
"kind": 6,
"label": "BLUE",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **RESET** - *defined in extension.inc*",
},
"kind": 6,
"label": "RESET",
},
Object {
"data": Object {
"type": 3,
},
"documentation": Object {
"kind": "markdown",
"value": "Variable: **FILE_PATH** - *defined in extension.inc*",
},
"kind": 6,
"label": "FILE_PATH",
},
]
`)
})
})

Expand Down
55 changes: 39 additions & 16 deletions server/src/util/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ export function getGlobalDeclarations({
TreeSitterUtil.forEach(tree.rootNode, (node) => {
const followChildren = !GLOBAL_DECLARATION_LEAF_NODE_TYPES.has(node.type)

if (TreeSitterUtil.isDefinition(node)) {
const symbol = nodeToSymbolInformation({ node, uri })
if (symbol) {
const word = symbol.name
globalDeclarations[word] = symbol
}
const symbol = getDeclarationSymbolFromNode({ node, uri })
if (symbol) {
const word = symbol.name
globalDeclarations[word] = symbol
}

return followChildren
Expand All @@ -71,15 +69,10 @@ export function getAllDeclarationsInTree({
const symbols: LSP.SymbolInformation[] = []

TreeSitterUtil.forEach(tree.rootNode, (node) => {
if (TreeSitterUtil.isDefinition(node)) {
const symbol = nodeToSymbolInformation({ node, uri })

if (symbol) {
symbols.push(symbol)
}
const symbol = getDeclarationSymbolFromNode({ node, uri })
if (symbol) {
symbols.push(symbol)
}

return
})

return symbols
Expand Down Expand Up @@ -122,8 +115,6 @@ export function getLocalDeclarations({
uri,
})
}
} 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') {
Expand All @@ -134,6 +125,8 @@ export function getLocalDeclarations({
uri,
)
}
} else {
symbol = getDeclarationSymbolFromNode({ node: childNode, uri })
}

if (symbol) {
Expand Down Expand Up @@ -222,3 +215,33 @@ function nodeToSymbolInformation({
containerName,
)
}

function getDeclarationSymbolFromNode({
node,
uri,
}: {
node: Parser.SyntaxNode
uri: string
}): LSP.SymbolInformation | null {
if (TreeSitterUtil.isDefinition(node)) {
return nodeToSymbolInformation({ node, uri })
} else if (node.type === 'command' && node.text.startsWith(': ')) {
// : does argument expansion and retains the side effects.
// A common usage is to define default values of environment variables, e.g. : "${VARIABLE:="default"}".
const variableNode = node.namedChildren
.find((c) => c.type === 'string')
?.namedChildren.find((c) => c.type === 'expansion')
?.namedChildren.find((c) => c.type === 'variable_name')

if (variableNode) {
return LSP.SymbolInformation.create(
variableNode.text,
LSP.SymbolKind.Variable,
TreeSitterUtil.range(variableNode),
uri,
)
}
}

return null
}
6 changes: 5 additions & 1 deletion testing/fixtures/extension.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ extensionFunc() {

if [ "${ENV}" = "prod" ]; then
RED="" # Ignored as we do not flowtrace.
fi
fi

: "${FILE_PATH:="/default/file"}"

printf 'Printing %s\n' "$FILE_PATH"
4 changes: 4 additions & 0 deletions testing/fixtures/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,7 @@ fi
exit $ret

iverilog -i wave.vpp *{}.v

: "${FILE_PATH_EXPANSION:="/default/file"}"

printf 'Printing %s\n' "$FILE_PATH_EXPANSION"