Skip to content

Multi-line string variable formatting #510

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

Closed
SubThr33 opened this issue Feb 18, 2017 · 2 comments
Closed

Multi-line string variable formatting #510

SubThr33 opened this issue Feb 18, 2017 · 2 comments
Labels

Comments

@SubThr33
Copy link

System Details

  • Operating system name and version: Win10 x64
  • VS Code version: 1.8.1
  • PowerShell extension version: 0.9.0
  • Output from $PSVersionTable:
Name                           Value
----                           -----
PSVersion                      5.1.14393.693
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.693
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

Hi, I am not sure if this has been reported before, but multi-line strings do not seem to store correctly within powershell scripts when they are not left aligned to line space 0. If the closing '"@' is not in space 0, all code following this end marker is highlighted as a continuation of the comment block.

E.g. the following code displays only the required multi-line string as a intended.

                $DefaultConfigFile = @"
    @{
    # Specify the desired logs directory, by default this is the current working directory in a subfolder named logs
    LogFileDirectory = '.\Logs'

    # Specify the desired log file prefix, the default prefix is the first token of the last line of input
    LogFilePrefix = 'ChangeMe'

    # Specify the log file extension, this can be safely left at .txt, please include the leading full stop
    LogFileExtension = '.txt'

    # Specify the log datestamp format, this string will be passed to Get-Date -Format 
    LogFileDateFormat = 'yyyyMMddHHmm'

    # Specify the amount of days that the logs will be retained for
    LogFileRetentionPeriod = 10    
    }
"@
                Add-Content -Path ($pwd.Path + '\' + 'Out-Log.conf.psd1') -Value $DefaultConfigFile


Whereas the following code shows the rest of the script as a singular string, causing code warnings (Red squiggly lines)

                $DefaultConfigFile = @"
    @{
    # Specify the desired logs directory, by default this is the current working directory in a subfolder named logs
    LogFileDirectory = '.\Logs'

    # Specify the desired log file prefix, the default prefix is the first token of the last line of input
    LogFilePrefix = 'ChangeMe'

    # Specify the log file extension, this can be safely left at .txt, please include the leading full stop
    LogFileExtension = '.txt'

    # Specify the log datestamp format, this string will be passed to Get-Date -Format 
    LogFileDateFormat = 'yyyyMMddHHmm'

    # Specify the amount of days that the logs will be retained for
    LogFileRetentionPeriod = 10    
    }
                "@
                Add-Content -Path ($pwd.Path + '\' + 'Out-Log.conf.psd1') -Value $DefaultConfigFile

Sorry about the rough formatting, I am currently out on the road so I am not able to make it look "pretty" or upload logs.

Cheers,
SubThr33

Attached Logs

@rkeithhill
Copy link
Contributor

The multi-line strings you refer to are called here strings and the syntax requires that the closing quotation has to be the first character on the line. See the help topic man about_quoting_rules. Your second example is invalid syntax. The red squiggles you see are telling you that.

@rkeithhill
Copy link
Contributor

If you are experiencing a bug with the PowerShell code formatter (messing up here strings). We are tracking that issue here #977

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants