snuyanzin commented on code in PR #22063: URL: https://github.com/apache/flink/pull/22063#discussion_r1156501755
########## flink-table/flink-sql-client/src/main/java/org/apache/flink/table/client/cli/parser/SqlClientSyntaxHighlighter.java: ########## @@ -0,0 +1,246 @@ +/* + * 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.gateway.Executor; + +import org.jline.reader.LineReader; +import org.jline.reader.impl.DefaultHighlighter; +import org.jline.utils.AttributedString; +import org.jline.utils.AttributedStringBuilder; + +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; + +import static org.apache.flink.table.client.cli.CliClient.COLOR_SCHEMA_VAR; + +/** Sql Client syntax highlighter. */ +public class SqlClientSyntaxHighlighter extends DefaultHighlighter { + private static final Set<String> FLINK_KEYWORD_SET; + private static final Set<String> HIVE_KEYWORD_SET; + + static { + try (InputStream is = + SqlClientSyntaxHighlighter.class.getResourceAsStream("/keywords.properties")) { + Properties props = new Properties(); + props.load(is); + FLINK_KEYWORD_SET = + Collections.unmodifiableSet( + Arrays.stream(props.get("default").toString().split(";")) + .collect(Collectors.toSet())); + HIVE_KEYWORD_SET = + Collections.unmodifiableSet( + Arrays.stream(props.get("hive").toString().split(";")) + .collect(Collectors.toSet())); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private final Executor executor; + + public SqlClientSyntaxHighlighter(Executor executor) { + this.executor = executor; + } + + @Override + public AttributedString highlight(LineReader reader, String buffer) { + + final Object colorSchemeOrdinal = reader.getVariable(COLOR_SCHEMA_VAR); + SyntaxHighlightStyle.BuiltInStyle style = + SyntaxHighlightStyle.BuiltInStyle.fromOrdinal( + colorSchemeOrdinal == null ? 0 : (Integer) colorSchemeOrdinal); + if (style == SyntaxHighlightStyle.BuiltInStyle.DEFAULT) { + return super.highlight(reader, buffer); + } + final SqlDialect dialect = + executor.getSessionConfig() + .get(TableConfigOptions.TABLE_SQL_DIALECT) + .equalsIgnoreCase(SqlDialect.HIVE.toString()) + ? SqlDialect.HIVE + : SqlDialect.DEFAULT; + return getHighlightedOutput(buffer, style.getHighlightStyle(), dialect); + } + + static AttributedString getHighlightedOutput( Review Comment: In general it shows the behavior howbeit there are some details like splitting words. In fact if there is a `DEFAULT` state and current symbol is not a part of word, e.g. whitespace or some other character then it's better to append it directly to output. This will allow to not care about any possible whitespace or non word related prefixes while checking against keywords -- 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]
