Skip to content

Commit

Permalink
feat: Add SQL++ completions and named parameters completions
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-couchbase committed May 28, 2024
1 parent 516221a commit 71f21cc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/commands/sqlpp/sqlppCompletions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
CompletionItem,
SnippetString,
CompletionItemKind,
TextDocument,
Position,
Range,
CancellationToken,
CompletionContext
} from "vscode";
import { getAllNamedParameters } from "../../util/namedParameters";

const sqlppKeywords =
"ADAPTER|ALL|ALTER|ANALYTICS|AND|ANY|APPLY|ARGS|AS|ASC|AT|AUTOGENERATED|BETWEEN|BTREE|BY|CASE|CAST|CLOSED|COLLECTION|COMPACT|COMPACTION|CONNECT|CONNECTED|CORRELATE|CREATE|CROSS|CUBE|CURRENT|DATASET|DATAVERSE|DECLARE|DEFINITION|DELETE|DESC|DISABLE|DISCONNECT|DISTINCT|DIV|DROP|ELEMENT|ELSE|ENABLE|END|ENFORCED|EVERY|EXCEPT|EXCLUDE|EXISTS|EXPLAIN|EXTERNAL|FEED|FILTER|FIRST|FLATTEN|FOLLOWING|FOR|FOREIGN|FROM|FULL|FULLTEXT|FUNCTION|GROUP|GROUPING|GROUPS|HAVING|HINTS|IF|IGNORE|IN|INCLUDE|INDEX|INGESTION|INNER|INSERT|INTERNAL|INTERSECT|INTO|IS|JOIN|KEY|KEYWORD|KNOWN|LAST|LEFT|LET|LETTING|LIKE|LIMIT|LINK|LOAD|MISSING|MOD|NGRAM|NO|NODEGROUP|NOT|NULL|NULLS|OFFSET|ON|OPEN|OR|ORDER|OTHERS|OUTER|OUTPUT|OVER|PARTITION|PATH|POLICY|PRECEDING|PRIMARY|RANGE|RAW|REFERENCES|REFRESH|REPLACE|RESPECT|RETURN|RETURNING|RETURNS|RIGHT|ROLLUP|ROW|ROWS|RTREE|RUN|SATISFIES|SCOPE|SECONDARY|SELECT|SET|SETS|SOME|START|STOP|SYNONYM|TEMPORARY|THEN|TIES|TO|TYPE|UNBOUNDED|UNION ALL|UNION|UNKNOWN|UNNEST|UPDATE|UPSERT|USE|USING|VALUE|VALUED|VIEW|WHEN|WHERE|WITH|WRITE";
const sqlppBuiltinFunctions =
"ABS|ACOS|ARRAY_APPEND|ARRAY_AVG|ARRAY_CONCAT|ARRAY_CONTAINS|ARRAY_COUNT|ARRAY_DISTINCT|ARRAY_FLATTEN|ARRAY_IFNULL|ARRAY_INSERT|ARRAY_INTERSECT|ARRAY_LENGTH|ARRAY_MAX|ARRAY_MIN|ARRAY_POSITION|ARRAY_PREPEND|ARRAY_PUT|ARRAY_RANGE|ARRAY_REMOVE|ARRAY_REPEAT|ARRAY_REPLACE|ARRAY_REVERSE|ARRAY_SORT|ARRAY_STAR|ARRAY_SUM|ARRAY_SYMDIFF|ARRAY_SYMDIFF1|ARRAY_SYMDIFFN|ARRAY_UNION|ASIN|ATAN|ATAN2|AVG|CEIL|CLOCK_LOCAL|CLOCK_MILLIS|CLOCK_STR|CLOCK_TZ|CLOCK_UTC|CONCAT|CONTAINS|COS|COUNT|DATE_ADD_MILLIS|DATE_ADD_STR|DATE_DIFF_MILLIS|DATE_DIFF_STR|DATE_FORMAT_STR|DATE_PART_MILLIS|DATE_PART_STR|DATE_RANGE_MILLIS|DATE_RANGE_STR|DATE_TRUNC_MILLIS|DATE_TRUNC_STR|DECODE_JSON|DEGREES|DURATION_TO_STR|E|ENCODE_JSON|ENCODED_SIZE|EXP|FLOOR|GREATEST|IF_INF|IF_MISSING|IF_MISSING_OR_NULL|IF_NAN|IF_NAN_OR_INF|IF_NULL|IFINF|IFMISSING|IFMISSINGORNULL|IFNAN|IFNANORINF|IFNULL|INITCAP|IS_ARRAY|IS_ATOM|IS_BOOL|IS_BOOLEAN|IS_NUM|IS_NUMBER|IS_OBJ|IS_OBJECT|IS_STR|IS_STRING|ISARRAY|ISATOM|ISBOOL|ISBOOLEAN|ISNUM|ISNUMBER|ISOBJ|ISOBJECT|ISSTR|ISSTRING|LEAST|LENGTH|LN|LOG|LOWER|LTRIM|MAX|META|MILLIS|MILLIS_TO_LOCAL|MILLIS_TO_STR|MILLIS_TO_TZ|MILLIS_TO_UTC|MILLIS_TO_ZONE_NAME|MIN|MISSING_IF|MISSINGIF|NAN_IF|NANIF|NEGINF_IF|NEGINFIF|NOW_LOCAL|NOW_MILLIS|NOW_STR|NOW_TZ|NOW_UTC|NULL_IF|NULLIF|OBJECT_ADD|OBJECT_CONCAT|OBJECT_INNER_VALUES|OBJECT_LENGTH|OBJECT_NAMES|OBJECT_PAIRS|OBJECT_PUT|OBJECT_REMOVE|OBJECT_RENAME|OBJECT_REPLACE|OBJECT_UNWRAP|OBJECT_VALUES|PAIRS|PI|POSINF_IF|POSINFIF|POSITION|POWER|RADIANS|RANDOM|REGEXP_CONTAINS|REGEXP_LIKE|REGEXP_POSITION|REGEXP_REPLACE|REPEAT|REPLACE|REVERSE|ROUND|RTRIM|SIGN|SIN|SPLIT|SQRT|STR_TO_DURATION|STR_TO_MILLIS|STR_TO_TZ|STR_TO_UTC|STR_TO_ZONE_NAME|SUBSTR|SUM|TAN|TITLE|TO_ARRAY|TO_ATOM|TO_BOOL|TO_BOOLEAN|TO_NUM|TO_NUMBER|TO_OBJ|TO_OBJECT|TO_STR|TO_STRING|TOARRAY|TOATOM|TOBOOL|TOBOOLEAN|TONUM|TONUMBER|TOOBJ|TOOBJECT|TOSTR|TOSTRING|TRIM|TRUNC|TYPE|TYPENAME|UPPER|UUID|WEEKDAY_MILLIS|WEEKDAY_STR";

