You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "snuyanzin (via GitHub)" <gi...@apache.org> on 2023/04/03 08:06:07 UTC

[GitHub] [flink] snuyanzin commented on a diff in pull request #22063: [FLINK-24909][Table SQL/Client] Add syntax highlighting

snuyanzin commented on code in PR #22063:
URL: https://github.com/apache/flink/pull/22063#discussion_r1155616710


##########
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(
+            String buffer, SyntaxHighlightStyle style, SqlDialect dialect) {
+        final Set<Character> stateStartSymbols =
+                Arrays.stream(State.values())
+                        .map(t -> t.getStart(dialect).charAt(0))
+                        .collect(Collectors.toSet());
+        final AttributedStringBuilder highlightedOutput = new AttributedStringBuilder();
+        State currentParseState = null;
+        int counter = 0;
+        StringBuilder word = new StringBuilder();
+        for (int i = 0; i < buffer.length(); i++) {
+            final char c = buffer.charAt(i);
+            if (currentParseState == null) {
+                if (stateStartSymbols.contains(c)) {

Review Comment:
   yes, sure
   I added more tests including mentioned here, also tried to make adding tests for syntax highlighting simpler



-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org