diff --git a/src/baseSQLWorker.ts b/src/baseSQLWorker.ts index 3d3029cf..4fe0166c 100644 --- a/src/baseSQLWorker.ts +++ b/src/baseSQLWorker.ts @@ -2,6 +2,7 @@ import { BasicSQL } from 'dt-sql-parser/dist/parser/common/basicSQL'; import { worker } from './fillers/monaco-editor-core'; import { Suggestions, ParseError, EntityContext } from 'dt-sql-parser'; import { Position } from './fillers/monaco-editor-core'; +import { SemanticContext } from 'dt-sql-parser/dist/parser/common/types'; export interface ICreateData { languageId: string; @@ -45,7 +46,11 @@ export abstract class BaseSQLWorker { async doCompletionWithEntities( code: string, position: Position - ): Promise<[Suggestions | null, EntityContext[] | null]> { + ): Promise<{ + suggestions: Suggestions | null; + allEntities: EntityContext[] | null; + context: SemanticContext | null; + }> { code = code || this.getTextDocument(); if (code) { const suggestions = this.parser.getSuggestionAtCaretPosition(code, position); @@ -53,9 +58,20 @@ export abstract class BaseSQLWorker { if (suggestions?.syntax?.length) { allEntities = this.parser.getAllEntities(code, position); } - return Promise.resolve([suggestions, allEntities]); + const semanticContext = this.parser.getSemanticContextAtCaretPosition(code, position); + + return Promise.resolve({ + suggestions, + allEntities, + context: semanticContext + }); } - return Promise.resolve([null, null]); + + return Promise.resolve({ + suggestions: null, + allEntities: null, + context: null + }); } async getAllEntities(code: string, position?: Position): Promise { diff --git a/src/languageFeatures.ts b/src/languageFeatures.ts index 2a9a869e..07baec21 100644 --- a/src/languageFeatures.ts +++ b/src/languageFeatures.ts @@ -11,7 +11,7 @@ import { import { debounce } from './common/utils'; import { BaseSQLWorker } from './baseSQLWorker'; import type { ParseError } from 'dt-sql-parser'; -import type { LanguageServiceDefaults } from './monaco.contribution'; +import type { CompletionSnippet, LanguageServiceDefaults } from './monaco.contribution'; export interface WorkerAccessor { (...uris: Uri[]): Promise; @@ -159,13 +159,22 @@ export class CompletionAdapter } return worker.doCompletionWithEntities(code, position); }) - .then(([suggestions, allEntities]) => { + .then(({ suggestions, allEntities, context: semanticContext }) => { + let snippets: CompletionSnippet[] = []; + if (semanticContext?.isStatementBeginning) { + snippets = this._defaults.completionSnippets.map((item) => ({ + ...item, + insertText: typeof item.body === 'string' ? item.body : item.body.join('\n') + })); + } + return this._defaults.completionService( model, position, context, suggestions, - allEntities + allEntities, + snippets ); }) .then((completions) => { diff --git a/src/languages/flink/flink.snippet.ts b/src/languages/flink/flink.snippet.ts new file mode 100644 index 00000000..d9b25eca --- /dev/null +++ b/src/languages/flink/flink.snippet.ts @@ -0,0 +1,115 @@ +import type { CompletionSnippetOption } from 'src/monaco.contribution'; + +export const flinkSnippets: CompletionSnippetOption[] = [ + { + label: 'create-source-table', + prefix: 'CREATE-SOURCE-TABLE', + body: [ + 'CREATE TABLE ${1:source_table} (', + '\tid STRING,', + '\tval BIGINT,', + '\tts TIMESTAMP(3),', + "\tWATERMARK FOR ts AS ts - INTERVAL '5' SECOND", + ') WITH (', + "\t'connector' = 'kafka',", + "\t'topic' = 'input_topic',", + "\t'properties.bootstrap.servers' = 'localhost:9092',", + "\t'format' = 'json',", + "\t'scan.startup.mode' = 'earliest-offset'", + ');' + ] + }, + { + label: 'create-sink-table', + prefix: 'CREATE-SINK-TABLE', + body: [ + 'CREATE TABLE ${1:sink_table} (', + '\tid STRING,', + '\tcnt BIGINT', + ') WITH (', + "\t'connector' = 'jdbc',", + "\t'url' = 'jdbc:mysql://localhost:3306/test',", + "\t'table-name' = 'output_table',", + "\t'username' = 'root',", + "\t'password' = 'password'", + ');' + ] + }, + { + label: 'tumble-window', + prefix: 'TUMBLE-WINDOW', + body: [ + 'SELECT', + '\twindow_start,', + '\twindow_end,', + '\tSUM(price) as total_price', + 'FROM', + '\tTABLE(TUMBLE(TABLE table_name2,', + '\tDESCRIPTOR(create_time),', + "\tINTERVAL '1' MINUTE))", + 'GROUP BY', + '\twindow_start,', + '\twindow_end;' + ] + }, + { + label: 'hop-window', + prefix: 'HOP-WINDOW', + body: [ + 'SELECT', + '\twindow_start,', + '\twindow_end,', + '\tSUM(price) as total_price', + 'FROM', + '\tTABLE(HOP(TABLE table_name2,', + '\tDESCRIPTOR(create_time),', + "\tINTERVAL '30' SECONDS,", + "\tINTERVAL '1' MINUTE))", + 'GROUP BY', + '\twindow_start,', + '\twindow_end;' + ] + }, + { + label: 'comulate-window', + prefix: 'CUMULATE-WINDOW', + body: [ + 'SELECT', + '\twindow_start,', + '\twindow_end,', + '\tSUM(price) as total_price', + 'FROM', + '\tTABLE(CUMULATE(TABLE table_name2,', + '\tDESCRIPTOR(create_time),', + "\tINTERVAL '30' SECONDS,", + "\tINTERVAL '1' MINUTE))", + 'GROUP BY', + '\twindow_start,', + '\twindow_end;' + ] + }, + { + label: 'session-window', + prefix: 'SESSION-WINDOW', + body: [ + 'SELECT', + "\tSESSION_START(create_time, INTERVAL '10' SECOND) AS session_beg,", + "\tSESSION_ROWTIME(create_time, INTERVAL '10' SECOND) AS session_end,", + '\tSUM(price) AS total_price', + 'FROM', + '\ttable_name', + 'GROUP BY', + "\tSESSION(create_time, INTERVAL '10' SECOND);" + ] + }, + { + label: 'insert-into-select', + prefix: 'INSERT-INTO-SELECT', + body: [ + 'INSERT INTO ${1:table_name}', + 'SELECT ${2:column1}, ${3:column2}', + 'FROM ${4:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + } +]; diff --git a/src/languages/hive/hive.snippet.ts b/src/languages/hive/hive.snippet.ts new file mode 100644 index 00000000..a1fe4ef6 --- /dev/null +++ b/src/languages/hive/hive.snippet.ts @@ -0,0 +1,144 @@ +import type { CompletionSnippetOption } from 'src/monaco.contribution'; + +export const hiveSnippets: CompletionSnippetOption[] = [ + { + label: 'select', + prefix: 'SELECT', + body: ['SELECT ${2:column1}, ${3:column2} FROM ${1:table_name};\n$4'] + }, + { + label: 'select-join', + prefix: 'SELECT-JOIN', + body: [ + 'SELECT ${8:column1} FROM ${1:table_name} ${2:t1}', + '${3:LEFT} JOIN ${4:table2} ${5:t2} ON ${2:t1}.${6:column1} = ${5:t2}.${7:column2};\n$9' + ] + }, + { + label: 'select-order-by', + prefix: 'SELECT-ORDERBY', + body: [ + 'SELECT ${2:column1}, ${3:column2} FROM ${1:table_name} ORDER BY ${4:column1} ${5:desc};\n$6' + ] + }, + { + label: 'insert', + prefix: 'INSERT-INTO', + body: [ + 'INSERT INTO ${1:table_name} (${2:column1}, ${3:column2}) VALUES (${4:value1}, ${5:value2});\n$6' + ] + }, + { + label: 'insert-into-select', + prefix: 'INSERT-INTO-SELECT', + body: [ + 'INSERT INTO TABLE ${1:table_name}', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'insert-overwrite-table', + prefix: 'INSERT-OVERWRITE-TABLE', + body: [ + 'INSERT OVERWRITE TABLE ${1:table_name}', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'update', + prefix: 'UPDATE', + body: [ + 'UPDATE ${1:table_name}', + 'SET ${2:column1} = ${3:value1}', + 'WHERE ${4:column2} = ${5:value2};\n$6' + ] + }, + { + label: 'delete', + prefix: 'DELETE', + body: ['DELETE FROM ${1:table_name}', 'WHERE ${2:column1} = ${3:value1};\n$4'] + }, + { + label: 'create-table', + prefix: 'CREATE-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + "COMMENT '${6:table_comment}'", + 'ROW FORMAT ${7:DELIMITED}', + "FIELDS TERMINATED BY '${8:\\t}'", + 'STORED AS ${9:PARQUET};\n$10' + ] + }, + { + label: 'create-table-as-select', + prefix: 'CREATE-TABLE-AS-SELECT', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name}', + 'AS', + 'SELECT ${2:column1}, ${3:column2}', + 'FROM ${4:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'create-partition-table', + prefix: 'CREATE-PARTITION-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + "COMMENT '${6:table_comment}'", + 'PARTITIONED BY (${7:part_column_name} STRING)', + 'ROW FORMAT ${8:DELIMITED}', + "FIELDS TERMINATED BY '${9:\\t}'", + 'STORED AS ${10:PARQUET};\n$11' + ] + }, + { + label: 'create-bucket-table', + prefix: 'CREATE-BUCKET-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + "COMMENT '${6:table_comment}'", + 'PARTITIONED BY (${7:part_column_name} STRING)', + 'CLUSTERED BY (${8:bucket_column_name}) INTO ${9:1} BUCKETS', + 'ROW FORMAT ${10:DELIMITED}', + "FIELDS TERMINATED BY '${11:\\t}'", + 'STORED AS ${12:PARQUET};\n$13' + ] + }, + { + label: 'alter-table-partition', + prefix: 'ALTER-TABLE-PARTITION', + body: [ + "ALTER TABLE ${1:table_name} ${2:ADD} PARTITION (${3:part_column}='${4:part_value}');\n$5" + ] + }, + { + label: 'alter-table-properties', + prefix: 'ALTER-TABLE-PROPERTIES', + body: [ + "ALTER TABLE ${1:table_name} SET TBLPROPERTIES ('${2:property_name}'='${3:property_value}');\n$4" + ] + }, + { + label: 'alter-table-columns', + prefix: 'ALTER-TABLE-COLUMNS', + body: [ + 'ALTER TABLE ${1:table_name} ADD COLUMNS (', + "\t${2:column_name} ${3:STRING} COMMENT '${4:desc}'", + ');\n$5' + ] + } +]; diff --git a/src/languages/impala/impala.snippet.ts b/src/languages/impala/impala.snippet.ts new file mode 100644 index 00000000..d0aec317 --- /dev/null +++ b/src/languages/impala/impala.snippet.ts @@ -0,0 +1,122 @@ +import type { CompletionSnippetOption } from 'src/monaco.contribution'; + +export const impalaSnippets: CompletionSnippetOption[] = [ + { + label: 'select', + prefix: 'SELECT', + body: ['SELECT ${2:column1}, ${3:column2} FROM ${1:table_name};\n$4'] + }, + { + label: 'select-join', + prefix: 'SELECT-JOIN', + body: [ + 'SELECT ${8:column1} FROM ${1:table_name} ${2:t1}', + '${3:LEFT} JOIN ${4:table2} ${5:t2} ON ${2:t1}.${6:column1} = ${5:t2}.${7:column2};\n$9' + ] + }, + { + label: 'select-order-by', + prefix: 'SELECT-ORDER-BY', + body: [ + 'SELECT ${2:column1}, ${3:column2} FROM ${1:table_name} ORDER BY ${4:column1} ${5:desc};\n$6' + ] + }, + { + label: 'insert', + prefix: 'INSERT-INTO', + body: [ + 'INSERT INTO ${1:table_name} (${2:column1}, ${3:column2}) VALUES (${4:value1}, ${5:value2});\n$6' + ] + }, + { + label: 'insert-into-select', + prefix: 'INSERT-INTO-SELECT', + body: [ + 'INSERT INTO ${1:table_name}', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'insert-overwrite-table', + prefix: 'INSERT-OVERWRITE-TABLE', + body: [ + 'INSERT OVERWRITE TABLE ${1:table_name}', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'update', + prefix: 'UPDATE', + body: [ + 'UPDATE ${1:table_name}', + 'SET ${2:column1} = ${3:value1}', + 'WHERE ${4:column2} = ${5:value2};\n$6' + ] + }, + { + label: 'delete', + prefix: 'DELETE', + body: ['DELETE FROM ${1:table_name}', 'WHERE ${2:column1} = ${3:value1};\n$4'] + }, + { + label: 'create-table', + prefix: 'CREATE-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + "COMMENT '${6:table_comment}'", + 'STORED AS ${7:PARQUET};\n$8' + ] + }, + { + label: 'create-table-as-select', + prefix: 'CREATE-TABLE-AS-SELECT', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name}', + 'AS', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'create-partition-table', + prefix: 'CREATE-PARTITION-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + 'PARTITIONED BY (${6:part_column_name} ${7:STRING})', + "COMMENT '${8:table_comment}'", + 'STORED AS ${9:PARQUET};\n$10' + ] + }, + { + label: 'alter-table-partition', + prefix: 'ALTER-TABLE-PARTITION', + body: [ + "ALTER TABLE ${1:table_name} ${2:ADD} PARTITION (${3:part_column} = '${4:part_value}');\n$5" + ] + }, + { + label: 'alter-table-properties', + prefix: 'ALTER-TABLE-PROPERTIES', + body: [ + "ALTER TABLE ${1:table_name} SET TBLPROPERTIES ('${2:property_name}' = '${3:property_value}');\n$4" + ] + }, + { + label: 'alter-table-column', + prefix: 'ALTER-TABLE-COLUMN', + body: [ + "ALTER TABLE ${1:table_name} ADD COLUMN ${2:column_name} ${3:STRING} COMMENT '${4:desc}';\n$5" + ] + } +]; diff --git a/src/languages/mysql/mysql.snippet.ts b/src/languages/mysql/mysql.snippet.ts new file mode 100644 index 00000000..b931b909 --- /dev/null +++ b/src/languages/mysql/mysql.snippet.ts @@ -0,0 +1,169 @@ +import type { CompletionSnippetOption } from 'src/monaco.contribution'; + +export const mysqlSnippets: CompletionSnippetOption[] = [ + { + label: 'select', + prefix: 'SELECT', + body: ['SELECT ${2:column1}, ${3:column2} FROM ${1:table_name};\n$4'] + }, + { + label: 'select-join', + prefix: 'SELECT-JOIN', + body: [ + 'SELECT ${8:column1} FROM ${1:table_name} ${2:t1}', + '${3:LEFT} JOIN ${4:table2} ${5:t2} ON ${2:t1}.${6:column1} = ${5:t2}.${7:column2};\n$9' + ] + }, + { + label: 'select-order-by', + prefix: 'SELECT-ORDER-BY', + body: [ + 'SELECT ${2:column1}, ${3:column2} FROM ${1:table_name} ORDER BY ${4:column1} ${5:desc};\n$6' + ] + }, + { + label: 'insert', + prefix: 'INSERT-INTO', + body: [ + 'INSERT INTO ${1:table_name} (${2:column1}, ${3:column2}) VALUES (${4:value1}, ${5:value2});\n$6' + ] + }, + { + label: 'insert-into-select', + prefix: 'INSERT-INTO-SELECT', + body: [ + 'INSERT INTO ${1:table_name}', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'replace-into-table', + prefix: 'REPLACE-INTO-TABLE', + body: [ + 'REPLACE INTO ${1:table_name} (${2:column1}, ${3:column2})', + 'VALUES (${4:value1}, ${5:value2});\n$6' + ] + }, + { + label: 'update', + prefix: 'UPDATE', + body: [ + 'UPDATE ${1:table_name}', + 'SET ${2:column1} = ${3:value1}', + 'WHERE ${4:column2} = ${5:value2};\n$6' + ] + }, + { + label: 'delete', + prefix: 'DELETE', + body: ['DELETE FROM ${1:table_name}', 'WHERE ${2:column1} = ${3:value1};\n$4'] + }, + { + label: 'create-table', + prefix: 'CREATE-TABLE', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT},', + '\tPRIMARY KEY (${2:column1})', + ')', + "COMMENT '${6:table_comment}';\n$7" + ] + }, + { + label: 'create-table-as-select', + prefix: 'CREATE-TABLE-AS-SELECT', + body: [ + 'CREATE TABLE ${1:table_name}', + 'AS', + 'SELECT ${2:column1}, ${3:column2}', + 'FROM ${4:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'create-table-partitioned-by-range', + prefix: 'CREATE-TABLE-PARTITIONED-BY-RANGE', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT},', + '\tPRIMARY KEY (${2:column1})', + ')', + 'PARTITION BY RANGE (${2:column1}) (', + '\tPARTITION ${6:p0} VALUES LESS THAN ($7)', + ');\n$8' + ] + }, + { + label: 'create-table-partitioned-by-list', + prefix: 'CREATE-TABLE-PARTITIONED-BY-LIST', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT},', + '\tPRIMARY KEY (${2:column1})', + ')', + 'PARTITION BY LIST (${2:column1}) (', + '\tPARTITION ${6:p0} VALUES IN ($7)', + ');\n$8' + ] + }, + { + label: 'create-table-partitioned-by-hash', + prefix: 'CREATE-TABLE-PARTITIONED-BY-HASH', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT},', + '\tPRIMARY KEY (${2:column1})', + ')', + 'PARTITION BY HASH (${2:column1})', + 'PARTITIONS ${6:4};\n$7' + ] + }, + { + label: 'create-table-partitioned-by-key', + prefix: 'CREATE-TABLE-PARTITIONED-BY-KEY', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT},', + '\tPRIMARY KEY (${2:column1})', + ')', + 'PARTITION BY KEY (${2:column1})', + 'PARTITIONS ${6:4};\n$7' + ] + }, + { + label: 'alter-table-add-column', + prefix: 'ALTER-TABLE-ADD-COLUMN', + body: ["ALTER TABLE ${1:table_name} ADD ${2:column_name} ${3:INT} COMMENT '${4:desc}';\n$5"] + }, + { + label: 'alter-table-add-partition', + prefix: 'ALTER-TABLE-ADD-PARTITION', + body: ['ALTER TABLE ${1:table_name} ADD PARTITION (', '\t$2', ');\n$4'] + }, + { + label: 'alter-table-add-index', + prefix: 'ALTER-TABLE-ADD-INDEX', + body: ['ALTER TABLE ${1:table_name} ADD INDEX ${2:index_name} (${3:column_name});\n$4'] + }, + { + label: 'alter-table-add-primary-key', + prefix: 'ALTER-TABLE-ADD-PRIMARY-KEY', + body: ['ALTER TABLE ${1:table_name} ADD PRIMARY KEY (${2:column_name});\n$3'] + }, + { + label: 'alter-table-add-constraint', + prefix: 'ALTER-TABLE-ADD-CONSTRAINT', + body: [ + 'ALTER TABLE ${1:table_name}', + 'ADD CONSTRAINT ${2:constraint_name}', + 'FOREIGN KEY (${3:foreign_column}) REFERENCES ${4:ref_table} (${5:ref_column});\n$6' + ] + } +]; diff --git a/src/languages/pgsql/pgsql.snippet.ts b/src/languages/pgsql/pgsql.snippet.ts new file mode 100644 index 00000000..5591f6d3 --- /dev/null +++ b/src/languages/pgsql/pgsql.snippet.ts @@ -0,0 +1,137 @@ +import type { CompletionSnippetOption } from 'src/monaco.contribution'; + +export const pgsqlSnippets: CompletionSnippetOption[] = [ + { + label: 'select', + prefix: 'SELECT', + body: ['SELECT ${2:column1}, ${3:column2} FROM ${1:table_name};\n$4'] + }, + { + label: 'select-join', + prefix: 'SELECT-JOIN', + body: [ + 'SELECT ${8:column1} FROM ${1:table_name} ${2:t1}', + '${3:LEFT} JOIN ${4:table2} ${5:t2} ON ${2:t1}.${6:column1} = ${5:t2}.${7:column2};\n$9' + ] + }, + { + label: 'select-order-by', + prefix: 'SELECT-ORDER-BY', + body: [ + 'SELECT ${2:column1}, ${3:column2} FROM ${1:table_name} ORDER BY ${4:column1} ${5:desc};\n$6' + ] + }, + { + label: 'insert', + prefix: 'INSERT-INTO', + body: [ + 'INSERT INTO ${1:table_name} (${2:column1}, ${3:column2}) VALUES (${4:value1}, ${5:value2});\n$6' + ] + }, + { + label: 'insert-into-select', + prefix: 'INSERT-INTO-SELECT', + body: [ + 'INSERT INTO ${1:table_name}', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'update', + prefix: 'UPDATE', + body: [ + 'UPDATE ${1:table_name}', + 'SET ${2:column1} = ${3:value1}', + 'WHERE ${4:column2} = ${5:value2};\n$6' + ] + }, + { + label: 'delete', + prefix: 'DELETE', + body: ['DELETE FROM ${1:table_name}', 'WHERE ${2:column1} = ${3:value1};\n$4'] + }, + { + label: 'create-table', + prefix: 'CREATE-TABLE', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT},', + '\tPRIMARY KEY (${2:column1})', + ');\n$6' + ] + }, + { + label: 'create-table-as-select', + prefix: 'CREATE-TABLE-AS-SELECT', + body: [ + 'CREATE TABLE ${1:table_name}', + 'AS', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'create-table-partitioned-by-range', + prefix: 'CREATE-TABLE-PARTITIONED-BY-RANGE', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT}', + ')', + 'PARTITION BY RANGE (${2:column1});\n', + 'CREATE TABLE ${6:p0} PARTITION OF ${1:table_name}', + '\tFOR VALUES FROM (${7:start_value}) TO (${8:end_value});\n$9' + ] + }, + { + label: 'create-table-partitioned-by-list', + prefix: 'CREATE-TABLE-PARTITIONED-BY-LIST', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT}', + ')', + 'PARTITION BY LIST (${2:column1});\n', + 'CREATE TABLE ${6:p0} PARTITION OF ${1:table_name}', + '\tFOR VALUES IN (${7:range_values});\n$8' + ] + }, + { + label: 'create-table-partitioned-by-hash', + prefix: 'CREATE-TABLE-PARTITIONED-BY-HASH', + body: [ + 'CREATE TABLE ${1:table_name} (', + '\t${2:column1} ${3:INT},', + '\t${4:column2} ${5:INT}', + ')', + 'PARTITION BY HASH (${2:column1});\n', + 'CREATE TABLE ${6:p0} PARTITION OF ${1:table_name}', + '\tFOR VALUES WITH (MODULUS ${7:4}, REMAINDER ${8:0});\n$9' + ] + }, + { + label: 'alter-table-add-column', + prefix: 'ALTER-TABLE-ADD-COLUMN', + body: ['ALTER TABLE ${1:table_name} ADD COLUMN ${2:column_name} ${3:INT};\n$4'] + }, + { + label: 'alter-table-add-primary-key', + prefix: 'ALTER-TABLE-ADD-PRIMARY-KEY', + body: [ + 'ALTER TABLE ${1:table_name} ADD CONSTRAINT ${2:pk_name} PRIMARY KEY (${3:column_name});\n$4' + ] + }, + { + label: 'alter-table-add-foreign-key', + prefix: 'ALTER-TABLE-ADD-FOREIGN-KEY', + body: [ + 'ALTER TABLE ${1:table_name}', + 'ADD CONSTRAINT ${2:fk_name}', + 'FOREIGN KEY (${3:foreign_column}) REFERENCES ${4:ref_table} (${5:ref_column});\n$6' + ] + } +]; diff --git a/src/languages/spark/spark.snippet.ts b/src/languages/spark/spark.snippet.ts new file mode 100644 index 00000000..251e4445 --- /dev/null +++ b/src/languages/spark/spark.snippet.ts @@ -0,0 +1,135 @@ +import type { CompletionSnippetOption } from 'src/monaco.contribution'; + +export const sparkSnippets: CompletionSnippetOption[] = [ + { + label: 'select', + prefix: 'SELECT', + body: ['SELECT ${2:column1}, ${3:column2} FROM ${1:table_name};\n$4'] + }, + { + label: 'select-join', + prefix: 'SELECT-JOIN', + body: [ + 'SELECT ${8:column1} FROM ${1:table_name} ${2:t1}', + '${3:LEFT} JOIN ${4:table2} ${5:t2} ON ${2:t1}.${6:column1} = ${5:t2}.${7:column2};\n$9' + ] + }, + { + label: 'select-order-by', + prefix: 'SELECT-ORDER-BY', + body: [ + 'SELECT ${2:column1}, ${3:column2} FROM ${1:table_name} ORDER BY ${4:column1} ${5:desc};\n$6' + ] + }, + { + label: 'insert', + prefix: 'INSERT-INTO', + body: [ + 'INSERT INTO ${1:table_name} (${2:column1}, ${3:column2}) VALUES (${4:value1}, ${5:value2});\n$6' + ] + }, + { + label: 'insert-into-select', + prefix: 'INSERT-INTO-SELECT', + body: [ + 'INSERT INTO ${1:table_name}', + 'SELECT ${2:column1}, ${3:column2}', + 'FROM ${4:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'insert-overwrite-table', + prefix: 'INSERT-OVERWRITE-TABLE', + body: [ + 'INSERT OVERWRITE TABLE ${1:table_name}', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'update', + prefix: 'UPDATE', + body: [ + 'UPDATE ${1:table_name}', + 'SET ${2:column1} = ${3:value1}', + 'WHERE ${4:column2} = ${5:value2};\n$6' + ] + }, + { + label: 'delete', + prefix: 'DELETE', + body: ['DELETE FROM ${1:table_name}', 'WHERE ${2:column1} = ${3:value1};\n$4'] + }, + { + label: 'create-table', + prefix: 'CREATE-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + 'USING ${6:parquet}', + "COMMENT '${7:table_comment}';\n$8" + ] + }, + { + label: 'create-table-as-select', + prefix: 'CREATE-TABLE-AS-SELECT', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name}', + 'AS', + 'SELECT ${2:column1}, ${3:column2}', + 'FROM ${4:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'create-partition-table', + prefix: 'CREATE-PARTITION-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + 'USING ${6:parquet}', + 'PARTITIONED BY (${7:part_column_name} ${8:STRING})', + "COMMENT '${9:table_comment}';\n$10" + ] + }, + { + label: 'create-bucket-table', + prefix: 'CREATE-BUCKET-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + 'USING ${6:parquet}', + 'CLUSTERED BY (${7:bucket_column}) INTO ${8:1} BUCKETS', + "COMMENT '${9:table_comment}';\n$10" + ] + }, + { + label: 'alter-table-partition', + prefix: 'ALTER-TABLE-PARTITION', + body: [ + "ALTER TABLE ${1:table_name} ${2:ADD} PARTITION (${3:part_column}='${4:part_value}');\n$5" + ] + }, + { + label: 'alter-table-properties', + prefix: 'ALTER-TABLE-PROPERTIES', + body: [ + "ALTER TABLE ${1:table_name} SET TBLPROPERTIES ('${2:property_name}'='${3:property_value}');\n$4" + ] + }, + { + label: 'alter-table-column', + prefix: 'ALTER-TABLE-COLUMN', + body: [ + "ALTER TABLE ${1:table_name} ADD COLUMN ${2:column_name} ${3:STRING} COMMENT '${4:desc}';\n$5" + ] + } +]; diff --git a/src/languages/trino/trino.snippet.ts b/src/languages/trino/trino.snippet.ts new file mode 100644 index 00000000..0d82d8ad --- /dev/null +++ b/src/languages/trino/trino.snippet.ts @@ -0,0 +1,112 @@ +import type { CompletionSnippetOption } from 'src/monaco.contribution'; + +export const trinoSnippets: CompletionSnippetOption[] = [ + { + label: 'select', + prefix: 'SELECT', + body: ['SELECT ${2:column1}, ${3:column2} FROM ${1:hive.schema_name.table_name};\n$4'] + }, + { + label: 'select-join', + prefix: 'SELECT-JOIN', + body: [ + 'SELECT ${8:column1} FROM ${1:hive.schema_name.table_name1} ${2:t1}', + '${3:LEFT} JOIN ${4:hive.schema_name.table_name2} ${5:t2} ON ${2:t1}.${6:column1} = ${5:t2}.${7:column2};\n$9' + ] + }, + { + label: 'select-order-by', + prefix: 'SELECT-ORDER-BY', + body: [ + 'SELECT ${2:column1}, ${3:column2} FROM ${1:hive.schema_name.table_name} ORDER BY ${4:column1} ${5:desc};\n$6' + ] + }, + { + label: 'insert', + prefix: 'INSERT-INTO', + body: [ + 'INSERT INTO ${1:hive.schema_name.table_name} (${2:column1}, ${3:column2}) VALUES (${4:value1}, ${5:value2});\n$6' + ] + }, + { + label: 'insert-into-select', + prefix: 'INSERT-INTO-SELECT', + body: [ + 'INSERT INTO ${1:hive.schema_name.table_name}', + 'SELECT ${2:column1}, ${3:column2}', + 'FROM ${4:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'update', + prefix: 'UPDATE', + body: [ + 'UPDATE ${1:hive.schema_name.table_name}', + 'SET ${2:column1} = ${3:value1}', + 'WHERE ${4:column2} = ${5:value2};\n$6' + ] + }, + { + label: 'delete', + prefix: 'DELETE', + body: [ + 'DELETE FROM ${1:hive.schema_name.table_name}', + 'WHERE ${2:column1} = ${3:value1};\n$4' + ] + }, + { + label: 'create-catalog', + prefix: 'CREATE-CATALOG', + body: [ + 'CREATE CATALOG ${1:catalog_name} USING ${2:hive}', + 'WITH (', + "\t${3:property_name} = '${4:property_value}'", + ');\n$5' + ] + }, + { + label: 'create-schema', + prefix: 'CREATE-SCHEMA', + body: ["CREATE SCHEMA ${1:hive.schema_name} WITH (LOCATION = '${2:/hive/data/web}');\n$3"] + }, + { + label: 'create-table', + prefix: 'CREATE-TABLE', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:hive.schema_name.table_name} (', + '\t${2:column1} ${3:STRING},', + '\t${4:column2} ${5:STRING}', + ')', + "COMMENT '${6:table_comment}'", + 'WITH (', + "\tformat = '${7:PARQUET}'", + ');\n$8' + ] + }, + { + label: 'create-table-as-select', + prefix: 'CREATE-TABLE-AS-SELECT', + body: [ + 'CREATE TABLE IF NOT EXISTS ${1:hive.schema_name.table_name}', + 'AS', + 'SELECT ${3:column1}, ${4:column2}', + 'FROM ${2:source_table}', + 'WHERE ${5:conditions};\n$6' + ] + }, + { + label: 'alter-table-properties', + prefix: 'ALTER-TABLE-PROPERTIES', + body: [ + "ALTER TABLE ${1:hive.schema_name.table_name} SET PROPERTIES ${2:property_name} = '${3:property_value}';\n$4" + ] + }, + { + label: 'alter-table-columns', + prefix: 'ALTER-TABLE-COLUMNS', + body: [ + "ALTER TABLE ${1:hive.schema_name.table_name} ADD COLUMN ${2:column_name} ${3:STRING} COMMENT '${4:desc}';\n$5" + ] + } +]; diff --git a/src/main.ts b/src/main.ts index 45036c2a..900243c0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ export * from './languageService'; export * from './setupLanguageFeatures'; export * from './common/constants'; export * from './theme'; +export * as snippets from './snippets'; export { EntityContextType, StmtContextType } from 'dt-sql-parser'; diff --git a/src/monaco.contribution.ts b/src/monaco.contribution.ts index e655fc54..cac8bfb4 100644 --- a/src/monaco.contribution.ts +++ b/src/monaco.contribution.ts @@ -31,7 +31,8 @@ export type CompletionService = ( position: Position, completionContext: languages.CompletionContext, suggestions: Suggestions | null, - entities: EntityContext[] | null + entities: EntityContext[] | null, + snippets?: CompletionSnippet[] ) => Promise; export interface CompletionOptions { @@ -42,8 +43,20 @@ export interface CompletionOptions { */ completionService: CompletionService; triggerCharacters: string[]; + snippets: CompletionSnippetOption[]; } +export interface CompletionSnippet { + prefix: string; + label: string; + body: string | string[]; + // generated by body + insertText?: string; + description?: string; +} + +export type CompletionSnippetOption = Omit; + export interface ModeConfiguration { /** * Defines whether the built-in completionItemProvider is enabled. @@ -90,6 +103,7 @@ export interface LanguageServiceDefaults { readonly modeConfiguration: ModeConfiguration; preprocessCode: PreprocessCode | null; completionService: CompletionService; + completionSnippets: CompletionSnippet[]; triggerCharacters: string[]; setModeConfiguration(modeConfiguration: ModeConfiguration): void; } @@ -126,6 +140,10 @@ export class LanguageServiceDefaultsImpl implements LanguageServiceDefaults { return this._modeConfiguration.completionItems.completionService; } + get completionSnippets(): CompletionSnippet[] { + return this._modeConfiguration.completionItems.snippets; + } + get triggerCharacters(): string[] { return this._modeConfiguration.completionItems.triggerCharacters; } @@ -149,7 +167,9 @@ export const defaultCompletionService: CompletionService = function ( _model, _position, _context, - suggestions + suggestions, + _entities, + snippets ) { if (!suggestions) { return Promise.resolve([]); @@ -162,11 +182,23 @@ export const defaultCompletionService: CompletionService = function ( detail: 'keyword' })); - return Promise.resolve(keywordsCompletionItems); + const snippetCompletionItems: ICompletionItem[] = + snippets?.map((item) => ({ + label: item.label || item.prefix, + kind: languages.CompletionItemKind.Snippet, + filterText: item.prefix, + insertText: item.insertText, + insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet, + detail: item.description || 'snippet', + documentation: item.insertText + })) || []; + + return Promise.resolve([...keywordsCompletionItems, ...snippetCompletionItems]); }; export const modeConfigurationDefault: Required = { completionItems: { + snippets: [], enable: true, completionService: defaultCompletionService, triggerCharacters: ['.', ' '] diff --git a/src/setupLanguageFeatures.ts b/src/setupLanguageFeatures.ts index bdaeb0f6..743cd3a3 100644 --- a/src/setupLanguageFeatures.ts +++ b/src/setupLanguageFeatures.ts @@ -8,6 +8,7 @@ import { } from './monaco.contribution'; import { languages, IDisposable } from './fillers/monaco-editor-core'; import { LanguageIdEnum } from './common/constants'; +import * as snippets from './snippets'; export interface FeatureConfiguration { /** @@ -80,6 +81,27 @@ export function setupLanguageFeatures( } } +function getDefaultSnippets(languageId: LanguageIdEnum) { + switch (languageId) { + case LanguageIdEnum.HIVE: + return snippets.hiveSnippets; + case LanguageIdEnum.FLINK: + return snippets.flinkSnippets; + case LanguageIdEnum.IMPALA: + return snippets.impalaSnippets; + case LanguageIdEnum.MYSQL: + return snippets.mysqlSnippets; + case LanguageIdEnum.PG: + return snippets.pgsqlSnippets; + case LanguageIdEnum.SPARK: + return snippets.sparkSnippets; + case LanguageIdEnum.TRINO: + return snippets.trinoSnippets; + default: + return []; + } +} + function processConfiguration( languageId: LanguageIdEnum, configuration: FeatureConfiguration @@ -111,12 +133,20 @@ function processConfiguration( : (defaults?.modeConfiguration.completionItems.triggerCharacters ?? modeConfigurationDefault.completionItems.triggerCharacters); + const snippets = + typeof configuration.completionItems !== 'boolean' && + Array.isArray(configuration.completionItems?.snippets) + ? configuration.completionItems!.snippets + : (defaults?.modeConfiguration.completionItems.snippets ?? + getDefaultSnippets(languageId)); + return { diagnostics, completionItems: { enable: completionEnable, completionService, - triggerCharacters + triggerCharacters, + snippets } }; } diff --git a/src/snippets.ts b/src/snippets.ts new file mode 100644 index 00000000..997e8f28 --- /dev/null +++ b/src/snippets.ts @@ -0,0 +1,7 @@ +export { hiveSnippets } from './languages/hive/hive.snippet'; +export { flinkSnippets } from './languages/flink/flink.snippet'; +export { trinoSnippets } from './languages/trino/trino.snippet'; +export { pgsqlSnippets } from './languages/pgsql/pgsql.snippet'; +export { sparkSnippets } from './languages/spark/spark.snippet'; +export { mysqlSnippets } from './languages/mysql/mysql.snippet'; +export { impalaSnippets } from './languages/impala/impala.snippet'; diff --git a/website/src/App.tsx b/website/src/App.tsx index 062d598e..ab5cfeac 100644 --- a/website/src/App.tsx +++ b/website/src/App.tsx @@ -4,12 +4,26 @@ import { create, Workbench } from '@dtinsight/molecule'; import InstanceService from '@dtinsight/molecule/esm/services/instanceService'; import { ExtendsWorkbench } from './extensions/workbench'; import { version, dependencies } from '../../package.json'; - +import { editor } from 'monaco-editor'; import './languages'; import '@dtinsight/molecule/esm/style/mo.css'; + import './App.css'; +/** + * Allow code completion when typing in snippets. + * + * You can also set configurations when creating monaco-editor instance + */ +editor.onDidCreateEditor((editor) => { + editor.updateOptions({ + suggest: { + snippetsPreventQuickSuggestions: false + } + }); +}); + function App(): React.ReactElement { const refMoInstance = useRef(); const [MyWorkbench, setMyWorkbench] = useState(); diff --git a/website/src/languages/helpers/completionService.ts b/website/src/languages/helpers/completionService.ts index c6fde960..c060536c 100644 --- a/website/src/languages/helpers/completionService.ts +++ b/website/src/languages/helpers/completionService.ts @@ -16,7 +16,9 @@ export const completionService: CompletionService = async function ( model, _position, _completionContext, - suggestions + suggestions, + _entities, + snippets ) { if (!suggestions) { return Promise.resolve([]); @@ -183,5 +185,18 @@ export const completionService: CompletionService = async function ( } } } - return [...syntaxCompletionItems, ...keywordsCompletionItems]; + + const snippetCompletionItems: ICompletionItem[] = + snippets?.map((item) => ({ + label: item.label || item.prefix, + kind: languages.CompletionItemKind.Snippet, + filterText: item.prefix, + insertText: item.insertText, + insertTextRules: languages.CompletionItemInsertTextRule.InsertAsSnippet, + sortText: '3' + item.prefix, + detail: item.description !== undefined ? item.description : 'SQL模板', + documentation: item.insertText + })) || []; + + return [...syntaxCompletionItems, ...keywordsCompletionItems, ...snippetCompletionItems]; }; diff --git a/website/src/languages/index.ts b/website/src/languages/index.ts index 88c0ccbb..5f64c296 100644 --- a/website/src/languages/index.ts +++ b/website/src/languages/index.ts @@ -2,7 +2,6 @@ import 'monaco-sql-languages/esm/all.contributions.js'; import './languageWorker'; import './theme'; import { setupLanguageFeatures, LanguageIdEnum } from 'monaco-sql-languages/esm/main.js'; - import { completionService } from './helpers/completionService'; /**