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

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