rust-shlex icon indicating copy to clipboard operation
rust-shlex copied to clipboard

Get span of each item

Open HKalbasi opened this issue 1 year ago • 4 comments

I need this to detect which item the cursor is in it. Would you accept a PR for this functionality?

HKalbasi avatar May 22 '24 16:05 HKalbasi

Sorry for my inattention to this repo. I happened to notice this when visiting the issue tracker due to another issue that was just opened. If you’re still interested, I’m willing to review PRs. Maybe this can be combined with the other two parsing feature requests (#9 and #13) into a more featureful lower-level API?

(@fenhl, I’ll make sure not to do anything without your input, unless you’re not available. Of course, this can wait until after the holidays. I’m just responding today because that’s when I noticed this.)

comex avatar Dec 25 '24 21:12 comex

That sounds like a good idea.

fenhl avatar Dec 25 '24 21:12 fenhl

I honestly don't remember exactly why I needed this. It was probably for my crate clap-repl to detect the cursor position for auto complete, but it seems now auto complete is working even when cursor is in the middle of the line, so maybe this was a XY problem and I didn't really need this.

HKalbasi avatar Dec 25 '24 21:12 HKalbasi

I looked at my code and found that I worked around the problem by trimming the input to the cursor position. That is, to detect the word position of the cursor in this example:

some             command --foo x                --bar --baz
                                         ^

We can parse the trimmed version using shlex:

some             command --f
                                         ^

and the number of tokens (in this case, 3) is the answer.

Although my original problem is solved, I still think there is some utility in returning the spans in a more featureful parsing api, and most of the other parsing libraries have such a api.

HKalbasi avatar Dec 25 '24 22:12 HKalbasi