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

Improved auto complete #2

Open
aszenz opened this issue Feb 3, 2025 · 3 comments
Open

Improved auto complete #2

aszenz opened this issue Feb 3, 2025 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@aszenz
Copy link

aszenz commented Feb 3, 2025

Idea Statement

Currently I see this tool manually defines completion's in the Monaco editor, I think using duckdbs own auto complete engine via the extensions may give better results.

Feature implementation brainstorm

See:
https://duckdb.org/docs/api/cli/autocomplete.html
It works on wasm

const query = `select suggestion from sql_auto_complete('${escape(input)}')`;
@caioricciuti caioricciuti self-assigned this Feb 3, 2025
@caioricciuti caioricciuti added the enhancement New feature or request label Feb 3, 2025
@caioricciuti
Copy link
Owner

Image

I can't use the autocomplete since there's no direct call to mocaco (editor) to keeping creating the suggestions but I add the funcions and keywords directly from the duckDb settings.

 const keywordsResult: any = await executeQuery(
        "SELECT DISTINCT(keyword_name) FROM duckdb_keywords()"
      );

      const functionsResult: any = await executeQuery(
        "SELECT DISTINCT ON (function_name) * FROM DUCKDB_FUNCTIONS();"
      );

@aszenz
Copy link
Author

aszenz commented Feb 7, 2025

I can't use the autocomplete since there's no direct call to mocaco (editor) to keeping creating the suggestions but I add the funcions and keywords directly from the duckDb settings.

Not sure I understood the reason, in my own implementation I pass the input to the auto complete extension via provideCompletionItems.

monaco.languages.registerCompletionItemProvider('sql', {
    async provideCompletionItems(model, position) {
        const word = model.getWordUntilPosition(position);
        const conn = await db.connect();
        const range = {
            startLineNumber: position.lineNumber,
            endLineNumber: position.lineNumber,
            startColumn: word.startColumn,
            endColumn: word.endColumn,
        };
        const textInRange = model.getValueInRange({
            startColumn: 0,
            endColumn: position.column,
            startLineNumber: position.lineNumber,
            endLineNumber: position.lineNumber,
        });
        const query = `select suggestion from sql_auto_complete('${escape(textInRange)}')`;
        const items = await queryNative<{ suggestion: arrow.DataType<arrow.Type.Utf8> }>(conn, query);
        if (!items) return { suggestions: [] };
        const suggestions = items.values.map((item) => {
            return {
                label: String(item.suggestion),
                kind: monaco.languages.CompletionItemKind.Field,
                insertText: String(item.suggestion),
                range,
            };
        });
        return {
            suggestions,
        };
    },
});

Note that the engine provides suggestions based on the context, and it got improved a lot in v1.2.0, so it's much better than plain matching.

Of course it also keeps suggestions in sync as the user adds db's/files etc.

@caioricciuti
Copy link
Owner

caioricciuti commented Feb 8, 2025

HEYYY! @aszenz genius thankss! I got it now and you are right it works like a charm, I just didn't implemented it right! Thanks for sharing your code!

Image

PD: I'll release that on the next one!

@caioricciuti caioricciuti reopened this Feb 8, 2025
caioricciuti added a commit that referenced this issue Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants