fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes Altmanningerdocs: restore default highlighting keywords and options in HTML557d8b033

master
4.1 KiB130 linesraw
1# This is a plugin for pygments that shells out to fish_indent.
2
3# Example of how to use this:
4# env PATH="/dir/containing/fish/indent/:$PATH" pygmentize -f terminal256 -l /path/to/fish_indent_lexer.py:FishIndentLexer -x ~/test.fish
5
6from pygments.lexer import Lexer
7from pygments.token import (
8    Name,
9    Comment,
10    String,
11    Error,
12    Number,
13    Operator,
14    Other,
15    Generic,
16    Whitespace,
17    String,
18    Text,
19    Punctuation,
20)
21import re
22import subprocess
23
24# A fallback token type.
25DEFAULT = Text
26
27# Mapping from fish token types to Pygments types.
28ROLE_TO_TOKEN = {
29    "normal": Name.Variable,
30    "error": Generic.Error,
31    "command": Name.Function,
32    "keyword": Name.Function,
33    "statement_terminator": Punctuation,
34    "param": Name.Constant,
35    "option": Name.Constant,
36    "comment": Comment,
37    "match": DEFAULT,
38    "search_match": DEFAULT,
39    "operat": Operator,
40    "escape": String.Escape,
41    "quote": String.Single,  # note, may be changed to double dynamically
42    "redirection": Punctuation,  # ?
43    "autosuggestion": Other,  # in practice won't be generated
44    "selection": DEFAULT,
45    "pager_progress": DEFAULT,
46    "pager_background": DEFAULT,
47    "pager_prefix": DEFAULT,
48    "pager_completion": DEFAULT,
49    "pager_description": DEFAULT,
50    "pager_secondary_background": DEFAULT,
51    "pager_secondary_prefix": DEFAULT,
52    "pager_secondary_completion": DEFAULT,
53    "pager_secondary_description": DEFAULT,
54    "pager_selected_background": DEFAULT,
55    "pager_selected_prefix": DEFAULT,
56    "pager_selected_completion": DEFAULT,
57    "pager_selected_description": DEFAULT,
58}
59
60
61def token_for_text_and_role(text, role):
62    """Return the pygments token for some input text and a fish role
63
64    This applies any special cases of ROLE_TO_TOKEN.
65    """
66    if text.isspace():
67        # Here fish will return 'normal' or 'statement_terminator' for newline.
68        return Text.Whitespace
69    elif role == "quote":
70        # Check for single or double.
71        return String.Single if text.startswith("'") else String.Double
72    else:
73        return ROLE_TO_TOKEN[role]
74
75
76def tokenize_fish_command(code, offset):
77    """Tokenize some fish code, offset in a parent string, by shelling
78    out to fish_indent.
79
80    fish_indent will output a list of csv lines: start,end,type.
81
82    This function returns a list of (start, tok, value) tuples, as
83    Pygments expects.
84    """
85    proc = subprocess.Popen(
86        ["fish_indent", "--pygments"],
87        stdin=subprocess.PIPE,
88        stdout=subprocess.PIPE,
89        universal_newlines=False,
90    )
91    stdout, _ = proc.communicate(code.encode("utf-8"))
92    result = []
93    for line in stdout.decode("utf-8").splitlines():
94        start, end, role = line.split(",")
95        start, end = int(start), int(end)
96        value = code[start:end]
97        tok = token_for_text_and_role(value, role)
98        result.append((start + offset, tok, value))
99    return result
100
101
102class FishIndentLexer(Lexer):
103    name = "FishIndentLexer"
104    aliases = ["fish", "fish-docs-samples"]
105    filenames = ["*.fish"]
106
107    def get_tokens_unprocessed(self, input_text):
108        """Return a list of (start, tok, value) tuples.
109
110        start is the index into the string
111        tok is the token type (as above)
112        value is the string contents of the token
113        """
114        result = []
115        if not any(s.startswith(">") for s in input_text.splitlines()):
116            # No prompt, just tokenize everything.
117            result = tokenize_fish_command(input_text, 0)
118        else:
119            # We have a prompt line.
120            # Use a regexp because it will maintain string indexes for us.
121            regex = re.compile(r"^(>_?\s*)?(.*\n?)", re.MULTILINE)
122            for m in regex.finditer(input_text):
123                if m.group(1):
124                    # Prompt line; highlight via fish syntax.
125                    result.append((m.start(1), Generic.Prompt, m.group(1)))
126                    result.extend(tokenize_fish_command(m.group(2), m.start(2)))
127                else:
128                    # Non-prompt line representing output from a command.
129                    result.append((m.start(2), Generic.Output, m.group(2)))
130        return result