fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
557d8b033
master
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 ( 8Name , 9Comment , 10String , 11Error , 12Number , 13Operator , 14Other , 15Generic , 16Whitespace , 17String , 18Text , 19Punctuation , 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 64This applies any special cases of ROLE_TO_TOKEN. 65""" 66if text .isspace (): 67# Here fish will return 'normal' or 'statement_terminator' for newline. 68return Text .Whitespace 69elif role == "quote" : 70# Check for single or double. 71return String .Single if text .startswith ("'" )else String .Double 72else : 73return ROLE_TO_TOKEN [role ] 74 75 76def tokenize_fish_command (code ,offset ): 77"""Tokenize some fish code, offset in a parent string, by shelling 78out to fish_indent. 79 80fish_indent will output a list of csv lines: start,end,type. 81 82This function returns a list of (start, tok, value) tuples, as 83Pygments expects. 84""" 85proc = subprocess .Popen ( 86 ["fish_indent" ,"--pygments" ], 87stdin = subprocess .PIPE , 88stdout = subprocess .PIPE , 89universal_newlines = False , 90 ) 91stdout ,_ = proc .communicate (code .encode ("utf-8" )) 92result = [] 93for line in stdout .decode ("utf-8" ).splitlines (): 94start ,end ,role = line .split ("," ) 95start ,end = int (start ),int (end ) 96value = code [start :end ] 97tok = token_for_text_and_role (value ,role ) 98result .append ((start + offset ,tok ,value )) 99return result 100 101 102class FishIndentLexer (Lexer ): 103name = "FishIndentLexer" 104aliases = ["fish" ,"fish-docs-samples" ] 105filenames = ["*.fish" ] 106 107def get_tokens_unprocessed (self ,input_text ): 108"""Return a list of (start, tok, value) tuples. 109 110start is the index into the string 111tok is the token type (as above) 112value is the string contents of the token 113""" 114result = [] 115if not any (s .startswith (">" )for s in input_text .splitlines ()): 116# No prompt, just tokenize everything. 117result = tokenize_fish_command (input_text ,0 ) 118else : 119# We have a prompt line. 120# Use a regexp because it will maintain string indexes for us. 121regex = re .compile (r"^(>_?\s*)?(.*\n?)" ,re .MULTILINE ) 122for m in regex .finditer (input_text ): 123if m .group (1 ): 124# Prompt line; highlight via fish syntax. 125result .append ((m .start (1 ),Generic .Prompt ,m .group (1 ))) 126result .extend (tokenize_fish_command (m .group (2 ),m .start (2 ))) 127else : 128# Non-prompt line representing output from a command. 129result .append ((m .start (2 ),Generic .Output ,m .group (2 ))) 130return result