-1

I often collapse long functions while writing scripts in PowerShell ISE. However, if I introduce a syntax error while editing the script above a collapsed function, PowerShell ISE expands all of the functions below the syntax error.

How can I prevent ISE from expanding the functions?

Everything is fine before I assign a value to my new variable: Image showing function collapsed

I temporarily cause a syntax error when assigning my value, causing ISE to expand all of the collapsed functions below: enter image description here

2 Answers2

0

You can avoid this by adding the ending quotation marks as a comment before each function or collapsible syntax.

Example:

Function Do_Something {Write-Host "Hello World}
#'#"
Function Do_Something_Else {...}

In the above example, the first double-quotation mark caused the commented section to become a string, making the commented double-quotation mark now eligible to end the string. Preventing the unfinished string from effecting any collapsed syntax below. This also works for the single-quotation marks.

-1

That is because, due to the fact that you left out the ending quote, all text beyond is considered part of the string. So what you recognise as the function below, your PowerShell editor does not. And therefore it displays the entire text in dark red, the colour in which it displays 'static' text. Only when it reaches the second single quote in line 221 it considers that as the end of your text... (Hence the colour change!)

  • This doesn't answer the question. I understand that while adding new code I temporarily "create" syntax errors. But this is unavoidable; I can't type a closing quote until I first type the opening one! My question is how to tell ISE **not to expand collapsed code** when adding new code above it. – I say Reinstate Monica Feb 15 '19 at 12:29
  • "My question is how to tell ISE not to expand collapsed code when adding new code above it." My point is that, where you see these as separate code fragments, PowerShell ISE does not.You claim to be adapting one code fragment and be surprised that PowerShell de-collapses the other code fragment. However when typing, this becomes one code fragment for PowerShell ISE. – Rogier Langeveld Apr 02 '19 at 13:47
  • After typing your opening quote, this all **becomes one code fragment for PS ISE; whether you like it or not**. (PS ISE does not _anticipate_ you typing a closing quote later. It just looks at the facts.) The text fragment **does not stop at the end of the line** (!!) and the other code fragment does not necessarily start at the beginning of the line (!!); also the collapsing is merely is a visual thing; the text fragment it sees only ends after the closing quote. _And this closing quote it finds in line 221........._ This is supported by both the decollapsing and the coloring it applies... – Rogier Langeveld Apr 02 '19 at 14:14
  • Forgot to say: PS ISE collapses based on syntax. If you (temporarily) break the syntax, you break the collapsing logic. (You can collapse scriptblocks and similar; but if you break the block, you break the ability to collapse it.) So, in short...;-) I'm sorry but you can't... – Rogier Langeveld Apr 02 '19 at 14:24