snuyanzin commented on code in PR #22063: URL: https://github.com/apache/flink/pull/22063#discussion_r1156488674
########## flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/parser/SqlClientSyntaxHighlighter.java: ########## @@ -0,0 +1,257 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.client.cli.parser; + +import org.apache.flink.table.api.SqlDialect; +import org.apache.flink.table.api.config.TableConfigOptions; +import org.apache.flink.table.client.config.SqlClientOptions; +import org.apache.flink.table.client.gateway.Executor; + +import org.jline.reader.LineReader; +import org.jline.reader.impl.DefaultHighlighter; +import org.jline.utils.AttributedString; +import org.jline.utils.AttributedStringBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; +import java.util.Properties; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** Sql Client syntax highlighter. */ +public class SqlClientSyntaxHighlighter extends DefaultHighlighter { + private static final Logger LOG = LoggerFactory.getLogger(SqlClientSyntaxHighlighter.class); + private static Set<String> flinkKeywordSet; + private static Set<Character> flinkKeywordCharacterSet; + + static { + try (InputStream is = + SqlClientSyntaxHighlighter.class.getResourceAsStream("/keywords.properties")) { + Properties props = new Properties(); + props.load(is); + flinkKeywordSet = + Collections.unmodifiableSet( + Arrays.stream(props.get("default").toString().split(";")) + .collect(Collectors.toSet())); + flinkKeywordCharacterSet = + flinkKeywordSet.stream() + .flatMap(t -> t.chars().mapToObj(c -> (char) c)) + .collect(Collectors.toSet()); + } catch (IOException e) { + LOG.error("Exception: ", e); + flinkKeywordSet = Collections.emptySet(); + } + } + + private final Executor executor; + + public SqlClientSyntaxHighlighter(Executor executor) { + this.executor = executor; + } + + @Override + public AttributedString highlight(LineReader reader, String buffer) { + final SyntaxHighlightStyle.BuiltInStyle style = + SyntaxHighlightStyle.BuiltInStyle.fromString( + executor.getSessionConfig() + .get(SqlClientOptions.DISPLAY_DEFAULT_COLOR_SCHEMA)); + + if (style == SyntaxHighlightStyle.BuiltInStyle.DEFAULT) { + return super.highlight(reader, buffer); + } + final String dialectName = + executor.getSessionConfig().get(TableConfigOptions.TABLE_SQL_DIALECT); + final SqlDialect dialect = + SqlDialect.HIVE.name().equalsIgnoreCase(dialectName) + ? SqlDialect.HIVE + : SqlDialect.DEFAULT; + return getHighlightedOutput(buffer, style.getHighlightStyle(), dialect); + } + + static AttributedString getHighlightedOutput( + String buffer, SyntaxHighlightStyle style, SqlDialect dialect) { + final AttributedStringBuilder highlightedOutput = new AttributedStringBuilder(); + State currentParseState = null; + StringBuilder word = new StringBuilder(); + for (int i = 0; i < buffer.length(); i++) { + final char currentChar = buffer.charAt(i); + if (currentParseState == null) { + currentParseState = State.computeStateAt(buffer, i, dialect); + if (currentParseState == null) { + if (!flinkKeywordCharacterSet.contains(Character.toUpperCase(currentChar))) { + handleWord(word, highlightedOutput, currentParseState, style, true); + highlightedOutput.append(currentChar); + } else { + word.append(currentChar); + } + } else { + handleWord(word, highlightedOutput, null, style, true); + currentParseState.getStyleSetter().accept(highlightedOutput, style); + highlightedOutput.append(currentParseState.getStart()); + i += currentParseState.getStart().length() - 1; + } + } else { + word.append(currentChar); + final String stateEnd = currentParseState.getEnd(); + if (buffer.regionMatches(i, stateEnd, 0, stateEnd.length())) { + handleWord(word, highlightedOutput, currentParseState, style, true); + i += stateEnd.length() - 1; + currentParseState = null; + } + } + } + handleWord(word, highlightedOutput, currentParseState, style, false); + return highlightedOutput.toAttributedString(); + } + + private static void handleWord( + StringBuilder word, + AttributedStringBuilder sb, + State currentState, Review Comment: in fact after adding `DEFAULT` state to enum no more `@Nullable` is required here -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
