| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package com.google.dart.compiler; | 5 package com.google.dart.compiler; |
| 6 | 6 |
| 7 import com.google.common.io.Closeables; | 7 import com.google.common.io.Closeables; |
| 8 import com.google.dart.compiler.CompilerConfiguration.ErrorFormat; |
| 8 | 9 |
| 9 import java.io.BufferedReader; | 10 import java.io.BufferedReader; |
| 10 import java.io.IOException; | 11 import java.io.IOException; |
| 11 import java.io.PrintStream; | 12 import java.io.PrintStream; |
| 12 import java.io.Reader; | 13 import java.io.Reader; |
| 13 | 14 |
| 14 /** | 15 /** |
| 15 * An error formatter that scans the source file and prints the error line and | 16 * An error formatter that scans the source file and prints the error line and |
| 16 * some context around it. This formatter has two modes: with or without color. | 17 * some context around it. This formatter has two modes: with or without color. |
| 17 * When using colors, it prints the error message in red, and it highlights the | 18 * When using colors, it prints the error message in red, and it highlights the |
| 18 * portion of the line containing the error in red. Without colors, it prints an | 19 * portion of the line containing the error in red. Without colors, it prints an |
| 19 * extra line underlying the portion of the line containing the error. | 20 * extra line underlying the portion of the line containing the error. |
| 20 */ | 21 */ |
| 21 public class PrettyErrorFormatter extends DefaultErrorFormatter { | 22 public class PrettyErrorFormatter extends DefaultErrorFormatter { |
| 22 private static String RED_BOLD_COLOR = "\033[31;1m"; | 23 private static String RED_BOLD_COLOR = "\033[31;1m"; |
| 23 private static String RED_COLOR = "\033[31m"; | 24 private static String RED_COLOR = "\033[31m"; |
| 24 private static String NO_COLOR = "\033[0m"; | 25 private static String NO_COLOR = "\033[0m"; |
| 25 | 26 |
| 26 private final boolean useColor; | 27 private final boolean useColor; |
| 27 private final boolean printMachineProblems; | |
| 28 | 28 |
| 29 public PrettyErrorFormatter(PrintStream outputStream, | 29 public PrettyErrorFormatter(PrintStream outputStream, |
| 30 boolean useColor, | 30 boolean useColor, |
| 31 boolean printMachineProblems) { | 31 ErrorFormat errorFormat) { |
| 32 super(outputStream); | 32 super(outputStream, errorFormat); |
| 33 this.useColor = useColor; | 33 this.useColor = useColor; |
| 34 this.printMachineProblems = printMachineProblems; | |
| 35 } | 34 } |
| 36 | 35 |
| 37 @Override | 36 @Override |
| 38 public void format(DartCompilationError event) { | 37 public void format(DartCompilationError event) { |
| 39 Source sourceFile = event.getSource(); | 38 Source sourceFile = event.getSource(); |
| 40 | 39 |
| 41 // if this is an unknown source type, default to the basic error formatter | 40 // if this is an unknown source type, default to the basic error formatter |
| 42 if (!(sourceFile instanceof DartSource) && !(sourceFile instanceof LibrarySo
urce)) { | 41 if (!(sourceFile instanceof DartSource) && !(sourceFile instanceof LibrarySo
urce)) { |
| 43 super.format(event); | 42 super.format(event); |
| 44 return; | 43 return; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 72 int length = event.getLength(); | 71 int length = event.getLength(); |
| 73 col = between(col, 0, lineText.length()); | 72 col = between(col, 0, lineText.length()); |
| 74 length = between(length, 0, lineText.length() - col); | 73 length = between(length, 0, lineText.length() - col); |
| 75 length = length == 0 ? lineText.length() - col : length; | 74 length = length == 0 ? lineText.length() - col : length; |
| 76 | 75 |
| 77 // print the error message | 76 // print the error message |
| 78 StringBuilder buf = new StringBuilder(); | 77 StringBuilder buf = new StringBuilder(); |
| 79 if (useColor) { | 78 if (useColor) { |
| 80 buf.append(RED_BOLD_COLOR); | 79 buf.append(RED_BOLD_COLOR); |
| 81 } | 80 } |
| 82 if (printMachineProblems) { | 81 if (errorFormat == ErrorFormat.MACHINE) { |
| 83 buf.append(String.format( | 82 buf.append(String.format( |
| 84 "%s:%s:%s:%s:%d:%d:%d: %s", | 83 "%s:%s:%s:%s:%d:%d:%d: %s", |
| 85 event.getErrorCode().getErrorSeverity(), | 84 event.getErrorCode().getErrorSeverity(), |
| 86 event.getErrorCode().getSubSystem(), | 85 event.getErrorCode().getSubSystem(), |
| 87 event.getErrorCode(), | 86 event.getErrorCode(), |
| 88 sourceFile.getName(), | 87 sourceFile.getName(), |
| 89 event.getLineNumber(), | 88 event.getLineNumber(), |
| 90 1 + col, | 89 1 + col, |
| 91 length, | 90 length, |
| 92 event.getMessage())); | 91 event.getMessage())); |
| 93 } else { | 92 } else { |
| 93 String sourceName = sourceFile.getUri().toString(); |
| 94 String includeFrom = getImportString(sourceFile); |
| 94 buf.append(String.format( | 95 buf.append(String.format( |
| 95 "%s:%d: %s", | 96 "%s:%d: %s%s", |
| 96 sourceFile.getName(), | 97 sourceName, |
| 97 event.getLineNumber(), | 98 event.getLineNumber(), |
| 98 event.getMessage())); | 99 event.getMessage(), |
| 100 includeFrom)); |
| 99 } | 101 } |
| 100 if (useColor) { | 102 if (useColor) { |
| 101 buf.append(NO_COLOR); | 103 buf.append(NO_COLOR); |
| 102 } | 104 } |
| 103 buf.append("\n"); | 105 buf.append("\n"); |
| 104 // show the previous line for context | 106 // show the previous line for context |
| 105 if (lineBefore != null) { | 107 if (lineBefore != null) { |
| 106 buf.append(String.format("%6d: %s\n", line - 1, lineBefore)); | 108 buf.append(String.format("%6d: %s\n", line - 1, lineBefore)); |
| 107 } | 109 } |
| 108 | 110 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 159 } |
| 158 | 160 |
| 159 /** | 161 /** |
| 160 * Returns the closest value in {@code [start,end]} to the given value. If | 162 * Returns the closest value in {@code [start,end]} to the given value. If |
| 161 * the given range is entirely empty, then {@code start} is returned. | 163 * the given range is entirely empty, then {@code start} is returned. |
| 162 */ | 164 */ |
| 163 private static int between(int val, int start, int end) { | 165 private static int between(int val, int start, int end) { |
| 164 return Math.max(start, Math.min(val, end)); | 166 return Math.max(start, Math.min(val, end)); |
| 165 } | 167 } |
| 166 } | 168 } |
| OLD | NEW |