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 all columns in a row #1599

Merged
merged 1 commit into from
Nov 8, 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
14 changes: 7 additions & 7 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 @@ -23,7 +23,7 @@
"@gravity-ui/react-data-table": "^2.1.1",
"@gravity-ui/table": "^0.5.0",
"@gravity-ui/uikit": "^6.33.0",
"@gravity-ui/websql-autocomplete": "^9.1.0",
"@gravity-ui/websql-autocomplete": "^12.1.2",
"@hookform/resolvers": "^3.9.0",
"@reduxjs/toolkit": "^2.2.3",
"@tanstack/react-table": "^8.19.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
}
&__definition {
text-align: right;
word-break: break-all;
word-break: break-word;
}
}
2 changes: 1 addition & 1 deletion src/utils/monaco/yql/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {YQLEntity} from '@gravity-ui/websql-autocomplete';
import type {YQLEntity} from '@gravity-ui/websql-autocomplete/yql';

export const SimpleTypes = [
'Text',
Expand Down
63 changes: 44 additions & 19 deletions src/utils/monaco/yql/generateSuggestions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type {
ColumnAliasSuggestion,
KeywordSuggestion,
YQLEntity,
YqlAutocompleteResult,
} from '@gravity-ui/websql-autocomplete';
} from '@gravity-ui/websql-autocomplete/shared';
import type {YQLEntity, YqlAutocompleteResult} from '@gravity-ui/websql-autocomplete/yql';
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';

import type {AutocompleteEntityType, TAutocompleteEntity} from '../../../types/api/autocomplete';
Expand Down Expand Up @@ -123,24 +122,27 @@ function normalizeEntityPrefix(value = '', database: string) {
return removeStartSlash(cleanedValue);
}

type SuggestionType = keyof Omit<YqlAutocompleteResult, 'errors' | 'suggestDatabases'>;
type SuggestionType =
| keyof Omit<YqlAutocompleteResult, 'errors' | 'suggestDatabases'>
| 'suggestAllColumns';

const SuggestionsWeight: Record<SuggestionType, number> = {
suggestTemplates: 0,
suggestPragmas: 1,
suggestEntity: 2,
suggestColumns: 3,
suggestColumnAliases: 4,
suggestTableIndexes: 5,
suggestTableHints: 6,
suggestEntitySettings: 7,
suggestKeywords: 8,
suggestAggregateFunctions: 9,
suggestTableFunctions: 10,
suggestWindowFunctions: 11,
suggestFunctions: 12,
suggestSimpleTypes: 13,
suggestUdfs: 14,
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,
};

function getSuggestionIndex(suggestionType: SuggestionType) {
Expand Down Expand Up @@ -181,6 +183,7 @@ export async function generateColumnsSuggestion(
return [];
}
const suggestions: monaco.languages.CompletionItem[] = [];
const normalizedColumns = suggestColumns.all ? ([] as string[]) : undefined;
const multi = suggestColumns.tables.length > 1;

const normalizedTableNames =
Expand Down Expand Up @@ -228,6 +231,7 @@ export async function generateColumnsSuggestion(

const normalizedParentName = normalizeEntityPrefix(col.Parent, database);
const aliases = tableNameToAliasMap[normalizedParentName];
const currentSuggestionIndex = suggestions.length;
if (aliases?.length) {
aliases.forEach((a) => {
const columnNameSuggestion = `${a}.${normalizedName}`;
Expand All @@ -237,8 +241,11 @@ export async function generateColumnsSuggestion(
kind: CompletionItemKind.Field,
detail: 'Column',
range: rangeToInsertSuggestion,
sortText: suggestionIndexToWeight(getSuggestionIndex('suggestColumns')),
sortText:
suggestionIndexToWeight(getSuggestionIndex('suggestColumns')) +
suggestionIndexToWeight(currentSuggestionIndex),
});
normalizedColumns?.push(columnNameSuggestion);
});
} else {
let columnNameSuggestion = normalizedName;
Expand All @@ -251,10 +258,24 @@ export async function generateColumnsSuggestion(
kind: CompletionItemKind.Field,
detail: 'Column',
range: rangeToInsertSuggestion,
sortText: suggestionIndexToWeight(getSuggestionIndex('suggestColumns')),
sortText:
suggestionIndexToWeight(getSuggestionIndex('suggestColumns')) +
suggestionIndexToWeight(currentSuggestionIndex),
});
normalizedColumns?.push(columnNameSuggestion);
}
});
if (normalizedColumns && normalizedColumns.length > 0) {
const allColumsn = normalizedColumns.join(', ');
suggestions.push({
label: allColumsn,
insertText: allColumsn,
kind: CompletionItemKind.Field,
detail: 'All columns',
range: rangeToInsertSuggestion,
sortText: suggestionIndexToWeight(getSuggestionIndex('suggestAllColumns')),
});
}
return suggestions;
}

Expand Down Expand Up @@ -309,6 +330,7 @@ export async function generateEntitiesSuggestion(
if (isDir && !withBackticks) {
labelAsSnippet = `\`${label}$0\``;
}
const suggestionIndex = acc.length;
acc.push({
label,
insertText: labelAsSnippet ?? label,
Expand All @@ -321,7 +343,10 @@ export async function generateEntitiesSuggestion(
command: label.endsWith('/')
? {id: 'editor.action.triggerSuggest', title: ''}
: undefined,
sortText: suggestionIndexToWeight(getSuggestionIndex('suggestEntity')),
// first argument is responsible for sorting groups of suggestions, the second - to preserve suggestions order returned from backend
sortText:
suggestionIndexToWeight(getSuggestionIndex('suggestEntity')) +
suggestionIndexToWeight(suggestionIndex),
});
return acc;
}, [] as monaco.languages.CompletionItem[]);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/monaco/yql/yqlSuggestions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {CursorPosition} from '@gravity-ui/websql-autocomplete';
import type {CursorPosition} from '@gravity-ui/websql-autocomplete/shared';
import type Monaco from 'monaco-editor';

import {
Expand Down Expand Up @@ -56,7 +56,7 @@ async function getSuggestions(
rangeToInsertSuggestion: Monaco.IRange,
database: string,
): Promise<Monaco.languages.CompletionItem[]> {
const {parseYqlQuery} = await import('@gravity-ui/websql-autocomplete');
const {parseYqlQuery} = await import('@gravity-ui/websql-autocomplete/yql');
const cursorForParsing: CursorPosition = {
line: cursorPosition.lineNumber,
column: cursorPosition.column,
Expand Down
Loading