001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.cli.common.messages;
018    
019    import com.intellij.openapi.util.SystemInfo;
020    import com.intellij.util.LineSeparator;
021    import kotlin.text.StringsKt;
022    import org.fusesource.jansi.Ansi;
023    import org.fusesource.jansi.internal.CLibrary;
024    import org.jetbrains.annotations.NotNull;
025    import org.jetbrains.annotations.Nullable;
026    
027    import java.util.EnumSet;
028    import java.util.Set;
029    
030    import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*;
031    
032    public abstract class PlainTextMessageRenderer implements MessageRenderer {
033        // AnsiConsole doesn't check isatty() for stderr (see https://github.com/fusesource/jansi/pull/35).
034        // TODO: investigate why ANSI escape codes on Windows only work in REPL for some reason
035        private static final boolean COLOR_ENABLED =
036                !SystemInfo.isWindows &&
037                !"false".equals(System.getProperty("kotlin.colors.enabled")) &&
038                CLibrary.isatty(CLibrary.STDERR_FILENO) != 0;
039    
040        private static final String LINE_SEPARATOR = LineSeparator.getSystemLineSeparator().getSeparatorString();
041    
042        private static final Set<CompilerMessageSeverity> IMPORTANT_MESSAGE_SEVERITIES = EnumSet.of(EXCEPTION, ERROR, WARNING);
043    
044        @Override
045        public String renderPreamble() {
046            return "";
047        }
048    
049        @Override
050        public String render(
051                @NotNull CompilerMessageSeverity severity, @NotNull String message, @NotNull CompilerMessageLocation location
052        ) {
053            StringBuilder result = new StringBuilder();
054    
055            int line = location.getLine();
056            int column = location.getColumn();
057            String lineContent = location.getLineContent();
058    
059            String path = getPath(location);
060            if (path != null) {
061                result.append(path);
062                result.append(":");
063                if (line > 0) {
064                    result.append(line).append(":");
065                    if (column > 0) {
066                        result.append(column).append(":");
067                    }
068                }
069                result.append(" ");
070            }
071    
072            if (COLOR_ENABLED) {
073                Ansi ansi = Ansi.ansi()
074                        .bold()
075                        .fg(severityColor(severity))
076                        .a(severity.name().toLowerCase())
077                        .a(": ")
078                        .reset();
079    
080                if (IMPORTANT_MESSAGE_SEVERITIES.contains(severity)) {
081                    ansi.bold();
082                }
083    
084                // Only make the first line of the message bold. Otherwise long overload ambiguity errors or exceptions are hard to read
085                String decapitalized = decapitalizeIfNeeded(message);
086                int firstNewline = decapitalized.indexOf(LINE_SEPARATOR);
087                if (firstNewline < 0) {
088                    result.append(ansi.a(decapitalized).reset());
089                }
090                else {
091                    result.append(ansi.a(decapitalized.substring(0, firstNewline)).reset().a(decapitalized.substring(firstNewline)));
092                }
093            }
094            else {
095                result.append(severity.name().toLowerCase());
096                result.append(": ");
097                result.append(decapitalizeIfNeeded(message));
098            }
099    
100            if (lineContent != null && 1 <= column && column <= lineContent.length() + 1) {
101                result.append(LINE_SEPARATOR);
102                result.append(lineContent);
103                result.append(LINE_SEPARATOR);
104                result.append(StringsKt.repeat(" ", column - 1));
105                result.append("^");
106            }
107    
108            return result.toString();
109        }
110    
111        @NotNull
112        private static String decapitalizeIfNeeded(@NotNull String message) {
113            // TODO: invent something more clever
114            // An ad-hoc heuristic to prevent decapitalization of some names
115            if (message.startsWith("Java") || message.startsWith("Kotlin")) {
116                return message;
117            }
118    
119            // For abbreviations and capitalized text
120            if (message.length() >= 2 && Character.isUpperCase(message.charAt(0)) && Character.isUpperCase(message.charAt(1))) {
121                return message;
122            }
123    
124            return StringsKt.decapitalize(message);
125        }
126    
127        @NotNull
128        private static Ansi.Color severityColor(@NotNull CompilerMessageSeverity severity) {
129            switch (severity) {
130                case EXCEPTION:
131                    return Ansi.Color.RED;
132                case ERROR:
133                    return Ansi.Color.RED;
134                case WARNING:
135                    return Ansi.Color.YELLOW;
136                case INFO:
137                    return Ansi.Color.BLUE;
138                case LOGGING:
139                    return Ansi.Color.BLUE;
140                case OUTPUT:
141                    return Ansi.Color.BLUE;
142                default:
143                    throw new UnsupportedOperationException("Unknown severity: " + severity);
144            }
145        }
146    
147        @Nullable
148        protected abstract String getPath(@NotNull CompilerMessageLocation location);
149    
150        @Override
151        public String renderConclusion() {
152            return "";
153        }
154    }