Debian Codegraph @

src/com/google/javascript/jscomp/LightweightMessageFormatter.java

raw symbols SCIP
1 /*
2 * Copyright 2007 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.google.javascript.jscomp;
17
18 import static com.google.javascript.jscomp.SourceExcerptProvider.SourceExcerpt.LINE;
19
20 import com.google.common.base.Preconditions;
21 import com.google.common.base.Strings;
22 import com.google.javascript.jscomp.CheckLevel;
23 import com.google.javascript.jscomp.SourceExcerptProvider.ExcerptFormatter;
24 import com.google.javascript.jscomp.SourceExcerptProvider.SourceExcerpt;
25
26 /**
27 * Lightweight message formatter. The format of messages this formatter
28 * produces is very compact and to the point.
29 *
30 */
31 public class LightweightMessageFormatter extends AbstractMessageFormatter {
32 private SourceExcerpt excerpt;
33 private static final ExcerptFormatter excerptFormatter =
34 new LineNumberingFormatter();
35
36 /**
37 * A constructor for when the client doesn't care about source information.
38 */
39 private LightweightMessageFormatter() {
40 super(null);
41 this.excerpt = LINE;
42 }
43
44 public LightweightMessageFormatter(SourceExcerptProvider source) {
45 this(source, LINE);
46 }
47
48 public LightweightMessageFormatter(SourceExcerptProvider source,
49 SourceExcerpt excerpt) {
50 super(source);
51 Preconditions.checkNotNull(source);
52 this.excerpt = excerpt;
53 }
54
55 static LightweightMessageFormatter withoutSource() {
56 return new LightweightMessageFormatter();
57 }
58
59 @Override
60 public String formatError(JSError error) {
61 return format(error, false);
62 }
63
64 @Override
65 public String formatWarning(JSError warning) {
66 return format(warning, true);
67 }
68
69 private String format(JSError error, boolean warning) {
70 // extract source excerpt
71 SourceExcerptProvider source = getSource();
72 String sourceExcerpt = source == null ? null :
73 excerpt.get(
74 source, error.sourceName, error.lineNumber, excerptFormatter);
75
76 // formatting the message
77 StringBuilder b = new StringBuilder();
78 if (error.sourceName != null) {
79 b.append(error.sourceName);
80 if (error.lineNumber > 0) {
81 b.append(':');
82 b.append(error.lineNumber);
83 }
84 b.append(": ");
85 }
86
87 b.append(getLevelName(warning ? CheckLevel.WARNING : CheckLevel.ERROR));
88 b.append(" - ");
89
90 b.append(error.description);
91 b.append('\n');
92 if (sourceExcerpt != null) {
93 b.append(sourceExcerpt);
94 b.append('\n');
95 int charno = error.getCharno();
96
97 // padding equal to the excerpt and arrow at the end
98 // charno == sourceExpert.length() means something is missing
99 // at the end of the line
100 if (excerpt.equals(LINE)
101 && 0 <= charno && charno <= sourceExcerpt.length()) {
102 for (int i = 0; i < charno; i++) {
103 char c = sourceExcerpt.charAt(i);
104 if (Character.isWhitespace(c)) {
105 b.append(c);
106 } else {
107 b.append(' ');
108 }
109 }
110 b.append("^\n");
111 }
112 }
113 return b.toString();
114 }
115
116 /**
117 * Formats a region by appending line numbers in front, e.g.
118 * <pre> 9| if (foo) {
119 * 10| alert('bar');
120 * 11| }</pre>
121 * and return line excerpt without any modification.
122 */
123 static class LineNumberingFormatter implements ExcerptFormatter {
124 @Override
125 public String formatLine(String line, int lineNumber) {
126 return line;
127 }
128
129 @Override
130 public String formatRegion(Region region) {
131 if (region == null) {
132 return null;
133 }
134 String code = region.getSourceExcerpt();
135 if (code.length() == 0) {
136 return null;
137 }
138
139 // max length of the number display
140 int numberLength = Integer.toString(region.getEndingLineNumber())
141 .length();
142
143 // formatting
144 StringBuilder builder = new StringBuilder(code.length() * 2);
145 int start = 0;
146 int end = code.indexOf('\n', start);
147 int lineNumber = region.getBeginningLineNumber();
148 while (start >= 0) {
149 // line extraction
150 String line;
151 if (end < 0) {
152 line = code.substring(start);
153 if (line.length() == 0) {
154 return builder.substring(0, builder.length() - 1);
155 }
156 } else {
157 line = code.substring(start, end);
158 }
159 builder.append(" ");
160
161 // nice spaces for the line number
162 int spaces = numberLength - Integer.toString(lineNumber).length();
163 builder.append(Strings.repeat(" ", spaces));
164 builder.append(lineNumber);
165 builder.append("| ");
166
167 // end & update
168 if (end < 0) {
169 builder.append(line);
170 start = -1;
171 } else {
172 builder.append(line);
173 builder.append('\n');
174 start = end + 1;
175 end = code.indexOf('\n', start);
176 lineNumber++;
177 }
178 }
179 return builder.toString();
180 }
181 }
182 }