OILS / display / ui.py View on Github | oils.pub

636 lines, 332 significant
1# Copyright 2016 Andy Chu. All rights reserved.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7"""
8ui.py - User interface constructs.
9"""
10from __future__ import print_function
11
12from _devbuild.gen.id_kind_asdl import Id, Id_t, Id_str
13from _devbuild.gen.syntax_asdl import (
14 Token,
15 SourceLine,
16 loc,
17 loc_e,
18 loc_t,
19 command_t,
20 command_str,
21 source,
22 source_e,
23)
24from _devbuild.gen.value_asdl import value_e, value_t
25from asdl import format as fmt
26from data_lang import j8_lite
27from display import pp_value
28from display import pretty
29from frontend import lexer
30from frontend import location
31from mycpp import mylib
32from mycpp.mylib import print_stderr, tagswitch, log
33import libc
34
35from typing import List, Tuple, Optional, Any, cast, TYPE_CHECKING
36if TYPE_CHECKING:
37 from _devbuild.gen import arg_types
38 from core import error
39 from core.error import _ErrorWithLocation
40
41_ = log
42
43
44def ValType(val):
45 # type: (value_t) -> str
46 """For displaying type errors in the UI."""
47
48 # TODO: consolidate these functions
49 return pp_value.ValType(val)
50
51
52def CommandType(cmd):
53 # type: (command_t) -> str
54 """For displaying commands in the UI."""
55
56 # Displays 'Simple', 'BraceGroup', etc.
57 return command_str(cmd.tag(), dot=False)
58
59
60def PrettyId(id_):
61 # type: (Id_t) -> str
62 """For displaying type errors in the UI."""
63
64 # Displays 'Id.BoolUnary_v' for now
65 return Id_str(id_)
66
67
68def PrettyToken(tok):
69 # type: (Token) -> str
70 """Returns a readable token value for the user.
71
72 For syntax errors.
73 """
74 if tok.id == Id.Eof_Real:
75 return 'EOF'
76
77 val = tok.line.content[tok.col:tok.col + tok.length]
78 # TODO: Print length 0 as 'EOF'?
79 return repr(val)
80
81
82def PrettyDir(dir_name, home_dir):
83 # type: (str, Optional[str]) -> str
84 """Maybe replace the home dir with ~.
85
86 Used by the 'dirs' builtin and the prompt evaluator.
87 """
88 if home_dir is not None:
89 if dir_name == home_dir or dir_name.startswith(home_dir + '/'):
90 return '~' + dir_name[len(home_dir):]
91
92 return dir_name
93
94
95def _PrintCodeExcerpt(line, col, length, f):
96 # type: (str, int, int, mylib.Writer) -> None
97
98 buf = mylib.BufWriter()
99
100 buf.write(' ')
101
102 # TODO: Be smart about horizontal space when printing code snippet
103 # - Accept max_width param, which is terminal width or perhaps 100
104 # when there's no terminal
105 # - If 'length' of token is greater than max_width, then perhaps print 10
106 # chars on each side
107 # - If len(line) is less than max_width, then print everything normally
108 # - If len(line) is greater than max_width, then print up to max_width
109 # but make sure to include the entire token, with some context
110 # Print > < or ... to show truncation
111 #
112 # ^col 80 ^~~~~ error
113
114 buf.write(line.rstrip())
115
116 buf.write('\n ')
117 # preserve tabs
118 for c in line[:col]:
119 buf.write('\t' if c == '\t' else ' ')
120 buf.write('^')
121 buf.write('~' * (length - 1))
122 buf.write('\n')
123
124 # Do this all in a single write() call so it's less likely to be
125 # interleaved. See test/runtime-errors.sh errexit_multiple_processes
126 f.write(buf.getvalue())
127
128
129def _PrintTokenTooLong(loc_tok, f):
130 # type: (loc.TokenTooLong, mylib.Writer) -> None
131 line = loc_tok.line
132 col = loc_tok.col
133
134 buf = mylib.BufWriter()
135
136 buf.write(' ')
137 # Only print 10 characters, since it's probably very long
138 buf.write(line.content[:col + 10].rstrip())
139 buf.write('\n ')
140
141 # preserve tabs, like _PrintCodeExcerpt
142 for c in line.content[:col]:
143 buf.write('\t' if c == '\t' else ' ')
144
145 buf.write('^\n')
146
147 source_str = GetLineSourceString(loc_tok.line, quote_filename=True)
148 buf.write(
149 '%s:%d: Token starting at column %d is too long: %d bytes (%s)\n' %
150 (source_str, line.line_num, loc_tok.col, loc_tok.length,
151 Id_str(loc_tok.id)))
152
153 # single write() call
154 f.write(buf.getvalue())
155
156
157def GetFilenameString(line):
158 # type: (SourceLine) -> str
159 """Get the path of the file that a line appears in.
160
161 Returns "main" if it's stdin or -c
162 Returns "?" if it's not in a file.
163
164 Used by declare -F, with shopt -s extdebug.
165 """
166 src = line.src
167 UP_src = src
168
169 filename_str = '?' # default
170 with tagswitch(src) as case:
171 # Copying bash, it uses the string 'main'.
172 # I think ? would be better here, because this can get confused with a
173 # file 'main'. But it's fine for our task file usage.
174 if case(source_e.CFlag):
175 filename_str = 'main'
176 elif case(source_e.Stdin):
177 filename_str = 'main'
178
179 elif case(source_e.MainFile):
180 src = cast(source.MainFile, UP_src)
181 filename_str = src.path
182 elif case(source_e.OtherFile):
183 src = cast(source.OtherFile, UP_src)
184 filename_str = src.path
185
186 else:
187 pass
188 return filename_str
189
190
191def GetLineSourceString(line, quote_filename=False):
192 # type: (SourceLine, bool) -> str
193 """Returns a human-readable string for dev tools.
194
195 This function is RECURSIVE because there may be dynamic parsing.
196 """
197 src = line.src
198 UP_src = src
199
200 with tagswitch(src) as case:
201 if case(source_e.Interactive):
202 s = '[ interactive ]' # This might need some changes
203 elif case(source_e.Headless):
204 s = '[ headless ]'
205 elif case(source_e.CFlag):
206 s = '[ -c flag ]'
207 elif case(source_e.Stdin):
208 src = cast(source.Stdin, UP_src)
209 s = '[ stdin%s ]' % src.comment
210
211 elif case(source_e.MainFile):
212 src = cast(source.MainFile, UP_src)
213 # This will quote a file called '[ -c flag ]' to disambiguate it!
214 # also handles characters that are unprintable in a terminal.
215 s = src.path
216 if quote_filename:
217 s = j8_lite.EncodeString(s, unquoted_ok=True)
218 elif case(source_e.OtherFile):
219 src = cast(source.OtherFile, UP_src)
220 # ditto
221 s = src.path
222 if quote_filename:
223 s = j8_lite.EncodeString(s, unquoted_ok=True)
224
225 elif case(source_e.Dynamic):
226 src = cast(source.Dynamic, UP_src)
227
228 # Note: _PrintWithLocation() uses this more specifically
229
230 # TODO: check loc.Missing; otherwise get Token from loc_t, then line
231 blame_tok = location.TokenFor(src.location)
232 if blame_tok is None:
233 s = '[ %s at ? ]' % src.what
234 else:
235 line = blame_tok.line
236 line_num = line.line_num
237 outer_source = GetLineSourceString(
238 line, quote_filename=quote_filename)
239 s = '[ %s at line %d of %s ]' % (src.what, line_num,
240 outer_source)
241
242 elif case(source_e.Variable):
243 src = cast(source.Variable, UP_src)
244
245 if src.var_name is None:
246 var_name = '?'
247 else:
248 var_name = repr(src.var_name)
249
250 if src.location.tag() == loc_e.Missing:
251 where = '?'
252 else:
253 blame_tok = location.TokenFor(src.location)
254 assert blame_tok is not None
255 line_num = blame_tok.line.line_num
256 outer_source = GetLineSourceString(
257 blame_tok.line, quote_filename=quote_filename)
258 where = 'line %d of %s' % (line_num, outer_source)
259
260 s = '[ var %s at %s ]' % (var_name, where)
261
262 elif case(source_e.VarRef):
263 src = cast(source.VarRef, UP_src)
264
265 orig_tok = src.orig_tok
266 line_num = orig_tok.line.line_num
267 outer_source = GetLineSourceString(orig_tok.line,
268 quote_filename=quote_filename)
269 where = 'line %d of %s' % (line_num, outer_source)
270
271 var_name = lexer.TokenVal(orig_tok)
272 s = '[ contents of var %r at %s ]' % (var_name, where)
273
274 elif case(source_e.Alias):
275 src = cast(source.Alias, UP_src)
276 s = '[ expansion of alias %r ]' % src.argv0
277
278 elif case(source_e.Reparsed):
279 src = cast(source.Reparsed, UP_src)
280 span2 = src.left_token
281 outer_source = GetLineSourceString(span2.line,
282 quote_filename=quote_filename)
283 s = '[ %s in %s ]' % (src.what, outer_source)
284
285 elif case(source_e.Synthetic):
286 src = cast(source.Synthetic, UP_src)
287 s = '-- %s' % src.s # use -- to say it came from a flag
288
289 else:
290 raise AssertionError(src)
291
292 return s
293
294
295def _PrintWithLocation(prefix, msg, blame_loc, show_code):
296 # type: (str, str, loc_t, bool) -> None
297 """Print an error message attached to a location.
298
299 We may quote code this:
300
301 echo $foo
302 ^~~~
303 [ -c flag ]:1: Failed
304
305 Should we have multiple locations?
306
307 - single line and verbose?
308 - and turn on "stack" tracing? For 'source' and more?
309 """
310 f = mylib.Stderr()
311 if blame_loc.tag() == loc_e.TokenTooLong:
312 # test/spec.sh parse-errors shows this
313 _PrintTokenTooLong(cast(loc.TokenTooLong, blame_loc), f)
314 return
315
316 blame_tok = location.TokenFor(blame_loc)
317 if blame_tok is None: # When does this happen?
318 f.write('[??? no location ???] %s%s\n' % (prefix, msg))
319 return
320
321 orig_col = blame_tok.col
322 src = blame_tok.line.src
323 line = blame_tok.line.content
324 line_num = blame_tok.line.line_num # overwritten by source.Reparsed case
325
326 if show_code:
327 UP_src = src
328
329 with tagswitch(src) as case:
330 if case(source_e.Reparsed):
331 # Special case for LValue/backticks
332
333 # We want the excerpt to look like this:
334 # a[x+]=1
335 # ^
336 # Rather than quoting the internal buffer:
337 # x+
338 # ^
339
340 # Show errors:
341 # test/parse-errors.sh text-arith-context
342
343 src = cast(source.Reparsed, UP_src)
344 tok2 = src.left_token
345 line_num = tok2.line.line_num
346
347 line2 = tok2.line.content
348 lbracket_col = tok2.col + tok2.length
349 # NOTE: The inner line number is always 1 because of reparsing.
350 # We overwrite it with the original token.
351 _PrintCodeExcerpt(line2, orig_col + lbracket_col, 1, f)
352
353 elif case(source_e.Dynamic):
354 src = cast(source.Dynamic, UP_src)
355 # Special case for eval, unset, printf -v, etc.
356
357 # Show errors:
358 # test/runtime-errors.sh test-assoc-array
359
360 #print('OUTER blame_loc', blame_loc)
361 #print('OUTER tok', blame_tok)
362 #print('INNER src.location', src.location)
363
364 # Print code and location for MOST SPECIFIC location
365 _PrintCodeExcerpt(line, blame_tok.col, blame_tok.length, f)
366 source_str = GetLineSourceString(blame_tok.line,
367 quote_filename=True)
368 f.write('%s:%d\n' % (source_str, line_num))
369 f.write('\n')
370
371 # Recursive call: Print OUTER location, with error message
372 _PrintWithLocation(prefix, msg, src.location, show_code)
373 return
374
375 else:
376 _PrintCodeExcerpt(line, blame_tok.col, blame_tok.length, f)
377
378 source_str = GetLineSourceString(blame_tok.line, quote_filename=True)
379
380 # TODO: If the line is blank, it would be nice to print the last non-blank
381 # line too?
382 f.write('%s:%d: %s%s\n' % (source_str, line_num, prefix, msg))
383
384
385def CodeExcerptAndPrefix(blame_tok):
386 # type: (Token) -> Tuple[str, str]
387 """Return a string that quotes code, and a string location prefix.
388
389 Similar logic as _PrintWithLocation, except we know we have a token.
390 """
391 line = blame_tok.line
392
393 buf = mylib.BufWriter()
394 _PrintCodeExcerpt(line.content, blame_tok.col, blame_tok.length, buf)
395
396 source_str = GetLineSourceString(line, quote_filename=True)
397 prefix = '%s:%d: ' % (source_str, blame_tok.line.line_num)
398
399 return buf.getvalue(), prefix
400
401
402class ctx_Location(object):
403
404 def __init__(self, errfmt, location):
405 # type: (ErrorFormatter, loc_t) -> None
406 errfmt.loc_stack.append(location)
407 self.errfmt = errfmt
408
409 def __enter__(self):
410 # type: () -> None
411 pass
412
413 def __exit__(self, type, value, traceback):
414 # type: (Any, Any, Any) -> None
415 self.errfmt.loc_stack.pop()
416
417
418# TODO:
419# - ColorErrorFormatter
420# - BareErrorFormatter? Could just display the foo.sh:37:8: and not quotation.
421#
422# Are these controlled by a flag? It's sort of like --comp-ui. Maybe
423# --error-ui.
424
425
426class ErrorFormatter(object):
427 """Print errors with code excerpts.
428
429 Philosophy:
430 - There should be zero or one code quotation when a shell exits non-zero.
431 Showing the same line twice is noisy.
432 - When running parallel processes, avoid interleaving multi-line code
433 quotations. (TODO: turn off in child processes?)
434 """
435
436 def __init__(self):
437 # type: () -> None
438 self.loc_stack = [] # type: List[loc_t]
439 self.one_line_errexit = False # root process
440
441 def OneLineErrExit(self):
442 # type: () -> None
443 """Unused now.
444
445 For SubprogramThunk.
446 """
447 self.one_line_errexit = True
448
449 # A stack used for the current builtin. A fallback for UsageError.
450 # TODO: Should we have PushBuiltinName? Then we can have a consistent style
451 # like foo.sh:1: (compopt) Not currently executing.
452 def _FallbackLocation(self, blame_loc):
453 # type: (Optional[loc_t]) -> loc_t
454 if blame_loc is None or blame_loc.tag() == loc_e.Missing:
455 if len(self.loc_stack):
456 return self.loc_stack[-1]
457 return loc.Missing
458
459 return blame_loc
460
461 def PrefixPrint(self, msg, prefix, blame_loc):
462 # type: (str, str, loc_t) -> None
463 """Print a hard-coded message with a prefix, and quote code."""
464 _PrintWithLocation(prefix,
465 msg,
466 self._FallbackLocation(blame_loc),
467 show_code=True)
468
469 def Print_(self, msg, blame_loc=None):
470 # type: (str, loc_t) -> None
471 """Print message and quote code."""
472 _PrintWithLocation('',
473 msg,
474 self._FallbackLocation(blame_loc),
475 show_code=True)
476
477 def PrintMessage(self, msg, blame_loc=None):
478 # type: (str, loc_t) -> None
479 """Print a message WITHOUT quoting code."""
480 _PrintWithLocation('',
481 msg,
482 self._FallbackLocation(blame_loc),
483 show_code=False)
484
485 def StderrLine(self, msg):
486 # type: (str) -> None
487 """Just print to stderr."""
488 print_stderr(msg)
489
490 def PrettyPrintError(self, err, prefix=''):
491 # type: (_ErrorWithLocation, str) -> None
492 """Print an exception that was caught, with a code quotation.
493
494 Unlike other methods, this doesn't use the GetLocationForLine()
495 fallback. That only applies to builtins; instead we check
496 e.HasLocation() at a higher level, in CommandEvaluator.
497 """
498 # TODO: Should there be a special span_id of 0 for EOF? runtime.NO_SPID
499 # means there is no location info, but 0 could mean that the location is EOF.
500 # So then you query the arena for the last line in that case?
501 # Eof_Real is the ONLY token with 0 span, because it's invisible!
502 # Well Eol_Tok is a sentinel with span_id == runtime.NO_SPID. I think that
503 # is OK.
504 # Problem: the column for Eof could be useful.
505
506 _PrintWithLocation(prefix, err.UserErrorString(), err.location, True)
507
508 def PrintErrExit(self, err, pid):
509 # type: (error.ErrExit, int) -> None
510
511 # TODO:
512 # - Don't quote code if you already quoted something on the same line?
513 # - _PrintWithLocation calculates the line_id. So you need to remember that?
514 # - return it here?
515 prefix = 'errexit PID %d: ' % pid
516 _PrintWithLocation(prefix, err.UserErrorString(), err.location,
517 err.show_code)
518
519
520def PrintAst(node, flag):
521 # type: (command_t, arg_types.main) -> None
522
523 if flag.ast_format == 'none':
524 print_stderr('AST not printed.')
525 if 0:
526 from _devbuild.gen.id_kind_asdl import Id_str
527 from frontend.lexer import ID_HIST, LAZY_ID_HIST
528
529 print(LAZY_ID_HIST)
530 print(len(LAZY_ID_HIST))
531
532 for id_, count in ID_HIST.most_common(10):
533 print('%8d %s' % (count, Id_str(id_)))
534 print()
535 total = sum(ID_HIST.values())
536 uniq = len(ID_HIST)
537 print('%8d total tokens' % total)
538 print('%8d unique tokens IDs' % uniq)
539 print()
540
541 for id_, count in LAZY_ID_HIST.most_common(10):
542 print('%8d %s' % (count, Id_str(id_)))
543 print()
544 total = sum(LAZY_ID_HIST.values())
545 uniq = len(LAZY_ID_HIST)
546 print('%8d total tokens' % total)
547 print('%8d tokens with LazyVal()' % total)
548 print('%8d unique tokens IDs' % uniq)
549 print()
550
551 if 0:
552 from osh.word_parse import WORD_HIST
553 #print(WORD_HIST)
554 for desc, count in WORD_HIST.most_common(20):
555 print('%8d %s' % (count, desc))
556
557 else: # text output
558 f = mylib.Stdout()
559
560 do_abbrev = 'abbrev-' in flag.ast_format
561 perf_stats = flag.ast_format.startswith('__') # __perf or __dumpdoc
562
563 if perf_stats:
564 log('')
565 log('___ GC: after parsing')
566 mylib.PrintGcStats()
567 log('')
568
569 tree = node.PrettyTree(do_abbrev)
570
571 if perf_stats:
572 # Warning: __dumpdoc should only be passed with tiny -c fragments.
573 # This tree is huge and can eat up all memory.
574 fmt._HNodePrettyPrint(True,
575 flag.ast_format == '__dumpdoc',
576 tree,
577 f,
578 max_width=_GetMaxWidth())
579 else:
580 fmt.HNodePrettyPrint(tree, f, max_width=_GetMaxWidth())
581
582
583def TypeNotPrinted(val):
584 # type: (value_t) -> bool
585 return val.tag() in (value_e.Null, value_e.Bool, value_e.Int,
586 value_e.Float, value_e.Str, value_e.List,
587 value_e.Dict, value_e.Obj)
588
589
590def _GetMaxWidth():
591 # type: () -> int
592 max_width = 80 # default value
593 try:
594 width = libc.get_terminal_width()
595 if width > 0:
596 max_width = width
597 except (IOError, OSError):
598 pass # leave at default
599
600 return max_width
601
602
603def PrettyPrintValue(prefix, val, f, max_width=-1):
604 # type: (str, value_t, mylib.Writer, int) -> None
605 """For the = keyword"""
606
607 encoder = pp_value.ValueEncoder()
608 encoder.SetUseStyles(f.isatty())
609
610 # TODO: pretty._Concat, etc. shouldn't be private
611 if TypeNotPrinted(val):
612 mdocs = encoder.TypePrefix(pp_value.ValType(val))
613 mdocs.append(encoder.Value(val))
614 doc = pretty._Concat(mdocs)
615 else:
616 doc = encoder.Value(val)
617
618 if len(prefix):
619 # If you want the type name to be indented, which we don't
620 # inner = pretty._Concat([pretty._Break(""), doc])
621
622 doc = pretty._Concat([
623 pretty.AsciiText(prefix),
624 #pretty._Break(""),
625 pretty._Indent(4, doc)
626 ])
627
628 if max_width == -1:
629 max_width = _GetMaxWidth()
630
631 printer = pretty.PrettyPrinter(max_width)
632
633 buf = mylib.BufWriter()
634 printer.PrintDoc(doc, buf)
635 f.write(buf.getvalue())
636 f.write('\n')