Skip to content

Commit

Permalink
feat(autocomplete): show column attributes (#1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raubzeug authored Nov 21, 2024
1 parent aa216b1 commit de0b9b6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/types/api/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ export interface TAutocompleteEntity {
Parent: string;
}

export interface AutocompleteColumn extends TAutocompleteEntity {
Type: 'column';
PKIndex?: number;
NotNull?: boolean;
Default?: number;
}

export function isAutocompleteColumn(entity: TAutocompleteEntity): entity is AutocompleteColumn {
return entity.Type === 'column';
}

export type AutocompleteEntityType =
| 'unknown'
| 'dir'
Expand Down
50 changes: 38 additions & 12 deletions src/utils/monaco/yql/generateSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import type {
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';
import {isAutocompleteColumn} from '../../../types/api/autocomplete';
import type {
AutocompleteColumn,
AutocompleteEntityType,
TAutocompleteEntity,
} from '../../../types/api/autocomplete';

import {
AggregateFunctions,
Expand Down Expand Up @@ -174,6 +179,23 @@ async function getSimpleTypes() {
return SimpleTypes;
}

function getColumnDetails(col: AutocompleteColumn) {
const {PKIndex, NotNull, Default} = col;
const details = [];
if (PKIndex !== undefined) {
details.push(`PK${PKIndex}`);
}
if (NotNull) {
details.push('NN');
}
if (Default) {
details.push('Default');
}
const detailsString = details.length ? details.join(', ') : '';
// return `Column${detailsString}`;
return detailsString;
}

export async function generateColumnsSuggestion(
rangeToInsertSuggestion: monaco.IRange,
suggestColumns: YqlAutocompleteResult['suggestColumns'] | undefined,
Expand Down Expand Up @@ -224,9 +246,11 @@ export async function generateColumnsSuggestion(
);

autocompleteResponse.Result.Entities.forEach((col) => {
if (col.Type !== 'column') {
if (!isAutocompleteColumn(col)) {
return;
}

const columnDetails = getColumnDetails(col);
const normalizedName = wrapStringToBackticks(col.Name);

const normalizedParentName = normalizeEntityPrefix(col.Parent, database);
Expand All @@ -236,9 +260,9 @@ export async function generateColumnsSuggestion(
aliases.forEach((a) => {
const columnNameSuggestion = `${a}.${normalizedName}`;
suggestions.push({
label: columnNameSuggestion,
label: {label: columnNameSuggestion, description: columnDetails},
insertText: columnNameSuggestion,
kind: CompletionItemKind.Field,
kind: CompletionItemKind.Variable,
detail: 'Column',
range: rangeToInsertSuggestion,
sortText:
Expand All @@ -253,9 +277,12 @@ export async function generateColumnsSuggestion(
columnNameSuggestion = `${wrapStringToBackticks(normalizedParentName)}.${normalizedName}`;
}
suggestions.push({
label: columnNameSuggestion,
label: {
label: columnNameSuggestion,
description: columnDetails,
},
insertText: columnNameSuggestion,
kind: CompletionItemKind.Field,
kind: CompletionItemKind.Variable,
detail: 'Column',
range: rangeToInsertSuggestion,
sortText:
Expand All @@ -266,12 +293,11 @@ export async function generateColumnsSuggestion(
}
});
if (normalizedColumns && normalizedColumns.length > 0) {
const allColumsn = normalizedColumns.join(', ');
const allColumns = normalizedColumns.join(', ');
suggestions.push({
label: allColumsn,
insertText: allColumsn,
kind: CompletionItemKind.Field,
detail: 'All columns',
label: allColumns,
insertText: allColumns,
kind: CompletionItemKind.Variable,
range: rangeToInsertSuggestion,
sortText: suggestionIndexToWeight(getSuggestionIndex('suggestAllColumns')),
});
Expand All @@ -289,7 +315,7 @@ export function generateColumnAliasesSuggestion(
return suggestColumnAliases?.map((columnAliasSuggestion) => ({
label: columnAliasSuggestion.name,
insertText: columnAliasSuggestion.name,
kind: CompletionItemKind.Field,
kind: CompletionItemKind.Variable,
detail: 'Column alias',
range: rangeToInsertSuggestion,
sortText: suggestionIndexToWeight(getSuggestionIndex('suggestColumnAliases')),
Expand Down

0 comments on commit de0b9b6

Please sign in to comment.