Sublime Forum

Get text inside current braces

#1

I was wondering if its possible to get all text within a given brace block (so while (true) { sleep 1; }, get the sleep 1;) and support nested blocks (so once I detect a {, I can find the code inside).

ST2 highlights the brace pair of the block you are currently in, so I was wondering if this information (what region is in the block I guess) is exposed through the API anywhere?

0 Likes

#2

Have you tried command+shift+space? (ctrl+shift+space on Windows)

0 Likes

#3

Yes that’s what I want, but sorry (wasn’t clear) how would I do this in a plugin?

0 Likes

#4

You can run the command from within the plugin, via:

view.run_command("expand_selection", { "to": "brackets" })

That will modify the selection, of course, which may or may not be ok for what you’re trying to do.

0 Likes

#5

Hm, not really looking to expand the selection, I just want to parse the text in the braces. I’m trying to write a code insight plugin for PHP, and still planning it out in my head.

0 Likes

#6

You could save the current selection, expand the selection, copy the text, then set the selection back to what it used to be…

0 Likes

#7

I just realized I’ll need a different solution since I’ll be working with all files in the current project, not just ones that are open.

Thanks anyways for the help!

0 Likes