Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/domaframework/doma/intellij/Sql.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private item ::= (comment | literal | word |
BOOLEAN )
private comment ::= (block_comment | LINE_COMMENT)
private literal ::= (STRING | NUMBER)
private word ::= (KEYWORD | WORD | DATATYPE)
private word ::= (KEYWORD | WORD | FUNCTION_NAME | DATATYPE)
block_comment ::= "/*" (el_directive | BLOCK_COMMENT_CONTENT?) "*/" {
pin=1
mixin="org.domaframework.doma.intellij.psi.SqlElCommentExprImpl"
Expand Down
143 changes: 16 additions & 127 deletions src/main/java/org/domaframework/doma/intellij/Sql.flex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.intellij.lexer.FlexLexer;

import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import org.domaframework.doma.intellij.SqlTokenHelper;

import org.domaframework.doma.intellij.psi.SqlTokenType;
import org.domaframework.doma.intellij.psi.SqlTypes;

%%
Expand All @@ -19,132 +21,19 @@ import org.domaframework.doma.intellij.psi.SqlTypes;
%function advance
%type IElementType
%{
// SQL keywords
private static final Set<String> KEYWORDS = Set.of(
"add",
"after",
"alter",
"all",
"and",
"as",
"asc",
"between",
"breadth",
"by",
"case",
"change",
"check",
"conflict",
"constraint",
"column",
"collate",
"comment",
"create",
"cross",
"cycle",
"database",
"default",
"delete",
"desc",
"distinct",
"do",
"drop",
"else",
"end",
"except",
"exists",
"first",
"foreign",
"from",
"full",
"group",
"having",
"if",
"in",
"index",
"inner",
"insert",
"intersect",
"into",
"is",
"join",
"key",
"lateral",
"left",
"like",
"limit",
"not",
"nothing",
"null",
"materialized",
"modify",
"offset",
"on",
"outer",
"or",
"order",
"primary",
"references",
"rename",
"returning",
"recursive",
"right",
"search",
"select",
"set",
"table",
"temporary",
"then",
"to",
"truncate",
"union",
"unique",
"update",
"using",
"values",
"view",
"when",
"where",
"with"
);

// COLUMN DataTypes
private static final Set<String> DATATYPES = Set.of(
"int",
"integer",
"smallint",
"bigint",
"tinyint",
"float",
"double",
"decimal",
"numeric",
"char",
"varchar",
"text",
"date",
"time",
"timestamp",
"datetime",
"boolean",
"bit",
"binary",
"varbinary",
"blob",
"clob",
"json",
"enum",
"set"
);

private static boolean isKeyword(CharSequence word) {
// TODO Reads plugin settings and allows users to register arbitrary keywords
return KEYWORDS.contains(word.toString().toLowerCase());
}

private static boolean isColumnDataType(CharSequence word) {
return DATATYPES.contains(word.toString().toLowerCase());
}
private static boolean isKeyword(CharSequence word) {
// TODO Reads plugin settings and allows users to register arbitrary keywords
return SqlTokenHelper.getKeyword().contains(word.toString().toLowerCase());
}

private static boolean isColumnDataType(CharSequence word) {
return SqlTokenHelper.getDataTypeTokens().contains(word.toString().toLowerCase());
}

private static boolean isWindowFunction(CharSequence word) {
// TODO Reads plugin settings and allows users to register arbitrary function names
return SqlTokenHelper.getFunctionTokens().contains(word.toString().toLowerCase());
}
%}

%eof{ return;
Expand Down Expand Up @@ -181,7 +70,7 @@ El_NonWordPart = [=<>\-,/*();\R \n\t\f]
{LineComment} { return SqlTypes.LINE_COMMENT; }
{String} { return SqlTypes.STRING; }
{Number} { return SqlTypes.NUMBER; }
{Word} { return isKeyword(yytext()) ? SqlTypes.KEYWORD : isColumnDataType(yytext()) ? SqlTypes.DATATYPE : SqlTypes.WORD; }
{Word} { return isKeyword(yytext()) ? SqlTypes.KEYWORD : isColumnDataType(yytext()) ? SqlTypes.DATATYPE : isWindowFunction(yytext()) ? SqlTypes.FUNCTION_NAME : SqlTypes.WORD; }
"." { return SqlTypes.DOT; }
"," { return SqlTypes.COMMA; }
"+" { return SqlTypes.PLUS;}
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/domaframework/doma/intellij/SqlTokenHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Doma Tools Authors
*
* Licensed 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
*
* https://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.domaframework.doma.intellij;

import java.util.HashSet;
import java.util.Set;
import org.domaframework.doma.intellij.tokens.MySqlFunctionToken;
import org.domaframework.doma.intellij.tokens.OracleFunctionToken;
import org.domaframework.doma.intellij.tokens.PostgresSqlFunctionToken;
import org.domaframework.doma.intellij.tokens.SqlDataTypeTokenUtil;
import org.domaframework.doma.intellij.tokens.SqlFunctionToken;
import org.domaframework.doma.intellij.tokens.SqlKeywordTokenUtil;

public class SqlTokenHelper {

static final Set<String> KEYWORD_TOKENS = new HashSet<>();
static final Set<String> DATA_TYPE_TOKENS = new HashSet<>();
static final Set<String> FUNCTION_TOKENS = new HashSet<>();

static {
// Initialize keyword tokens
KEYWORD_TOKENS.addAll(SqlKeywordTokenUtil.getTokens());

// Initialize DataType tokens
DATA_TYPE_TOKENS.addAll(SqlDataTypeTokenUtil.getTokens());

// Initialize function tokens
FUNCTION_TOKENS.addAll(SqlFunctionToken.getTokens());
FUNCTION_TOKENS.addAll(PostgresSqlFunctionToken.getTokens());
FUNCTION_TOKENS.addAll(MySqlFunctionToken.getTokens());
FUNCTION_TOKENS.addAll(OracleFunctionToken.getTokens());
}

// Keywords
public static Set<String> getKeyword() {
return KEYWORD_TOKENS;
}

// Data Types
public static Set<String> getDataTypeTokens() {
return DATA_TYPE_TOKENS;
}

// Functions
public static Set<String> getFunctionTokens() {
return FUNCTION_TOKENS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class SqlColorSettingsPage implements ColorSettingsPage {
new AttributesDescriptor("SQL//Literal//String", SqlSyntaxHighlighter.STRING),
new AttributesDescriptor("SQL//Literal//Number", SqlSyntaxHighlighter.NUMBER),
new AttributesDescriptor("SQL//Syntax//Keyword", SqlSyntaxHighlighter.KEYWORD),
new AttributesDescriptor("SQL//Syntax//Function name", SqlSyntaxHighlighter.FUNCTION_NAME),
new AttributesDescriptor("SQL//Syntax//DataType", SqlSyntaxHighlighter.DATATYPE),
new AttributesDescriptor("SQL//Syntax//Other", SqlSyntaxHighlighter.OTHER),
new AttributesDescriptor("SQL//Syntax//Word", SqlSyntaxHighlighter.WORD),
Expand Down Expand Up @@ -106,59 +107,64 @@ public class SqlColorSettingsPage implements ColorSettingsPage {
/**
* Set highlights as you like
*/
update product set /*%populate*/
category = /* category */'category',
expected_sales = /* price * pieces */0,
unit_price = /* purchase / pieces */0,
/*%if mark != "XXX" */
mark = /* mark */'mark',
/*%end */
/*%! This comment delete */
employee_type = (
select /*%expand "e" */* from employee e
where
/*%for name : names */
orderer = /* name */'orderer'
/*%if name.startWith("AA") && name.contains("_A") */
AND div = 'A'
AND rank >= 5
/*%elseif name.startWith("BB") || name.contains("_B") */
AND div = 'B'
AND rank > 2
AND rank < 5
/*%else */
AND div = 'C'
/*%end */
/*%if name_has_next */
/*# "OR" */
/*%end */
/*%end*/
)
where
type = /* @[email protected]() */'type'
and cost_limit <= /* cost + 100 */0
and cost_limit >= /* cost - 100 */99999;
UPDATE product
SET /*%populate*/
category = /* category */'category'
, expected_sales = /* price * pieces */0
, unit_price = /* purchase / pieces */0
/*%if mark != "XXX" */
, mark = /* mark */'mark'
/*%end */
/*%! This comment delete */
, employee_type = ( SELECT /*%expand "e" */*
FROM employee e
WHERE
/*%for name : names */
orderer = /* name */'orderer'
/*%if name.startWith("AA") && name.contains("_A") */
AND div = 'A'
AND rank >= 5
/*%elseif name.startWith("BB") || name.contains("_B") */
AND div = 'B'
AND rank > 2
AND rank < 5
/*%else */
AND div = 'C'
/*%end */
/*%if name_has_next */
/*# "OR" */
/*%end */
/*%end*/ )
WHERE type = /* @[email protected]() */'type'
AND cost_limit <= /* cost + 100 */0
AND cost_limit >= /* cost - 100 */99999;
-- Demo Text2
select p.project_id
, p.project_name
, p.project_type
, p.project_category
, p.pre_project
from project p
where p.project_type = /* new Project().type */'type'
and(
/*%for project : preProjects */
/*%if project.category != null */
project_category = /* project.category.plus('t') */'category'
/*%elseif project.pre == true */
pre_project = /* project.preProjectId */0
/*%if project_has_next */
/*# "OR" */
/*%end */
/*%end */
/*%end */
)
SELECT p.project_id
, p.project_name
, p.project_type
, p.project_category
, p.pre_project
FROM project p
WHERE p.project_type = /* new Project().type */'type'
AND (
/*%for project : preProjects */
/*%if project.category != null */
project_category = /* project.category.plus('t') */'category'
/*%elseif project.pre == true */
pre_project = /* project.preProjectId */0
/*%if project_has_next */
/*# "OR" */
/*%end */
/*%end */
/*%end */)
-- DemoText3
SELECT common
, amount
, date
, SUM(amount) OVER(PARTITION BY common ORDER BY date) AS common_amount
FROM ammount_table
""";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class SqlSyntaxHighlighter extends SyntaxHighlighterBase {
createTextAttributesKey("DOMA_SQL_WORD", DefaultLanguageHighlighterColors.IDENTIFIER);
public static final TextAttributesKey DATATYPE =
createTextAttributesKey("DOMA_SQL_DATATYPE", DefaultLanguageHighlighterColors.METADATA);
public static final TextAttributesKey FUNCTION_NAME =
createTextAttributesKey(
"DOMA_SQL_FUNCTION_NAME", DefaultLanguageHighlighterColors.FUNCTION_DECLARATION);
public static final TextAttributesKey STRING =
createTextAttributesKey("DOMA_SQL_STRING", DefaultLanguageHighlighterColors.STRING);
public static final TextAttributesKey NUMBER =
Expand Down Expand Up @@ -141,6 +144,7 @@ public class SqlSyntaxHighlighter extends SyntaxHighlighterBase {
static {
map.put(SqlTypes.KEYWORD, KEYWORD);
map.put(SqlTypes.DATATYPE, DATATYPE);
map.put(SqlTypes.FUNCTION_NAME, FUNCTION_NAME);
map.put(SqlTypes.STRING, STRING);
map.put(SqlTypes.OTHER, OTHER);
map.put(SqlTypes.WORD, WORD);
Expand Down
Loading
Loading