Skip to content

Commit

Permalink
CB-5121 use different regex for long proposals mode (#2778)
Browse files Browse the repository at this point in the history
Co-authored-by: Evgenia Bezborodova <[email protected]>
  • Loading branch information
devnaumov and EvgeniaBzzz authored Jul 15, 2024
1 parent fb9cd47 commit d0f82d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useService } from '@cloudbeaver/core-di';
import { LocalizationService } from '@cloudbeaver/core-localization';
import { GlobalConstants } from '@cloudbeaver/core-utils';
import type { Compartment, Completion, CompletionConfig, CompletionContext, CompletionResult, Extension } from '@cloudbeaver/plugin-codemirror6';
import type { ISQLEditorData, SQLProposal } from '@cloudbeaver/plugin-sql-editor';
import { type ISQLEditorData, SqlEditorSettingsService, type SQLProposal } from '@cloudbeaver/plugin-sql-editor';

const codemirrorComplexLoader = createComplexLoader(() => import('@cloudbeaver/plugin-codemirror6'));

Expand All @@ -22,20 +22,27 @@ type SqlCompletion = Completion & {

const CLOSE_CHARACTERS = /[\s()[\]{};:>,=\\*]/;
const COMPLETION_WORD = /[\w*]*/;
const COMPLETION_WORD_LONG_PROPOSALS = /[\w.*]*/;

export function useSqlDialectAutocompletion(data: ISQLEditorData): [Compartment, Extension] {
const { closeCompletion, useEditorAutocompletion } = useComplexLoader(codemirrorComplexLoader);
const localizationService = useService(LocalizationService);
const optionsRef = useObjectRef({ data });
const sqlEditorSettingsService = useService(SqlEditorSettingsService);
const optionsRef = useObjectRef({ data, sqlEditorSettingsService });

const [config] = useState<CompletionConfig>(() => {
function getOptionsFromProposals(explicit: boolean, word: string, proposals: SQLProposal[]): SqlCompletion[] {
const wordLowerCase = word.toLocaleLowerCase();
const hasSameName = proposals.some(({ displayString }) => displayString.toLocaleLowerCase() === wordLowerCase);
const hasSameName = proposals.some(
({ replacementString, displayString }) =>
displayString.toLocaleLowerCase() === wordLowerCase || replacementString.toLocaleLowerCase() === wordLowerCase,
);
const filteredProposals = proposals
.filter(
({ displayString }) =>
word === '*' || (displayString.toLocaleLowerCase() !== wordLowerCase && displayString.toLocaleLowerCase().startsWith(wordLowerCase)),
({ replacementString, displayString }) =>
word === '*' ||
(displayString.toLocaleLowerCase() !== wordLowerCase && displayString.toLocaleLowerCase().startsWith(wordLowerCase)) ||
(replacementString.toLocaleLowerCase() !== wordLowerCase && replacementString.toLocaleLowerCase().startsWith(wordLowerCase)),
)
.sort((a, b) => (b.score ?? 0) - (a.score ?? 0));

Expand Down Expand Up @@ -63,7 +70,7 @@ export function useSqlDialectAutocompletion(data: ISQLEditorData): [Compartment,
return null;
}

const word = context.matchBefore(COMPLETION_WORD);
const word = context.matchBefore(optionsRef.sqlEditorSettingsService.longNameProposals ? COMPLETION_WORD_LONG_PROPOSALS : COMPLETION_WORD);

if (word === null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const defaultSettings = schema.object({
})
.pipe(schema.enum(TABLE_ALIAS_OPTIONS))
.default('PLAIN'),
'SQLEditor.ContentAssistant.proposals.long.name': schema.coerce.boolean().default(false),
});

export type SqlEditorSettings = schema.infer<typeof defaultSettings>;
Expand All @@ -78,6 +79,10 @@ export class SqlEditorSettingsService extends Dependency {
return this.settings.getValue('sql.proposals.insert.table.alias');
}

get longNameProposals(): boolean {
return this.settings.getValue('SQLEditor.ContentAssistant.proposals.long.name');
}

readonly settings: SettingsProvider<typeof defaultSettings>;

constructor(
Expand Down

0 comments on commit d0f82d9

Please sign in to comment.