Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(autocomplete): suggest variables #1752

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@gravity-ui/react-data-table": "^2.1.1",
"@gravity-ui/table": "^1.7.0",
"@gravity-ui/uikit": "^6.33.0",
"@gravity-ui/websql-autocomplete": "^12.1.2",
"@gravity-ui/websql-autocomplete": "^12.7.0",
"@hookform/resolvers": "^3.9.0",
"@reduxjs/toolkit": "^2.2.3",
"@tanstack/react-table": "^8.19.3",
Expand Down
43 changes: 32 additions & 11 deletions src/utils/monaco/yql/generateSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,17 @@ const SuggestionsWeight: Record<SuggestionType, number> = {
suggestAllColumns: 3,
suggestColumns: 4,
suggestColumnAliases: 5,
suggestTableIndexes: 6,
suggestTableHints: 7,
suggestEntitySettings: 8,
suggestKeywords: 9,
suggestAggregateFunctions: 10,
suggestTableFunctions: 11,
suggestWindowFunctions: 12,
suggestFunctions: 13,
suggestSimpleTypes: 14,
suggestUdfs: 15,
suggestVariables: 6,
suggestTableIndexes: 7,
suggestTableHints: 8,
suggestEntitySettings: 9,
suggestKeywords: 10,
suggestAggregateFunctions: 11,
suggestTableFunctions: 12,
suggestWindowFunctions: 13,
suggestFunctions: 14,
suggestSimpleTypes: 15,
suggestUdfs: 16,
};

function getSuggestionIndex(suggestionType: SuggestionType) {
Expand Down Expand Up @@ -328,7 +329,7 @@ export function generateKeywordsSuggestion(
if (!suggestKeywords) {
return [];
}
return suggestKeywords?.map((keywordSuggestion) => ({
return suggestKeywords.map((keywordSuggestion) => ({
label: keywordSuggestion.value,
insertText: keywordSuggestion.value,
kind: CompletionItemKind.Keyword,
Expand All @@ -338,6 +339,26 @@ export function generateKeywordsSuggestion(
}));
}

export function generateVariableSuggestion(
rangeToInsertSuggestion: monaco.IRange,
suggestVariables?: string[],
) {
if (!suggestVariables) {
return [];
}
return suggestVariables.map((rawVariable) => {
const variable = '$' + rawVariable;
return {
label: variable,
insertText: variable,
kind: CompletionItemKind.Variable,
detail: 'Variable',
range: rangeToInsertSuggestion,
sortText: suggestionIndexToWeight(getSuggestionIndex('suggestVariables')),
};
});
}

export async function generateEntitiesSuggestion(
rangeToInsertSuggestion: monaco.IRange,
suggestEntities: YQLEntity[],
Expand Down
9 changes: 9 additions & 0 deletions src/utils/monaco/yql/yqlSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
generateSimpleTypesSuggestion,
generateTableFunctionsSuggestion,
generateUdfSuggestion,
generateVariableSuggestion,
generateWindowFunctionsSuggestion,
} from './generateSuggestions';

Expand Down Expand Up @@ -71,6 +72,7 @@ async function getSuggestions(
let simpleTypesSuggestions: Monaco.languages.CompletionItem[] = [];
let pragmasSuggestions: Monaco.languages.CompletionItem[] = [];
let entitySettingsSuggestions: Monaco.languages.CompletionItem[] = [];
let variableSuggestions: Monaco.languages.CompletionItem[] = [];

if (parseResult.suggestEntity) {
const entityNamePrefix = getEntityNameAtCursor(model, cursorPosition);
Expand Down Expand Up @@ -99,6 +101,12 @@ async function getSuggestions(
if (parseResult.suggestSimpleTypes) {
simpleTypesSuggestions = await generateSimpleTypesSuggestion(rangeToInsertSuggestion);
}
if (parseResult.suggestVariables) {
variableSuggestions = generateVariableSuggestion(
rangeToInsertSuggestion,
parseResult.suggestVariables,
);
}
if (parseResult.suggestUdfs) {
udfsSuggestions = await generateUdfSuggestion(rangeToInsertSuggestion);
}
Expand Down Expand Up @@ -141,6 +149,7 @@ async function getSuggestions(
...keywordsSuggestions,
...aggregateFunctionsSuggestions,
...entitySettingsSuggestions,
...variableSuggestions,
];

return suggestions;
Expand Down
Loading