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

Function to get all tokens of a line #2908

Closed

Conversation

marcelgerber
Copy link
Contributor

So in Brackets, we've a function that runs getTokenAt() for every single token to find all modes in a particular file.
While this is OK on normal-length lines, it takes ages on very long lines. So, assuming you have a line with 10000 tokens, the main while loop, and therefore readToken will run 50,005,000 times! (t / 2 * (t + 1), where t is the token count).
With the function getTokensOfLine, newly introduced in this PR, the while loop will only run 10,000 times, as it pushes all its results into an array ;)

Hope you're interested in this function as well.

@marijnh
Copy link
Member

marijnh commented Nov 3, 2014

All token objects will end up with the same state. It seems that your use case requires getting information from this state, so it likely won't work with your function. Calling CodeMirror.copyState when storing the state should help there.

Can you find a way to express this without duplicating most of the code in getTokenAt? Maybe define a utility that both methods use.

Also, public methods need documentation.

@marcelgerber
Copy link
Contributor Author

Renamed the function to getLineTokens, added documentation, made use of copyState. Also, using a utility function now.

marijnh added a commit that referenced this pull request Nov 4, 2014
@marijnh
Copy link
Member

marijnh commented Nov 4, 2014

Thanks! Merged as da3e8f2, followed up by 71ef0cf

@marijnh marijnh closed this Nov 4, 2014
@marcelgerber
Copy link
Contributor Author

Thank you :)

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

Successfully merging this pull request may close these issues.

2 participants