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

Consecutive keyword not proposed without space #1313

Open
cdietrich opened this issue Dec 7, 2023 · 5 comments
Open

Consecutive keyword not proposed without space #1313

cdietrich opened this issue Dec 7, 2023 · 5 comments
Labels
as designed The feature in question is working as designed completion Completion related issue

Comments

@cdietrich
Copy link
Contributor

given the grammar

Person:
    'person' name=ID;

Greeting:
    'Hello' ':' person=[Person:ID] '!';

in the the sample model

person A
Hello|

the : keyword is not proposed

in the completion provider i allow proposals for previous/next context and for single char keywords

export class HelloWorldCompletionProvider extends DefaultCompletionProvider {

    protected override continueCompletion(items: CompletionItem[]): boolean {
        return true
    }

    protected override filterKeyword(context: CompletionContext, keyword: GrammarAST.Keyword): boolean {
        return true
    }
}
@cdietrich cdietrich added the bug Something isn't working label Dec 7, 2023
@msujew
Copy link
Contributor

msujew commented Dec 7, 2023

It's not a bug, it's a feature, see:

protected performNextTokenCompletion(document: LangiumDocument, text: string, _offset: number, _end: number): boolean {
// This regex returns false if the text ends with a letter.
// We don't want to complete new text immediately after a keyword, ID etc.
// We only care about the last character in the text, so we use $ here.
// The \P{L} used here is a Unicode category that matches any character that is not a letter
return /\P{L}$/u.test(text);
}

@msujew msujew added completion Completion related issue as designed The feature in question is working as designed and removed bug Something isn't working labels Dec 7, 2023
@cdietrich
Copy link
Contributor Author

i will check

@cdietrich
Copy link
Contributor Author

cdietrich commented Dec 7, 2023

i wonder if we can have access to both the current and following token.
thus we can decide if we need a space between two alphanumeric things or not.
or should this be done in completionFor ?

@cdietrich
Copy link
Contributor Author

cdietrich commented Dec 7, 2023

this is also hard to achieve. backtrackToAnyToken wont give a whitespace token

something like

    protected override async completionFor(context: CompletionContext, next: NextFeature<GrammarAST.AbstractElement>, acceptor: CompletionAcceptor): Promise<void> {
        const { nextTokenStart, previousTokenStart, previousTokenEnd } = this.backtrackToAnyToken(context.textDocument.getText(), context.tokenEndOffset)
        if (previousTokenEnd === nextTokenStart) {
            const x = context.textDocument.getText().slice(previousTokenStart, previousTokenEnd)
            if (HelloWorldCompletionProvider.AllKeywords.has(x)) {
                if (GrammarAST.isKeyword(next.feature)) {
                    const kw = next.feature.value
                    if (/^[a-z0-9]+$/i.test(kw.charAt(0))) {
                        return;
                    }
                } else {
                    return;
                }
            }
        }
        
        await super.completionFor(context, next, acceptor)
    }

seems to work

@cdietrich
Copy link
Contributor Author

pot. workaround: adjust keyword completion to add the :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
as designed The feature in question is working as designed completion Completion related issue
Projects
None yet
Development

No branches or pull requests

2 participants