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

Probably bug in any word hint demo #6653

Closed
jcubic opened this issue Apr 12, 2021 · 6 comments
Closed

Probably bug in any word hint demo #6653

jcubic opened this issue Apr 12, 2021 · 6 comments

Comments

@jcubic
Copy link
Contributor

jcubic commented Apr 12, 2021

I was just writing hint in my code based on any anywordhint.html and it seem that the code is not correct. The regex using global flag and according to MDN test function change lastIndex of the regex test function. I'm not 100% sure if the implementation have bug, but in my code that use almost the same while loop it matched only first character of the word because lastIndex on next call was 1 and it din't much for second character. So the start while loop ended after first iteration. To fix this just remove g flag from the regex.

TL;DR here is the bug:

var WORD = /[\w$]+/g, RANGE = 500;
   ...
    while (end < curLine.length && word.test(curLine.charAt(end))) ++end;
    while (start && word.test(curLine.charAt(start - 1))) --start;
@marijnh
Copy link
Member

marijnh commented Apr 12, 2021

Do you have an example where it behaves incorrectly?

@jcubic
Copy link
Contributor Author

jcubic commented Apr 12, 2021

Sorry I was not testing your code maybe it magically work but you can see the same code in my project:

https://codepen.io/jcubic/pen/abpqXvO?editors=0010

If you type (defi and CTRL+Space it much every word. The code is little bit refactored to make it cleaner but if you look at function get_range it have exactly same code and use regex that have g variable. Maybe your example works because it find every word in document but it's bad documentation because those while loops don't do what they suppose to do. At least this is what I thought then I tried to implement my code based on this demo.

@jcubic
Copy link
Contributor Author

jcubic commented Apr 12, 2021

Maybe it just need a comment that explain what the while loop do, if it suppose to match only one letter.

@marijnh
Copy link
Member

marijnh commented Apr 12, 2021

WORD, in this codebase, doesn't have a g flag.

@marijnh marijnh closed this as completed Apr 12, 2021
@jcubic
Copy link
Contributor Author

jcubic commented Apr 12, 2021

But demo have https://github.com/codemirror/CodeMirror/blob/master/demo/anywordhint.html#L32
Demos are in fact kind of documentation.

@marijnh
Copy link
Member

marijnh commented Apr 12, 2021

Ah, that's just filler content for the editor, which appears to contain an old implementation of the addon. Attached patch updates it to the current code.

marijnh added a commit that referenced this issue Apr 12, 2021
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

No branches or pull requests

2 participants