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

How to correctly use collectCandidates()? #21

Closed
omurbekjk opened this issue Feb 27, 2019 · 5 comments
Closed

How to correctly use collectCandidates()? #21

omurbekjk opened this issue Feb 27, 2019 · 5 comments

Comments

@omurbekjk
Copy link

Say I have mysql grammar, and want to get autocomplete after select query.
Simple grammar:

query: SELECT tableRef;
tableRef: ID;
ID
    : [a-zA-Z] [a-zA-Z0-9_]*
    ;

How should I correctly find caretTokenIndex?
const candidates = core.collectCandidates('select'.length-1); OR const candidates = core.collectCandidates(0);

@mike-lischke
Copy link
Owner

A token index is the index of the token in the token stream, not a character index. What you first have to do is to take your caret position and find the correct token index for that. You can do that by simply iterating over the token stream and find the token that encloses the caret position.

@omurbekjk
Copy link
Author

Say I have this select name from u|sers query and caret position is in the last token.
By iterating over the token stream, I need to find the token index that has caret position right?

let index=-1;
for(let token of tokenStream.tokens) {
    console.log(token.type + " : " + parser.symbolicNames[token.type]);
// TODO find caret index
}
// index = 3 

@mike-lischke
Copy link
Owner

Correct.

@omurbekjk
Copy link
Author

I noticed that tokens are protected protected tokens: Token[]; in ts. How to get tokens in ts do you know?

@mike-lischke
Copy link
Owner

Easy, check the C3 source how it is done there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants