17
17
18
18
# Eol chars accepted by the LSP protocol
19
19
# the ordering affects performance
20
- EOL_CHARS = [' \r \n ' , ' \r ' , ' \n ' ]
20
+ EOL_CHARS = [" \r \n " , " \r " , " \n " ]
21
21
EOL_REGEX = re .compile (f'({ "|" .join (EOL_CHARS )} )' )
22
22
23
23
log = logging .getLogger (__name__ )
24
24
25
25
26
26
def debounce (interval_s , keyed_by = None ):
27
27
"""Debounce calls to this function until interval_s seconds have passed."""
28
+
28
29
def wrapper (func ):
29
30
timers = {}
30
31
lock = threading .Lock ()
@@ -48,7 +49,9 @@ def run():
48
49
timer = threading .Timer (interval_s , run )
49
50
timers [key ] = timer
50
51
timer .start ()
52
+
51
53
return debounced
54
+
52
55
return wrapper
53
56
54
57
@@ -78,7 +81,9 @@ def find_parents(root, path, names):
78
81
# Search each of /a/b/c, /a/b, /a
79
82
while dirs :
80
83
search_dir = os .path .join (* dirs )
81
- existing = list (filter (os .path .exists , [os .path .join (search_dir , n ) for n in names ]))
84
+ existing = list (
85
+ filter (os .path .exists , [os .path .join (search_dir , n ) for n in names ])
86
+ )
82
87
if existing :
83
88
return existing
84
89
dirs .pop ()
@@ -92,11 +97,11 @@ def path_to_dot_name(path):
92
97
directory = os .path .dirname (path )
93
98
module_name , _ = os .path .splitext (os .path .basename (path ))
94
99
full_name = [module_name ]
95
- while os .path .exists (os .path .join (directory , ' __init__.py' )):
100
+ while os .path .exists (os .path .join (directory , " __init__.py" )):
96
101
this_directory = os .path .basename (directory )
97
102
directory = os .path .dirname (directory )
98
103
full_name = [this_directory ] + full_name
99
- return '.' .join (full_name )
104
+ return "." .join (full_name )
100
105
101
106
102
107
def match_uri_to_workspace (uri , workspaces ):
@@ -128,6 +133,7 @@ def merge_dicts(dict_a, dict_b):
128
133
129
134
If override_nones is True, then
130
135
"""
136
+
131
137
def _merge_dicts_ (a , b ):
132
138
for key in set (a .keys ()).union (b .keys ()):
133
139
if key in a and key in b :
@@ -143,15 +149,16 @@ def _merge_dicts_(a, b):
143
149
yield (key , a [key ])
144
150
elif b [key ] is not None :
145
151
yield (key , b [key ])
152
+
146
153
return dict (_merge_dicts_ (dict_a , dict_b ))
147
154
148
155
149
156
def escape_plain_text (contents : str ) -> str :
150
157
"""
151
158
Format plain text to display nicely in environments which do not respect whitespaces.
152
159
"""
153
- contents = contents .replace (' \t ' , ' \u00A0 ' * 4 )
154
- contents = contents .replace (' ' , ' \u00A0 ' * 2 )
160
+ contents = contents .replace (" \t " , " \u00A0 " * 4 )
161
+ contents = contents .replace (" " , " \u00A0 " * 2 )
155
162
return contents
156
163
157
164
@@ -160,17 +167,17 @@ def escape_markdown(contents: str) -> str:
160
167
Format plain text to display nicely in Markdown environment.
161
168
"""
162
169
# escape markdown syntax
163
- contents = re .sub (r' ([\\*_#[\]])' , r' \\\1' , contents )
170
+ contents = re .sub (r" ([\\*_#[\]])" , r" \\\1" , contents )
164
171
# preserve white space characters
165
172
contents = escape_plain_text (contents )
166
173
return contents
167
174
168
175
169
176
def wrap_signature (signature ):
170
- return ' ```python\n ' + signature + ' \n ```\n '
177
+ return " ```python\n " + signature + " \n ```\n "
171
178
172
179
173
- SERVER_SUPPORTED_MARKUP_KINDS = {' markdown' , ' plaintext' }
180
+ SERVER_SUPPORTED_MARKUP_KINDS = {" markdown" , " plaintext" }
174
181
175
182
176
183
def choose_markup_kind (client_supported_markup_kinds : List [str ]):
@@ -181,10 +188,12 @@ def choose_markup_kind(client_supported_markup_kinds: List[str]):
181
188
for kind in client_supported_markup_kinds :
182
189
if kind in SERVER_SUPPORTED_MARKUP_KINDS :
183
190
return kind
184
- return ' markdown'
191
+ return " markdown"
185
192
186
193
187
- def format_docstring (contents : str , markup_kind : str , signatures : Optional [List [str ]] = None ):
194
+ def format_docstring (
195
+ contents : str , markup_kind : str , signatures : Optional [List [str ]] = None
196
+ ):
188
197
"""Transform the provided docstring into a MarkupContent object.
189
198
190
199
If `markup_kind` is 'markdown' the docstring will get converted to
@@ -195,33 +204,24 @@ def format_docstring(contents: str, markup_kind: str, signatures: Optional[List[
195
204
to the provided contents of the docstring if given.
196
205
"""
197
206
if not isinstance (contents , str ):
198
- contents = ''
207
+ contents = ""
199
208
200
- if markup_kind == ' markdown' :
209
+ if markup_kind == " markdown" :
201
210
try :
202
211
value = docstring_to_markdown .convert (contents )
203
- return {
204
- 'kind' : 'markdown' ,
205
- 'value' : value
206
- }
212
+ return {"kind" : "markdown" , "value" : value }
207
213
except docstring_to_markdown .UnknownFormatError :
208
214
# try to escape the Markdown syntax instead:
209
215
value = escape_markdown (contents )
210
216
211
217
if signatures :
212
- value = wrap_signature (' \n ' .join (signatures )) + ' \n \n ' + value
218
+ value = wrap_signature (" \n " .join (signatures )) + " \n \n " + value
213
219
214
- return {
215
- 'kind' : 'markdown' ,
216
- 'value' : value
217
- }
220
+ return {"kind" : "markdown" , "value" : value }
218
221
value = contents
219
222
if signatures :
220
- value = '\n ' .join (signatures ) + '\n \n ' + value
221
- return {
222
- 'kind' : 'plaintext' ,
223
- 'value' : escape_plain_text (value )
224
- }
223
+ value = "\n " .join (signatures ) + "\n \n " + value
224
+ return {"kind" : "plaintext" , "value" : escape_plain_text (value )}
225
225
226
226
227
227
def clip_column (column , lines , line_number ):
@@ -230,7 +230,9 @@ def clip_column(column, lines, line_number):
230
230
231
231
https://microsoft.github.io/language-server-protocol/specification#position
232
232
"""
233
- max_column = len (lines [line_number ].rstrip ('\r \n ' )) if len (lines ) > line_number else 0
233
+ max_column = (
234
+ len (lines [line_number ].rstrip ("\r \n " )) if len (lines ) > line_number else 0
235
+ )
234
236
return min (column , max_column )
235
237
236
238
@@ -242,14 +244,16 @@ def position_to_jedi_linecolumn(document, position):
242
244
"""
243
245
code_position = {}
244
246
if position :
245
- code_position = {'line' : position ['line' ] + 1 ,
246
- 'column' : clip_column (position ['character' ],
247
- document .lines ,
248
- position ['line' ])}
247
+ code_position = {
248
+ "line" : position ["line" ] + 1 ,
249
+ "column" : clip_column (
250
+ position ["character" ], document .lines , position ["line" ]
251
+ ),
252
+ }
249
253
return code_position
250
254
251
255
252
- if os .name == 'nt' :
256
+ if os .name == "nt" :
253
257
import ctypes
254
258
255
259
kernel32 = ctypes .windll .kernel32
0 commit comments