From 43f19b29819557cae3e9d88303ba6add89bd2f95 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 26 Oct 2022 21:12:22 +0200 Subject: [PATCH] shell session: allow continuation without marker for PowerShell Fixes #2262 --- pygments/lexers/shell.py | 16 ++- .../pwsh-session/test_continuation.txt | 124 ++++++++++++++++++ 2 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 tests/snippets/pwsh-session/test_continuation.txt diff --git a/pygments/lexers/shell.py b/pygments/lexers/shell.py index cb9cb1debe..2a9513e787 100644 --- a/pygments/lexers/shell.py +++ b/pygments/lexers/shell.py @@ -157,6 +157,7 @@ class ShellSessionBaseLexer(Lexer): .. versionadded:: 2.1 """ + _bare_continuation = False _venv = re.compile(r'^(\([^)]*\))(\s*)') def get_tokens_unprocessed(self, text): @@ -175,10 +176,10 @@ def get_tokens_unprocessed(self, text): venv = venv_match.group(1) venv_whitespace = venv_match.group(2) insertions.append((len(curcode), - [(0, Generic.Prompt.VirtualEnv, venv)])) + [(0, Generic.Prompt.VirtualEnv, venv)])) if venv_whitespace: insertions.append((len(curcode), - [(0, Text, venv_whitespace)])) + [(0, Text, venv_whitespace)])) line = line[venv_match.end():] m = self._ps1rgx.match(line) @@ -196,11 +197,17 @@ def get_tokens_unprocessed(self, text): elif backslash_continuation: if line.startswith(self._ps2): insertions.append((len(curcode), - [(0, Generic.Prompt, line[:len(self._ps2)])])) + [(0, Generic.Prompt, + line[:len(self._ps2)])])) curcode += line[len(self._ps2):] else: curcode += line backslash_continuation = curcode.endswith('\\\n') + elif self._bare_continuation and line.startswith(self._ps2): + insertions.append((len(curcode), + [(0, Generic.Prompt, + line[:len(self._ps2)])])) + curcode += line[len(self._ps2):] else: if insertions: toks = innerlexer.get_tokens_unprocessed(curcode) @@ -777,8 +784,9 @@ class PowerShellSessionLexer(ShellSessionBaseLexer): mimetypes = [] _innerLexerCls = PowerShellLexer + _bare_continuation = True _ps1rgx = re.compile(r'^((?:\[[^]]+\]: )?PS[^>]*> ?)(.*\n?)') - _ps2 = '>> ' + _ps2 = '> ' class FishShellLexer(RegexLexer): diff --git a/tests/snippets/pwsh-session/test_continuation.txt b/tests/snippets/pwsh-session/test_continuation.txt new file mode 100644 index 0000000000..4735d369a2 --- /dev/null +++ b/tests/snippets/pwsh-session/test_continuation.txt @@ -0,0 +1,124 @@ +---input--- +PS> python -m doctest ` +> -o DONT_ACCEPT_TRUE_FOR_1 ` +> -o ELLIPSIS options.txt + +PS> $Params = @{ +> Height = 50 +> Width = 50 +> Depth = 50 +> Name = 'My Widget' +> ID = '10dbe43f-0269-48b8-96cb-447a755add55' +> } + + +PS> ls | +> grep "python" + +---tokens--- +'PS> ' Generic.Prompt +'python' Name +' ' Text +'-m' Name +' ' Text +'doctest' Name +' ' Text +'`' Punctuation +'\n' Text + +'> ' Generic.Prompt +'-o' Name +' ' Text +'DONT_ACCEPT_TRUE_FOR_1' Name +' ' Text +'`' Punctuation +'\n' Text + +'> ' Generic.Prompt +'-o' Name +' ' Text +'ELLIPSIS' Name +' ' Text +'options' Name +'.' Punctuation +'txt' Name +'\n' Text + +'\n' Generic.Output + +'PS> ' Generic.Prompt +' ' Text +'$Params' Name.Variable +' ' Text +'=' Punctuation +' ' Text +'@' Punctuation +'{' Punctuation +'\n' Text + +'> ' Generic.Prompt +' ' Text +'Height' Name +' ' Text +'=' Punctuation +' ' Text +'50' Name +'\n' Text + +'> ' Generic.Prompt +' ' Text +'Width' Name +' ' Text +'=' Punctuation +' ' Text +'50' Name +'\n' Text + +'> ' Generic.Prompt +' ' Text +'Depth' Name +' ' Text +'=' Punctuation +' ' Text +'50' Name +'\n' Text + +'> ' Generic.Prompt +' ' Text +'Name' Name +' ' Text +'=' Punctuation +' ' Text +"'My Widget'" Literal.String.Single +'\n' Text + +'> ' Generic.Prompt +' ' Text +'ID' Name +' ' Text +'=' Punctuation +' ' Text +"'10dbe43f-0269-48b8-96cb-447a755add55'" Literal.String.Single +'\n' Text + +'> ' Generic.Prompt +'}' Punctuation +'\n' Text + +'\n' Generic.Output + +'\n' Generic.Output + +'PS> ' Generic.Prompt +' ' Text +'ls ' Name.Builtin +'|' Punctuation +'\n' Text + +'> ' Generic.Prompt +'grep' Name +' ' Text +'"' Literal.String.Double +'python' Literal.String.Double +'"' Literal.String.Double +'\n' Text