Skip to content

Commit

Permalink
Work in progress venv parser.
Browse files Browse the repository at this point in the history
This is reported as issue #1253. This fix does not work yet -- the
parsing does not resume with the correct parser yet.
  • Loading branch information
Anteru committed Nov 24, 2019
1 parent 3c43bd4 commit 4bca06e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pygments/lexers/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class ShellSessionBaseLexer(Lexer):
.. versionadded:: 2.1
"""

_venv = r'^\([^)]*\)'

def get_tokens_unprocessed(self, text):
innerlexer = self._innerLexerCls(**self.options)

Expand All @@ -165,6 +168,7 @@ def get_tokens_unprocessed(self, text):
for match in line_re.finditer(text):
line = match.group()
m = re.match(self._ps1rgx, line)
venv_re = re.match(self._venv, line)
if backslash_continuation:
curcode += line
backslash_continuation = curcode.endswith('\\\n')
Expand All @@ -184,6 +188,11 @@ def get_tokens_unprocessed(self, text):
[(0, Generic.Prompt, line[:len(self._ps2)])]))
curcode += line[len(self._ps2):]
backslash_continuation = curcode.endswith('\\\n')
elif venv_re:
venv_match = venv_re.group(0)
insertions.append((len(venv_match),
[(0, Generic.Prompt, line[:len(venv_match)])]))
curcode += line[len(venv_match)]
else:
if insertions:
toks = innerlexer.get_tokens_unprocessed(curcode)
Expand Down
17 changes: 16 additions & 1 deletion tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_end_of_line_nums(lexer_bash):
assert list(lexer_bash.get_tokens(fragment)) == tokens


def test_needs_name(lexer_session):
def test_newline_in_echo(lexer_session):
fragment = u'$ echo \\\nhi\nhi\n'
tokens = [
(Token.Text, u''),
Expand Down Expand Up @@ -162,3 +162,18 @@ def test_msdos_gt_only(lexer_msdos):
(Token.Generic.Output, u'hi\n'),
]
assert list(lexer_msdos.get_tokens(fragment)) == tokens

def test_virtualenv(lexer_session):
fragment = u'(env) [~/project]$ foo -h\n'
tokens = [
(Token.Text, u''),
(Token.Generic.Prompt, u'(env)'),
(Token.Text, u' '),
(Token.Generic.Prompt, u'[~/project]$'),
(Token.Text, u' '),
(Token.Text, u'foo'),
(Token.Text, u' '),
(Token.Text, u'-h'),
(Token.Text, u'\n'),
]
assert list(lexer_session.get_tokens(fragment)) == tokens

0 comments on commit 4bca06e

Please sign in to comment.