export const sqlppComlpletions = (): CompletionItem[] => {
const allKeywordsArray: string[] = [];

allKeywordsArray.push(...sqlppKeywords.split("|"));
allKeywordsArray.push(...sqlppBuiltinFunctions.split("|"));

const completionItems: CompletionItem[] = allKeywordsArray.map(
(keyword) => {
const item = new CompletionItem(keyword, CompletionItemKind.Text);
item.insertText = new SnippetString(keyword);
return item;
}
);

return completionItems;
};

export const sqlppNamedParametersCompletions = (
document: TextDocument,
position: Position,
): CompletionItem[] => {
const allKeywordsArray: string[] = [];

const allNamedParameters = getAllNamedParameters();
for (const key in allNamedParameters) {
allKeywordsArray.push(`$${key}`);
}

let start = position.character;
const line = document.lineAt(position.line).text;

while (start > 0 && /\w/.test(line[start - 1])) {
start--;
}
const currentWordRange = new Range(position.line, Math.max(0,start - 1), position.line, position.character);
const currentWord = document.getText(currentWordRange);
const isSubstrRequired = currentWord.startsWith('$');

const completionItems: CompletionItem[] = allKeywordsArray.map(
(keyword) => {
const item = new CompletionItem(keyword, CompletionItemKind.Text);
item.insertText = isSubstrRequired
? keyword.substring(1)
: keyword;
return item;
}
);

// Return the array of CompletionItem objects
return completionItems;
};
File renamed without changes.
13 changes: 12 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { QueryWorkbench } from "./workbench/queryWorkbench";
import { WorkbenchWebviewProvider } from "./workbench/workbenchWebviewProvider";
import { fetchClusterOverview } from "./pages/overviewCluster/overviewCluster";
import DependenciesDownloader from "./handlers/handleCLIDownloader";
import { sqlppFormatter } from "./commands/formatting/sqlppFormatter";
import { sqlppFormatter } from "./commands/sqlpp/sqlppFormatter";
import { fetchQueryContext } from "./pages/queryContext/queryContext";
import { fetchFavoriteQueries } from "./pages/FavoriteQueries/FavoriteQueries";
import { markFavoriteQuery } from "./commands/favoriteQueries/markFavoriteQuery";
Expand All @@ -76,6 +76,7 @@ import { newChatHandler } from "./commands/iq/chat/newChatHandler";
import { SecretService } from "./util/secretService";
import { kvTypeFilterDocuments } from "./commands/documents/documentFilters/kvTypeFilterDocuments";
import { fetchNamedParameters } from "./pages/namedParameters/namedParameters";
import { sqlppComlpletions, sqlppNamedParametersCompletions } from "./commands/sqlpp/sqlppCompletions";

export function activate(context: vscode.ExtensionContext) {
Global.setState(context.globalState);
Expand Down Expand Up @@ -526,6 +527,16 @@ export function activate(context: vscode.ExtensionContext) {
},
});

vscode.languages.registerCompletionItemProvider("SQL++", {
provideCompletionItems() {
return sqlppComlpletions();
}});

vscode.languages.registerCompletionItemProvider("SQL++", {
provideCompletionItems(document, position) {
return sqlppNamedParametersCompletions(document, position);
}}, '$');

subscriptions.push(
vscode.commands.registerCommand(
Commands.getClusterOverview,
Expand Down

0 comments on commit 71f21cc

Please sign in to comment.