Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork252
Expand file tree
/
Copy pathformatter.py
More file actions
136 lines (117 loc) · 4.09 KB
/
formatter.py
File metadata and controls
136 lines (117 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# The MIT License
#
# Copyright (c) 2008 Bob Farrell
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# A simple formatter for bpython to work with Pygments.
# Pygments really kicks ass, it made it really easy to
# get the exact behaviour I wanted, thanks Pygments.:)
# mypy: disallow_untyped_defs=True
# mypy: disallow_untyped_calls=True
fromtypingimportAny,TextIO
fromcollections.abcimportMutableMapping,Iterable
frompygments.formatterimportFormatter
frompygments.tokenimport (
_TokenType,
Keyword,
Name,
Comment,
String,
Error,
Number,
Operator,
Token,
Whitespace,
Literal,
Punctuation,
)
"""These format strings are pretty ugly.
\x01 represents a colour marker, which
can be preceded by one or two of
the following letters:
k, r, g, y, b, m, c, w, d
Which represent:
blacK, Red, Green, Yellow, Blue, Magenta,
Cyan, White, Default
e.g.\x01y for yellow,
\x01gb for green on blue background
\x02 represents the bold attribute
\x03 represents the start of the actual
text that is output (in this case it's
a %s for substitution)
\x04 represents the end of the string; this is
necessary because the strings are all joined
together at the end so the parser needs them
as delimiters
"""
Parenthesis=Token.Punctuation.Parenthesis
theme_map= {
Keyword:"keyword",
Name:"name",
Comment:"comment",
String:"string",
Literal:"string",
Error:"error",
Number:"number",
Token.Literal.Number.Float:"number",
Operator:"operator",
Punctuation:"punctuation",
Token:"token",
Whitespace:"background",
Parenthesis:"paren",
Parenthesis.UnderCursor:"operator",
}
classBPythonFormatter(Formatter):
"""This is the custom formatter for bpython.
Its format() method receives the tokensource
and outfile params passed to it from the
Pygments highlight() method and slops
them into the appropriate format string
as defined above, then writes to the outfile
object the final formatted string.
See the Pygments source for more info; it's pretty
straightforward."""
def__init__(
self,color_scheme:MutableMapping[str,str],**options:Any
)->None:
self.f_strings= {}
fork,vintheme_map.items():
self.f_strings[k]=f"\x01{color_scheme[v]}"
ifkisParenthesis:
# FIXME: Find a way to make this the inverse of the current
# background colour
self.f_strings[k]+="I"
super().__init__(**options)
defformat(
self,
tokensource:Iterable[MutableMapping[_TokenType,str]],
outfile:TextIO,
)->None:
o:str=""
fortoken,textintokensource:
iftext=="\n":
continue
whiletokennotinself.f_strings:
iftoken.parentisNone:
break
else:
token=token.parent
o+=f"{self.f_strings[token]}\x03{text}\x04"
outfile.write(o.rstrip())
# vim: sw=4 ts=4 sts=4 ai et