Navigating in Sourced Files

BashSupport Pro supports the source command and also follows sourced files to locate declarations of variables and functions.

Both of the following scenarios are supported:

  1. The declaration is in a sourced file
  2. The declaration is in a file, which sources the current file

Please note, that Go to Declaration and related features are only available if the definition is in scope.

Source Command Support

If you use the source command with relative paths, then the plugin will automatically follow these files. It assumes that the paths are relative to the current file’s parent directory.

Definitions found in sourced files are supported by all features of BashSupport Pro, for example:

  • Code completion
  • Go to declaration
  • Rename
  • Quick documentation
  • Find usages
1
2
3
4
5
6
7
## a.bash

source ./lib.bash

# Renaming 'myFunc' here
# also renames it in lib.bash 
myFunc
1
2
3
4
5
6
7
## lib.bash, sourced by a.bash

# Renaming 'myFunc' here
# also renames it in a.bash 
myFunc() {
    echo hello world
}

Shellcheck Support

If you’re using dynamic values with the source command, then ShellCheck and BashSupport Pro need help to locate the file.

For example, code like this is commonly used:

1
. "${_basedir}/included-file.sh"

$_basedir is only known at runtime, of course. You need to provide a hint where the file is located inside your project.

Define a shellcheck source directive to tell BashSupport Pro where to find the sourced file:

1
2
3
# hint where to find the sources file
# shellcheck source=./lib/included-file.sh
. "${_basedir}/included-file.sh"

Now Go to Declaration, Find Usages…, Rename…, etc. will also take care of the definitions in the sourced file.

© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款